+
[code=php]function text_highlight($s,$lang)[/code]+ +
- function text_highlight($s,$lang)
+
++
[quote]quote[/quote]
quotediff --git a/doc/de/BBCode.md b/doc/de/BBCode.md index cd9fa7673..d852a4a4f 100644 --- a/doc/de/BBCode.md +++ b/doc/de/BBCode.md @@ -42,6 +42,12 @@ Block Tags
+
[code=php]function text_highlight($s,$lang)[/code]+ +
- function text_highlight($s,$lang)
+
++
[quote]Zitat[/quote]
Zitatdiff --git a/include/bbcode.php b/include/bbcode.php index 84425e8ef..359a7ba2f 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -724,6 +724,13 @@ function bb_CleanPictureLinks($text) { return ($text); } +function bb_highlight($match) { + if(in_array(strtolower($match[1]),['php','css','mysql','sql','abap','diff','html','perl','ruby', + 'vbscript','avrc','dtd','java','xml','cpp','python','javascript','js','sh'])) + return text_highlight($match[2],strtolower($match[1])); + return $match[0]; +} + // BBcode 2 HTML was written by WAY2WEB.net // extended to work with Mistpark/Friendica - Mike Macgirvin @@ -776,6 +783,11 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal if (!$tryoembed) $Text = preg_replace("/\[share(.*?)avatar\s?=\s?'.*?'\s?(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","\n[share$1$2]$3[/share]",$Text); + // Check for [code] text here, before the linefeeds are messed with. + // The highlighter will unescape and re-escape the content. + if (strpos($Text,'[code=') !== false) { + $Text = preg_replace_callback("/\[code=(.*?)\](.*?)\[\/code\]/ism", 'bb_highlight', $Text); + } // Convert new line chars to html
' . $o . '
');
+}
diff --git a/library/Text_Highlighter/README b/library/Text_Highlighter/README
new file mode 100644
index 000000000..88f71aed2
--- /dev/null
+++ b/library/Text_Highlighter/README
@@ -0,0 +1,455 @@
+# $Id$
+
+Introduction
+============
+
+Text_Highlighter is a class for syntax highlighting. The main idea is to
+simplify creation of subclasses implementing syntax highlighting for
+particular language. Subclasses do not implement any new functioanality, they
+just provide syntax highlighting rules. The rules sources are in XML format.
+To create a highlighter for a language, there is no need to code a new class
+manually. Simply describe the rules in XML file and use Text_Highlighter_Generator
+to create a new class.
+
+
+This document does not contain a formal description of API - it is very
+simple, and I believe providing some examples of code is sufficient.
+
+
+Highlighter XML source
+======================
+
+Basics
+------
+
+Creating a new syntax highlighter begins with describing the highlighting
+rules. There are two basic elements: block and region. A block is just a
+portion of text matching a regular expression and highlighted with a single
+color. Keyword is an example of a block. A region is defined by two regular
+expressions: one for start of region, and another for the end. The main
+difference from a block is that a region can contain blocks and regions
+(including same-named regions). An example of a region is a group of
+statements enclosed in curly brackets (this is used in many languages, for
+example PHP and C). Also, characters matching start and end of a region may be
+highlighted with their own color, and region contents with another.
+
+Blocks and regions may be declared as contained. Contained blocks and regions
+can only appear inside regions. If a region or a block is not declared as
+contained, it can appear both on top level and inside regions. Block or region
+declared as not-contained can only appear on top level.
+
+For any region, a list of blocks and regions that can appear inside this
+region can be specified.
+
+In this document, the term "color group" is used. Chunks of text assigned to
+same color group will be highlighted with same color. Note that in versions
+prior 0.5.0 color goups were refered as CSS classes, but since 0.5.0 not only
+HTML output is supported, so "color group" is more appropriate term.
+
+Elements
+--------
+
+The toplevel element is
+ *require_once 'Text/Highlighter.php';
+ *$hlSQL = Text_Highlighter::factory('SQL',array('numbers'=>true));
+ *echo $hlSQL->highlight('SELECT * FROM table a WHERE id = 12');
+ *
+ *
+ * @author Andrey Demenev