", $line);
// make email addresses into mailtos....
// MARKDOWN doesn't quite support this, so do as html
$line = preg_replace("/([[:space:]]|^)([[:alnum:]._-]+@[[:alnum:]._-]+)\(([^)]+)\)/i",
"\\1\\3", $line);
// !# at the beginning of any lines means a heading
// MARKDOWN: value (1-6) becomes number of hashes
if (preg_match( "/^!([1-6]) (.*)$/i", $line, $regs )) {
$depth = substr( $line, 1, 1 );
$out = substr( '##########', 0, $depth);
$line = preg_replace( "/^!([1-6]) (.*)$/i", "$out \\2", $line );
}
// acronym handing, example HTML(Hypertext Markyp Language)
// MARKDOWN: no equiv. so just leave as HTML
$line = preg_replace( "/([A-Z]+)\(([^)]+)\)/", "\\1", $line );
// Replace resource link >>##(Description Text)
// MARKDOWN: change to MD web link style
$line = preg_replace("/ ([a-zA-Z]+):([0-9]+)\(([^)]+)\)/i",
" [\\3](".$CFG->wwwroot."/mod/\\1/view.php?id=\\2) ", $line );
$coursefileurl = array(moodle_url::make_legacyfile_url($this->courseid, null));
// Replace picture resource link
$line = preg_replace("#/([a-zA-Z0-9./_-]+)(png|gif|jpg)\(([^)]+)\)#i",
"![\\3](".$coursefileurl."/\\1\\2)", $line );
// Replace file resource link
$line = preg_replace("#file:/([[:alnum:]/._-]+)\(([^)]+)\)#i",
"[\\2](".$coursefileurl."/\\1)", $line );
return $line;
}
function convert( $content,$courseid ) {
// main entry point for processing Wiki-like text
// $content is string containing text with Wiki-Like formatting
// return: string containing Markdown formatting
// initialisation stuff
$this->output = "";
$this->block_state = STATE_NONE;
$this->list_state = LIST_NONE;
$this->list_depth = 0;
$this->list_backtrack = array();
$this->spelling_on = false;
$this->courseid = $courseid;
// split content into array of single lines
$lines = explode( "\n",$content );
$buffer = "";
// run through lines
foreach( $lines as $line ) {
// is this a blank line?
$blank_line = preg_match( "/^[[:blank:]\r]*$/i", $line );
if ($blank_line) {
// first end current block according to state
$buffer = $buffer . $this->close_block( $this->block_state );
$this->block_state = STATE_NONE;
continue;
}
// act now depending on current block state
if ($this->block_state == STATE_NONE) {
// first character of line defines block type
if (preg_match( "/^> /i",$line )) {
// blockquote
$buffer = $buffer . $this->line_replace( $line ). "\n";
$this->block_state = STATE_BLOCKQUOTE;
}
else
if (preg_match( "/^ /i",$line) ) {
// preformatted text
// MARKDOWN: no real equiv. so just use
$buffer = $buffer . "\n";
$buffer = $buffer . $this->line_replace($line) . "\n";
$this->block_state = STATE_PREFORM;
}
else
if (preg_match("/^\% /i",$line) ) {
// preformatted text - no processing
// MARKDOWN: this is MD code form of a paragraph
$buffer = $buffer . " " . preg_replace( "/^\%/i","",$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;
}
}