Comments (12)
PHPBB is not official BBCode syntax, it's just a forum that wrote their own code for it.
Decoda is written the way it is to support its lexical parser.
from decoda.
Invision Power Board use this syntax too, and many others services !
from decoda.
The only thing different is [*] which I will not support.
from decoda.
It may be interessent to support a syntax knowned by many users !
from decoda.
Why ? I'ts may be interessent to respect a syntax used by millions of
persons ?
2012/9/20 Miles Johnson [email protected]
The only thing different is [*] which I will not support.
—
Reply to this email directly or view it on GitHubhttps://github.com/milesj/php-decoda/issues/22#issuecomment-8710676.
from decoda.
Because they use regex parsing while I use lexical parsing, and frankly, it's not easy to add support for that kind of tag into the lexical parser. Secondly, how powerful is [*]? From the looks of it, not so much. It wouldn't handle nested lists or tags efficiently.
from decoda.
But you can do modes, with support who wants the user. If you want an idea for the code, I've a code who does not function very well.
from decoda.
Not sure what you are talking about, but feel free to show me an example.
from decoda.
Here is the code :
function liste($m)
{
if( strpos($m[2], '[*]') !== false ) // Si au moins une puce existe entre [list] et [/list]
{
$type = $m[1] == '1' ? 'ol' : 'ul'; // Type de la liste
// On créé un array contenant les éléments de la liste -> explode()
// On supprime les éléments de la liste vides avec array_map() et trim() combiné à array_filter
$items = array_filter(array_map('trim', explode('[*]', $m[2])));
// On ajoute simplement à chaque éléments les éléments li
foreach( $items as &$item )
$item = '<li>' . $item . '</li>';
// On retourne le tout en appliquant un implode sur l'array des listes
return '<' . $type . '>' . implode(" ", $items) . '</' . $type . '>';
}
return $m[0]; // Si y'a pas de puces dans la liste, on remplace rien et on retourne la chaîne
}
/*
@param $string : la chaîne qui doit subir les modifications
@param $o : pattern de l'élément ouvrant (les méta-caractères doivent-être echappés)
@param $c : pattern de l'élément fermant (idem, echapper les méta-caractères)
@param $callbackFunction : function de rappel qui sera appliquée sur chaque chaînes comprises entre $o et $c
*/
function recursiveReplaceElements($string, $o, $c, $callbackFunction)
{
// Captures des tags et leurs positions dans la chaîne
if( !preg_match_all('`(?:' . $o . '|' . $c . ')`', $string, $m, PREG_OFFSET_CAPTURE) )
return $string;
// Construction d'un array à 2 dimensions regroupant chaque blocs de 1er niveaux
// Et leurs niveaux d'imbrications
$i = 0;
$level = 0;
$last = null;
$arrPieces= array();
foreach( $m[0] as $values )
{
// $values[0] => tag ouvrant|fermant
// $values[1] => position du tag dans la chaîne
// Tags ouvrants
if ( strpos($values[0], '/') === false )
{
if( $last == 'start' )
$level--;
$arrPieces[$i][$level]['start'] = $values[1];
$last = 'start';
}
else
{
if( $last == 'end' )
$level++;
$arrPieces[$i][$level]['end'] = $values[1];
$last = 'end';
}
if( $level == 0 && $last == 'end' )
{
ksort($arrPieces[$i]);
$i++;
}
}
// Pas autant de tags fermants que d'ouvrants...
if( $level !== 0 )
{
return $string;
}
$lengthCloseTag = strlen(stripslashes($c)); // longueur du tag fermant
// Remplacements des éléments de sous-niveaux dans chaque niveaux supérieurs
$elementsTopLvl = array();
$replacementsTopLvl = array();
foreach( $arrPieces as $key => $blocks )
{
$lastPiece = $lastPieceReplacement = null;
foreach( $blocks as $lvl => $pos )
{
// Extraction du bloc de la chaîne
$piece = substr($string, $pos['start'], $pos['end'] + $lengthCloseTag - $pos['start']);
// Remplacements
if( $lastPieceReplacement !== null ) // Si éléments de niveaux inférieurs
$lastPieceReplacement = preg_replace_callback('`^' . $o . '(.+)' . $c . '$`s', 'liste', str_replace($lastPiece, $lastPieceReplacement, $piece));
else // Niveau le plus profond
$lastPieceReplacement = preg_replace_callback('`^' . $o . '(.+)' . $c . '$`s', 'liste', $piece);
$lastPiece = $piece;
if( $lvl == 0 )
{
$elementsTopLvl[] = $piece;
$replacementsTopLvl[] = $lastPieceReplacement;
}
}
}
$string = str_replace($elementsTopLvl, $replacementsTopLvl, $string);
return $string;
}
from decoda.
That's using regex which I do not use.
from decoda.
Why ?
from decoda.
Because regex is the worst possible route to take when creating parsers. I use lexical parsing: http://en.wikipedia.org/wiki/Lexical_analysis
from decoda.
Related Issues (20)
- Emoticons not parsed when having the next to it HOT 3
- Uppercase Tags don't set defaults HOT 1
- Two bugs HOT 3
- Links and ClickableHook HOT 8
- Support for BBCode standalone tags? HOT 4
- caching HOT 2
- Problem with ulr tag which contain "_" HOT 2
- Force img tag to be https on https site HOT 3
- Support for [noparse] tag HOT 2
- Library Broken with PHP 7.3 HOT 2
- removeFilter('Video') Broken
- PHP5.6+? HOT 3
- Did not parse color if the color parameter is not enclosed with " HOT 1
- PHP7.2+ HOT 6
- Pass string as method argument HOT 3
- Use MediaEmbed for video filter HOT 1
- Normal "quotes" are not escaped HOT 4
- Url filter problems HOT 1
- Is there any way to use this without composer? HOT 1
- 6 UU
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from decoda.