MDL-26502 check browser - fix for Symbian (backported)
[moodle.git] / lib / wiki_to_markdown.php
blobf1ad19a503e2dfd36ff2d1bc6e3e42dd18fa8f65
1 <?php
2 // Utility function to convert wiki-like to Markdown format
3 // Howard Miller, 2005
5 // state defines
6 define( "STATE_NONE",1 ); // blank line has been detected, so looking for first line on next para
7 define( "STATE_PARAGRAPH",2 ); // currently processing vanilla paragraph
8 define( "STATE_BLOCKQUOTE",3 ); // currently processing blockquote section
9 define( "STATE_PREFORM",4 ); // currently processing preformatted text
10 define( "STATE_NOTIKI",5 ); // currently processing preformatted / no formatting
12 // list defines
13 define( "LIST_NONE", 1 ); // no lists active
14 define( "LIST_UNORDERED", 2 ); // unordered list active
15 define( "LIST_ORDERED", 3 ); // ordered list active
16 define( "LIST_DEFINITION", 4 ); // definition list active
18 class WikiToMarkdown {
20 var $block_state;
21 var $list_state;
22 var $list_depth;
23 var $list_backtrack;
24 var $output; // output buffer
25 var $courseid;
27 function close_block( $state ) {
28 // provide appropriate closure for block according to state
30 // if in list close this first
31 $lclose = "";
32 if ($this->list_state != LIST_NONE) {
33 $lclose = $this->do_list( " ",true );
36 $sclose = "";
37 switch ($state) {
38 case STATE_PARAGRAPH:
39 $sclose = "\n";
40 break;
41 case STATE_BLOCKQUOTE:
42 $sclose = "\n";
43 break;
44 case STATE_PREFORM:
45 $sclose = "</pre>\n";
46 break;
47 case STATE_NOTIKI:
48 $sclose = "\n";
49 break;
52 return $lclose . $sclose;
55 function do_replace( $line, $mark, $tag ) {
56 // do the regex thingy for things like bold, italic etc
57 // $mark is the magic character, and $tag the HTML tag to insert
59 // BODGE: replace inline $mark characters in places where we want them ignored
60 // they will be put back after main substitutue, stops problems with eg, and/or
61 $bodge = chr(1);
62 $line = eregi_replace( '([[:alnum:]])'.$mark.'([[:alnum:]])', '\\1'.$bodge.'\\2',$line );
64 $regex = '(^| |[(.,])'.$mark.'([^'.$mark.']*)'.$mark.'([^[:alnum:]]|$)';
65 $replace = '\\1<'.$tag.'>\\2</'.$tag.'>\\3';
66 $line = eregi_replace( $regex, $replace, $line );
68 // BODGE: back we go
69 $line = eregi_replace( $bodge, $mark, $line );
71 return $line;
75 function do_replace_markdown( $line, $mark, $tag ) {
76 // do the regex thingy for things like bold, italic etc
77 // $mark is the magic character, and $tag the HTML tag to insert
78 // MARKDOWN version does not generate HTML tags, just straigt replace
80 // BODGE: replace inline $mark characters in places where we want them ignored
81 // they will be put back after main substitutue, stops problems with eg, and/or
82 $bodge = chr(1);
83 $line = eregi_replace( '([[:alnum:]])'.$mark.'([[:alnum:]])', '\\1'.$bodge.'\\2',$line );
85 $regex = '(^| |[(.,])'.$mark.'([^'.$mark.']*)'.$mark.'([^[:alnum:]]|$)';
86 $replace = '\\1'.$tag.'\\2'.$tag.'\\3';
87 $line = eregi_replace( $regex, $replace, $line );
89 // BODGE: back we go
90 $line = eregi_replace( $bodge, $mark, $line );
92 return $line;
96 function do_replace_sub( $line, $mark, $tag ) {
97 // do regex for subscript and superscript (slightly different)
98 // $mark is the magic character and $tag the HTML tag to insert
100 $regex = $mark.'([^'.$mark.']*)'.$mark;
101 $replace = '<'.$tag.'>\\1</'.$tag.'>';
103 return eregi_replace( $regex, $replace, $line );
106 function do_list( $line, $blank=false ) {
107 // handle line with list character on it
108 // if blank line implies drop to level 0
110 // get magic character and then delete it from the line if not blank
111 if ($blank) {
112 $listchar="";
113 $count = 0;
115 else {
116 $listchar = $line{0};
117 $count = strspn( $line, $listchar );
118 $line = eregi_replace( "^[".$listchar."]+ ", "", $line );
121 // find what sort of list this character represents
122 $list_tag = "";
123 $list_close_tag = "";
124 $item_tag = "";
125 $item_close_tag = "";
126 $list_style = LIST_NONE;
127 switch ($listchar) {
128 case '*':
129 $list_tag = "";
130 $list_close_tag = "";
131 $item_tag = "*";
132 $item_close_tag = "";
133 $list_style = LIST_UNORDERED;
134 break;
135 case '#':
136 $list_tag = "";
137 $list_close_tag = "";
138 $item_tag = "1.";
139 $item_close_tag = "";
140 $list_style = LIST_ORDERED;
141 break;
142 case ';':
143 $list_tag = "<dl>";
144 $list_close_tag = "</dl>";
145 $item_tag = "<dd>";
146 $item_close_tag = "</dd>";
147 $list_style = LIST_DEFINITION;
148 break;
149 case ':':
150 $list_tag = "<dl>";
151 $list_close_tag = "</dl>";
152 $item_tag = "<dt>";
153 $item_close_tag = "</dt>";
154 $list_style = LIST_DEFINITION;
155 break;
158 // tag opening/closing regime now - fun bit :-)
159 $tags = "";
161 // if depth has reduced do number of closes to restore level
162 for ($i=$this->list_depth; $i>$count; $i-- ) {
163 $close_tag = array_pop( $this->list_backtrack );
164 $tags = $tags . $close_tag;
167 // if depth has increased do number of opens to balance
168 for ($i=$this->list_depth; $i<$count; $i++ ) {
169 array_push( $this->list_backtrack, "$list_close_tag" );
170 $tags = $tags . "$list_tag";
173 // ok, so list state is now same as style and depth same as count
174 $this->list_state = $list_style;
175 $this->list_depth = $count;
177 // get indent
178 $indent = substr( " ",1,$count-1 );
180 if ($blank) {
181 $newline = $tags;
183 else {
184 $newline = $tags . $indent . "$item_tag " . $line . "$item_close_tag";
187 return $newline;
191 function line_replace( $line ) {
192 // return line after various formatting replacements
193 // have been made - order is vital to stop them interfering with each other
195 global $CFG;
197 // ---- (at least) means a <hr />
198 // MARKDOWN: no change so leave
200 // is this a list line (starts with * # ; :)
201 if (eregi( "^([*]+|[#]+|[;]+|[:]+) ", $line )) {
202 $line = $this->do_list( $line );
205 // typographic conventions
206 // MARKDOWN: no equiv. so convert to entity as before
207 // $line = str_replace( "--", "&#8212;", $line );
208 // $line = str_replace( " - ", " &#8211; ", $line );
209 $line = str_replace( "...", " &#8230; ", $line );
210 $line = str_replace( "(R)", "&#174;", $line );
211 $line = str_replace( "(r)", "&#174;", $line );
212 $line = str_replace( "(TM)", "&#8482;", $line );
213 $line = str_replace( "(tm)", "&#8482;", $line );
214 $line = str_replace( "(C)", "&#169;", $line );
215 $line = str_replace( "1/4", "&#188;", $line );
216 $line = str_replace( "1/2", "&#189;", $line );
217 $line = str_replace( "3/4", "&#190;", $line );
218 $line = eregi_replace( "([[:digit:]]+[[:space:]]*)x([[:space:]]*[[:digit:]]+)", "\\1&#215;\\2", $line ); // (digits) x (digits) - multiply
219 // do formatting tags
220 // NOTE: The / replacement *has* to be first, or it will screw the
221 // HTML tags that are added by the other ones
222 // MARKDOWN: only bold and italic change, rest are just HTML
223 $line = $this->do_replace_markdown( $line, "\*", "**" );
224 $line = $this->do_replace_markdown( $line, "/", "*" );
225 $line = $this->do_replace( $line, "\+", "ins" );
226 // $line = $this->do_replace( $line, "-", "del" );
227 $line = $this->do_replace_sub( $line, "~", "sub" );
228 $line = $this->do_replace_sub( $line, "\^", "sup" );
229 $line = $this->do_replace( $line, "%", "code" );
230 $line = $this->do_replace( $line, "@", "cite" );
232 // convert urls into proper link with optional link text URL(text)
233 // MARDOWN: HTML conversion should work fine
234 $line = eregi_replace("([[:space:]]|^)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])\(([^)]+)\)",
235 "\\1[\\5](\\2://\\3\\4)", $line);
236 $line = eregi_replace("([[:space:]])www\.([^[:space:]]*)([[:alnum:]#?/&=])\(([^)]+)\)",
237 "\\1[\\5](http://www.\\2\\3)", $line);
239 // make urls (with and without httpd) into proper links
240 $line = eregi_replace("([[:space:]]|^)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
241 "\\1<\\2://\\3\\4>", $line);
242 $line = eregi_replace("([[:space:]])www\.([^[:space:]]*)([[:alnum:]#?/&=])",
243 "\\1<http://www.\\2\\3\>", $line);
245 // make email addresses into mailtos....
246 // MARKDOWN doesn't quite support this, so do as html
247 $line = eregi_replace("([[:space:]]|^)([[:alnum:]._-]+@[[:alnum:]._-]+)\(([^)]+)\)",
248 "\\1<a href=\"mailto:\\2\">\\3</a>", $line);
250 // !# at the beginning of any lines means a heading
251 // MARKDOWN: value (1-6) becomes number of hashes
252 if (eregi( "^!([1-6]) (.*)$", $line, $regs )) {
253 $depth = substr( $line, 1, 1 );
254 $out = substr( '##########', 0, $depth);
255 $line = eregi_replace( "^!([1-6]) (.*)$", "$out \\2", $line );
258 // acronym handing, example HTML(Hypertext Markyp Language)
259 // MARKDOWN: no equiv. so just leave as HTML
260 $line = ereg_replace( "([A-Z]+)\(([^)]+)\)", "<acronym title=\"\\2\">\\1</acronym>", $line );
262 // Replace resource link >>##(Description Text)
263 // MARKDOWN: change to MD web link style
264 $line = eregi_replace( " ([a-zA-Z]+):([0-9]+)\(([^)]+)\)",
265 " [\\3](".$CFG->wwwroot."/mod/\\1/view.php?id=\\2) ", $line );
267 require_once($CFG->libdir.'/filelib.php');
268 $coursefileurl = get_file_url($this->courseid);
270 // Replace picture resource link
271 $line = eregi_replace( "/([a-zA-Z0-9./_-]+)(png|gif|jpg)\(([^)]+)\)",
272 "![\\3](".$coursefileurl."/\\1\\2)", $line );
274 // Replace file resource link
275 $line = eregi_replace( "file:/([[:alnum:]/._-]+)\(([^)]+)\)",
276 "[\\2](".$coursefileurl."/\\1)", $line );
278 return $line;
281 function convert( $content,$courseid ) {
283 // main entry point for processing Wiki-like text
284 // $content is string containing text with Wiki-Like formatting
285 // return: string containing Markdown formatting
287 // initialisation stuff
288 $this->output = "";
289 $this->block_state = STATE_NONE;
290 $this->list_state = LIST_NONE;
291 $this->list_depth = 0;
292 $this->list_backtrack = array();
293 $this->spelling_on = false;
294 $this->courseid = $courseid;
296 // split content into array of single lines
297 $lines = explode( "\n",$content );
298 $buffer = "";
300 // run through lines
301 foreach( $lines as $line ) {
302 // is this a blank line?
303 $blank_line = eregi( "^[[:blank:]\r]*$", $line );
304 if ($blank_line) {
305 // first end current block according to state
306 $buffer = $buffer . $this->close_block( $this->block_state );
307 $this->block_state = STATE_NONE;
308 continue;
311 // act now depending on current block state
312 if ($this->block_state == STATE_NONE) {
313 // first character of line defines block type
314 if (eregi( "^> ",$line )) {
315 // blockquote
316 $buffer = $buffer . $this->line_replace( $line ). "\n";
317 $this->block_state = STATE_BLOCKQUOTE;
319 else
320 if (eregi( "^ ",$line) ) {
321 // preformatted text
322 // MARKDOWN: no real equiv. so just use <pre>
323 $buffer = $buffer . "<pre>\n";
324 $buffer = $buffer . $this->line_replace($line) . "\n";
325 $this->block_state = STATE_PREFORM;
327 else
328 if (eregi("^\% ",$line) ) {
329 // preformatted text - no processing
330 // MARKDOWN: this is MD code form of a paragraph
331 $buffer = $buffer . " " . eregi_replace( "^\%","",$line) . "\n";
332 $this->block_state = STATE_NOTIKI;
334 else {
335 // ordinary paragraph
336 $buffer = $buffer . $this->line_replace($line) . "\n";
337 $this->block_state = STATE_PARAGRAPH;
339 continue;
342 if (($this->block_state == STATE_PARAGRAPH) |
343 ($this->block_state == STATE_BLOCKQUOTE) |
344 ($this->block_state == STATE_PREFORM) ) {
345 $buffer = $buffer . $this->line_replace($line) . "\n";
346 continue;
348 elseif ($this->block_state == STATE_NOTIKI) {
349 $buffer = $buffer . " " .$line . "\n";
353 // close off any block level tags
354 $buffer = $buffer . $this->close_block( $this->block_state );
356 //return $buffer;
357 return $buffer;
360 function update( $thing, $textfield, $formatfield, $coursesql='' ) {
361 // converts the text in a particular activity (or sub-activity)
362 // $thing = the database name for that 'thing' (eg, resource, choice)
363 // $textfield = the name of the field that might hold the wiki-text
364 // $formatfield = the name of the field that contains the format type
365 // $coursesql = if supplied, the query to get the courseid, if not get from the 'course' field
366 // ($id of record is tacked on right at the end, so phrase accordingly)
367 // returns a count of records converted
368 $count = 0;
369 if ($records = get_records( $thing,$formatfield,FORMAT_WIKI )) {
370 foreach( $records as $record ) {
371 $text = $record->$textfield;
372 $id = $record->id;
373 if (!$coursesql) {
374 $courseid = $record->course;
375 } else {
376 $r = get_record_sql( $coursesql . "$id" );
377 $courseid = $r->course;
379 $newtext = $this->convert( $text,$courseid );
381 $record->$textfield = $newtext;
382 $record->$formatfield = FORMAT_MARKDOWN;
383 update_record( $thing, addslashes_object( $record ) );
384 $count++;
387 return $count;