diff --git a/src/Ciconia/Common/Tag.php b/src/Ciconia/Common/Tag.php index 91b17c0..ade307d 100644 --- a/src/Ciconia/Common/Tag.php +++ b/src/Ciconia/Common/Tag.php @@ -169,6 +169,22 @@ public function getType() return $this->type; } + /** + * @return bool + */ + public function isBlock() + { + return $this->type === self::TYPE_BLOCK; + } + + /** + * @return bool + */ + public function isInline() + { + return $this->type === self::TYPE_INLINE; + } + /** * Sets an attribute * diff --git a/src/Ciconia/Markdown.php b/src/Ciconia/Markdown.php index 484ecf8..df5686d 100644 --- a/src/Ciconia/Markdown.php +++ b/src/Ciconia/Markdown.php @@ -4,6 +4,7 @@ use Ciconia\Common\Collection; use Ciconia\Common\Text; +use Ciconia\Event\EmitterAwareInterface; use Ciconia\Event\EmitterInterface; use Ciconia\Event\EmitterTrait; use Ciconia\Renderer\RendererInterface; @@ -59,6 +60,10 @@ class Markdown implements EmitterInterface */ public function __construct(RendererInterface $renderer, Text $rawContent = null, array $options = array()) { + if ($renderer instanceof EmitterAwareInterface) { + $renderer->setEmitter($this); + } + $this->renderer = $renderer; $this->options = $this->parseOptions($options); $this->hashRegistry = new HashRegistry(); diff --git a/src/Ciconia/Renderer/HtmlRenderer.php b/src/Ciconia/Renderer/HtmlRenderer.php index 5936f95..bafc312 100644 --- a/src/Ciconia/Renderer/HtmlRenderer.php +++ b/src/Ciconia/Renderer/HtmlRenderer.php @@ -4,6 +4,8 @@ use Ciconia\Common\Tag; use Ciconia\Common\Text; +use Ciconia\Event\EmitterAwareInterface; +use Ciconia\Event\EmitterAwareTrait; use Symfony\Component\OptionsResolver\OptionsResolver; /** @@ -11,15 +13,23 @@ * * @author Kazuyuki Hayashi */ -class HtmlRenderer implements RendererInterface +class HtmlRenderer implements RendererInterface, EmitterAwareInterface { + use EmitterAwareTrait; + /** * {@inheritdoc} */ public function renderParagraph($content, array $options = array()) { - return '

' . $content . "

"; + $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,15 +60,15 @@ 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(); } /** @@ -67,7 +76,11 @@ public function renderCodeBlock($content, array $options = array()) */ public function renderCodeSpan($content, array $options = array()) { - return "$content"; + $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 "
\n$content\n
"; + $tag = Tag::create('blockquote') + ->setText($content->wrap("\n", "\n")); + + return $tag->render(); } /** @@ -130,7 +146,7 @@ public function renderList($content, array $options = array()) */ public function renderListItem($content, array $options = array()) { - return "
  • " . $content . "
  • "; + return Tag::create('li')->setText($content)->render(); } /** @@ -138,7 +154,10 @@ public function renderListItem($content, array $options = array()) */ public function renderHorizontalRule(array $options = array()) { - return 'getEmptyTagSuffix(); + return Tag::create('hr') + ->setType(Tag::TYPE_INLINE) + ->setEmptyTagSuffix($this->getEmptyTagSuffix()) + ->render(); } /** @@ -163,7 +182,7 @@ public function renderImage($src, array $options = array()) */ public function renderBoldText($text, array $options = array()) { - return '' . $text . ''; + return Tag::create('strong')->setText($text)->render(); } /** @@ -171,7 +190,7 @@ public function renderBoldText($text, array $options = array()) */ public function renderItalicText($text, array $options = array()) { - return '' . $text . ''; + return Tag::create('em')->setText($text)->render(); } /** @@ -179,7 +198,10 @@ public function renderItalicText($text, array $options = array()) */ public function renderLineBreak(array $options = array()) { - return 'getEmptyTagSuffix(); + return Tag::create('br') + ->setType(Tag::TYPE_INLINE) + ->setEmptyTagSuffix($this->getEmptyTagSuffix()) + ->render(); } /**