From fa48de33c2f6cefbac8bfec7cde75b75390d5f39 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Wed, 15 Jun 2016 19:44:15 -0700 Subject: provide syntax based [colour] highlighting on code blocks for popular languages. I'm not happy with the line height on the list elements but couldn't see where this was defaulted. This uses the syntax [code=xxx]some code snippet[/code], where xxx represents a code/language style - with about 18 builtins. --- include/text.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'include/text.php') diff --git a/include/text.php b/include/text.php index bd59aa732..7b43ece43 100644 --- a/include/text.php +++ b/include/text.php @@ -2881,3 +2881,41 @@ function flatten_array_recursive($arr) { } return($ret); } + +function text_highlight($s,$lang) { + + if(! strpos('Text_Highlighter',get_include_path())) { + set_include_path(get_include_path() . PATH_SEPARATOR . 'library/Text_Highlighter'); + head_add_css('/library/Text_Highlighter/sample.css'); + } + require_once('library/Text_Highlighter/Text/Highlighter.php'); + require_once('library/Text_Highlighter/Text/Highlighter/Renderer/Html.php'); + $options = array( + 'numbers' => HL_NUMBERS_LI, + 'tabsize' => 4, + ); + $tag_added = false; + $s = trim(html_entity_decode($s,ENT_COMPAT)); + $s = str_replace(" ","\t",$s); + if($lang === 'php') { + if(strpos('setRenderer($renderer); + $o = $hl->highlight($s); + $o = str_replace(" ","    ",$o); + + if($tag_added) { + $b = substr($o,0,strpos($o,'
  • ')); + $e = substr($o,strpos($o,'
  • ')); + $o = $b . $e; + } + + return('' . $o . ''); +} + -- cgit v1.2.3 From 854c23a751bc8a24e4c43e2d16175bcef6021663 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Wed, 15 Jun 2016 19:59:30 -0700 Subject: This was the reason for the large line widths - an extra linefeed after each li element --- include/text.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/text.php') diff --git a/include/text.php b/include/text.php index 7b43ece43..ec42c650d 100644 --- a/include/text.php +++ b/include/text.php @@ -2908,7 +2908,7 @@ function text_highlight($s,$lang) { $hl = Text_Highlighter::factory($lang); $hl->setRenderer($renderer); $o = $hl->highlight($s); - $o = str_replace(" ","    ",$o); + $o = str_replace([" ","\n"],["    ",''],$o); if($tag_added) { $b = substr($o,0,strpos($o,'
  • ')); -- cgit v1.2.3 From 1dc35db1fe68514b55ddcc70e60dd06b117f81b7 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Wed, 15 Jun 2016 20:25:19 -0700 Subject: ignore case in language names and add 'js' as an alias for 'javascript' --- include/text.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'include/text.php') diff --git a/include/text.php b/include/text.php index ec42c650d..59190a9ca 100644 --- a/include/text.php +++ b/include/text.php @@ -2884,9 +2884,11 @@ function flatten_array_recursive($arr) { function text_highlight($s,$lang) { + if($lang === 'js') + $lang = 'javascript'; + if(! strpos('Text_Highlighter',get_include_path())) { set_include_path(get_include_path() . PATH_SEPARATOR . 'library/Text_Highlighter'); - head_add_css('/library/Text_Highlighter/sample.css'); } require_once('library/Text_Highlighter/Text/Highlighter.php'); require_once('library/Text_Highlighter/Text/Highlighter/Renderer/Html.php'); -- cgit v1.2.3 From a2a87fec54e9da8defea30b8e252a8691a83dfc0 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Wed, 15 Jun 2016 22:36:00 -0700 Subject: code comments --- include/text.php | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'include/text.php') diff --git a/include/text.php b/include/text.php index 59190a9ca..50a3d8892 100644 --- a/include/text.php +++ b/include/text.php @@ -2899,6 +2899,11 @@ function text_highlight($s,$lang) { $tag_added = false; $s = trim(html_entity_decode($s,ENT_COMPAT)); $s = str_replace(" ","\t",$s); + + // The highlighter library insists on an opening php tag for php code blocks. If + // it isn't present, nothing is highlighted. So we're going to see if it's present. + // If not, we'll add it, and then quietly remove it after we get the processed output back. + if($lang === 'php') { if(strpos('