From fe541eb52ea8e3d1e765d13c33f5cb6faf2b904c Mon Sep 17 00:00:00 2001 From: saper Date: Sat, 17 Oct 2015 19:47:08 +0200 Subject: [PATCH] DeToc MediaWiki extension 1.0.0 This extension allows the skins to move or remove table of contents on a fully parsed page in HTML. --- DeToc.php | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ extension.json | 13 ++++++++++ 2 files changed, 88 insertions(+) create mode 100644 DeToc.php create mode 100644 extension.json diff --git a/DeToc.php b/DeToc.php new file mode 100644 index 0000000..45f2714 --- /dev/null +++ b/DeToc.php @@ -0,0 +1,75 @@ +. + */ + +/** + * DeToc extension + * + * @file + * @ingroup Extensions + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 3.0 or later + */ + +class DeToc { + public static function RemoveToc($body, &$extracted_toc) { + $extracted_toc = ''; + $last_toc_ptr = 0; + $new_body = ''; + while ( preg_match( '/
(.*)/s', + $body, $toc_matches, + PREG_OFFSET_CAPTURE, $last_toc_ptr ) === 1 ) { + + $toc_offset = $toc_matches[0][1]; + + $rest = $toc_matches[1][0]; + $rest_ptr = $toc_matches[1][1]; + + $divlevel = 1; + $endtoc_ptr = $rest_ptr; + while ( $divlevel > 0 && strlen($rest) > 0) { + if ( preg_match(',<(/?)div.*?>(.*),s', + $rest, $div_matches, + PREG_OFFSET_CAPTURE ) === 1 ) { + + $rest = $div_matches[2][0]; + + if ( $div_matches[1][0] === '/' ) { + $divlevel = $divlevel - 1; + if ( $divlevel > 0 ) { + $endtoc_ptr += $div_matches[2][1]; + } else { + /* We skip the first
, so we need to skip the closing one */ + $endtoc_ptr += $div_matches[0][1]; + } + } else { + $divlevel = $divlevel + 1; + $endtoc_ptr += $div_matches[2][1]; + } + } else { + $rest = ''; + } + } + $extracted_toc .= substr( $body, + $last_toc_ptr + $rest_ptr, $endtoc_ptr - $rest_ptr ); + + $new_body .= substr( $body, $last_toc_ptr, $toc_offset ); + $last_toc_ptr += $endtoc_ptr; + } + $new_body .= substr( $body, $last_toc_ptr ); + return $new_body; + } +} diff --git a/extension.json b/extension.json new file mode 100644 index 0000000..8a8d5bc --- /dev/null +++ b/extension.json @@ -0,0 +1,13 @@ +{ + "name": "DeToc", + "version": "1.0.0", + "author": [ + "[http://saper.info Marcin Cieślak]" + ], + "url": "http://repo.or.cz/DeToc.git", + "description": "Removes or relocates generated table of contents", + "license-name": "GPL-3.0+", + "type": "other", + "AutoloadClasses": { "DeToc": "DeToc.php" }, + "manifest_version": 1 +} -- 2.11.4.GIT