list_state != LIST_NONE) { $lclose = $this->do_list( " ",true ); } $sclose = ""; switch ($state) { case STATE_PARAGRAPH: $sclose = "\n"; break; case STATE_BLOCKQUOTE: $sclose = "\n"; break; case STATE_PREFORM: $sclose = "\n"; break; case STATE_NOTIKI: $sclose = "\n"; break; } return $lclose . $sclose; } function do_replace( $line, $mark, $tag ) { // do the regex thingy for things like bold, italic etc // $mark is the magic character, and $tag the HTML tag to insert // BODGE: replace inline $mark characters in places where we want them ignored // they will be put back after main substitutue, stops problems with eg, and/or $bodge = chr(1); $line = eregi_replace( '([[:alnum:]])'.$mark.'([[:alnum:]])', '\\1'.$bodge.'\\2',$line ); $regex = '(^| |[(.,])'.$mark.'([^'.$mark.']*)'.$mark.'([^[:alnum:]]|$)'; $replace = '\\1<'.$tag.'>\\2'.$tag.'>\\3'; $line = eregi_replace( $regex, $replace, $line ); // BODGE: back we go $line = eregi_replace( $bodge, $mark, $line ); return $line; } function do_replace_markdown( $line, $mark, $tag ) { // do the regex thingy for things like bold, italic etc // $mark is the magic character, and $tag the HTML tag to insert // MARKDOWN version does not generate HTML tags, just straigt replace // BODGE: replace inline $mark characters in places where we want them ignored // they will be put back after main substitutue, stops problems with eg, and/or $bodge = chr(1); $line = eregi_replace( '([[:alnum:]])'.$mark.'([[:alnum:]])', '\\1'.$bodge.'\\2',$line ); $regex = '(^| |[(.,])'.$mark.'([^'.$mark.']*)'.$mark.'([^[:alnum:]]|$)'; $replace = '\\1'.$tag.'\\2'.$tag.'\\3'; $line = eregi_replace( $regex, $replace, $line ); // BODGE: back we go $line = eregi_replace( $bodge, $mark, $line ); return $line; } function do_replace_sub( $line, $mark, $tag ) { // do regex for subscript and superscript (slightly different) // $mark is the magic character and $tag the HTML tag to insert $regex = $mark.'([^'.$mark.']*)'.$mark; $replace = '<'.$tag.'>\\1'.$tag.'>'; return eregi_replace( $regex, $replace, $line ); } function do_list( $line, $blank=false ) { // handle line with list character on it // if blank line implies drop to level 0 // get magic character and then delete it from the line if not blank if ($blank) { $listchar=""; $count = 0; } else { $listchar = $line{0}; $count = strspn( $line, $listchar ); $line = eregi_replace( "^[".$listchar."]+ ", "", $line ); } // find what sort of list this character represents $list_tag = ""; $list_close_tag = ""; $item_tag = ""; $item_close_tag = ""; $list_style = LIST_NONE; switch ($listchar) { case '*': $list_tag = ""; $list_close_tag = ""; $item_tag = "*"; $item_close_tag = ""; $list_style = LIST_UNORDERED; break; case '#': $list_tag = ""; $list_close_tag = ""; $item_tag = "1."; $item_close_tag = ""; $list_style = LIST_ORDERED; break; case ';': $list_tag = "
$buffer = $buffer . "\n";
$buffer = $buffer . $this->line_replace($line) . "\n";
$this->block_state = STATE_PREFORM;
}
else
if (eregi("^\% ",$line) ) {
// preformatted text - no processing
// MARKDOWN: this is MD code form of a paragraph
$buffer = $buffer . " " . eregi_replace( "^\%","",$line) . "\n";
$this->block_state = STATE_NOTIKI;
}
else {
// ordinary paragraph
$buffer = $buffer . $this->line_replace($line) . "\n";
$this->block_state = STATE_PARAGRAPH;
}
continue;
}
if (($this->block_state == STATE_PARAGRAPH) |
($this->block_state == STATE_BLOCKQUOTE) |
($this->block_state == STATE_PREFORM) ) {
$buffer = $buffer . $this->line_replace($line) . "\n";
continue;
}
elseif ($this->block_state == STATE_NOTIKI) {
$buffer = $buffer . " " .$line . "\n";
}
}
// close off any block level tags
$buffer = $buffer . $this->close_block( $this->block_state );
//return $buffer;
return $buffer;
}
function update( $thing, $textfield, $formatfield, $coursesql='' ) {
// converts the text in a particular activity (or sub-activity)
// $thing = the database name for that 'thing' (eg, resource, choice)
// $textfield = the name of the field that might hold the wiki-text
// $formatfield = the name of the field that contains the format type
// $coursesql = if supplied, the query to get the courseid, if not get from the 'course' field
// ($id of record is tacked on right at the end, so phrase accordingly)
// returns a count of records converted
$count = 0;
if ($records = get_records( $thing,$formatfield,FORMAT_WIKI )) {
foreach( $records as $record ) {
$text = $record->$textfield;
$id = $record->id;
if (!$coursesql) {
$courseid = $record->course;
} else {
$r = get_record_sql( $coursesql . "$id" );
$courseid = $r->course;
}
$newtext = $this->convert( $text,$courseid );
$record->$textfield = $newtext;
$record->$formatfield = FORMAT_MARKDOWN;
update_record( $thing, addslashes_object( $record ) );
$count++;
}
}
return $count;
}
}
?>