Stud.IP  5.4
TextFormat Class Reference
Inheritance diagram for TextFormat:
StudipCoreFormat StudipFormat StudipTransformFormat BlubberFormat WikiFormat

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)
 

Detailed Description

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 . ''; } );

Constructor & Destructor Documentation

◆ __construct()

__construct (   $markup_rules = [])

Initializes a new TextFormat instance with an initial set of markup rules.

Parameters
array$markup_ruleslist of markup rules

Member Function Documentation

◆ addMarkup()

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:

  • $markup the markup parser object
  • $matches match results of preg_match for $start
  • $contents (parsed) contents of this markup rule

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.

Parameters
string$namename of this rule
string$startstart regular expression
string$endend regular expression (optional)
callback$callbackfunction generating output of this rule
string$beforemark before which rule this rule should be appended

◆ format()

format (   $text)

Applies the markup rules to the input text and returns the result.

Parameters
string$textstring to format
Returns
string formatted text

◆ formatParts()

formatParts (   $text,
$parts,
  $open_rule = NULL 
)
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.

Parameters
string$textstring to format
array$parstoken list of input string
array$open_ruleopen markup rule, if any (may be NULL)
Returns
string formatted text

◆ getMarkup()

getMarkup (   $name)

Returns a single markup-rule if it exists.

Returns
array: array('start' => "...", 'end' => "...", 'callback' => "...")

◆ isInsideOf()

isInsideOf (   $rule)

Return true if the current markup is surrounded by another markup.

Parameters
string$ruleName of the rule (it's key in markup_rules).
Returns
boolean True if inside of $rule-markup.

◆ quote()

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.

Parameters
string$textstring to quote
Returns
string quoted text

◆ removeMarkup()

removeMarkup (   $name)

Removes a markup rule from this TextFormat instance.

Parameters
string$namename of the rule

The documentation for this class was generated from the following file: