transform($text);
}
// ###################################################################
/**
* Resets the parser
*/
public function __construct()
{
$this->htmlBlockMap = array();
$this->text = '';
}
// ###################################################################
/**
* Transforms the Markdown text into XHTML with the parser's set options
*
* @param string Text to transform
*
* @return string HTML output
*/
public function transform($text)
{
// reset the data arrays
$this->__construct();
// convert line breaks and remove empty lines of whitespace
$this->text = BSFunctions::ConvertLineBreaks($text);
$this->text = preg_replace('/^\s*?$/m', '', $this->text);
$this->_extractHtmlBlocks();
$this->_extractLinkMap();
$this->_convertHardLineBreaks();
$this->_convertAtxHeaders();
$this->_convertSetextHeaders();
$this->_convertHorizontalRules();
$this->_formatParagraphs();
$this->text = $this->_expandHtmlBlocks($this->text);
// convert entitites
$this->_convertFloatingEntities();
return $this->text;
}
// ###################################################################
/**
* Description of the function
*
* @param string A string param
*
* @return integer Return value
*/
private function _extractHtmlBlocks()
{
$start = -1;
$blockStart = 0;
$nest = array();
$matches = array();
// find the first insance of a block tag
$regex = implode('|', $this->htmlBockTags);
while ($start <= strlen($this->text))
{
$start++;
if (preg_match("/^<($regex)/i", substr($this->text, $start)) === 1)
{
if (sizeof($nest) == 0)
{
$blockStart = $start;
}
array_push($nest, $start);
}
else if (preg_match("#^($regex)>#i", substr($this->text, $start), $matches) === 1)
{
array_pop($nest);
if (sizeof($nest) == 0)
{
$block = substr($this->text, $blockStart, $start - $blockStart + strlen($matches[0]));
$hash = md5($block . microtime());
$this->htmlBlockMap[$hash] = $block;
$this->text = substr_replace($this->text, $hash, $blockStart, strlen($block));
$start = $blockStart;
}
}
}
}
// ###################################################################
/**
* Expands the hashed HTML blocks back into their originial form
*/
private function _expandHtmlBlocks($text)
{
return str_replace(array_keys($this->htmlBlockMap), array_values($this->htmlBlockMap), $text);
}
// ###################################################################
/**
* Extracts all links in the "[id]: link" form
*/
public function _extractLinkMap()
{
$this->text = preg_replace_callback('/\[(\w+)\]:\s*(\S+)>?(\s*(("|\')|\()(.*?)(\4|\)))?\n?/', array(&$this, '_extractLinkMapCallback'), $this->text);
}
// ###################################################################
/**
* Converts extracted link definitions into the map
*
* @param array Matches array
*/
private function _extractLinkMapCallback($matches)
{
$this->linkMap[$matches[1]] = array($matches[2], $matches[6]);
}
// ###################################################################
/**
* Converts text surrounded by #sings to headers (## Heading 2)
*/
private function _convertAtxHeaders()
{
$this->text = preg_replace_callback('/^(\#{1,6})\s*(.+)(\s*\#+)?$/', array(&$this, '_convertAtxHeadersCallback'), $this->text);
}
// ###################################################################
/**
* Callback function for preg_replace() in _convertAtxHeaders()
*
* @param array Matches
*/
private function _convertAtxHeadersCallback($matches)
{
// var_dump($matches);
$html = '
$value
"; } } $this->text = implode("\n\n", $blocks); } // ################################################################### /** * Converts two spaces followed by a new line with text on it to * a hard line break (