/[drupal]/contributions/modules/easylists/easylists-filter.inc
ViewVC logotype

Contents of /contributions/modules/easylists/easylists-filter.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download) (as text)
Wed Apr 2 08:22:13 2008 UTC (19 months, 3 weeks ago) by deelight
Branch: MAIN
CVS Tags: DRUPAL-5--1-1-1, DRUPAL-5--1-1, HEAD
Changes since 1.3: +17 -16 lines
File MIME type: text/x-php
Multiple issues fixed (thanks to maartenvg - http://drupal.org/node/241538) :
- Long PHP-tags introduced, for increased compatibility.
- Filter tips added.
- Filter-setting page to choose bullet character (defaults to '-').
- Space before a line gets trimmed to make sure that an indented list also gets transformed
1 <?php
2 // $Id: easylists-filter.inc,v 1.3 2007/06/06 14:42:10 deelight Exp $
3
4 // Credits: most of the parsing code is taken from MediaWiki 1.10.0
5 // (licenced under GPL2)
6
7 function _easylists_filter_process(&$body, $format = -1)
8 {
9 $listprocessor = new listprocessor;
10 $body = $listprocessor->process($body, variable_get('easylists_character_' . $format, '-'));
11 return $body;
12 }
13
14 class listprocessor
15 {
16 function process($body, $token)
17 {
18 $textLines = explode( "\n", $body );
19 $lastPrefix = $output = '';
20 $inBlockElem = false;
21 $prefixLength = 0;
22 $paragraphStack = false;
23 foreach ( $textLines as $oLine )
24 {
25 // lists
26 $oLine = trim($oLine);
27 $lastPrefixLength = strlen( $lastPrefix );
28 $prefixLength = strspn( $oLine, $token );
29 $pref = substr( $oLine, 0, $prefixLength );
30 $t = substr( $oLine, $prefixLength );
31
32 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref ) )
33 {
34 # Same as the last item, so no need to deal with nesting or opening stuff
35 $output .= $this->nextItem( $token, substr( $pref, -1 ) );
36 $paragraphStack = false;
37 }
38 elseif( $prefixLength || $lastPrefixLength )
39 {
40 # Either open or close a level...
41 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
42 $paragraphStack = false;
43 while( $commonPrefixLength < $lastPrefixLength )
44 {
45 $output .= $this->closeList( $token, $lastPrefix{$lastPrefixLength-1} );
46 --$lastPrefixLength;
47 }
48 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
49 $output .= $this->nextItem( $token, $pref{$commonPrefixLength-1} );
50 }
51 while ( $prefixLength > $commonPrefixLength ) {
52 $char = substr( $pref, $commonPrefixLength, 1 );
53 $output .= $this->openList( $token, $char, $prefixLength > 1 );
54 ++$commonPrefixLength;
55 }
56 $lastPrefix = $pref;
57 }
58
59 // somewhere above we forget to get out of pre block (bug 785)
60 if ($paragraphStack === false) {
61 $output .= $t."\n";
62 }
63 }
64 while ( $prefixLength ) {
65 $output .= $this->closeList( $token, $pref{$prefixLength-1} );
66 --$prefixLength;
67 }
68 if ( '' != $this->mLastSection ) {
69 $output .= '</' . $this->mLastSection . '>';
70 $this->mLastSection = '';
71 }
72
73 return $output;
74 }
75
76 function getCommon( $st1, $st2 )
77 {
78 $fl = strlen( $st1 );
79 $shorter = strlen( $st2 );
80 if ( $fl < $shorter ) { $shorter = $fl; }
81
82 for ( $i = 0; $i < $shorter; ++$i ) {
83 if ( $st1{$i} != $st2{$i} ) { break; }
84 }
85 return $i;
86 }
87
88 function openList( $token, $char, $sublist=false )
89 {
90 $result = $this->closeParagraph();
91 if ($sublist)
92 $class = "easylist sublist";
93 else
94 $class = "easylist";
95
96 if ( $token == $char ) { $result .= '<ul class="'.$class.'"><li>'; }
97 else { $result = '<!-- ERR 1 -->'; }
98 return $result;
99 }
100
101 function closeParagraph() {
102 $result = '';
103 if ( '' != $this->mLastSection ) {
104 $result = '</' . $this->mLastSection . ">\n";
105 }
106 $this->mInPre = false;
107 $this->mLastSection = '';
108 return $result;
109 }
110
111 function nextItem( $token, $char ) {
112 if ( $token == $char ) { return '</li><li>'; }
113 return '<!-- ERR 2 -->';
114 }
115
116 function closeList( $token, $char ) {
117 if ( $token == $char ) { $text = '</li></ul>'; }
118 else { return '<!-- ERR 3 -->'; }
119 return $text."\n";
120 }
121 }

  ViewVC Help
Powered by ViewVC 1.1.2