-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For get the text without the bb-codes (I use to create a search index).
- Loading branch information
Showing
2 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (c) 2016-2017 Visman. All rights reserved. | ||
* @copyright Copyright (c) 2016-2018 Visman. All rights reserved. | ||
* @author Visman <[email protected]> | ||
* @link https://github.com/MioVisman/Parserus | ||
* @license https://opensource.org/licenses/MIT The MIT License (MIT) | ||
|
@@ -205,6 +205,7 @@ public function addBBCode(array $bb) | |
} | ||
|
||
$res['handler'] = isset($bb['handler']) ? $bb['handler'] : null; | ||
$res['text handler'] = isset($bb['text handler']) ? $bb['text handler'] : null; | ||
|
||
$required = []; | ||
$attrs = []; | ||
|
@@ -1067,6 +1068,49 @@ public function getCode($id = 0) | |
return '[' . $tag . $def . $other . ']' . (isset($this->bbcodes[$tag]['single']) ? '' : $body . '[/' . $tag .']'); | ||
} | ||
|
||
/** | ||
* Метод возвращает текст без bb-кодов построенный на основании дерева тегов | ||
* | ||
* @param int $id Указатель на текущий тег | ||
* | ||
* @return string | ||
*/ | ||
public function getText($id = 0) | ||
{ | ||
if (isset($this->data[$id]['tag'])) { | ||
|
||
$body = ''; | ||
foreach ($this->data[$id]['children'] as $cid) { | ||
$child = $this->getText($cid); | ||
if (isset($body{0}, $child{0})) { | ||
$body .= ' ' . $child; | ||
} else { | ||
$body .= $child; | ||
} | ||
} | ||
|
||
$bb = $this->bbcodes[$this->data[$id]['tag']]; | ||
|
||
if (null === $bb['text handler']) { | ||
return $body; | ||
} | ||
|
||
$attrs = []; | ||
foreach ($this->data[$id]['attrs'] as $key => $val) { | ||
if (isset($bb['attrs'][$key])) { | ||
$attrs[$key] = $val; | ||
} | ||
} | ||
|
||
return $bb['text handler']($body, $attrs, $this); | ||
} | ||
|
||
$pid = $this->data[$id]['parent']; | ||
$bb = $this->bbcodes[$this->data[$pid]['tag']]; | ||
|
||
return isset($bb['tags only']) ? '' : $this->data[$id]['text']; | ||
} | ||
|
||
/** | ||
* Метод ищет в текстовых узлах ссылки и создает на их месте узлы с bb-кодами url | ||
* Для уменьшения нагрузки использовать при сохранении, а не при выводе | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
include '../Parserus.php'; | ||
|
||
$parser = new Parserus(); | ||
|
||
echo $parser->setBBCodes([ | ||
['tag' => 'table', | ||
'type' => 'table', | ||
'tags only' => true, | ||
'self nesting' => 3, | ||
'attrs' => [ | ||
'no attr' => true, | ||
'style' => true, | ||
'align' => true, | ||
'background' => true, | ||
'bgcolor' => true, | ||
'border' => true, | ||
'bordercolor' => true, | ||
'cellpadding' => true, | ||
'cellspacing' => true, | ||
'frame' => true, | ||
'rules' => true, | ||
], | ||
'handler' => function($body, $attrs) { | ||
$attr = ''; | ||
foreach ($attrs as $key => $val) { | ||
$attr .= ' ' . $key . '="' . $val . '"'; | ||
} | ||
return '<table' . $attr . '>' . $body . '</table>'; | ||
}, | ||
], | ||
['tag' => 'tr', | ||
'type' => 'tr', | ||
'parents' => ['table', 't'], | ||
'tags only' => true, | ||
'self nesting' => 3, | ||
'attrs' => [ | ||
'no attr' => true, | ||
'style' => true, | ||
], | ||
'handler' => function($body, $attrs) { | ||
$attr = ''; | ||
foreach ($attrs as $key => $val) { | ||
$attr .= ' ' . $key . '="' . $val . '"'; | ||
} | ||
return '<tr' . $attr . '>' . $body . '</tr>'; | ||
}, | ||
], | ||
['tag' => 'th', | ||
'type' => 'block', | ||
'parents' => ['tr'], | ||
'self nesting' => 3, | ||
'attrs' => [ | ||
'no attr' => true, | ||
'style' => true, | ||
'colspan' => true, | ||
'rowspan' => true, | ||
], | ||
'handler' => function($body, $attrs) { | ||
$attr = ''; | ||
foreach ($attrs as $key => $val) { | ||
$attr .= ' ' . $key . '="' . $val . '"'; | ||
} | ||
return '<th' . $attr . '>' . $body . '</th>'; | ||
}, | ||
], | ||
['tag' => 'td', | ||
'type' => 'block', | ||
'parents' => ['tr'], | ||
'self nesting' => 3, | ||
'attrs' => [ | ||
'no attr' => true, | ||
'style' => true, | ||
'colspan' => true, | ||
'rowspan' => true, | ||
], | ||
'handler' => function($body, $attrs) { | ||
$attr = ''; | ||
foreach ($attrs as $key => $val) { | ||
$attr .= ' ' . $key . '="' . $val . '"'; | ||
} | ||
return '<td' . $attr . '>' . $body . '</td>'; | ||
}, | ||
], | ||
['tag' => 'email', | ||
'type' => 'email', | ||
'attrs' => [ | ||
'Def' => [ | ||
'format' => '%^[^\x00-\x1f\s]+?@[^\x00-\x1f\s]+$%', | ||
], | ||
'no attr' => [ | ||
'body format' => '%^[^\x00-\x1f\s]+?@[^\x00-\x1f\s]+$%D', | ||
'text only' => true, | ||
], | ||
], | ||
'handler' => function($body, $attrs) { | ||
if (empty($attrs['Def'])) { | ||
return '<a href="mailto:' . $body . '">' . $body . '</a>'; | ||
} else { | ||
return '<a href="mailto:' . $attrs['Def'] . '">' . $body . '</a>'; | ||
} | ||
}, | ||
'text handler' => function($body, $attrs) { | ||
if (empty($attrs['Def'])) { | ||
return $body; | ||
} else { | ||
return $body . ' ' . $attrs['Def']; | ||
} | ||
}, | ||
], | ||
])->parse(' | ||
[table align=right border=1 bordercolor=#ccc cellpadding=5 cellspacing=0 style="border-collapse:collapse; width:500px"] | ||
[tr] | ||
[th style="width:50%"]Position[/th] | ||
[th style=width:50%]Astronaut[/th] | ||
[/tr] | ||
[tr] | ||
[td]Commander[/td] | ||
[td]Neil A. Armstrong[/td] | ||
[/tr] | ||
[tr] | ||
[td]Command Module Pilot[/td] | ||
[td]Michael Collins[/td] | ||
[/tr] | ||
[tr] | ||
[td]Lunar Module Pilot[/td] | ||
[td]Edwin "Buzz" E. Aldrin, Jr.[/td] | ||
[/tr] | ||
[/table] | ||
[[email protected]]My email[/email] | ||
[email][email protected][/email] | ||
')->getText(); | ||
|
||
#output: | ||
# | ||
#Position Astronaut Commander Neil A. Armstrong Command Module Pilot Michael Collins Lunar Module Pilot Edwin "Buzz" E. Aldrin, Jr. | ||
#My email [email protected] | ||
#[email protected] | ||
# |