예제 #1
0
 /**
  * @param Emphasis $inline
  * @param HtmlRendererInterface $htmlRenderer
  *
  * @return HtmlElement
  */
 public function render(AbstractInline $inline, HtmlRendererInterface $htmlRenderer)
 {
     if (!$inline instanceof Emphasis) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     return new HtmlElement('em', array(), $htmlRenderer->renderInlines($inline->getChildren()));
 }
 /**
  * @param AbstractBlock $block
  * @param HtmlRendererInterface $htmlRenderer
  * @param bool $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, HtmlRendererInterface $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof IndentedCode) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     return new HtmlElement('pre', array(), new HtmlElement('code', array(), $htmlRenderer->escape($block->getStringContent())));
 }
예제 #3
0
 /**
  * @param Code $inline
  * @param HtmlRendererInterface $htmlRenderer
  *
  * @return HtmlElement
  */
 public function render(AbstractInline $inline, HtmlRendererInterface $htmlRenderer)
 {
     if (!$inline instanceof Code) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     return new HtmlElement('code', array(), $htmlRenderer->escape($inline->getContent()));
 }
예제 #4
0
 /**
  * @param Header $block
  * @param HtmlRendererInterface $htmlRenderer
  * @param bool $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, HtmlRendererInterface $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof Header) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     $tag = 'h' . $block->getLevel();
     return new HtmlElement($tag, array(), $htmlRenderer->renderInlines($block->getInlines()));
 }
예제 #5
0
 /**
  * @param AbstractBlock|Document $block
  * @param HtmlRendererInterface $htmlRenderer
  * @param bool $inTightList
  *
  * @return string
  */
 public function render(AbstractBlock $block, HtmlRendererInterface $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof Document) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     $wholeDoc = $htmlRenderer->renderBlocks($block->getChildren());
     return $wholeDoc === '' ? '' : $wholeDoc . "\n";
 }
 /**
  * @param FencedCode $block
  * @param HtmlRendererInterface $htmlRenderer
  * @param bool $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, HtmlRendererInterface $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof FencedCode) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     $infoWords = $block->getInfoWords();
     $attr = count($infoWords) === 0 || strlen($infoWords[0]) === 0 ? array() : array('class' => 'language-' . $htmlRenderer->escape($infoWords[0], true));
     return new HtmlElement('pre', array(), new HtmlElement('code', $attr, $htmlRenderer->escape($block->getStringContent())));
 }
예제 #7
0
 /**
  * @param ListBlock $block
  * @param HtmlRendererInterface $htmlRenderer
  * @param bool $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, HtmlRendererInterface $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof ListBlock) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     $listData = $block->getListData();
     $tag = $listData->type == ListBlock::TYPE_UNORDERED ? 'ul' : 'ol';
     $attr = $listData->start === null || $listData->start === 1 ? array() : array('start' => (string) $listData->start);
     return new HtmlElement($tag, $attr, $htmlRenderer->getOption('inner_separator', "\n") . $htmlRenderer->renderBlocks($block->getChildren(), $block->isTight()) . $htmlRenderer->getOption('inner_separator', "\n"));
 }
예제 #8
0
 /**
  * @param Aside                 $block
  * @param HtmlRendererInterface $htmlRenderer
  * @param bool                  $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, HtmlRendererInterface $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof Aside) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     $filling = $htmlRenderer->renderBlocks($block->getChildren());
     if ($filling === '') {
         return new HtmlElement('aside', array(), $htmlRenderer->getOption('inner_separator'));
     }
     return new HtmlElement('aside', array(), $htmlRenderer->getOption('inner_separator', "\n") . $filling . $htmlRenderer->getOption('inner_separator', "\n"));
 }
예제 #9
0
 /**
  * @param Newline $inline
  * @param HtmlRendererInterface $htmlRenderer
  *
  * @return HtmlElement|string
  */
 public function render(AbstractInline $inline, HtmlRendererInterface $htmlRenderer)
 {
     if (!$inline instanceof Newline) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     if ($inline->getType() === Newline::HARDBREAK) {
         return new HtmlElement('br', [], '', true) . "\n";
     } else {
         return $htmlRenderer->getOption('soft_break', "\n");
     }
 }
예제 #10
0
 /**
  * @param Paragraph $block
  * @param HtmlRendererInterface $htmlRenderer
  * @param bool $inTightList
  *
  * @return HtmlElement|string
  */
 public function render(AbstractBlock $block, HtmlRendererInterface $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof Paragraph) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     if ($inTightList) {
         return $htmlRenderer->renderInlines($block->getInlines());
     } else {
         return new HtmlElement('p', [], $htmlRenderer->renderInlines($block->getInlines()));
     }
 }
예제 #11
0
 /**
  * @param Link $inline
  * @param HtmlRendererInterface $htmlRenderer
  *
  * @return HtmlElement
  */
 public function render(AbstractInline $inline, HtmlRendererInterface $htmlRenderer)
 {
     if (!$inline instanceof Link) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     $attrs = [];
     $attrs['href'] = $htmlRenderer->escape($inline->getUrl(), true);
     if (isset($inline->data['title'])) {
         $attrs['title'] = $htmlRenderer->escape($inline->data['title'], true);
     }
     return new HtmlElement('a', $attrs, $htmlRenderer->renderInlines($inline->getChildren()));
 }
예제 #12
0
 /**
  * @param Image $inline
  * @param HtmlRendererInterface $htmlRenderer
  *
  * @return HtmlElement
  */
 public function render(AbstractInline $inline, HtmlRendererInterface $htmlRenderer)
 {
     if (!$inline instanceof Image) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     $attrs = array();
     $attrs['src'] = $htmlRenderer->escape($inline->getUrl(), true);
     $alt = $htmlRenderer->renderInlines($inline->getChildren());
     $alt = preg_replace('/\\<[^>]*alt="([^"]*)"[^>]*\\>/', '$1', $alt);
     $attrs['alt'] = preg_replace('/\\<[^>]*\\>/', '', $alt);
     if (isset($inline->data['title'])) {
         $attrs['title'] = $htmlRenderer->escape($inline->data['title'], true);
     }
     return new HtmlElement('img', $attrs, '', true);
 }
예제 #13
0
 /**
  * @param ListItem $block
  * @param HtmlRendererInterface $htmlRenderer
  * @param bool $inTightList
  *
  * @return string
  */
 public function render(AbstractBlock $block, HtmlRendererInterface $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof ListItem) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     $contents = $htmlRenderer->renderBlocks($block->getChildren(), $inTightList);
     if (substr($contents, 0, 1) === '<') {
         $contents = "\n" . $contents;
     }
     if (substr($contents, -1, 1) === '>') {
         $contents .= "\n";
     }
     $li = new HtmlElement('li', [], $contents);
     return trim($li);
 }
예제 #14
0
 /**
  * Converts CommonMark to HTML.
  *
  * @param string $commonMark
  *
  * @return string
  *
  * @api
  */
 public function convertToHtml($commonMark)
 {
     $documentAST = $this->docParser->parse($commonMark);
     return $this->htmlRenderer->renderBlock($documentAST);
 }
 /**
  * {@inheritdoc}
  */
 public function convert(ConverterContextInterface $converterContext)
 {
     $documentAST = $this->parser->parse($converterContext->content());
     $converterContext->setContent($this->renderer->renderBlock($documentAST));
 }