Skip to content

Commit

Permalink
Add ability to modify text before rendering HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
kzykhys committed Jan 24, 2014
1 parent 1b3a9ef commit 63bf138
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 18 deletions.
16 changes: 16 additions & 0 deletions src/Ciconia/Common/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
5 changes: 5 additions & 0 deletions src/Ciconia/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
58 changes: 40 additions & 18 deletions src/Ciconia/Renderer/HtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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);
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -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();
}

/**
Expand Down

0 comments on commit 63bf138

Please sign in to comment.