-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to modify text before rendering HTML
- Loading branch information
Showing
3 changed files
with
61 additions
and
18 deletions.
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
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
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 |
---|---|---|
|
@@ -4,22 +4,32 @@ | |
|
||
use Ciconia\Common\Tag; | ||
use Ciconia\Common\Text; | ||
use Ciconia\Event\EmitterAwareInterface; | ||
use Ciconia\Event\EmitterAwareTrait; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* Renders markdown result to HTML format | ||
* | ||
* @author Kazuyuki Hayashi <[email protected]> | ||
*/ | ||
class HtmlRenderer implements RendererInterface | ||
class HtmlRenderer implements RendererInterface, EmitterAwareInterface | ||
{ | ||
|
||
use EmitterAwareTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function renderParagraph($content, array $options = array()) | ||
{ | ||
return '<p>' . $content . "</p>"; | ||
$options = $this->createResolver()->resolve($options); | ||
|
||
$tag = new Tag('p'); | ||
$tag->setText($content); | ||
$tag->setAttributes($options['attr']); | ||
|
||
return $tag->render(); | ||
} | ||
|
||
/** | ||
|
@@ -28,7 +38,6 @@ public function renderParagraph($content, array $options = array()) | |
public function renderHeader($content, array $options = array()) | ||
{ | ||
$options = $this->createResolver() | ||
->setDefaults(['attr' => []]) | ||
->setRequired(['level']) | ||
->setAllowedValues(['level' => [1, 2, 3, 4, 5, 6]]) | ||
->resolve($options); | ||
|
@@ -51,23 +60,27 @@ public function renderCodeBlock($content, array $options = array()) | |
|
||
$options = $this->createResolver()->resolve($options); | ||
|
||
$pre = new Tag('pre'); | ||
$pre->setAttributes($options['attr']); | ||
|
||
$code = new Tag('code'); | ||
$code->setText($content->append("\n")); | ||
$tag = Tag::create('pre') | ||
->setAttributes($options['attr']) | ||
->setText( | ||
Tag::create('code') | ||
->setText($content->append("\n")) | ||
->render() | ||
); | ||
|
||
$pre->setText($code->render()); | ||
|
||
return $pre->render(); | ||
return $tag->render(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function renderCodeSpan($content, array $options = array()) | ||
{ | ||
return "<code>$content</code>"; | ||
$tag = new Tag('code'); | ||
$tag->setType(Tag::TYPE_INLINE); | ||
$tag->setText($content); | ||
|
||
return $tag->render(); | ||
} | ||
|
||
/** | ||
|
@@ -100,7 +113,10 @@ public function renderLink($content, array $options = array()) | |
*/ | ||
public function renderBlockQuote($content, array $options = array()) | ||
{ | ||
return "<blockquote>\n$content\n</blockquote>"; | ||
$tag = Tag::create('blockquote') | ||
->setText($content->wrap("\n", "\n")); | ||
|
||
return $tag->render(); | ||
} | ||
|
||
/** | ||
|
@@ -130,15 +146,18 @@ public function renderList($content, array $options = array()) | |
*/ | ||
public function renderListItem($content, array $options = array()) | ||
{ | ||
return "<li>" . $content . "</li>"; | ||
return Tag::create('li')->setText($content)->render(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function renderHorizontalRule(array $options = array()) | ||
{ | ||
return '<hr' . $this->getEmptyTagSuffix(); | ||
return Tag::create('hr') | ||
->setType(Tag::TYPE_INLINE) | ||
->setEmptyTagSuffix($this->getEmptyTagSuffix()) | ||
->render(); | ||
} | ||
|
||
/** | ||
|
@@ -163,23 +182,26 @@ public function renderImage($src, array $options = array()) | |
*/ | ||
public function renderBoldText($text, array $options = array()) | ||
{ | ||
return '<strong>' . $text . '</strong>'; | ||
return Tag::create('strong')->setText($text)->render(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function renderItalicText($text, array $options = array()) | ||
{ | ||
return '<em>' . $text . '</em>'; | ||
return Tag::create('em')->setText($text)->render(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function renderLineBreak(array $options = array()) | ||
{ | ||
return '<br' . $this->getEmptyTagSuffix(); | ||
return Tag::create('br') | ||
->setType(Tag::TYPE_INLINE) | ||
->setEmptyTagSuffix($this->getEmptyTagSuffix()) | ||
->render(); | ||
} | ||
|
||
/** | ||
|