Skip to content

Commit 7b429a7

Browse files
committed
restructured
1 parent a13154a commit 7b429a7

File tree

5 files changed

+40
-16
lines changed

5 files changed

+40
-16
lines changed

src/Modules/Html5/Singletags.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ class Singletags extends Html5BaseModule implements ModuleInterface{
2525
* @var array
2626
* @see \chillerlan\bbcode\Modules\Tagmap::$tags
2727
*/
28-
protected $tags = ['br', 'clear', 'hr'];
28+
protected $tags = ['br', 'clear', 'hr', 'xkcd'];
2929

3030
/**
3131
* An optional array of tags contained in self::$tags which are marked as "single tag"
3232
*
3333
* @var array
3434
* @see \chillerlan\bbcode\Modules\Tagmap::$singletags
3535
*/
36-
protected $singletags = ['br', 'clear', 'hr'];
36+
protected $singletags = ['br', 'clear', 'hr', 'xkcd'];
3737

3838
/**
3939
* Transforms the bbcode, called from BaseModuleInterface
@@ -44,12 +44,21 @@ class Singletags extends Html5BaseModule implements ModuleInterface{
4444
*/
4545
public function __transform():string{
4646

47-
switch($this->tag){
48-
case 'clear':
49-
return '<br'.$this->getCssClass(['bb-clear', $this->bbtagIn(['both', 'left', 'right'], 'both')]).' />';
50-
default:
51-
return '<'.$this->tag.$this->getCssClass().' />';
47+
if($this->tag === 'clear'){
48+
return '<br'.$this->getCssClass(['bb-clear', $this->bbtagIn(['both', 'left', 'right'], 'both')]).' />';
5249
}
50+
else if($this->tag === 'xkcd'){
51+
$num = intval($this->bbtag());
52+
if(!empty($num)){
53+
return '<a href="https://xkcd.com/'.$num.'/"'.$this->getCssClass(['bb-xkcd']).'>[xkcd '.$num.']</a>';
54+
}
55+
}
56+
else{
57+
return '<'.$this->tag.$this->getCssClass().' />';
58+
}
59+
60+
return '';
61+
5362
}
5463

5564
}

src/Modules/Markdown/Singletags.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ class Singletags extends MarkdownBaseModule implements ModuleInterface{
2525
* @var array
2626
* @see \chillerlan\bbcode\Modules\Tagmap::$tags
2727
*/
28-
protected $tags = ['br', 'hr'];
28+
protected $tags = ['br', 'hr', 'xkcd'];
2929

3030
/**
3131
* An optional array of tags contained in self::$tags which are marked as "single tag"
3232
*
3333
* @var array
3434
* @see \chillerlan\bbcode\Modules\Tagmap::$singletags
3535
*/
36-
protected $singletags = ['br', 'hr'];
36+
protected $singletags = ['br', 'hr', 'xkcd'];
3737

3838
/**
3939
* Transforms the bbcode, called from BaseModuleInterface
@@ -47,6 +47,7 @@ public function __transform():string{
4747
return [
4848
'br' => $this->eol_token,
4949
'hr' => $this->wrap('----', $this->eol_token),
50+
'xkcd' => 'https://xkcd.com/'.intval($this->bbtag()).'/',
5051
][$this->tag];
5152
}
5253

tests/normal/Modules/HTML5ModuleTest.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function testEmptyTags(){
5555
'br' => '<br />',
5656
'col' => '<col />',
5757
'hr' => '<hr />',
58+
'xkcd' => '',
5859
'clear' => '<br class="bb-clear both" />',
5960
];
6061

@@ -166,12 +167,22 @@ public function testSimpletextModule(){
166167
}
167168
}
168169

169-
public function testSingletagModule(){
170-
foreach(array_keys($this->parser->getTagmap(), Singletags::class) as $tag){
171-
$parsed = $this->parser->parse('['.$tag.']');
172-
$expected = $tag === 'clear' ? '<br class="bb-clear both" />' : '<'.$tag.' />';
173-
$this->assertEquals($expected, $parsed);
174-
}
170+
public function singletagDataProvider(){
171+
return [
172+
['[br]', '<br />'],
173+
['[hr]', '<hr />'],
174+
['[clear]', '<br class="bb-clear both" />'],
175+
['[col]', '<col />'],
176+
['[xkcd]', ''],
177+
['[xkcd=1530]', '<a href="https://xkcd.com/1530/" class="bb-xkcd">[xkcd 1530]</a>'],
178+
];
179+
}
180+
181+
/**
182+
* @dataProvider singletagDataProvider
183+
*/
184+
public function testSingletagModule($tag, $expected){
185+
$this->assertEquals($expected, $this->parser->parse($tag));
175186
}
176187

177188
public function styledTextDataProvider(){

tests/normal/Modules/MarkdownModuleTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public function testEmptyTags(){
4949
if(!in_array($tag, $singletags)){
5050
$this->assertEquals('', $this->parser->parse('['.$tag.'][/'.$tag.']'));
5151
}
52+
else if($tag === 'xkcd'){
53+
$this->assertEquals('https://xkcd.com/221/', $this->parser->parse('[xkcd=221]'));
54+
}
5255
else{
5356
$this->assertEquals($_singletags[$tag], $this->parser->parse('['.$tag.']'));
5457
}

tests/normal/ParserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testGetNoparse(){
5454
}
5555

5656
public function testGetSingle(){
57-
$singletags = ['br','clear','col','hr'];
57+
$singletags = ['br','clear','col','hr','xkcd'];
5858

5959
$method = $this->reflectionClass->getMethod('getSingle');
6060
$this->parser = $this->reflectionClass->newInstance();

0 commit comments

Comments
 (0)