From 38c5616a185f97a654510bad119562a204110070 Mon Sep 17 00:00:00 2001 From: danpros Date: Fri, 23 Feb 2024 20:31:29 +0700 Subject: [PATCH] Cleaning up TOC --- system/includes/functions.php | 240 ++++++++++-------------------------------- 1 file changed, 56 insertions(+), 184 deletions(-) diff --git a/system/includes/functions.php b/system/includes/functions.php index 442032f..cf3bc7d 100644 --- a/system/includes/functions.php +++ b/system/includes/functions.php @@ -520,34 +520,7 @@ function get_posts($posts, $page = 1, $perpage = 0) $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); $toc = explode('', $post->body); if (isset($toc['1'])) { - $state = config('toc.state'); - if ($state !== 'open') { - $state = ''; - } - $label = config('toc.label'); - if (empty($label)) { - $label = 'Table of Contents'; - } - $style = config('toc.style'); - if ($style == 'default' || empty($style)) { - $style = ''; - } else { - $style = ''; - } - $load = << - document.addEventListener("DOMContentLoaded", function() { - if (document.getElementById('toc-wrapper.post-{$post->date}').parentNode.classList.contains('post-{$post->date}')) { - generateTOC('.post-{$post->date}'); - } else { - document.getElementById('toc-wrapper.post-{$post->date}').parentNode.classList.add('post-{$post->date}'); - generateTOC('.post-{$post->date}'); - } - - }); - - EOF; - $post->body = $toc['0'] . '' . $toc['1']; + $post->body = insert_toc($toc['0'], $toc['1'], 'post-' . $post->date); } // Convert image tags to figures @@ -616,34 +589,7 @@ function get_pages($pages, $page = 1, $perpage = 0) $toc = explode('', $post->body); if (isset($toc['1'])) { - $state = config('toc.state'); - if ($state !== 'open') { - $state = ''; - } - $label = config('toc.label'); - if (empty($label)) { - $label = 'Table of Contents'; - } - $style = config('toc.style'); - if ($style == 'default' || empty($style)) { - $style = ''; - } else { - $style = ''; - } - $load = << - document.addEventListener("DOMContentLoaded", function() { - if (document.getElementById('toc-wrapper.page-{$post->slug}').parentNode.classList.contains('page-{$post->slug}')) { - generateTOC('.page-{$post->slug}'); - } else { - document.getElementById('toc-wrapper.page-{$post->slug}').parentNode.classList.add('page-{$post->slug}'); - generateTOC('.page-{$post->slug}'); - } - - }); - - EOF; - $post->body = $toc['0'] . '' . $toc['1']; + $post->body = insert_toc($toc['0'], $toc['1'], 'page-' . $post->slug); } if (config('views.counter') == 'true') { @@ -718,34 +664,7 @@ function get_subpages($sub_pages, $page = 1, $perpage = 0) $toc = explode('', $post->body); if (isset($toc['1'])) { - $state = config('toc.state'); - if ($state !== 'open') { - $state = ''; - } - $label = config('toc.label'); - if (empty($label)) { - $label = 'Table of Contents'; - } - $style = config('toc.style'); - if ($style == 'default' || empty($style)) { - $style = ''; - } else { - $style = ''; - } - $load = << - document.addEventListener("DOMContentLoaded", function() { - if (document.getElementById('toc-wrapper.subpage-{$post->slug}').parentNode.classList.contains('subpage-{$post->slug}')) { - generateTOC('.subpage-{$post->slug}'); - } else { - document.getElementById('toc-wrapper.subpage-{$post->slug}').parentNode.classList.add('subpage-{$post->slug}'); - generateTOC('.subpage-{$post->slug}'); - } - - }); - - EOF; - $post->body = $toc['0'] . '' . $toc['1']; + $post->body = insert_toc($toc['0'], $toc['1'], 'subpage-' . $post->slug); } if (config('views.counter') == 'true') { @@ -1027,34 +946,7 @@ function read_category_info($category) $toc = explode('', $desc->body); if (isset($toc['1'])) { - $state = config('toc.state'); - if ($state !== 'open') { - $state = ''; - } - $label = config('toc.label'); - if (empty($label)) { - $label = 'Table of Contents'; - } - $style = config('toc.style'); - if ($style == 'default' || empty($style)) { - $style = ''; - } else { - $style = ''; - } - $load = << - document.addEventListener("DOMContentLoaded", function() { - if (document.getElementById('toc-wrapper.taxonomy-{$desc->slug}').parentNode.classList.contains('taxonomy-{$desc->slug}')) { - generateTOC('.taxonomy-{$desc->slug}'); - } else { - document.getElementById('toc-wrapper.taxonomy-{$desc->slug}').parentNode.classList.add('taxonomy-{$desc->slug}'); - generateTOC('.taxonomy-{$desc->slug}'); - } - - }); - - EOF; - $desc->body = $toc['0'] . '' . $toc['1']; + $desc->body = insert_toc($toc['0'], $toc['1'], 'taxonomy-' . $desc->slug); } $desc->description = get_content_tag("d", $content, get_description($desc->body)); @@ -1286,34 +1178,7 @@ function get_author($name) $toc = explode('', $author->about); if (isset($toc['1'])) { - $state = config('toc.state'); - if ($state !== 'open') { - $state = ''; - } - $label = config('toc.label'); - if (empty($label)) { - $label = 'Table of Contents'; - } - $style = config('toc.style'); - if ($style == 'default' || empty($style)) { - $style = ''; - } else { - $style = ''; - } - $load = << - document.addEventListener("DOMContentLoaded", function() { - if (document.getElementById('toc-wrapper.profile-{$author->slug}').parentNode.classList.contains('profile-{$author->slug}')) { - generateTOC('.profile-{$author->slug}'); - } else { - document.getElementById('toc-wrapper.profile-{$author->slug}').parentNode.classList.add('profile-{$author->slug}'); - generateTOC('.profile-{$author->slug}'); - } - - }); - - EOF; - $author->about = $toc['0'] . '' . $toc['1']; + $author->about = insert_toc($toc['0'], $toc['1'], 'profile-' . $author->slug); } $author->body = $author->about; @@ -1367,34 +1232,7 @@ function get_frontpage() $front->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); $toc = explode('', $front->body); if (isset($toc['1'])) { - $state = config('toc.state'); - if ($state !== 'open') { - $state = ''; - } - $label = config('toc.label'); - if (empty($label)) { - $label = 'Table of Contents'; - } - $style = config('toc.style'); - if ($style == 'default' || empty($style)) { - $style = ''; - } else { - $style = ''; - } - $load = << - document.addEventListener("DOMContentLoaded", function() { - if (document.getElementById('toc-wrapper.page-front').parentNode.classList.contains('page-front')) { - generateTOC('.page-front'); - } else { - document.getElementById('toc-wrapper.page-front').parentNode.classList.add('page-front'); - generateTOC('.page-front'); - } - - }); - - EOF; - $front->body = $toc['0'] . '' . $toc['1']; + $front->body = insert_toc($toc['0'], $toc['1'], 'page-front'); } } else { $front->title = 'Welcome'; @@ -2278,15 +2116,8 @@ function get_teaser($string, $url = null, $char = null) } else { return $string; } - } elseif (strlen(strip_tags($string)) < $char) { - $string = preg_replace('/\s\s+/', ' ', strip_tags($string)); - $string = ltrim(rtrim($string)); - return $string; } else { - $string = preg_replace('/\s\s+/', ' ', strip_tags($string)); - $string = ltrim(rtrim($string)); - $string = substr($string, 0, $char); - $string = substr($string, 0, strrpos($string, ' ')); + $string = shorten($string, $char); return $string; } } @@ -3563,17 +3394,24 @@ function shorten($string = null, $char = null) return; } - if (strlen(strip_tags($string)) < $char) { - $string = preg_replace('/\s\s+/', ' ', strip_tags($string)); - $string = ltrim(rtrim($string)); - return $string; - } else { - $string = preg_replace('/\s\s+/', ' ', strip_tags($string)); - $string = ltrim(rtrim($string)); + libxml_use_internal_errors(true); + $dom = new DOMDocument(); + $dom->loadHTML('' . $string); + $tags_to_remove = array('script', 'style'); + foreach($tags_to_remove as $tag){ + $element = $dom->getElementsByTagName($tag); + foreach($element as $item){ + $item->parentNode->removeChild($item); + } + } + $string = preg_replace('~<(?:!DOCTYPE|/?(?:html|head|body))[^>]*>\s*~i', '', mb_convert_encoding($dom->saveHTML($dom->documentElement), 'UTF-8')); + $string = preg_replace('/\s\s+/', ' ', strip_tags($string)); + $string = ltrim(rtrim($string)); + if (strlen(strip_tags($string)) > $char) { $string = substr($string, 0, $char); $string = substr($string, 0, strrpos($string, ' ')); - return $string; } + return $string; } @@ -3679,3 +3517,37 @@ function publish_scheduled() } } } + +// Prepare toc +function insert_toc($part_1, $part_2, $id) +{ + $state = config('toc.state'); + if ($state !== 'open') { + $state = ''; + } + $label = config('toc.label'); + if (empty($label)) { + $label = 'Table of Contents'; + } + $style = config('toc.style'); + if ($style == 'default' || empty($style)) { + $style = ''; + } else { + $style = ''; + } + $load = << + document.addEventListener("DOMContentLoaded", function() { + if (document.getElementById('toc-wrapper.{$id}').parentNode.classList.contains('{$id}')) { + generateTOC('.{$id}'); + } else { + document.getElementById('toc-wrapper.{$id}').parentNode.classList.add('{$id}'); + generateTOC('.{$id}'); + } + + }); + + EOF; + $result = $part_1 . '' . $part_2; + return $result; +}