Skip to content

Commit 3915f17

Browse files
committed
1 parent fe350ef commit 3915f17

15 files changed

+820
-1
lines changed

src/Output/HTML/Basic.php

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
/**
3+
* Class Basic
4+
*
5+
* @filesource Basic.php
6+
* @created 26.04.2018
7+
* @package chillerlan\BBCode\Output\HTML
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2018 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\BBCode\Output\HTML;
14+
15+
class Basic extends HTMLModuleAbstract{
16+
17+
/**
18+
* @var array
19+
*/
20+
protected $tags = [
21+
'noparse', 'nobb', 'color', 'font', 'size','br', 'hr', 'clear','img', 'url', 'c', 'list',
22+
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
23+
'strong', 'sub', 'sup', 'del', 'small', 'em', 's', 'b', 'u', 'i', 'tt',
24+
];
25+
26+
/**
27+
* @var array
28+
*/
29+
protected $singletags = ['br', 'hr', 'clear'];
30+
31+
/**
32+
* @var array
33+
*/
34+
protected $noparse = ['noparse', 'nobb', 'c'];
35+
36+
/**
37+
* @return string
38+
*/
39+
protected function transform():string{
40+
41+
if(empty($this->content)){
42+
return '';
43+
}
44+
45+
return '<'.$this->tag.' class="bb-text '.$this->tag.'">'.$this->content.'</'.$this->tag.'>';
46+
}
47+
48+
/**
49+
* @return string
50+
*/
51+
protected function nobb():string{
52+
return $this->noparse();
53+
}
54+
55+
/**
56+
* @return string
57+
*/
58+
protected function noparse():string{
59+
$this->clearPseudoClosingTags();
60+
61+
return '<pre class="bb-noparse">'.$this->content.'</pre>'; // $this->match
62+
}
63+
64+
/**
65+
* @return string
66+
*/
67+
protected function c(){ // @todo
68+
$this->clearPseudoClosingTags();
69+
return '<code class="bb-inline-code" style="display: inline">'.$this->content.'</code>';
70+
}
71+
72+
/**
73+
* @return string
74+
*/
75+
protected function color(){
76+
// @todo: preg_match('/^#([a-f\d]{3}){1,2}$/i', $value)
77+
return '<span class="bb-text color" style="color: '.'; background-color: '.';">'.$this->content.'</span>';
78+
}
79+
80+
/**
81+
* @return string
82+
*/
83+
protected function font(){ // @todo: restrict fonts via classname
84+
return '<span class="bb-text font comic-sans">'.$this->content.'</span>';
85+
}
86+
87+
/**
88+
* @return string
89+
*/
90+
protected function size(){ // @todo: restrict sizes via css
91+
return '<span class="bb-text size extra-tiny">'.$this->content.'</span>';
92+
}
93+
94+
/**
95+
* @return string
96+
*/
97+
public function br():string{ // @todo: adjust padding-bottom
98+
return '<br class="bb-br"/>';
99+
}
100+
101+
/**
102+
* @return string
103+
*/
104+
public function hr():string{ // @todo line style
105+
return '<hr class="bb-hr"/>';
106+
}
107+
108+
/**
109+
* @return string
110+
*/
111+
protected function clear(){
112+
return '<br class="bb-clear '.$this->bbtagIn(['both', 'left', 'right'], 'both').'"/>';
113+
}
114+
115+
/**
116+
* @return string
117+
*/
118+
protected function img():string{
119+
$url = filter_var($this->content, FILTER_VALIDATE_URL);// @todo
120+
121+
if(!$url){
122+
return '';
123+
}
124+
125+
return '<img src="'.$url.'" class="bb-img" alt />'; // @todo: alt, title
126+
}
127+
128+
/**
129+
* @return string
130+
*/
131+
protected function url():string{
132+
133+
if(empty($this->content)){
134+
return '';
135+
}
136+
137+
$url = filter_var($this->bbtag() ?? $this->content, FILTER_VALIDATE_URL);// @todo
138+
$host = parse_url($url, PHP_URL_HOST);
139+
$target = (!empty($host) && (isset($_SERVER['SERVER_NAME']) && $host === $_SERVER['SERVER_NAME'])) || empty($host) ? 'self' : 'blank';
140+
141+
return '<a class="bb-url '.$target.'" '.($url ? ' href="'.$url.'" target="_'.$target.'"' : '').'>'.$this->content.'</a>'; // .$this->getTitle()
142+
143+
}
144+
145+
/**
146+
* @return string
147+
*/
148+
protected function list():string{
149+
150+
if(empty($this->content)){
151+
return '';
152+
}
153+
154+
$ol = ['0', '1', 'a', 'A', 'i', 'I'];
155+
$ul = ['c', 'd', 's'];
156+
$types = [
157+
'0' => 'decimal-leading-zero',
158+
'1' => 'decimal',
159+
'a' => 'lower-alpha',
160+
'A' => 'upper-alpha',
161+
'i' => 'lower-roman',
162+
'I' => 'upper-roman',
163+
'c' => 'circle',
164+
's' => 'square',
165+
'd' => 'disc',
166+
];
167+
168+
$start = $this->bbtag();
169+
$list_tag = (count($this->attributes) === 0 || $this->attributeIn('type', $ul) ? 'ul' : 'ol');
170+
171+
return '<'.$list_tag.' class="bb-list '.$this->attributeKeyIn('type', $types, 'disc').'" '
172+
.(is_numeric($start) && $this->attributeIn('type', $ol) ? ' start="'.ceil($start).'"' : '')
173+
.($this->getAttribute('reversed') && $this->attributeIn('type', $ol) ? ' reversed="true"' : '')
174+
.'>'
175+
.'<li>'.implode(array_slice(explode('[*]', $this->content), true), '</li><li>').'</li>' // nasty
176+
.'</'.$list_tag.'>';
177+
}
178+
179+
}

src/Output/HTML/Code.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Class Code
4+
*
5+
* @filesource Code.php
6+
* @created 25.04.2018
7+
* @package chillerlan\BBCode\Output\HTML
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2018 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\BBCode\Output\HTML;
14+
15+
class Code extends HTMLModuleAbstract{
16+
17+
/**
18+
* @var array
19+
*/
20+
protected $tags = ['code', 'pre', 'css', 'php', 'sql', 'xml', 'html', 'js', 'json', 'nsis'];
21+
22+
/**
23+
* @var array
24+
*/
25+
protected $noparse = ['code', 'pre', 'css', 'php', 'sql', 'xml', 'html', 'js', 'json', 'nsis'];
26+
27+
/**
28+
* @return string
29+
*/
30+
protected function transform():string{
31+
32+
if(empty($this->content)){
33+
return '';
34+
}
35+
36+
$this->clearPseudoClosingTags();
37+
38+
$id = $this->randomID();
39+
$desc = $this->getAttribute('desc');
40+
41+
// @todo
42+
return '<div data-id="'.$id.'" class="expander code-header '.$this->tag.'">'.($desc ? ' - <span>'.$desc.'</span>' : '').'</div>'
43+
.'<pre id="'.$id.'" class="code-body" style="display:'.($this->getAttribute('hide') ? 'none' : 'block').';">'
44+
.'<code class="language-'.$this->tag.'">'.$this->content.'</code></pre>'; // sanitize
45+
}
46+
47+
}

src/Output/HTML/Containers.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Class Containers
4+
*
5+
* @filesource Containers.php
6+
* @created 24.04.2018
7+
* @package chillerlan\BBCode\Output\HTML
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2018 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\BBCode\Output\HTML;
14+
15+
class Containers extends HTMLModuleAbstract{
16+
17+
/**
18+
* @var array
19+
*/
20+
protected $tags = ['p', 'div', 'left', 'right', 'center'];
21+
22+
/**
23+
* @return string
24+
*/
25+
protected function transform():string{
26+
27+
if(empty($this->content)){
28+
return '';
29+
}
30+
31+
$align_attr = ['left', 'center', 'right', 'justify', 'start', 'end', 'inherit'];
32+
$tag = $this->tagIn(['p', 'div'], 'p');
33+
$align = $this->tagIn($align_attr, '');
34+
35+
if(!$align){
36+
$align = $this->attributeIn('align', $align_attr, 'left');
37+
}
38+
39+
return '<'.$tag.' class="bb-container '.$align.'">'.$this->content.'</'.$tag.'>';
40+
}
41+
}

src/Output/HTML/Expanders.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Class Expanders
4+
*
5+
* @filesource Expanders.php
6+
* @created 24.04.2018
7+
* @package chillerlan\BBCode\Output\HTML
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2018 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\BBCode\Output\HTML;
14+
15+
class Expanders extends HTMLModuleAbstract{
16+
17+
/**
18+
* @var array
19+
*/
20+
protected $tags = ['expander', 'quote', 'spoiler', 'cw'];
21+
22+
/**
23+
* @return string
24+
*/
25+
protected function transform():string{
26+
27+
if(empty($this->content)){
28+
return '';
29+
}
30+
31+
$id = $this->randomID();
32+
33+
return '<div class="'.$this->tag.'-container">'.
34+
'<div data-id="'.$id.'" class="'.$this->tag.'-header expander"><span>'.$this->tag.': '.$this->getAttribute('desc').'</span></div>'. // @todo: desc in tag attribute
35+
'<div id="'.$id.'" class="'.$this->tag.'-body" style="display:none;">'.$this->content.'</div>'.
36+
'</div>';
37+
}
38+
39+
/**
40+
* @return string
41+
*/
42+
protected function quote():string{
43+
44+
if(empty($this->content)){
45+
return '';
46+
}
47+
48+
$id = $this->randomID();
49+
$url = filter_var($this->getAttribute('url'), FILTER_VALIDATE_URL);
50+
51+
// @todo
52+
return '<div class="quote-container">'.
53+
'<div data-id="'.$id.'" class="quote-header expander">quote '.($this->getAttribute('source', null) ?? '').(!empty($url) ? ' <small>[<a href="'.$url.'">link</a>]<small>' : '').'</div>'.
54+
'<blockquote id="'.$id.'" class="quote-body" style="display:block;">'.$this->content.'</blockquote>'. // @todo: collapse (js: collapse child elements etc.)
55+
'</div>';
56+
}
57+
58+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Class HTMLModuleAbstract
4+
*
5+
* @filesource HTMLModuleAbstract.php
6+
* @created 24.04.2018
7+
* @package chillerlan\BBCode\Output\HTML
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2018 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\BBCode\Output\HTML;
14+
15+
use chillerlan\BBCode\Output\BBCodeModuleAbstract;
16+
17+
abstract class HTMLModuleAbstract extends BBCodeModuleAbstract{
18+
19+
}

src/Output/HTML/HTMLOutput.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Class HTMLOutput
4+
*
5+
* @filesource HTMLOutput.php
6+
* @created 23.04.2018
7+
* @package chillerlan\BBCode\Output\HTML
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2018 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\BBCode\Output\HTML;
14+
15+
use chillerlan\BBCode\Output\BBCodeOutputAbstract;
16+
17+
// @todo: sanitize quotes in attributes
18+
final class HTMLOutput extends BBCodeOutputAbstract{
19+
20+
/**
21+
* @var array
22+
*/
23+
protected $modules = [
24+
Basic::class,
25+
Code::class,
26+
Containers::class,
27+
Expanders::class,
28+
Tables::class,
29+
];
30+
31+
/**
32+
* @var string
33+
*/
34+
protected $eol = '<br/>';
35+
36+
}

src/Output/HTML/HTMLSanitizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use chillerlan\BBCode\SanitizerAbstract;
1616

17-
class HTMLSanitizer extends SanitizerAbstract{
17+
final class HTMLSanitizer extends SanitizerAbstract{
1818

1919
/**
2020
* Sanitizes the input before parsing to prevent vulnerabilities or compatibility problems.

0 commit comments

Comments
 (0)