Stud.IP
6.0
|
Public Member Functions | |
__construct ($markup_rules=[]) | |
addMarkup ($name, $start, $end, $callback, $before=null) | |
getMarkup ($name) | |
removeMarkup ($name) | |
format ($text) | |
quote ($text) | |
isInsideOf ($rule) | |
Protected Member Functions | |
formatParts ($text, &$parts, $open_rule=NULL) | |
This class implements a somewhat generic text markup parser. It is optimized for rules of the form:
markup_rule : START_TAG | START_TAG markup END_TAG markup : TEXT | TEXT markup_rule markup
where START_TAG and END_TAG are defined using regular expressions. All rules are applied simultaneously, i.e. the output of a markup rule is not processed again by the parser. The order of the rules matters, however, because the first matching expression determines which markup rule is applied (in case multiple rules match at the same position in the input string).
This example adds a new markup rule for 'smile' that replaces each occurrence of the string ':-)' with a corresponding image tag:
$markup->addMarkup('smile', ':-)', NULL, function($markup) { return '
'; } );
This example adds markup for the BBCode '[b]...[/b]' construct:
$markup->addMarkup('bold', '[b]', '[\/b]', function($markup, $matches, $contents) { return '' . $contents . ''; } );
__construct | ( | $markup_rules = [] | ) |
Initializes a new TextFormat instance with an initial set of markup rules.
array | $markup_rules | list of markup rules |
addMarkup | ( | $name, | |
$start, | |||
$end, | |||
$callback, | |||
$before = null |
|||
) |
Adds a new markup rule to this TextFormat instance. This can also be used to replace an existing markup rule. The end regular expression is optional (i.e. may be NULL) to indicate that this rule has an empty content model. The callback is called whenever the rule matches and is passed the following arguments:
Sometimes you may want your rule to apply before another specific rule will apply. For this case the parameter $before defines a rulename of existing markup, before which your rule should apply.
string | $name | name of this rule |
string | $start | start regular expression |
string | $end | end regular expression (optional) |
callback | $callback | function generating output of this rule |
string | $before | mark before which rule this rule should be appended |
format | ( | $text | ) |
Applies the markup rules to the input text and returns the result.
string | $text | string to format |
|
protected |
Internal method used by format() to apply markup rules to the individual tokens of the input string. $open_rule indicates whether a closing element (and which one) is expected.
string | $text | string to format |
array | $pars | token list of input string |
array | $open_rule | open markup rule, if any (may be NULL) |
getMarkup | ( | $name | ) |
Returns a single markup-rule if it exists.
isInsideOf | ( | $rule | ) |
Return true if the current markup is surrounded by another markup.
string | $rule | Name of the rule (it's key in markup_rules). |
quote | ( | $text | ) |
Quotes the input text in a way appropriate for the output format, but does not apply any markup rules. This could involve escaping special characters (similar to htmlentities) or other processing.
The default implementation in this class does nothing.
string | $text | string to quote |
removeMarkup | ( | $name | ) |
Removes a markup rule from this TextFormat instance.
string | $name | name of the rule |