Пример #1
0
 /**
  * @param AbstractBlock            $block
  * @param ElementRendererInterface $htmlRenderer
  * @param bool                     $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof IndentedCode) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     return new HtmlElement('pre', [], new HtmlElement('code', [], $htmlRenderer->escape($block->getStringContent())));
 }
 /**
  * {@inheritdoc}
  */
 public function render(AbstractInline $inline, ElementRendererInterface $html_renderer)
 {
     if (!$inline instanceof Link) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     $attributes = [];
     foreach ($inline->getData('attributes', []) as $key => $value) {
         $attributes[$key] = $html_renderer->escape($value, TRUE);
     }
     // Retrieve the URL.
     $url = $inline->getUrl();
     $external = $this->isExternalUrl($url);
     $attributes['href'] = $html_renderer->escape($url, TRUE);
     // Make external links open in a new window.
     if ($this->getSetting('external_new_window') && $external) {
         $attributes['target'] = '_blank';
     }
     // rel="nofollow"
     $no_follow = $this->getSetting('no_follow');
     if ($no_follow === 'all' || $external && $no_follow === 'external' || !$external && $no_follow === 'internal') {
         $attributes['rel'] = 'nofollow';
     }
     if (isset($inline->data['title'])) {
         $attributes['title'] = $html_renderer->escape($inline->data['title'], TRUE);
     }
     return new HtmlElement('a', $attributes, $html_renderer->renderInlines($inline->getChildren()));
 }
 /**
  * @param Text                     $inline
  * @param ElementRendererInterface $htmlRenderer
  *
  * @return string
  */
 public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
 {
     if (!$inline instanceof Text) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     return $htmlRenderer->escape($inline->getContent());
 }
Пример #4
0
 /**
  * @param Emphasis                 $inline
  * @param ElementRendererInterface $htmlRenderer
  *
  * @return HtmlElement
  */
 public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
 {
     if (!$inline instanceof Emphasis) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     return new HtmlElement('em', [], $htmlRenderer->renderInlines($inline->getChildren()));
 }
 /**
  * @param AbstractBlock|Document   $block
  * @param ElementRendererInterface $htmlRenderer
  * @param bool                     $inTightList
  *
  * @return string
  */
 public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof Document) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     $wholeDoc = $htmlRenderer->renderBlocks($block->children());
     return $wholeDoc === '' ? '' : $wholeDoc . "\n";
 }
Пример #6
0
 /**
  * @param FencedCode               $block
  * @param ElementRendererInterface $htmlRenderer
  * @param bool                     $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, ElementRendererInterface $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 ? [] : ['class' => 'language-' . $htmlRenderer->escape($infoWords[0], true)];
     return new HtmlElement('pre', [], new HtmlElement('code', $attr, $htmlRenderer->escape($block->getStringContent())));
 }
Пример #7
0
 /**
  * @param ListBlock                $block
  * @param ElementRendererInterface $htmlRenderer
  * @param bool                     $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, ElementRendererInterface $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 ? [] : ['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 Newline                  $inline
  * @param ElementRendererInterface $htmlRenderer
  *
  * @return HtmlElement|string
  */
 public function render(AbstractInline $inline, ElementRendererInterface $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");
     }
 }
 /**
  * @param AbstractBlock            $block
  * @param ElementRendererInterface $htmlRenderer
  * @param bool                     $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof IndentedCode) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     $attrs = [];
     foreach ($block->getData('attributes', []) as $key => $value) {
         $attrs[$key] = $htmlRenderer->escape($value, true);
     }
     return new HtmlElement('pre', [], new HtmlElement('code', $attrs, $htmlRenderer->escape($block->getStringContent())));
 }
Пример #10
0
 /**
  * @param Paragraph                $block
  * @param ElementRendererInterface $htmlRenderer
  * @param bool                     $inTightList
  *
  * @return HtmlElement|string
  */
 public function render(AbstractBlock $block, ElementRendererInterface $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()));
     }
 }
 /**
  * @param Code                     $inline
  * @param ElementRendererInterface $htmlRenderer
  *
  * @return HtmlElement
  */
 public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
 {
     if (!$inline instanceof Code) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     $attrs = [];
     foreach ($inline->getData('attributes', []) as $key => $value) {
         $attrs[$key] = $htmlRenderer->escape($value, true);
     }
     return new HtmlElement('code', $attrs, $htmlRenderer->escape($inline->getContent()));
 }
Пример #12
0
 /**
  * @param BlockQuote               $block
  * @param ElementRendererInterface $htmlRenderer
  * @param bool                     $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof BlockQuote) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     $filling = $htmlRenderer->renderBlocks($block->getChildren());
     if ($filling === '') {
         return new HtmlElement('blockquote', [], $htmlRenderer->getOption('inner_separator', "\n"));
     }
     return new HtmlElement('blockquote', [], $htmlRenderer->getOption('inner_separator', "\n") . $filling . $htmlRenderer->getOption('inner_separator', "\n"));
 }
 /**
  * @param Heading                  $block
  * @param ElementRendererInterface $htmlRenderer
  * @param bool                     $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof Heading) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     $tag = 'h' . $block->getLevel();
     $attrs = [];
     foreach ($block->getData('attributes', []) as $key => $value) {
         $attrs[$key] = $htmlRenderer->escape($value, true);
     }
     return new HtmlElement($tag, $attrs, $htmlRenderer->renderInlines($block->children()));
 }
 public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof TableRow) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     $attrs = [];
     foreach ($block->getData('attributes', []) as $key => $value) {
         $attrs[$key] = $htmlRenderer->escape($value, true);
     }
     $separator = $htmlRenderer->getOption('inner_separator', "\n");
     return new HtmlElement('tr', $attrs, $separator . $htmlRenderer->renderBlocks($block->children()) . $separator);
 }
Пример #15
0
 /**
  * @param Link                     $inline
  * @param ElementRendererInterface $htmlRenderer
  *
  * @return HtmlElement
  */
 public function render(AbstractInline $inline, ElementRendererInterface $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()));
 }
 public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof TableCell) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     $attrs = [];
     foreach ($block->getData('attributes', []) as $key => $value) {
         $attrs[$key] = $htmlRenderer->escape($value, true);
     }
     if ($block->align) {
         $attrs['align'] = $block->align;
     }
     return new HtmlElement($block->type, $attrs, $htmlRenderer->renderInlines($block->getInlines()));
 }
Пример #17
0
 public function getLanguage($infoWords, ElementRendererInterface $htmlRenderer)
 {
     if (count($infoWords) === 0 || strlen($infoWords[0]) === 0) {
         return false;
     }
     $language = $htmlRenderer->escape($infoWords[0], true);
     if (array_key_exists($language, $this->known_conversions)) {
         $language = $this->known_conversions[$language];
     }
     if (in_array($language, $this->supported_languages)) {
         return $language;
     }
     return false;
 }
Пример #18
0
 /**
  * @param Image                    $inline
  * @param ElementRendererInterface $htmlRenderer
  *
  * @return HtmlElement
  */
 public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
 {
     if (!$inline instanceof Image) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     $attrs = [];
     $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);
 }
Пример #19
0
 /**
  * @param ListItem                 $block
  * @param ElementRendererInterface $htmlRenderer
  * @param bool                     $inTightList
  *
  * @return string
  */
 public function render(AbstractBlock $block, ElementRendererInterface $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);
 }
 /**
  * @param FencedCode               $block
  * @param ElementRendererInterface $htmlRenderer
  * @param bool                     $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof FencedCode) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     $attrs = [];
     foreach ($block->getData('attributes', []) as $key => $value) {
         $attrs[$key] = $htmlRenderer->escape($value, true);
     }
     $infoWords = $block->getInfoWords();
     if (count($infoWords) !== 0 && strlen($infoWords[0]) !== 0) {
         $attrs['class'] = isset($attrs['class']) ? $attrs['class'] . ' ' : '';
         $attrs['class'] .= 'language-' . $htmlRenderer->escape($infoWords[0], true);
     }
     return new HtmlElement('pre', [], new HtmlElement('code', $attrs, $htmlRenderer->escape($block->getStringContent())));
 }
Пример #21
0
 /**
  * @param ListBlock                $block
  * @param ElementRendererInterface $htmlRenderer
  * @param bool                     $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, ElementRendererInterface $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';
     $attrs = [];
     foreach ($block->getData('attributes', []) as $key => $value) {
         $attrs[$key] = $htmlRenderer->escape($value, true);
     }
     if ($listData->start !== null && $listData->start !== 1) {
         $attrs['start'] = (string) $listData->start;
     }
     return new HtmlElement($tag, $attrs, $htmlRenderer->getOption('inner_separator', "\n") . $htmlRenderer->renderBlocks($block->getChildren(), $block->isTight()) . $htmlRenderer->getOption('inner_separator', "\n"));
 }
 /**
  * @param AbstractInline           $inline
  * @param ElementRendererInterface $htmlRenderer
  *
  * @return HtmlElement
  */
 public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
 {
     if (!$inline instanceof TaskListsCheckbox) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     $attrs = [];
     foreach ($inline->getData('attributes', []) as $key => $value) {
         $attrs[$key] = $htmlRenderer->escape($value, true);
     }
     $attrs['type'] = 'checkbox';
     $attrs['disabled'] = '';
     if ($inline->isChecked()) {
         $attrs['checked'] = 'checked';
     }
     return new HtmlElement('input', $attrs);
 }
Пример #23
0
 /**
  * @param Link                     $inline
  * @param ElementRendererInterface $htmlRenderer
  *
  * @return HtmlElement
  */
 public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
 {
     if (!$inline instanceof Link) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     $attrs = [];
     foreach ($inline->getData('attributes', []) as $key => $value) {
         $attrs[$key] = $htmlRenderer->escape($value, true);
     }
     if (!($this->config->getConfig('safe') && RegexHelper::isLinkPotentiallyUnsafe($inline->getUrl()))) {
         $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->children()));
 }
 /**
  * @param ListItem                 $block
  * @param ElementRendererInterface $htmlRenderer
  * @param bool                     $inTightList
  *
  * @return string
  */
 public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof ListItem) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     $contents = $htmlRenderer->renderBlocks($block->children(), $inTightList);
     if (substr($contents, 0, 1) === '<') {
         $contents = "\n" . $contents;
     }
     if (substr($contents, -1, 1) === '>') {
         $contents .= "\n";
     }
     $attrs = [];
     foreach ($block->getData('attributes', []) as $key => $value) {
         $attrs[$key] = $htmlRenderer->escape($value, true);
     }
     $li = new HtmlElement('li', $attrs, $contents);
     return $li;
 }
Пример #25
0
 /**
  * @param Image                    $inline
  * @param ElementRendererInterface $htmlRenderer
  *
  * @return HtmlElement
  */
 public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
 {
     if (!$inline instanceof Image) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     $attrs = [];
     foreach ($inline->getData('attributes', []) as $key => $value) {
         $attrs[$key] = $htmlRenderer->escape($value, true);
     }
     if ($this->config->getConfig('safe') && RegexHelper::isLinkPotentiallyUnsafe($inline->getUrl())) {
         $attrs['src'] = '';
     } else {
         $attrs['src'] = $htmlRenderer->escape($inline->getUrl(), true);
     }
     $alt = $htmlRenderer->renderInlines($inline->children());
     $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);
 }
Пример #26
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);
 }
Пример #27
0
 public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
 {
     return $htmlRenderer->renderBlocks($block->children());
 }