Permalink
Browse files

Wiki_Pages::table_of_contents - use functions to filter titles of hea…

…ders to produce anchors for TOC.
  • Loading branch information...
Rad Geek
Rad Geek committed Dec 26, 2016
1 parent 0bb3d7e commit b51af13fec006373d51fda5109aa8b6e1ce26763
Showing with 23 additions and 10 deletions.
  1. +18 −6 controllers/wiki_pages.php
  2. +2 −2 lib/class_wikiparser.php
  3. +3 −2 lib/wpw_wikiparser.php
View
@@ -45,8 +45,17 @@ function post_revisions() {
}
}
function table_of_contents($content) {
protected function _header_to_slug ($title) {
return trim(strip_tags($title));
}
protected function _header_to_anchored ($m) {
$slug = $this->_header_to_slug($m[2]);
return '<a name="'.$slug.'"></a><' . $m[1] . '>' . $m[2] . '</' . $m[3] . '>';
}
function table_of_contents($content) {
//This creates the Table of Contents
global $wpdb,$post;
@@ -65,9 +74,9 @@ function table_of_contents($content) {
}
preg_match_all("|<h2[^>]*>(.*)</h2>|", $content, $h2s, PREG_PATTERN_ORDER);
$content = preg_replace("|<h2[^>]*>(.*)</h2>|", "<a name='$1'></a><h2>$1</h2>", $content);
$content = preg_replace("|<h3[^>]*>(.*)</h3>|", "<a name='$1'></a><h3>$1</h3>", $content);
$h2s = array_map('strip_tags', $h2s[1]);
$content = preg_replace_callback("|<(h2[^>]*)>(.*)</(h2)>|", [$this, '_header_to_anchored'], $content);
$content = preg_replace_callback("|<(h3[^>]*)>(.*)</(h3)>|", [$this, '_header_to_anchored'], $content);
$h2s = $h2s[1];
$content = str_replace("\n", "::newline::", $content);
preg_match_all("|</h2.*>(.*)<h2>|U", $content, $h3s_contents, PREG_PATTERN_ORDER);
@@ -88,10 +97,12 @@ function table_of_contents($content) {
}
$table = "<ol class='content_list'>";
foreach($h2s as $key => $h2) {
$table .= "<li><a href='#$h2'>".($key+1)." ".$h2."</a></li>";
$slug = $this->_header_to_slug($h2);
$table .= "<li><a href='#${slug}'>".($key+1)." ".strip_tags($h2)."</a></li>";
if (!empty($h3s[$key][1])) {
foreach($h3s[$key][1] as $key1 => $h3) {
$table .= "<li class='lvl2'><a href='#$h3'>".($key+1).".".($key1+1)." ".$h3."</a></li>";
$slug = $this->_header_to_slug($h3);
$table .= "<li class='lvl2'><a href='#${slug}'>".($key+1).".".($key1+1)." ".strip_tags($h3)."</a></li>";
}
}
}
@@ -215,6 +226,7 @@ function wiki_parser($content) {
function get_content($content, $class = null ){
global $post;
return '<div id="wpw_read_div" '.$class.'>'.$this->table_of_contents( wptexturize( $this->wiki_parser($content) ) ).'</div>';
}
View
@@ -47,6 +47,7 @@ function handle_sections($matches) {
$content = $matches[2];
$this->stop = true;
// avoid accidental run-on emphasis
return $this->emphasize_off() . "\n\n<h{$level}>{$content}</h{$level}>\n\n";
}
@@ -359,7 +360,7 @@ function parse_line($line) {
// suppress linebreaks for the next line if we just displayed one; otherwise re-enable them
if ($isline) $this->suppress_linebreaks = ($called['newline'] || $called['sections']);
return $line;
}
@@ -460,7 +461,6 @@ function parse($text,$title="") {
$output = preg_replace_callback('/<nowiki><\/nowiki>/i',array(&$this,"handle_restore_nowiki"),$output);
return $output;
}
View
@@ -212,7 +212,7 @@ function parse_line($line) {
$called = array();
//$line = trim($line);
foreach ($line_regexes as $func=>$regex) {
if (preg_match("\007" . $regex ."\007ix",$line,$matches)) {
$called[$func] = true;
@@ -221,6 +221,7 @@ function parse_line($line) {
if ($this->stop || $this->stop_all) break;
}
}
if (!$this->stop_all) {
$this->stop = false;
foreach ($char_regexes as $func=>$regex) {
@@ -229,7 +230,7 @@ function parse_line($line) {
if ($this->stop) break;
}
}
$isline = strlen(trim($line))>0;
// if this wasn't a list item, and we are in a list, close the list tag(s)

0 comments on commit b51af13

Please sign in to comment.