tags
$this->out($this->parser->node);
static $indent;
$indent = $this->indent;
$this->indent = '';
}
} else {
if (!$this->parser->keepWhitespace) {
$this->output = rtrim($this->output);
}
if ($this->parser->tagName != 'pre') {
$this->indent(' ');
$this->out("\n".$this->indent.$this->parser->node);
} else {
# reset indentation
$this->out($this->parser->node);
static $indent;
$this->indent = $indent;
}
if (in_array($this->parent(), array('ins', 'del'))) {
# ins or del was block element
$this->out("\n");
$this->indent(' ');
}
if ($this->parser->tagName == 'li') {
$this->setLineBreaks(1);
} else {
$this->setLineBreaks(2);
}
}
} else {
$this->out($this->parser->node);
}
if (in_array($this->parser->tagName, array('code', 'pre'))) {
if ($this->parser->isStartTag) {
$this->buffer();
} else {
# add stuff so cleanup just reverses this
$this->out(str_replace('<', '<', str_replace('>', '>', $this->unbuffer())));
}
}
}
}
/**
* handle plain text
*
* @param void
* @return void
*/
function handleText() {
if ($this->hasParent('pre') && strpos($this->parser->node, "\n") !== false) {
$this->parser->node = str_replace("\n", "\n".$this->indent, $this->parser->node);
}
if (!$this->hasParent('code') && !$this->hasParent('pre')) {
# entity decode
$this->parser->node = $this->decode($this->parser->node);
if (!$this->skipConversion) {
# escape some chars in normal Text
$this->parser->node = preg_replace($this->escapeInText['search'], $this->escapeInText['replace'], $this->parser->node);
}
} else {
$this->parser->node = str_replace(array('"', '&apos'), array('"', '\''), $this->parser->node);
}
$this->out($this->parser->node);
$this->lastClosedTag = '';
}
/**
* handle and tags
*
* @param void
* @return void
*/
function handleTag_em() {
$this->out('*', true);
}
function handleTag_i() {
$this->handleTag_em();
}
/**
* handle and tags
*
* @param void
* @return void
*/
function handleTag_strong() {
$this->out('**', true);
}
function handleTag_b() {
$this->handleTag_strong();
}
/**
* handle tags
*
* @param void
* @return void
*/
function handleTag_h1() {
$this->handleHeader(1);
}
/**
* handle tags
*
* @param void
* @return void
*/
function handleTag_h2() {
$this->handleHeader(2);
}
/**
* handle tags
*
* @param void
* @return void
*/
function handleTag_h3() {
$this->handleHeader(3);
}
/**
* handle tags
*
* @param void
* @return void
*/
function handleTag_h4() {
$this->handleHeader(4);
}
/**
* handle tags
*
* @param void
* @return void
*/
function handleTag_h5() {
$this->handleHeader(5);
}
/**
* handle tags
*
* @param void
* @return void
*/
function handleTag_h6() {
$this->handleHeader(6);
}
/**
* number of line breaks before next inline output
*/
var $lineBreaks = 0;
/**
* handle header tags ( - )
*
* @param int $level 1-6
* @return void
*/
function handleHeader($level) {
if ($this->parser->isStartTag) {
$this->out(str_repeat('#', $level).' ', true);
} else {
$this->setLineBreaks(2);
}
}
/**
* handle
tags
*
* @param void
* @return void
*/
function handleTag_p() {
if (!$this->parser->isStartTag) {
$this->setLineBreaks(2);
}
}
/**
* handle tags
*
* @param void
* @return void
*/
function handleTag_a() {
if ($this->parser->isStartTag) {
$this->buffer();
if (isset($this->parser->tagAttributes['title'])) {
$this->parser->tagAttributes['title'] = $this->decode($this->parser->tagAttributes['title']);
} else {
$this->parser->tagAttributes['title'] = null;
}
$this->parser->tagAttributes['href'] = $this->decode(trim($this->parser->tagAttributes['href']));
$this->stack();
} else {
$tag = $this->unstack();
$buffer = $this->unbuffer();
if (empty($tag['href']) && empty($tag['title'])) {
# empty links... testcase mania, who would possibly do anything like that?!
$this->out('['.$buffer.']()', true);
return;
}
if ($buffer == $tag['href'] && empty($tag['title'])) {
#
$this->out('<'.$buffer.'>', true);
return;
}
$bufferDecoded = $this->decode(trim($buffer));
if (substr($tag['href'], 0, 7) == 'mailto:' && 'mailto:'.$bufferDecoded == $tag['href']) {
if (is_null($tag['title'])) {
#
$this->out('<'.$bufferDecoded.'>', true);
return;
}
# [mail@example.com][1]
# ...
# [1]: mailto:mail@example.com Title
$tag['href'] = 'mailto:'.$bufferDecoded;
}
# [This link][id]
foreach ($this->stack['a'] as $tag2) {
if ($tag2['href'] == $tag['href'] && $tag2['title'] === $tag['title']) {
$tag['linkID'] = $tag2['linkID'];
break;
}
}
if (!isset($tag['linkID'])) {
$tag['linkID'] = count($this->stack['a']) + 1;
array_push($this->stack['a'], $tag);
}
$this->out('['.$buffer.']['.$tag['linkID'].']', true);
}
}
/**
* handle
tags
*
* @param void
* @return void
*/
function handleTag_img() {
if (!$this->parser->isStartTag) {
return; # just to be sure this is really an empty tag...
}
if (isset($this->parser->tagAttributes['title'])) {
$this->parser->tagAttributes['title'] = $this->decode($this->parser->tagAttributes['title']);
} else {
$this->parser->tagAttributes['title'] = null;
}
if (isset($this->parser->tagAttributes['alt'])) {
$this->parser->tagAttributes['alt'] = $this->decode($this->parser->tagAttributes['alt']);
} else {
$this->parser->tagAttributes['alt'] = null;
}
if (empty($this->parser->tagAttributes['src'])) {
# support for "empty" images... dunno if this is really needed
# but there are some testcases which do that...
if (!empty($this->parser->tagAttributes['title'])) {
$this->parser->tagAttributes['title'] = ' '.$this->parser->tagAttributes['title'].' ';
}
$this->out('!['.$this->parser->tagAttributes['alt'].']('.$this->parser->tagAttributes['title'].')', true);
return;
} else {
$this->parser->tagAttributes['src'] = $this->decode($this->parser->tagAttributes['src']);
}
// 
if ($this->parser->tagAttributes['title'] != "")
$this->out('!['.$this->parser->tagAttributes['alt'].']('.$this->parser->tagAttributes['src'].'"'.$this->parser->tagAttributes['title'].'")', true);
else
$this->out('!['.$this->parser->tagAttributes['alt'].']('.$this->parser->tagAttributes['src'].')', true);
/*
# [This link][id]
$link_id = false;
if (!empty($this->stack['a'])) {
foreach ($this->stack['a'] as $tag) {
if ($tag['href'] == $this->parser->tagAttributes['src']
&& $tag['title'] === $this->parser->tagAttributes['title']) {
$link_id = $tag['linkID'];
break;
}
}
} else {
$this->stack['a'] = array();
}
if (!$link_id) {
$link_id = count($this->stack['a']) + 1;
$tag = array(
'href' => $this->parser->tagAttributes['src'],
'linkID' => $link_id,
'title' => $this->parser->tagAttributes['title']
);
array_push($this->stack['a'], $tag);
}
$this->out('!['.$this->parser->tagAttributes['alt'].']['.$link_id.']', true);
*/
}
/**
* handle tags
*
* @param void
* @return void
*/
function handleTag_code() {
if ($this->hasParent('pre')) {
# ignore code blocks inside
return;
}
if ($this->parser->isStartTag) {
$this->buffer();
} else {
$buffer = $this->unbuffer();
# use as many backticks as needed
preg_match_all('#`+#', $buffer, $matches);
if (!empty($matches[0])) {
rsort($matches[0]);
$ticks = '`';
while (true) {
if (!in_array($ticks, $matches[0])) {
break;
}
$ticks .= '`';
}
} else {
$ticks = '`';
}
if ($buffer[0] == '`' || substr($buffer, -1) == '`') {
$buffer = ' '.$buffer.' ';
}
$this->out($ticks.$buffer.$ticks, true);
}
}
/**
* handle tags
*
* @param void
* @return void
*/
function handleTag_pre() {
if ($this->keepHTML && $this->parser->isStartTag) {
# check if a simple follows
if (!preg_match('#^\s*#Us', $this->parser->html)) {
# this is no standard markdown code block
$this->handleTagToText();
return;
}
}
$this->indent(' ');
if (!$this->parser->isStartTag) {
$this->setLineBreaks(2);
} else {
$this->parser->html = ltrim($this->parser->html);
}
}
/**
* handle tags
*
* @param void
* @return void
*/
function handleTag_blockquote() {
$this->indent('> ');
}
/**
* handle tags
*
* @param void
* @return void
*/
function handleTag_ul() {
if ($this->parser->isStartTag) {
$this->stack();
if (!$this->keepHTML && $this->lastClosedTag == $this->parser->tagName) {
$this->out("\n".$this->indent.''."\n".$this->indent."\n".$this->indent);
}
} else {
$this->unstack();
if ($this->parent() != 'li' || preg_match('#^\s*(\s*- \s*)?<(p|blockquote)\s*>#sU', $this->parser->html)) {
# dont make Markdown add unneeded paragraphs
$this->setLineBreaks(2);
}
}
}
/**
* handle
tags
*
* @param void
* @return void
*/
function handleTag_ol() {
# same as above
$this->parser->tagAttributes['num'] = 0;
$this->handleTag_ul();
}
/**
* handle - tags
*
* @param void
* @return void
*/
function handleTag_li() {
if ($this->parent() == 'ol') {
$parent =& $this->getStacked('ol');
if ($this->parser->isStartTag) {
$parent['num']++;
$this->out($parent['num'].'.'.str_repeat(' ', 3 - strlen($parent['num'])), true);
}
$this->indent(' ', false);
} else {
if ($this->parser->isStartTag) {
$this->out('* ', true);
}
$this->indent(' ', false);
}
if (!$this->parser->isStartTag) {
$this->setLineBreaks(1);
}
}
/**
* handle
tags
*
* @param void
* @return void
*/
function handleTag_hr() {
if (!$this->parser->isStartTag) {
return; # just to be sure this really is an empty tag
}
$this->out('* * *', true);
$this->setLineBreaks(2);
}
/**
* handle
tags
*
* @param void
* @return void
*/
function handleTag_br() {
$this->out(" \n".$this->indent, true);
$this->parser->html = ltrim($this->parser->html);
}
/**
* node stack, e.g. for and tags
*
* @var array
*/
var $stack = array();
/**
* add current node to the stack
* this only stores the attributes
*
* @param void
* @return void
*/
function stack() {
if (!isset($this->stack[$this->parser->tagName])) {
$this->stack[$this->parser->tagName] = array();
}
array_push($this->stack[$this->parser->tagName], $this->parser->tagAttributes);
}
/**
* remove current tag from stack
*
* @param void
* @return array
*/
function unstack() {
if (!isset($this->stack[$this->parser->tagName]) || !is_array($this->stack[$this->parser->tagName])) {
trigger_error('Trying to unstack from empty stack. This must not happen.', E_USER_ERROR);
}
return array_pop($this->stack[$this->parser->tagName]);
}
/**
* get last stacked element of type $tagName
*
* @param string $tagName
* @return array
*/
function & getStacked($tagName) {
// no end() so it can be referenced
return $this->stack[$tagName][count($this->stack[$tagName])-1];
}
/**
* set number of line breaks before next start tag
*
* @param int $number
* @return void
*/
function setLineBreaks($number) {
if ($this->lineBreaks < $number) {
$this->lineBreaks = $number;
}
}
/**
* stores current buffers
*
* @var array
*/
var $buffer = array();
/**
* buffer next parser output until unbuffer() is called
*
* @param void
* @return void
*/
function buffer() {
array_push($this->buffer, '');
}
/**
* end current buffer and return buffered output
*
* @param void
* @return string
*/
function unbuffer() {
return array_pop($this->buffer);
}
/**
* append string to the correct var, either
* directly to $this->output or to the current
* buffers
*
* @param string $put
* @return void
*/
function out($put, $nowrap = false) {
if (empty($put)) {
return;
}
if (!empty($this->buffer)) {
$this->buffer[count($this->buffer) - 1] .= $put;
} else {
if ($this->bodyWidth && !$this->parser->keepWhitespace) { # wrap lines
// get last line
$pos = strrpos($this->output, "\n");
if ($pos === false) {
$line = $this->output;
} else {
$line = substr($this->output, $pos);
}
if ($nowrap) {
if ($put[0] != "\n" && $this->strlen($line) + $this->strlen($put) > $this->bodyWidth) {
$this->output .= "\n".$this->indent.$put;
} else {
$this->output .= $put;
}
return;
} else {
$put .= "\n"; # make sure we get all lines in the while below
$lineLen = $this->strlen($line);
while ($pos = strpos($put, "\n")) {
$putLine = substr($put, 0, $pos+1);
$put = substr($put, $pos+1);
$putLen = $this->strlen($putLine);
if ($lineLen + $putLen < $this->bodyWidth) {
$this->output .= $putLine;
$lineLen = $putLen;
} else {
$split = preg_split('#^(.{0,'.($this->bodyWidth - $lineLen).'})\b#', $putLine, 2, PREG_SPLIT_OFFSET_CAPTURE | PREG_SPLIT_DELIM_CAPTURE);
$this->output .= rtrim($split[1][0])."\n".$this->indent.$this->wordwrap(ltrim($split[2][0]), $this->bodyWidth, "\n".$this->indent, false);
}
}
$this->output = substr($this->output, 0, -1);
return;
}
} else {
$this->output .= $put;
}
}
}
/**
* current indentation
*
* @var string
*/
var $indent = '';
/**
* indent next output (start tag) or unindent (end tag)
*
* @param string $str indentation
* @param bool $output add indendation to output
* @return void
*/
function indent($str, $output = true) {
if ($this->parser->isStartTag) {
$this->indent .= $str;
if ($output) {
$this->out($str, true);
}
} else {
$this->indent = substr($this->indent, 0, -strlen($str));
}
}
/**
* decode email addresses
*
* @author derernst@gmx.ch
* @author Milian Wolff
*/
function decode($text, $quote_style = ENT_QUOTES) {
if (version_compare(PHP_VERSION, '5', '>=')) {
# UTF-8 is only supported in PHP 5.x.x and above
$text = html_entity_decode($text, $quote_style, 'UTF-8');
} else {
if (function_exists('html_entity_decode')) {
$text = html_entity_decode($text, $quote_style, 'ISO-8859-1');
} else {
static $trans_tbl;
if (!isset($trans_tbl)) {
$trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES, $quote_style));
}
$text = strtr($text, $trans_tbl);
}
$text = preg_replace_callback('~([0-9a-f]+);~i', array(&$this, '_decode_hex'), $text);
$text = preg_replace_callback('~(\d{2,5});~', array(&$this, '_decode_numeric'), $text);
}
return $text;
}
/**
* callback for decode() which converts a hexadecimal entity to UTF-8
*
* @param array $matches
* @return string UTF-8 encoded
*/
function _decode_hex($matches) {
return $this->unichr(hexdec($matches[1]));
}
/**
* callback for decode() which converts a numerical entity to UTF-8
*
* @param array $matches
* @return string UTF-8 encoded
*/
function _decode_numeric($matches) {
return $this->unichr($matches[1]);
}
/**
* UTF-8 chr() which supports numeric entities
*
* @author grey - greywyvern - com
* @param array $matches
* @return string UTF-8 encoded
*/
function unichr($dec) {
if ($dec < 128) {
$utf = chr($dec);
} else if ($dec < 2048) {
$utf = chr(192 + (($dec - ($dec % 64)) / 64));
$utf .= chr(128 + ($dec % 64));
} else {
$utf = chr(224 + (($dec - ($dec % 4096)) / 4096));
$utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64));
$utf .= chr(128 + ($dec % 64));
}
return $utf;
}
/**
* UTF-8 strlen()
*
* @param string $str
* @return int
*
* @author dtorop 932 at hotmail dot com
* @author Milian Wolff
*/
function strlen($str) {
if (function_exists('mb_strlen')) {
return mb_strlen($str, 'UTF-8');
} else {
return preg_match_all('/[\x00-\x7F\xC0-\xFD]/', $str, $var_empty);
}
}
/**
* wordwrap for utf8 encoded strings
*
* @param string $str
* @param integer $len
* @param string $what
* @return string
*/
function wordwrap($str, $width, $break, $cut = false){
if (!$cut) {
$regexp = '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){1,'.$width.'}\b#';
} else {
$regexp = '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){'.$width.'}#';
}
$return = '';
while (preg_match($regexp, $str, $matches)) {
$string = $matches[0];
$str = ltrim(substr($str, strlen($string)));
if (!$cut && isset($str[0]) && in_array($str[0], array('.', '!', ';', ':', '?', ','))) {
$string .= $str[0];
$str = ltrim(substr($str, 1));
}
$return .= $string.$break;
}
return $return.ltrim($str);
}
/**
* check if current node has a $tagName as parent (somewhere, not only the direct parent)
*
* @param string $tagName
* @return bool
*/
function hasParent($tagName) {
return in_array($tagName, $this->parser->openTags);
}
/**
* get tagName of direct parent tag
*
* @param void
* @return string $tagName
*/
function parent() {
return end($this->parser->openTags);
}
}