devel:parser
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
devel:parser [2016-05-08 06:52] – [Renderer] correct link to Wikipedia 84.142.137.15 | devel:parser [2023-09-18 16:38] (current) – [Linefeed grabbing] Add link to Github for workaround 69.14.68.110 | ||
---|---|---|---|
Line 8: | Line 8: | ||
Broadly these elements are; | Broadly these elements are; | ||
- | - Lexer((__L__exer refers to the class '' | + | - Lexer((__L__exer refers to the class '' |
- Handler((__H__andler refers to the class '' | - Handler((__H__andler refers to the class '' | ||
- | - Parser((__P__arser refers to the class '' | + | - Parser((__P__arser refers to the class '' |
- Renderer((__R__ender refers to some class implementing '' | - Renderer((__R__ender refers to some class implementing '' | ||
Line 46: | Line 46: | ||
==== Lexer ==== | ==== Lexer ==== | ||
- | Defined in '' | + | Defined in the folder [[xref>inc/Parsing/Lexer.php|inc/ |
In the most general sense, it provides a tool for managing complex regular expressions, | In the most general sense, it provides a tool for managing complex regular expressions, | ||
Line 58: | Line 58: | ||
The Lexer as a whole is made of three main classes; | The Lexer as a whole is made of three main classes; | ||
- | * '' | + | * [[xref> |
- | * '' | + | * [[xref> |
- | * '' | + | * [[xref> |
===The need for state=== | ===The need for state=== | ||
Line 85: | Line 85: | ||
<code php> | <code php> | ||
- | $Handler | + | $handler |
- | $Lexer = & new Doku_Lexer($Handler, ' | + | $lexer = new dokuwiki\Lexer\Lexer($handler, ' |
</ | </ | ||
Line 92: | Line 92: | ||
== addEntryPattern / addExitPattern == | == addEntryPattern / addExitPattern == | ||
- | Used to register a pattern for entering and exiting a particular parsing mode. For example; | + | [[xref> |
<code php> | <code php> | ||
Line 98: | Line 98: | ||
// arg1: name of mode where this entry pattern may be used | // arg1: name of mode where this entry pattern may be used | ||
// arg2: name of mode to enter | // arg2: name of mode to enter | ||
- | $Lexer-> | + | $lexer-> |
// arg0: regex to match | // arg0: regex to match | ||
// arg1: name of mode to exit | // arg1: name of mode to exit | ||
- | $Lexer-> | + | $lexer-> |
</ | </ | ||
Line 110: | Line 110: | ||
== addPattern == | == addPattern == | ||
- | Used to trigger additional " | + | [[xref> |
This is best seen from considering the list syntax in the parser. Lists syntax looks like this in DokuWiki; | This is best seen from considering the list syntax in the parser. Lists syntax looks like this in DokuWiki; | ||
Line 122: | Line 122: | ||
</ | </ | ||
- | Using '' | + | Using '' |
<code php> | <code php> | ||
// Match the opening list item and change mode | // Match the opening list item and change mode | ||
- | $Lexer-> | + | $lexer-> |
// Match new list items but stay in the list mode | // Match new list items but stay in the list mode | ||
- | $Lexer-> | + | $lexer-> |
// If it's a linefeed that fails to match the above addPattern rule, exit the mode | // If it's a linefeed that fails to match the above addPattern rule, exit the mode | ||
- | $Lexer-> | + | $lexer-> |
</ | </ | ||
== addSpecialPattern == | == addSpecialPattern == | ||
- | Used to enter a new mode just for the match then drop straight back into the " | + | [[xref> |
<code php> | <code php> | ||
- | $Lexer-> | + | $lexer-> |
</ | </ | ||
== mapHandler == | == mapHandler == | ||
- | Allows | + | [[xref> |
<code php> | <code php> | ||
- | $Lexer-> | + | $lexer-> |
- | $Lexer-> | + | $lexer-> |
- | $Lexer-> | + | $lexer-> |
- | $Lexer-> | + | $lexer-> |
// Both syntaxes should be handled the same way... | // Both syntaxes should be handled the same way... | ||
- | $Lexer-> | + | $lexer-> |
</ | </ | ||
=== Subpatterns Not Allowed === | === Subpatterns Not Allowed === | ||
- | Because the Lexer itself uses subpatterns (inside the '' | + | Because the Lexer itself uses subpatterns (inside the '' |
=== Syntax Errors and State === | === Syntax Errors and State === | ||
Line 167: | Line 167: | ||
<code php> | <code php> | ||
// Use lookahead in entry pattern... | // Use lookahead in entry pattern... | ||
- | $Lexer-> | + | $lexer-> |
- | $Lexer-> | + | $lexer-> |
</ | </ | ||
Line 177: | Line 177: | ||
==== Handler ==== | ==== Handler ==== | ||
- | Defined in '' | + | Defined in [[xref>inc/ |
The Handler is a class providing methods which are called by the Lexer as it matches tokens. It then "fine tunes" the tokens into a sequence of instructions ready for a Renderer. | The Handler is a class providing methods which are called by the Lexer as it matches tokens. It then "fine tunes" the tokens into a sequence of instructions ready for a Renderer. | ||
Line 183: | Line 183: | ||
The Handler as a whole contains the following classes: | The Handler as a whole contains the following classes: | ||
- | * '' | + | * [[xref> |
- | * '' | + | * [[xref> |
- | * '' | + | * [[xref> |
- | * '' | + | * [[xref>dokuwiki\Parsing\Handler\Quote]]: responsible for transforming blockquote tokens (text beginning with one or more >) into instructions while lexing is still in progress |
- | * '' | + | * [[xref> |
- | * '' | + | * [[xref> |
- | * '' | + | * [[xref> |
- | * '' | + | * [[xref> |
- | * '' | + | * [[xref> |
+ | |||
+ | |||
+ | Interfaces: | ||
+ | * [[xref> | ||
+ | * [[xref> | ||
=== Handler Token Methods === | === Handler Token Methods === | ||
Line 200: | Line 205: | ||
<code php> | <code php> | ||
- | $Lexer-> | + | $lexer-> |
- | $Lexer-> | + | $lexer-> |
</ | </ | ||
Line 214: | Line 219: | ||
* @param int pos - byte index where match was made | * @param int pos - byte index where match was made | ||
*/ | */ | ||
- | function file($match, | + | |
- | return | + | return |
} | } | ||
} | } | ||
</ | </ | ||
- | **Note:** a Handler method //must// return | + | **Note:** a Handler method //must// return |
The arguments provided to a handler method are; | The arguments provided to a handler method are; | ||
Line 236: | Line 241: | ||
<code php> | <code php> | ||
- | | + | public |
- | $this-> | + | $this-> |
- | $this-> | + | $this-> |
- | + | ||
- | $this-> | + | $this-> |
- | $this-> | + | $this-> |
- | + | } | |
- | | + | |
- | + | public | |
- | function postConnect() { | + | $this-> |
- | $this-> | + | } |
- | } | + | |
</ | </ | ||
Line 253: | Line 257: | ||
<code php> | <code php> | ||
- | | + | public |
- | + | switch ($state) { | |
- | | + | // The start of the list... |
- | + | case DOKU_LEXER_ENTER: | |
- | | + | // Create the List rewriter, passing in the current CallWriter |
- | case DOKU_LEXER_ENTER: | + | $reWriter |
- | // Create the List rewriter, passing in the current CallWriter | + | |
- | $ReWriter | + | // Replace the current CallWriter with the List rewriter |
- | + | // all incoming tokens (even if not list tokens) | |
- | // Replace the current CallWriter with the List rewriter | + | // are now diverted to the list |
- | // all incoming tokens (even if not list tokens) | + | $this->callWriter |
- | // are now diverted to the list | + | |
- | $this->CallWriter | + | $this->addCall(' |
- | + | ||
- | $this->__addCall(' | + | |
break; | break; | ||
- | | + | |
- | // The end of the list | + | // The end of the list |
- | case DOKU_LEXER_EXIT: | + | case DOKU_LEXER_EXIT: |
- | $this->__addCall(' | + | $this->addCall(' |
- | + | ||
- | // Tell the List rewriter to clean up | + | // Tell the List rewriter to clean up |
- | $this->CallWriter-> | + | $this->callWriter-> |
- | + | ||
- | // Restore the old CallWriter | + | // Restore the old CallWriter |
- | $ReWriter | + | $reWriter |
- | $this->CallWriter | + | $this->callWriter |
- | | + | |
break; | break; | ||
- | | + | |
- | case DOKU_LEXER_MATCHED: | + | case DOKU_LEXER_MATCHED: |
- | $this->__addCall(' | + | $this->addCall(' |
break; | break; | ||
- | | + | |
- | case DOKU_LEXER_UNMATCHED: | + | case DOKU_LEXER_UNMATCHED: |
- | $this->__addCall(' | + | $this->addCall(' |
break; | break; | ||
- | } | ||
- | return TRUE; | ||
} | } | ||
+ | return true; | ||
+ | } | ||
</ | </ | ||
Line 341: | Line 342: | ||
- | In the case of lists, this requires the help of the '' | + | In the case of lists, this requires the help of the '' |
==== Parser ==== | ==== Parser ==== | ||
+ | Defined in [[xref> | ||
- | The Parser acts as the front end to external code and sets up the Lexer with the patterns and modes describing DokuWiki syntax. | + | The [[xref> |
Using the Parser will generally look like: | Using the Parser will generally look like: | ||
<code php> | <code php> | ||
- | // Create the parser | + | // Create the Handler |
- | $Parser | + | $handler |
- | // Create | + | // Create the parser |
- | $Parser-> | + | $parser |
// Add required syntax modes to parser | // Add required syntax modes to parser | ||
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
# etc. | # etc. | ||
$doc = file_get_contents(' | $doc = file_get_contents(' | ||
- | $instructions = $Parser-> | + | $instructions = $parser-> |
</ | </ | ||
More detailed examples are below. | More detailed examples are below. | ||
- | As a whole, the Parser also contains classes representing each available syntax mode, the base class for all of these being '' | + | As a whole, the Parser also contains classes representing each available syntax mode, the base class for all of these being [[xref> |
The //reason// for representing the modes with classes is to avoid repeated calls to the Lexer methods. Without them it would be necessary to hard code each pattern rule for every mode that pattern can be matched in, for example, registering a single pattern rule for the CamelCase link syntax would require something like; | The //reason// for representing the modes with classes is to avoid repeated calls to the Lexer methods. Without them it would be necessary to hard code each pattern rule for every mode that pattern can be matched in, for example, registering a single pattern rule for the CamelCase link syntax would require something like; | ||
<code php> | <code php> | ||
- | $Lexer-> | + | $lexer-> |
- | $Lexer-> | + | $lexer-> |
- | $Lexer-> | + | $lexer-> |
- | $Lexer-> | + | $lexer-> |
- | $Lexer-> | + | $lexer-> |
- | $Lexer-> | + | $lexer-> |
// etc. | // etc. | ||
</ | </ | ||
Line 386: | Line 389: | ||
<code php> | <code php> | ||
- | class Doku_Parser_Mode_CamelCaseLink | + | namespace dokuwiki\Parsing\ParserMode; |
- | + | ||
- | function connectTo($mode) { | + | class CamelCaseLink |
+ | |||
+ | | ||
$this-> | $this-> | ||
- | | + | |
- | ); | + | ); |
} | } | ||
- | | ||
} | } | ||
</ | </ | ||
- | When setting up the Lexer, the Parser calls the '' | + | When setting up the Lexer, the Parser calls the '' |
At the expense of making the Lexer setup harder to understand, this allows the code to be more flexible when adding new syntax. | At the expense of making the Lexer setup harder to understand, this allows the code to be more flexible when adding new syntax. | ||
Line 419: | Line 423: | ||
< | < | ||
- | Array | + | Array( |
- | ( | + | [0] => Array( |
- | [0] => Array | + | |
- | | + | |
[0] => document_start | [0] => document_start | ||
- | [1] => Array | + | [1] => Array() |
- | | + | |
- | | + | |
[2] => 0 | [2] => 0 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => p_open | [0] => p_open | ||
- | [1] => Array | + | [1] => Array() |
- | | + | |
- | | + | |
[2] => 0 | [2] => 0 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => cdata | [0] => cdata | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => | [0] => | ||
abc | abc | ||
) | ) | ||
- | |||
[2] => 0 | [2] => 0 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => p_close | [0] => p_close | ||
- | [1] => Array | + | [1] => Array() |
- | | + | |
- | | + | |
[2] => 5 | [2] => 5 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => table_open | [0] => table_open | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => 3 | [0] => 3 | ||
[1] => 2 | [1] => 2 | ||
) | ) | ||
- | |||
[2] => 5 | [2] => 5 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => tablerow_open | [0] => tablerow_open | ||
- | [1] => Array | + | [1] => Array() |
- | | + | |
- | | + | |
[2] => 5 | [2] => 5 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => tablecell_open | [0] => tablecell_open | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => 1 | [0] => 1 | ||
[1] => left | [1] => left | ||
) | ) | ||
- | |||
[2] => 5 | [2] => 5 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => cdata | [0] => cdata | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => Row 0 Col 1 | [0] => Row 0 Col 1 | ||
) | ) | ||
- | |||
[2] => 7 | [2] => 7 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => cdata | [0] => cdata | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => | [0] => | ||
) | ) | ||
- | |||
[2] => 19 | [2] => 19 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => tablecell_close | [0] => tablecell_close | ||
- | [1] => Array | + | [1] => Array() |
- | | + | |
- | | + | |
[2] => 23 | [2] => 23 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => tablecell_open | [0] => tablecell_open | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => 1 | [0] => 1 | ||
[1] => left | [1] => left | ||
) | ) | ||
- | |||
[2] => 23 | [2] => 23 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => cdata | [0] => cdata | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => Row 0 Col 2 | [0] => Row 0 Col 2 | ||
) | ) | ||
- | |||
[2] => 24 | [2] => 24 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => cdata | [0] => cdata | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => | [0] => | ||
) | ) | ||
- | |||
[2] => 36 | [2] => 36 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => tablecell_close | [0] => tablecell_close | ||
- | [1] => Array | + | [1] => Array() |
- | | + | |
- | | + | |
[2] => 41 | [2] => 41 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => tablecell_open | [0] => tablecell_open | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => 1 | [0] => 1 | ||
[1] => left | [1] => left | ||
) | ) | ||
- | |||
[2] => 41 | [2] => 41 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => cdata | [0] => cdata | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => Row 0 Col 3 | [0] => Row 0 Col 3 | ||
) | ) | ||
- | |||
[2] => 42 | [2] => 42 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => cdata | [0] => cdata | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => | [0] => | ||
) | ) | ||
- | |||
[2] => 54 | [2] => 54 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => tablecell_close | [0] => tablecell_close | ||
- | [1] => Array | + | [1] => Array() |
- | | + | |
- | | + | |
[2] => 62 | [2] => 62 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => tablerow_close | [0] => tablerow_close | ||
- | [1] => Array | + | [1] => Array() |
- | | + | |
- | | + | |
[2] => 63 | [2] => 63 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => tablerow_open | [0] => tablerow_open | ||
- | [1] => Array | + | [1] => Array() |
- | | + | |
- | | + | |
[2] => 63 | [2] => 63 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => tablecell_open | [0] => tablecell_open | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => 1 | [0] => 1 | ||
[1] => left | [1] => left | ||
) | ) | ||
- | |||
[2] => 63 | [2] => 63 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => cdata | [0] => cdata | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => Row 1 Col 1 | [0] => Row 1 Col 1 | ||
) | ) | ||
- | |||
[2] => 65 | [2] => 65 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => cdata | [0] => cdata | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => | [0] => | ||
) | ) | ||
- | |||
[2] => 77 | [2] => 77 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => tablecell_close | [0] => tablecell_close | ||
- | [1] => Array | + | [1] => Array() |
- | | + | |
- | | + | |
[2] => 81 | [2] => 81 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => tablecell_open | [0] => tablecell_open | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => 1 | [0] => 1 | ||
[1] => left | [1] => left | ||
) | ) | ||
- | |||
[2] => 81 | [2] => 81 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => cdata | [0] => cdata | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => Row 1 Col 2 | [0] => Row 1 Col 2 | ||
) | ) | ||
- | |||
[2] => 82 | [2] => 82 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => cdata | [0] => cdata | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => | [0] => | ||
) | ) | ||
- | |||
[2] => 94 | [2] => 94 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => tablecell_close | [0] => tablecell_close | ||
- | [1] => Array | + | [1] => Array() |
- | | + | |
- | | + | |
[2] => 99 | [2] => 99 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => tablecell_open | [0] => tablecell_open | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => 1 | [0] => 1 | ||
[1] => left | [1] => left | ||
) | ) | ||
- | |||
[2] => 99 | [2] => 99 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => cdata | [0] => cdata | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => Row 1 Col 3 | [0] => Row 1 Col 3 | ||
) | ) | ||
- | |||
[2] => 100 | [2] => 100 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => cdata | [0] => cdata | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => | [0] => | ||
) | ) | ||
- | |||
[2] => 112 | [2] => 112 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => tablecell_close | [0] => tablecell_close | ||
- | [1] => Array | + | [1] => Array() |
- | | + | |
- | | + | |
[2] => 120 | [2] => 120 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => tablerow_close | [0] => tablerow_close | ||
- | [1] => Array | + | [1] => Array() |
- | | + | |
- | | + | |
[2] => 121 | [2] => 121 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => table_close | [0] => table_close | ||
- | [1] => Array | + | [1] => Array() |
- | | + | |
- | | + | |
[2] => 121 | [2] => 121 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => p_open | [0] => p_open | ||
- | [1] => Array | + | [1] => Array() |
- | | + | |
- | | + | |
[2] => 121 | [2] => 121 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => cdata | [0] => cdata | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => def | [0] => def | ||
) | ) | ||
- | |||
[2] => 122 | [2] => 122 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => p_close | [0] => p_close | ||
- | [1] => Array | + | [1] => Array() |
- | | + | |
- | | + | |
[2] => 122 | [2] => 122 | ||
) | ) | ||
- | + | | |
- | | + | |
- | | + | |
[0] => document_end | [0] => document_end | ||
- | [1] => Array | + | [1] => Array() |
- | | + | |
- | | + | |
[2] => 122 | [2] => 122 | ||
) | ) | ||
- | |||
) | ) | ||
</ | </ | ||
Line 842: | Line 676: | ||
< | < | ||
- | [35] => Array | + | [35] => Array( |
- | | + | |
[0] => cdata | [0] => cdata | ||
- | [1] => Array | + | [1] => Array( |
- | | + | |
[0] => def | [0] => def | ||
) | ) | ||
- | |||
[2] => 122 | [2] => 122 | ||
) | ) | ||
</ | </ | ||
- | The first element (index 0 ) is the name of a method or function in the Renderer to execute. | + | The first element (index 0) is the name of a method or function in the Renderer to execute. |
The second element (index 1) is itself an array, each of //its// elements being the arguments for the Renderer method that will be called. | The second element (index 1) is itself an array, each of //its// elements being the arguments for the Renderer method that will be called. | ||
Line 862: | Line 693: | ||
<code php> | <code php> | ||
- | $Render-> | + | $render-> |
</ | </ | ||
- | The third element (index 2) is the byte index of the first character that " | + | The third element (index 2) is the byte index of the first character that " |
**Note:** The Parser' | **Note:** The Parser' | ||
Line 873: | Line 704: | ||
==== Renderer ==== | ==== Renderer ==== | ||
- | The Renderer is a class (or a collection of functions can be used) which you define. The interface is defined in '' | + | The Renderer is a class which you define |
<code php> | <code php> | ||
Line 881: | Line 712: | ||
// snip | // snip | ||
| | ||
- | function header($text, | + | |
| | ||
- | function section_open($level) {} | + | |
| | ||
- | function section_close() {} | + | |
| | ||
- | function cdata($text) {} | + | |
| | ||
- | function p_open() {} | + | |
| | ||
- | function p_close() {} | + | |
| | ||
- | function linebreak() {} | + | |
| | ||
- | function hr() {} | + | |
| | ||
// snip | // snip | ||
Line 904: | Line 735: | ||
The basic principle for how the instructions, | The basic principle for how the instructions, | ||
- | In the section of the Renderer shown above, the '' | + | In the section of the Renderer shown above, the '' |
=== Invoking the Renderer with Instructions === | === Invoking the Renderer with Instructions === | ||
- | It is left up to the client code using the Parser to execute the list of instructions against a Renderer. Typically this will be done using PHP's [[phpfn> | + | It is left up to the client code using the Parser to execute the list of instructions against a Renderer. Typically this will be done using PHP's [[phpfn> |
<code php> | <code php> | ||
// Get a list of instructions from the parser | // Get a list of instructions from the parser | ||
- | $instructions = $Parser-> | + | $instructions = $parser-> |
// Create a renderer | // Create a renderer | ||
- | $Renderer | + | $renderer |
// Loop through the instructions | // Loop through the instructions | ||
- | foreach ( $instructions as $instruction ) { | + | foreach ($instructions as $instruction) { |
- | | + | |
// Execute the callback against the Renderer | // Execute the callback against the Renderer | ||
- | call_user_func_array(array(&$Renderer, $instruction[0]), | + | call_user_func_array([$renderer, $instruction[0]], $instruction[1]); |
} | } | ||
</ | </ | ||
Line 929: | Line 759: | ||
The key Renderer methods for handling the different kinds of link are; | The key Renderer methods for handling the different kinds of link are; | ||
- | * '' | + | * '' |
*This can probably be ignored for spam checking - it shouldn' | *This can probably be ignored for spam checking - it shouldn' | ||
- | * '' | + | * '' |
*Although '' | *Although '' | ||
- | * '' | + | * '' |
*Both '' | *Both '' | ||
- | * '' | + | * '' |
*The '' | *The '' | ||
- | * '' | + | * '' |
*Technically only valid '' | *Technically only valid '' | ||
- | * '' | + | * '' |
*Should only match valid Windows share URLs but check anyway plus '' | *Should only match valid Windows share URLs but check anyway plus '' | ||
- | * '' | + | * '' |
*'' | *'' | ||
- | * '' | + | * '' |
*This shouldn' | *This shouldn' | ||
- | * '' | + | * '' |
*'' | *'' | ||
Line 951: | Line 781: | ||
<code html> | <code html> | ||
- | <a href="http:// | + | <a href="https:// |
</ | </ | ||
The '' | The '' | ||
- | - '' | + | - '' |
- string: a plain text string was used as the title | - string: a plain text string was used as the title | ||
- array (hash): an image was used as the title. | - array (hash): an image was used as the title. | ||
Line 963: | Line 793: | ||
<code php> | <code php> | ||
- | $title = array( | + | $title = [ |
// Could be ' | // Could be ' | ||
- | ' | + | ' |
| | ||
- | // The URL to the image (may be a wiki URL or http:// | + | // The URL to the image (may be a wiki URL or https:// |
- | ' | + | ' |
| | ||
- | // For the alt attribute - a string or NULL | + | // For the alt attribute - a string or null |
- | ' | + | ' |
| | ||
- | // ' | + | // ' |
- | ' | + | ' |
| | ||
- | // Width in pixels or NULL | + | // Width in pixels or null |
- | ' | + | ' |
| | ||
- | // Height in pixels or NULL | + | // Height in pixels or null |
- | ' | + | ' |
| | ||
// Whether to cache the image (for external images) | // Whether to cache the image (for external images) | ||
- | ' | + | ' |
- | ); | + | ]; |
</ | </ | ||
Line 996: | Line 826: | ||
<code php> | <code php> | ||
- | require_once DOKU_INC . ' | + | global $conf; |
- | // Create the parser | + | // Create the Handler |
- | $Parser | + | $handler |
- | // Add the Handler | + | // Create |
- | $Parser-> | + | $parser |
// Load all the modes | // Load all the modes | ||
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $formats = array ( | + | $formats = [ |
' | ' | ||
' | ' | ||
- | ); | + | ]; |
- | foreach ( $formats as $format ) { | + | foreach ($formats as $format) { |
- | $Parser-> | + | $parser-> |
} | } | ||
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | |
- | $Parser-> | + | |
// These need data files. The get* functions are left to your imagination | // These need data files. The get* functions are left to your imagination | ||
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | // not used anymore, and unsure if getWordblocks() actually works here? |
- | $Parser-> | + | //$parser-> |
- | $Parser-> | + | $parser-> |
+ | $parser-> | ||
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
// Loads the raw wiki document | // Loads the raw wiki document | ||
- | $doc = file_get_contents(DOKU_DATA | + | $doc = file_get_contents($conf[' |
// Get a list of instructions | // Get a list of instructions | ||
- | $instructions = $Parser-> | + | $instructions = $parser-> |
// Create a renderer | // Create a renderer | ||
- | require_once DOKU_INC . ' | + | $renderer |
- | $Renderer | + | |
# Load data like smileys into the Renderer here | # Load data like smileys into the Renderer here | ||
// Loop through the instructions | // Loop through the instructions | ||
- | foreach ( $instructions as $instruction ) { | + | foreach ($instructions as $instruction) { |
- | | + | |
// Execute the callback against the Renderer | // Execute the callback against the Renderer | ||
- | call_user_func_array(array(&$Renderer, $instruction[0]), | + | call_user_func_array([$renderer, $instruction[0]], $instruction[1]); |
} | } | ||
// Display the output | // Display the output | ||
- | echo $Renderer->doc; | + | echo $renderer->doc; |
</ | </ | ||
Line 1076: | Line 904: | ||
<code php> | <code php> | ||
- | // Create the parser | + | global |
- | $Parser = & new Doku_Parser(); | + | |
- | // Add the Handler | + | // Create |
- | $Parser-> | + | $handler |
+ | |||
+ | // Create the parser with the handler | ||
+ | $parser = new dokuwiki\Parsing\Parser($handler); | ||
// Load the header mode to find headers | // Load the header mode to find headers | ||
- | $Parser-> | + | $parser-> |
// Load the modes which could contain markup that might be | // Load the modes which could contain markup that might be | ||
// mistaken for a header | // mistaken for a header | ||
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | |
- | $Parser-> | + | |
// Loads the raw wiki document | // Loads the raw wiki document | ||
- | $doc = file_get_contents(DOKU_DATA | + | $doc = file_get_contents($conf[' |
// Get a list of instructions | // Get a list of instructions | ||
- | $instructions = $Parser-> | + | $instructions = $parser-> |
// Use this to watch when we're inside the section we want | // Use this to watch when we're inside the section we want | ||
- | $inSection = FALSE; | + | $inSection = false; |
$startPos = 0; | $startPos = 0; | ||
$endPos = 0; | $endPos = 0; | ||
// Loop through the instructions | // Loop through the instructions | ||
- | foreach ( $instructions as $instruction ) { | + | foreach ($instructions as $instruction) { |
- | | + | if (!$inSection) { |
- | if ( !$inSection ) { | + | |
- | | + | |
// Look for the header for the " | // Look for the header for the " | ||
- | if ( $instruction[0] == ' | + | if ($instruction[0] == ' |
- | trim($instruction[1][0]) == ' | + | trim($instruction[1][0]) == ' |
- | + | ||
$startPos = $instruction[2]; | $startPos = $instruction[2]; | ||
- | $inSection = TRUE; | + | $inSection = true; |
} | } | ||
} else { | } else { | ||
- | | ||
// Look for the end of the section | // Look for the end of the section | ||
- | if ( $instruction[0] == ' | + | if ($instruction[0] == ' |
$endPos = $instruction[2]; | $endPos = $instruction[2]; | ||
break; | break; | ||
Line 1139: | Line 964: | ||
// Normalize and pad the document in the same way the parse does | // Normalize and pad the document in the same way the parse does | ||
// so that byte indexes with match | // so that byte indexes with match | ||
- | $doc = " | + | $doc = " |
// Get the text before the section we want | // Get the text before the section we want | ||
$before = substr($doc, | $before = substr($doc, | ||
- | $section = substr($doc, | + | $section = substr($doc, |
$after = substr($doc, | $after = substr($doc, | ||
</ | </ | ||
Line 1157: | Line 982: | ||
<code php> | <code php> | ||
// A plain list of smiley tokens... | // A plain list of smiley tokens... | ||
- | $smileys = array( | + | $smileys = [ |
': | ': | ||
': | ': | ||
'; | '; | ||
// etc. | // etc. | ||
- | ); | + | ]; |
| | ||
// Create the mode | // Create the mode | ||
- | $SmileyMode | + | $smileyMode |
// Add it to the parser | // Add it to the parser | ||
- | $Parser-> | + | $parser-> |
</ | </ | ||
Line 1175: | Line 1000: | ||
The other modes this applies to are defined by the classes; | The other modes this applies to are defined by the classes; | ||
- | * '' | + | * '' |
- | * '' | + | * '' |
- | * '' | + | * '' |
Each accepts a list of " | Each accepts a list of " | ||
Line 1185: | Line 1010: | ||
<code php> | <code php> | ||
function getSmileys() { | function getSmileys() { | ||
+ | static $smileys = null; | ||
| | ||
- | | + | if (!$smileys) { |
- | + | ||
- | | + | |
- | | + | |
$smileys = array(); | $smileys = array(); | ||
- | | ||
$lines = file( DOKU_CONF . ' | $lines = file( DOKU_CONF . ' | ||
| | ||
foreach($lines as $line){ | foreach($lines as $line){ | ||
- | | ||
//ignore comments | //ignore comments | ||
$line = preg_replace('/# | $line = preg_replace('/# | ||
- | | ||
$line = trim($line); | $line = trim($line); | ||
| | ||
Line 1209: | Line 1029: | ||
} | } | ||
} | } | ||
- | | ||
return $smileys; | return $smileys; | ||
} | } | ||
Line 1218: | Line 1037: | ||
<code php> | <code php> | ||
// Load the smiley patterns into the mode | // Load the smiley patterns into the mode | ||
- | $SmileyMode | + | $smileyMode |
</ | </ | ||
<code php> | <code php> | ||
// Load the associate array in a renderer for lookup on output | // Load the associate array in a renderer for lookup on output | ||
- | $Renderer-> | + | $renderer-> |
</ | </ | ||
Line 1243: | Line 1062: | ||
function getSpamPattern() { | function getSpamPattern() { | ||
static $spamPattern = NULL; | static $spamPattern = NULL; | ||
- | | + | |
- | if ( is_null($spamPattern) ) { | + | if (is_null($spamPattern)) { |
- | | + | |
$lines = @file(DOKU_CONF . ' | $lines = @file(DOKU_CONF . ' | ||
- | | + | if (!$lines) { |
- | | + | |
- | | + | |
$spamPattern = ''; | $spamPattern = ''; | ||
- | | ||
} else { | } else { | ||
- | | ||
$spamPattern = '#'; | $spamPattern = '#'; | ||
$sep = ''; | $sep = ''; | ||
- | | + | foreach ($lines as $line) { |
- | | + | |
- | | + | |
// Strip comments | // Strip comments | ||
- | $line = preg_replace('/# | + | $line = preg_replace('/# |
- | + | ||
// Ignore blank lines | // Ignore blank lines | ||
$line = trim($line); | $line = trim($line); | ||
- | if(empty($line)) continue; | + | if (empty($line)) continue; |
- | + | ||
- | $spamPattern.= $sep.$line; | + | $spamPattern .= $sep . $line; |
- | | + | |
$sep = ' | $sep = ' | ||
} | } | ||
- | | ||
$spamPattern .= '# | $spamPattern .= '# | ||
} | } | ||
} | } | ||
- | | ||
return $spamPattern; | return $spamPattern; | ||
} | } | ||
Line 1282: | Line 1091: | ||
<code php> | <code php> | ||
- | require_once DOKU_INC . ' | + | class Doku_Renderer_SpamCheck extends Doku_Renderer { |
- | class Doku_Renderer_SpamCheck extends Doku_Renderer { | ||
- | | ||
// This should be populated by the code executing the instructions | // This should be populated by the code executing the instructions | ||
- | | + | |
- | + | ||
// An array of instructions that contain spam | // An array of instructions that contain spam | ||
- | | + | |
- | + | ||
// pcre pattern for finding spam | // pcre pattern for finding spam | ||
- | | + | |
- | + | ||
- | function internallink($link, | + | |
- | $this->__checkTitle($title); | + | $this->checkTitle($title); |
} | } | ||
- | | + | |
- | function externallink($link, | + | |
- | $this->__checkLinkForSpam($link); | + | $this->checkLinkForSpam($link); |
- | $this->__checkTitle($title); | + | $this->checkTitle($title); |
} | } | ||
- | | + | |
- | function interwikilink($link, | + | |
- | $this->__checkTitle($title); | + | $this->checkTitle($title); |
} | } | ||
- | | + | |
- | function filelink($link, | + | |
- | $this->__checkLinkForSpam($link); | + | $this->checkLinkForSpam($link); |
- | $this->__checkTitle($title); | + | $this->checkTitle($title); |
} | } | ||
- | | + | |
- | function windowssharelink($link, | + | |
- | $this->__checkLinkForSpam($link); | + | $this->checkLinkForSpam($link); |
- | $this->__checkTitle($title); | + | $this->checkTitle($title); |
} | } | ||
- | | + | |
- | function | + | |
- | $this->__checkLinkForSpam($address); | + | $this->checkLinkForSpam($address); |
- | $this->__checkTitle($title); | + | $this->checkTitle($title); |
} | } | ||
- | | + | |
- | function internalmedialink ($src) { | + | |
- | $this->__checkLinkForSpam($src); | + | $this->checkLinkForSpam($src); |
} | } | ||
- | function externalmedialink($src) { | + | |
- | $this->__checkLinkForSpam($src); | + | $this->checkLinkForSpam($src); |
} | } | ||
- | function | + | |
- | if ( is_array($title) && isset($title[' | + | if (is_array($title) && isset($title[' |
- | $this->__checkLinkForSpam($title[' | + | $this->checkLinkForSpam($title[' |
} | } | ||
} | } | ||
- | | + | |
// Pattern matching happens here | // Pattern matching happens here | ||
- | function | + | |
- | if( preg_match($this-> | + | if (preg_match($this-> |
$spam = $this-> | $spam = $this-> | ||
$spam[3] = $link; | $spam[3] = $link; | ||
$this-> | $this-> | ||
} | } | ||
+ | } | ||
+ | | ||
+ | public getSpamFound() { | ||
+ | return $this-> | ||
} | } | ||
} | } | ||
</ | </ | ||
- | Note the line '' | + | Note the line '' |
Finally we can use this spam checking renderer like; | Finally we can use this spam checking renderer like; | ||
<code php> | <code php> | ||
- | // Create the parser | + | global |
- | $Parser = & new Doku_Parser(); | + | |
- | // Add the Handler | + | // Create |
- | $Parser-> | + | $handler |
+ | |||
+ | // Create the parser with the handler | ||
+ | $parser = new dokuwiki\Parsing\Parser($handler); | ||
// Load the modes which could contain markup that might be | // Load the modes which could contain markup that might be | ||
// mistaken for a link | // mistaken for a link | ||
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | |
- | $Parser-> | + | |
// Load the link modes... | // Load the link modes... | ||
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
// Loads the raw wiki document | // Loads the raw wiki document | ||
- | $doc = file_get_contents(DOKU_DATA | + | $doc = file_get_contents($conf[' |
// Get a list of instructions | // Get a list of instructions | ||
- | $instructions = $Parser-> | + | $instructions = $parser-> |
// Create a renderer | // Create a renderer | ||
- | require_once DOKU_INC . ' | + | $renderer |
- | $Renderer | + | |
// Load the spam regex | // Load the spam regex | ||
- | $Renderer-> | + | $renderer-> |
// Loop through the instructions | // Loop through the instructions | ||
- | foreach ( $instructions as $instruction ) { | + | foreach ($instructions as $instruction) { |
- | | + | |
// Store the current instruction | // Store the current instruction | ||
- | $Renderer-> | + | $renderer-> |
- | | + | call_user_func_array([$renderer, $instruction[0]], $instruction[1]); |
- | call_user_func_array(array(&$Renderer, $instruction[0]), | + | |
} | } | ||
// What spam did we find? | // What spam did we find? | ||
echo '< | echo '< | ||
- | print_r($Renderer->spamFound); | + | print_r($renderer->getSpamFound()); |
echo '</ | echo '</ | ||
</ | </ | ||
Line 1429: | Line 1237: | ||
- Create a parser syntax mode to register with the Lexer | - Create a parser syntax mode to register with the Lexer | ||
- | - Update the '' | + | - Update the '' |
- Update the Handler with a method to catch bookmark tokens | - Update the Handler with a method to catch bookmark tokens | ||
- Update the abstract Renderer as documentation and any concrete Renderer implementations that need it. | - Update the abstract Renderer as documentation and any concrete Renderer implementations that need it. | ||
- | Creating the parser mode means extending the '' | + | Creating the parser mode means extending the '' |
<code php> | <code php> | ||
- | class Doku_Parser_Mode_Bookmark | + | namespace dokuwiki\Parsing\ParserMode; |
- | | + | |
- | | + | class Bookmark |
+ | | ||
// Allow word and space characters | // Allow word and space characters | ||
- | $this-> | + | $this-> |
} | } | ||
- | | ||
} | } | ||
</ | </ | ||
- | This will match the complete bookmark using a single pattern (extracting the bookmark name from the rest of the syntax will be left to the Handler). It uses the Lexer '' | + | This will match the complete bookmark using a single pattern (extracting the bookmark name from the rest of the syntax will be left to the Handler). It uses the Lexer '' |
**Note** the Lexer does not require the start / end pattern delimiters - it takes care of this for you. | **Note** the Lexer does not require the start / end pattern delimiters - it takes care of this for you. | ||
Line 1453: | Line 1261: | ||
Because nothing //inside// the bookmark should be considered valid wiki markup, there is no reference here to other modes which this mode might accept. | Because nothing //inside// the bookmark should be considered valid wiki markup, there is no reference here to other modes which this mode might accept. | ||
- | Next the '' | + | Next the subarray |
<code php> | <code php> | ||
- | function Doku_Parser_Substition() { | + | global $PARSER_MODES; |
- | $modes = array( | + | $PARSER_MODES = [ |
- | ' | + | ... |
- | ' | + | |
- | ' | + | // modes where the token is simply replaced - they can not contain any |
- | ' | + | // other modes |
- | + | ' | |
- | | + | ' |
- | | + | ' |
- | } | + | ' |
+ | ' | ||
+ | ], | ||
+ | |||
+ | | ||
+ | ]; | ||
</ | </ | ||
- | This function | + | This subarray |
- | **Note:** Similar | + | **Note:** Similar |
With the syntax now described, a new method, which matches the name of the mode (i.e. '' | With the syntax now described, a new method, which matches the name of the mode (i.e. '' | ||
Line 1476: | Line 1289: | ||
<code php> | <code php> | ||
class Doku_Handler { | class Doku_Handler { | ||
+ | // ... | ||
- | // ... | ||
- | | ||
// $match is the string which matched the Lexers regex for bookmarks | // $match is the string which matched the Lexers regex for bookmarks | ||
// $state identifies the type of match (see the Lexer notes above) | // $state identifies the type of match (see the Lexer notes above) | ||
// $pos is the byte index in the raw doc of the first character of the match | // $pos is the byte index in the raw doc of the first character of the match | ||
- | function bookmark($match, | + | |
- | + | ||
// Technically don’t need to worry about the state; | // Technically don’t need to worry about the state; | ||
// should always be DOKU_LEXER_SPECIAL or there' | // should always be DOKU_LEXER_SPECIAL or there' | ||
// a very serious bug | // a very serious bug | ||
- | switch ( $state ) { | + | switch ($state) { |
- | | + | |
case DOKU_LEXER_SPECIAL: | case DOKU_LEXER_SPECIAL: | ||
- | | ||
// Attempt to extract the bookmark name | // Attempt to extract the bookmark name | ||
- | if ( preg_match('/ | + | if (preg_match('/ |
- | | + | |
$name = $nameMatch[1]; | $name = $nameMatch[1]; | ||
- | | + | |
// arg0: name of the Renderer method to call | // arg0: name of the Renderer method to call | ||
// arg1: array of arguments to the Renderer method | // arg1: array of arguments to the Renderer method | ||
// arg2: the byte index as before | // arg2: the byte index as before | ||
- | $this->__addCall(' | + | $this->addCall(' |
- | + | ||
- | // If the bookmark didn't have a valid name, simply pass it | + | // If the bookmark didn't have a valid name, simply pass it |
- | // through unmodified as plain text (cdata) | + | // through unmodified as plain text (cdata) |
} else { | } else { | ||
- | + | | |
- | | + | |
- | | + | |
} | } | ||
- | | + | |
- | | + | |
} | } | ||
- | | + | |
- | // Must return | + | // Must return |
- | return | + | return |
} | } | ||
- | | + | |
// ... | // ... | ||
- | | ||
} | } | ||
</ | </ | ||
Line 1525: | Line 1330: | ||
<code php> | <code php> | ||
class Doku_Renderer { | class Doku_Renderer { | ||
- | |||
// ... | // ... | ||
| | ||
- | function bookmark($name) {} | + | |
// ... | // ... | ||
- | |||
} | } | ||
</ | </ | ||
<code php> | <code php> | ||
- | class Doku_Renderer_XHTML | + | class Doku_Renderer_xhtml |
+ | // ... | ||
+ | |||
+ | public function bookmark($name) { | ||
+ | $name = $this-> | ||
- | // ... | ||
- | | ||
- | function bookmark($name) { | ||
- | $name = $this-> | ||
- | | ||
// id is required in XHTML while name still supported in 1.0 | // id is required in XHTML while name still supported in 1.0 | ||
- | echo '<a class=" | + | echo '<a class=" |
- | | + | |
} | } | ||
// ... | // ... | ||
- | |||
} | } | ||
</ | </ | ||
- | See the '' | + | See the '' |
==== Adding Formatting Syntax (with state) ==== | ==== Adding Formatting Syntax (with state) ==== | ||
Line 1574: | Line 1374: | ||
<code php> | <code php> | ||
- | class Doku_Parser_Mode_TextColors | + | namespace dokuwiki\Parsing\ParserMode; |
- | + | ||
- | | + | class TextColors |
- | | + | |
- | var $colors = array(' | + | |
- | + | | |
- | function | + | |
- | | + | |
+ | | ||
// Just to help prevent mistakes using this mode | // Just to help prevent mistakes using this mode | ||
- | if ( !array_key_exists($color, | + | if (!array_key_exists($color, |
- | trigger_error(' | + | trigger_error(' |
} | } | ||
- | | ||
$this-> | $this-> | ||
- | | + | |
// This mode accepts other modes; | // This mode accepts other modes; | ||
- | $this-> | + | $this-> |
- | | + | $PARSER_MODES[' |
- | | + | |
- | | + | |
); | ); | ||
- | | + | |
+ | unset($this-> | ||
} | } | ||
- | | + | |
// connectTo is called once for every mode registered with the Lexer | // connectTo is called once for every mode registered with the Lexer | ||
- | function connectTo($mode) { | + | |
- | | + | |
// The lookahead pattern makes sure there' | // The lookahead pattern makes sure there' | ||
- | $pattern = '<' | + | $pattern = '<' |
- | + | ||
// arg0: pattern to match to enter this mode | // arg0: pattern to match to enter this mode | ||
// arg1: other modes where this pattern may match | // arg1: other modes where this pattern may match | ||
// arg2: name of the this mode | // arg2: name of the this mode | ||
- | $this-> | + | $this-> |
} | } | ||
- | | + | |
// post connect is only called once | // post connect is only called once | ||
- | function postConnect() { | + | |
- | | + | |
// arg0: pattern to match to exit this mode | // arg0: pattern to match to exit this mode | ||
// arg1: name of mode to exit | // arg1: name of mode to exit | ||
- | $this-> | + | $this-> |
- | | + | } |
+ | |||
+ | // if pattern belongs to two or more modes, the one with the lowest sort number wins | ||
+ | public function getSort() { | ||
+ | | ||
} | } | ||
- | | ||
} | } | ||
</ | </ | ||
Line 1626: | Line 1429: | ||
- It actually represents multiple modes, one for each color. The colors need separating into different modes so that '' | - It actually represents multiple modes, one for each color. The colors need separating into different modes so that '' | ||
- These modes can contain other modes, for example '' | - These modes can contain other modes, for example '' | ||
+ | - the constructor unset one of the modes to prevent a formatting mode being nested inside itself (e.g. we don't want '' | ||
- When registering the entry pattern, it's a good idea to check the exit pattern exists (which is done with the lookahead). This should help protect users from themselves, when they forget to add the closing tag. | - When registering the entry pattern, it's a good idea to check the exit pattern exists (which is done with the lookahead). This should help protect users from themselves, when they forget to add the closing tag. | ||
- The entry pattern needs to be registered for each mode within which the color tags could be used. By contrast we only need one exit pattern, so this is placed in the '' | - The entry pattern needs to be registered for each mode within which the color tags could be used. By contrast we only need one exit pattern, so this is placed in the '' | ||
- | With the parsing mode class done, the new modes now need adding to the '' | + | With the parsing mode class done, the new modes now need adding to the '' |
<code php> | <code php> | ||
- | function Doku_Parser_Formatting($remove | + | global |
- | | + | $PARSER_MODES |
- | ' | + | |
- | | + | // modes for styling text -- footnote behaves similar to styling |
- | ' | + | ' |
- | ); | + | ' |
- | | + | |
- | | + | ' |
- | | + | |
- | } | + | |
- | + | ]; | |
- | return $modes; | + | |
- | } | + | |
</ | </ | ||
- | |||
- | **Note** this function is primed to unset one of the modes to prevent a formatting mode being nested inside itself (e.g. we don't want '' | ||
Next the Handler needs updating with one method for each color; | Next the Handler needs updating with one method for each color; | ||
Line 1653: | Line 1452: | ||
<code php> | <code php> | ||
class Doku_Handler { | class Doku_Handler { | ||
- | |||
// ... | // ... | ||
| | ||
- | function red($match, $state, $pos) { | + | |
// The nestingTag method in the Handler is there | // The nestingTag method in the Handler is there | ||
// to save having to repeat the same code many | // to save having to repeat the same code many | ||
Line 1662: | Line 1460: | ||
// instruction for the entry and exit patterns, | // instruction for the entry and exit patterns, | ||
// while passing through the rest as cdata | // while passing through the rest as cdata | ||
- | $this->__nestingTag($match, $state, $pos, ' | + | $this->nestingTag($match, $state, $pos, ' |
- | return | + | return |
} | } | ||
| | ||
- | function yellow($match, | + | |
- | $this->__nestingTag($match, $state, $pos, ' | + | $this->nestingTag($match, $state, $pos, ' |
- | return | + | return |
} | } | ||
| | ||
- | function green($match, | + | |
- | $this->__nestingTag($match, $state, $pos, ' | + | $this->nestingTag($match, $state, $pos, ' |
- | return | + | return |
} | } | ||
| | ||
// ... | // ... | ||
- | | ||
} | } | ||
</ | </ | ||
Line 1685: | Line 1482: | ||
<code php> | <code php> | ||
class Doku_Renderer { | class Doku_Renderer { | ||
- | |||
// ... | // ... | ||
| | ||
- | function red_open() {} | + | |
- | function red_close() {} | + | |
| | ||
- | function yellow_open() {} | + | |
- | function yellow_close() {} | + | |
| | ||
- | function green_open() {} | + | |
- | function green_close() {} | + | |
// ... | // ... | ||
- | |||
} | } | ||
</ | </ | ||
<code php> | <code php> | ||
- | class Doku_Renderer_XHTML | + | class Doku_Renderer_xhtml |
// ... | // ... | ||
| | ||
- | function red_open() { | + | |
echo '< | echo '< | ||
} | } | ||
- | function red_close() { | + | |
echo '</ | echo '</ | ||
} | } | ||
| | ||
- | function yellow_open() { | + | |
echo '< | echo '< | ||
} | } | ||
- | function yellow_close() { | + | |
echo '</ | echo '</ | ||
} | } | ||
| | ||
- | function green_open() { | + | |
echo '< | echo '< | ||
} | } | ||
- | function green_close() { | + | |
echo '</ | echo '</ | ||
} | } | ||
// ... | // ... | ||
- | |||
} | } | ||
</ | </ | ||
- | See the '' | + | See the '' |
==== Adding Block-Level Syntax ==== | ==== Adding Block-Level Syntax ==== | ||
Line 1755: | Line 1548: | ||
This syntax might allow a tool to be added to search wiki pages and find things that still need something doing, as well as making it stand out in the document with some eye-catching style. | This syntax might allow a tool to be added to search wiki pages and find things that still need something doing, as well as making it stand out in the document with some eye-catching style. | ||
- | What's different about this syntax is it should be displayed in a separate block in the document (e.g. inside ''< | + | What's different about this syntax is it should be displayed in a separate block in the document (e.g. inside ''< |
The parser mode for this syntax might be; | The parser mode for this syntax might be; | ||
<code php> | <code php> | ||
- | class Doku_Parser_Mode_Todo | + | namespace dokuwiki\Parsing\ParserMode; |
+ | |||
+ | class Todo extends | ||
| | ||
- | function | + | |
- | | + | |
$this-> | $this-> | ||
- | | + | |
- | | + | |
- | | + | |
); | ); | ||
- | | ||
} | } | ||
| | ||
- | function connectTo($mode) { | + | |
- | | + | |
$pattern = '< | $pattern = '< | ||
- | $this-> | + | $this-> |
} | } | ||
| | ||
- | function postConnect() { | + | |
$this-> | $this-> | ||
+ | } | ||
+ | | ||
+ | public function getSort() { | ||
+ | return 150; | ||
} | } | ||
| | ||
Line 1785: | Line 1581: | ||
</ | </ | ||
- | This mode is then added to the '' | + | This mode is then added to the '' |
<code php> | <code php> | ||
- | function Doku_Parser_BlockContainers() { | + | global $PARSER_MODES; |
- | $modes = array( | + | $PARSER_MODES |
- | ' | + | // containers are complex modes that can contain many other modes |
- | // hr breaks the principle but HRs should not be used in tables / lists | + | // hr breaks the principle but they shouldn' |
- | // so put it here | + | // so they are put here |
- | ' | + | ' |
- | | + | |
- | | + | |
- | | + | ]; |
- | } | + | |
</ | </ | ||
Line 1807: | Line 1602: | ||
// ... | // ... | ||
| | ||
- | function todo($match, | + | |
- | $this->__nestingTag($match, $state, $pos, ' | + | $this->nestingTag($match, $state, $pos, ' |
- | return | + | return |
} | } | ||
| | ||
Line 1817: | Line 1612: | ||
</ | </ | ||
- | But the '' | + | But the [[xref> |
<code php> | <code php> | ||
- | class Doku_Handler_Block | + | namespace dokuwiki\Parsing\Handler; |
- | | + | |
+ | class Block { | ||
// ... | // ... | ||
| | ||
// Blocks don't contain linefeeds | // Blocks don't contain linefeeds | ||
- | | + | |
- | ' | + | ' |
- | ' | + | ' |
- | ' | + | ' |
- | | + | |
- | | + | |
- | | + | ' |
- | ' | + | ]; |
- | ); | + | |
- | + | | |
- | | + | ' |
- | ' | + | ' |
- | | + | |
- | ' | + | |
- | | + | ' |
- | | + | ]; |
- | | + | |
- | ' | + | |
- | ); | + | |
| | ||
</ | </ | ||
- | By registering the '' | + | By registering the '' |
With that done, the Renderers can be updated; | With that done, the Renderers can be updated; | ||
Line 1856: | Line 1649: | ||
// ... | // ... | ||
| | ||
- | function todo_open() {} | + | |
- | function todo_close() {} | + | |
| | ||
// ... | // ... | ||
Line 1865: | Line 1658: | ||
<code php> | <code php> | ||
- | class Doku_Renderer_XHTML | + | class Doku_Renderer_xhtml |
// ... | // ... | ||
| | ||
- | function todo_open() { | + | |
echo '< | echo '< | ||
} | } | ||
- | function todo_close() { | + | |
echo '</ | echo '</ | ||
} | } | ||
Line 1882: | Line 1675: | ||
==== Serializing the Renderer Instructions ==== | ==== Serializing the Renderer Instructions ==== | ||
+ | |||
+ | FIXME Dokuwiki uses a [[caching]] mechanism inherited from the '' | ||
It is possible to serialize the list of instructions output from the Handler, to eliminate the overhead of re-parsing the original document on each request, if the document itself hasn't changed. | It is possible to serialize the list of instructions output from the Handler, to eliminate the overhead of re-parsing the original document on each request, if the document itself hasn't changed. | ||
Line 1888: | Line 1683: | ||
<code php> | <code php> | ||
- | $ID = DOKU_DATA | + | global |
- | $cacheID | + | |
+ | $filename | ||
+ | $cacheId | ||
// If there' | // If there' | ||
// (the original modified), get a fresh list of instructions | // (the original modified), get a fresh list of instructions | ||
- | if ( !file_exists($cacheID) || (filemtime($ID) > filemtime($cacheID)) ) { | + | if (!file_exists($cacheId) || (filemtime($filename) > filemtime($cacheId))) { |
- | + | // Create the Handler | |
- | require_once DOKU_INC . ' | + | $handler |
- | | + | |
- | // Create the parser | + | // Create |
- | $Parser | + | $parser |
- | + | ||
- | // Add the Handler | + | |
- | $Parser-> | + | |
- | + | ||
// Load all the modes | // Load all the modes | ||
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
- | + | ||
// etc. etc. | // etc. etc. | ||
- | | + | |
- | $instructions = $Parser-> | + | $instructions = $parser-> |
- | + | ||
// Serialize and cache | // Serialize and cache | ||
$sInstructions = serialize($instructions); | $sInstructions = serialize($instructions); | ||
- | | + | |
- | if ($fh = @fopen($cacheID, ' | + | if ($fh = @fopen($cacheId, ' |
- | | + | if (fwrite($fh, |
- | if (fwrite($fh, | + | die(" |
- | die(" | + | |
} | } | ||
- | | + | |
fclose($fh); | fclose($fh); | ||
} | } | ||
- | | ||
} else { | } else { | ||
// Load the serialized instructions and unserialize | // Load the serialized instructions and unserialize | ||
- | $sInstructions = file_get_contents($cacheID); | + | $sInstructions = file_get_contents($cacheId); |
$instructions = unserialize($sInstructions); | $instructions = unserialize($sInstructions); | ||
} | } | ||
- | $Renderer | + | $renderer |
- | foreach ( $instructions as $instruction ) { | + | foreach ($instructions as $instruction) { |
call_user_func_array( | call_user_func_array( | ||
- | array(&$Renderer, $instruction[0]), | + | array($renderer, $instruction[0]), |
- | ); | + | ); |
} | } | ||
- | echo $Renderer->doc; | + | echo $renderer->doc; |
</ | </ | ||
Line 1946: | Line 1738: | ||
==== Serializing the Parser ==== | ==== Serializing the Parser ==== | ||
+ | FIXME connecting modes is protected and already called in parse(), but that is outside the cache here. Probably not useful anymore. If still usefull, rewrite with dokuwiki\Cache\Cache | ||
Similar to the above example, it is also possible to serialize the Parser itself, before parsing begins. Because setting up the modes carries a fairly high overhead, this can add a small increase in performance. From loose benchmarking, | Similar to the above example, it is also possible to serialize the Parser itself, before parsing begins. Because setting up the modes carries a fairly high overhead, this can add a small increase in performance. From loose benchmarking, | ||
Line 1952: | Line 1745: | ||
<code php> | <code php> | ||
- | $cacheId = DOKU_CACHE . ' | ||
- | if ( !file_exists($cacheId) ) { | + | global $conf; |
+ | |||
+ | $cacheId = $conf[' | ||
+ | if (!file_exists($cacheId)) { | ||
+ | // Create the parser with the handler | ||
+ | $handler = new Doku_Handler(); | ||
+ | $parser = new dokuwiki\Parsing\Parser($handler); | ||
- | // Create the parser... | ||
- | $Parser = & new Doku_Parser(); | ||
- | $Parser-> | ||
- | | ||
// Load all the modes | // Load all the modes | ||
- | $Parser-> | + | $parser-> |
- | $Parser-> | + | $parser-> |
# etc. | # etc. | ||
- | + | ||
- | // IMPORTANT: call connectModes() | + | |
- | $Parser-> | + | |
- | | + | |
// Serialize | // Serialize | ||
- | $sParser = serialize($Parser); | + | $sParser = serialize($parser); |
- | + | ||
// Write to file | // Write to file | ||
- | if ($fh = @fopen($cacheID, ' | + | if ($fh = @fopen($cacheId, ' |
- | | + | if (fwrite($fh, |
- | if (fwrite($fh, | + | die(" |
- | die(" | + | |
} | } | ||
- | | ||
fclose($fh); | fclose($fh); | ||
} | } | ||
Line 1983: | Line 1772: | ||
} else { | } else { | ||
// Otherwise load the serialized version | // Otherwise load the serialized version | ||
- | $sParser = file_get_contents($cacheID); | + | $sParser = file_get_contents($cacheId); |
- | $Parser | + | $parser |
} | } | ||
- | + | $doc = ... | |
- | $Parser-> | + | $parser-> |
</ | </ | ||
Line 1998: | Line 1787: | ||
===== Testing ===== | ===== Testing ===== | ||
- | The [[wp> | + | See [[devel:unittesting]] for setup and details. |
For the DokuWiki parser, tests have been provided for all the syntax implemented and I // | For the DokuWiki parser, tests have been provided for all the syntax implemented and I // | ||
- | |||
- | To get the tests running, you should only need to modify the file '' | ||
Some notes / recommendations; | Some notes / recommendations; | ||
Line 2012: | Line 1799: | ||
===== Bugs / Issues ===== | ===== Bugs / Issues ===== | ||
- | Some things off the top of my head. | + | Some things off the top of my head. FIXME move to bug tracker? |
==== Order of adding modes important ==== | ==== Order of adding modes important ==== | ||
Line 2089: | Line 1876: | ||
[[bug> | [[bug> | ||
+ | |||
+ | NOTE: See [[https:// | ||
Because the header, horizontal rule, list, table, quote and preformatted (indented text) syntax relies on linefeed characters to mark their starts and ends, they require regexes which consume linefeed characters. This means users need to add an additional linefeed if a table appears immediately after a list, for example. | Because the header, horizontal rule, list, table, quote and preformatted (indented text) syntax relies on linefeed characters to mark their starts and ends, they require regexes which consume linefeed characters. This means users need to add an additional linefeed if a table appears immediately after a list, for example. |
devel/parser.1462683155.txt.gz · Last modified: 2016-05-08 06:52 by 84.142.137.15