AdvancedBBCodeParser
The
AdvancedBBCodeParser is a generic implementation of a bbcode formatter. In contrast
to the previous implementation, this component is much more lightweight and the parser logic is
highly extensible. The parser itself contains a set of four output provider, that can handle font
styles (bold, italic, underlined), font sizes and colors (depending on the corresponding
configuration files) and new lines.
The bbcode parser consists of three classes: the
BBCodeParserDefinition, that represent
an output provider, a provider implementation interface class (
BBCodeParserProvider)
and the parser itself (
AdvancedBBCodeParser). Due to the fact, that each provider
must be able to load a configuration file depending on the current context of the application, the
AdvancedBBCodeParser must be created via the service manager.
In case of extending the list of providers or replacing a particular one, the newly implemented
class must inherit from the
BBCodeParserProvider class. The follwing code box
presents a simple provider implementation:
PHP-Code
class NewLineProvider extends BBCodeParserProvider
{
function NewLineProvider(){
}
function getOutput($string){
return nl2br($string);
}
}
Thanks to the generic implementation, the
AdvancedBBCodeParser can execute as many
providers as you like.
In case of standard application, you need to provide the necessary configuration files, that contain
the font size and color definitions used by the default provider. These configuration files are
expected to reside in the
config::tools::string::bbcpprovider namespace and the
current context path. Thereby, the font size definition must be included in the
APF-Template
{ENVIRONMENT}_fontsize.ini
file and the color definition must be stored in the file
APF-Template
{ENVIRONMENT}_fontcolor.ini
The two configuration files do consist of a fixed section and a mutable amount of size or color
directives:
DEFAULT_fontsize.ini:
APF-Konfiguration
[Sizes]
1 = "10px"
2 = "12px"
3 = "16px"
4 = "18px"
5 = "24px"
6 = "32px"
DEFAULT_fontcolor.ini:
APF-Template
[Colors]
green = "#56a437"
blue = "#002488"
lightblau = "rgb(102 153 255)"
orange = "rgb(233 142 31)"
gray = "#666666"
red = "red"
After providing these configuration files, the parser can be used like described in the following
code box:
PHP-Code
$bP = &$this->__getServiceObject('tools::string','AdvancedBBCodeParser');
echo $bP->parseCode($string);
In order to add further provider or replace existing ones, the
addProvider() and
removeProvider() functions can be used. Please use the following alias names to
address the existing providers:
-
standard.font.style: Formats the font variants (bold, italic, underlined)
-
standard.font.size: Formats the font size declarations
-
standard.font.color: Formats the font color tags
-
standard.newline: Replaces newlines by HTML linebreaks
When adding new output provider, the aliases can be choosen as desired. The following code sample
explains, how to replace existing provider and how to add new output formatter:
PHP-Code
$bP = &$this->__getServiceObject('tools::string','AdvancedBBCodeParser');
// replace an existing provider
$fontProv = new BBCodeParserDefinition('my::namespace::bbcp','MyFontParser');
$bP->addProvider('standard.font.style',$fontProv);
// add a new provider
$specProv = new BBCodeParserDefinition('my::namespace::bbcp','MySpecialParser');
$bP->addProvider('special_provider',$specProv);
// remove an existing provider
$bP->removeProvider('standard.newline');
echo $bP->parseCode($string);
Comments
Do you want to add a comment to the article above, or do you want to post additional hints? So please click
here. Comments already posted can be found below.
There are no comments belonging to this article.