diff --git a/src/markup/engine/__tests__/remarkup/headers.txt b/src/markup/engine/__tests__/remarkup/headers.txt index 92018ee..9e9b460 100644 --- a/src/markup/engine/__tests__/remarkup/headers.txt +++ b/src/markup/engine/__tests__/remarkup/headers.txt @@ -1,18 +1,77 @@ +@nolint (UTF8) + =a= blah blah blah = b = + +Markdown-Style Large Header +==== + +Markdown-Style Small Header +---- + +=== Remarkup-Style Smaller Header + + += ☃☃☃ UTF8 Header ☃☃☃ = + +This is not +a header +=== + +This is also += 2 + 2 +not a header. ~~~~~~~~~~ +

@nolint (UTF8)

+

a

blah blah blah

b

+ +

Markdown-Style Large Header

+ +

Markdown-Style Small Header

+ +

Remarkup-Style Smaller Header

+ +

☃☃☃ UTF8 Header ☃☃☃

+ +

This is not +a header +===

+ +

This is also += 2 + 2 +not a header.

~~~~~~~~~~ -= a = +@nolint (UTF8) + +a += blah blah blah -= b = +b += + +Markdown-Style Large Header +=========================== + +Markdown-Style Small Header +--------------------------- + +Remarkup-Style Smaller Header +----------------------------- + +☃☃☃ UTF8 Header ☃☃☃ +=================== + +This is not a header === + +This is also = 2 + 2 not a header. diff --git a/src/markup/engine/__tests__/remarkup/toc.txt b/src/markup/engine/__tests__/remarkup/toc.txt index 04ec351..5cc7466 100644 --- a/src/markup/engine/__tests__/remarkup/toc.txt +++ b/src/markup/engine/__tests__/remarkup/toc.txt @@ -1,26 +1,29 @@ = [[ http://www.example.com/ | link_name ]] = == **bold** == = http://www.example.com = ~~~~~~~~~~

link_name

bold

http://www.example.com

~~~~~~~~~~ -= [[ http://www.example.com/ | link_name ]] = +[[ http://www.example.com/ | link_name ]] +========================================= -== **bold** == +**bold** +-------- -= http://www.example.com = +http://www.example.com +====================== diff --git a/src/markup/engine/remarkup/blockrule/PhutilRemarkupEngineRemarkupHeaderBlockRule.php b/src/markup/engine/remarkup/blockrule/PhutilRemarkupEngineRemarkupHeaderBlockRule.php index b5252d7..e0040c6 100644 --- a/src/markup/engine/remarkup/blockrule/PhutilRemarkupEngineRemarkupHeaderBlockRule.php +++ b/src/markup/engine/remarkup/blockrule/PhutilRemarkupEngineRemarkupHeaderBlockRule.php @@ -1,143 +1,148 @@ 1) { + $level = ($lines[1][0] == '=') ? 1 : 2; + $text = trim($lines[0]); + } else { + $level = 0; + for ($ii = 0; $ii < min(5, strlen($text)); $ii++) { + if ($text[$ii] == '=') { + ++$level; + } else { + break; + } } + $text = trim($text, ' ='); } - $text = trim($text, ' ='); $engine = $this->getEngine(); if ($engine->isTextMode()) { - return - str_repeat('=', $level).' '. - $this->applyRules($text). - ' '.str_repeat('=', $level); + $char = ($level == 1) ? '=' : '-'; + return $text."\n".str_repeat($char, phutil_utf8_strlen($text)); } $use_anchors = $engine->getConfig('header.generate-toc'); $anchor = null; if ($use_anchors) { $anchor = $this->generateAnchor($level, $text); } $text = phutil_tag( 'h'.($level + 1), array(), array($anchor, $this->applyRules($text))); return $text; } private function generateAnchor($level, $text) { $anchor = strtolower($text); $anchor = preg_replace('/[^a-z0-9]/', '-', $anchor); $anchor = preg_replace('/--+/', '-', $anchor); $anchor = trim($anchor, '-'); $anchor = substr($anchor, 0, 24); $anchor = trim($anchor, '-'); $base = $anchor; $key = self::KEY_HEADER_TOC; $engine = $this->getEngine(); $anchors = $engine->getTextMetadata($key, array()); $suffix = 1; while (!strlen($anchor) || isset($anchors[$anchor])) { $anchor = $base.'-'.$suffix; $anchor = trim($anchor, '-'); $suffix++; } // When a document contains a link inside a header, like this: // // = [[ http://wwww.example.com/ | example ]] = // // ...we want to generate a TOC entry with just "example", but link the // header itself. We push the 'toc' state so all the link rules generate // just names. $engine->pushState('toc'); $text = $this->applyRules($text); $text = $engine->restoreText($text); $anchors[$anchor] = array($level, $text); $engine->popState('toc'); $engine->setTextMetadata($key, $anchors); return phutil_tag( 'a', array( 'name' => $anchor, ), ''); } public static function renderTableOfContents(PhutilRemarkupEngine $engine) { $key = self::KEY_HEADER_TOC; $anchors = $engine->getTextMetadata($key, array()); if (count($anchors) < 2) { // Don't generate a TOC if there are no headers, or if there's only // one header (since such a TOC would be silly). return null; } $depth = 0; $toc = array(); foreach ($anchors as $anchor => $info) { list($level, $name) = $info; while ($depth < $level) { $toc[] = hsprintf(''); $depth--; } $toc[] = phutil_tag( 'li', array(), phutil_tag( 'a', array( 'href' => '#'.$anchor, ), $name)); } while ($depth > 0) { $toc[] = hsprintf(''); $depth--; } return phutil_implode_html("\n", $toc); } }