Inheritance: implements League\CommonMark\ElementRendererInterface
Esempio n. 1
0
 /**
  * @param Emphasis $inline
  * @param HtmlRenderer $htmlRenderer
  *
  * @return HtmlElement
  */
 public function render(AbstractInline $inline, HtmlRenderer $htmlRenderer)
 {
     if (!$inline instanceof Emphasis) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     return new HtmlElement('em', array(), $htmlRenderer->renderInlines($inline->getChildren()));
 }
Esempio n. 2
0
 /**
  * @param Text $inline
  * @param HtmlRenderer $htmlRenderer
  *
  * @return string
  */
 public function render(AbstractInline $inline, HtmlRenderer $htmlRenderer)
 {
     if (!$inline instanceof Text) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     return $htmlRenderer->escape($inline->getContent());
 }
Esempio n. 3
0
 /**
  * @param Document $block
  * @param bool $inTightList
  *
  * @return string
  */
 public function render(AbstractBlock $block, HtmlRenderer $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";
 }
Esempio n. 4
0
 /**
  * @param Header $block
  * @param HtmlRenderer $htmlRenderer
  * @param bool $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, HtmlRenderer $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()));
 }
Esempio n. 5
0
 /**
  * @param FencedCode $block
  * @param HtmlRenderer $htmlRenderer
  * @param bool $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, HtmlRenderer $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())));
 }
Esempio n. 6
0
 /**
  * Convert body to html
  */
 public function markdown($description)
 {
     $environment = Environment::createCommonMarkEnvironment();
     $parser = new DocParser($environment);
     $htmlRenderer = new HtmlRenderer($environment);
     $markdown = $this->description;
     $documentAST = $parser->parse($markdown);
     return $htmlRenderer->renderBlock($documentAST);
 }
Esempio n. 7
0
 /**
  * @param BlockQuote $block
  * @param HtmlRenderer $htmlRenderer
  * @param bool $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, HtmlRenderer $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', array(), $htmlRenderer->getOption('inner_separator', "\n"));
     }
     return new HtmlElement('blockquote', array(), $htmlRenderer->getOption('inner_separator', "\n") . $filling . $htmlRenderer->getOption('inner_separator', "\n"));
 }
Esempio n. 8
0
 /**
  * @param Paragraph $block
  * @param HtmlRenderer $htmlRenderer
  * @param bool $inTightList
  *
  * @return HtmlElement|string
  */
 public function render(AbstractBlock $block, HtmlRenderer $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', array(), $htmlRenderer->renderInlines($block->getInlines()));
     }
 }
Esempio n. 9
0
 /**
  * @param Newline $inline
  * @param HtmlRenderer $htmlRenderer
  *
  * @return HtmlElement|string
  */
 public function render(AbstractInline $inline, HtmlRenderer $htmlRenderer)
 {
     if (!$inline instanceof Newline) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     if ($inline->getType() === Newline::HARDBREAK) {
         return new HtmlElement('br', array(), '', true) . "\n";
     } else {
         return $htmlRenderer->getOption('soft_break', "\n");
     }
 }
Esempio n. 10
0
 /**
  * @param ListBlock $block
  * @param HtmlRenderer $htmlRenderer
  * @param bool $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, HtmlRenderer $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof ListBlock) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     $listData = $block->getListData();
     $start = $listData->start ?: null;
     $tag = $listData->type == ListBlock::TYPE_UNORDERED ? 'ul' : 'ol';
     $attr = !$start || $start == 1 ? array() : array('start' => (string) $start);
     return new HtmlElement($tag, $attr, $htmlRenderer->getOption('inner_separator', "\n") . $htmlRenderer->renderBlocks($block->getChildren(), $block->isTight()) . $htmlRenderer->getOption('inner_separator', "\n"));
 }
Esempio n. 11
0
 /**
  * @param Link $inline
  * @param HtmlRenderer $htmlRenderer
  *
  * @return HtmlElement
  */
 public function render(AbstractInline $inline, HtmlRenderer $htmlRenderer)
 {
     if (!$inline instanceof Link) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     $attrs = array();
     $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()));
 }
Esempio n. 12
0
 public function htmlIntroduction($before = '', $after = '')
 {
     $introduction = trim($this->introduction);
     if (empty($introduction)) {
         return;
     }
     $environment = Environment::createCommonMarkEnvironment();
     $parser = new DocParser($environment);
     $htmlRenderer = new HtmlRenderer($environment);
     $text = $parser->parse($introduction);
     return $before . $htmlRenderer->renderBlock($text) . $after;
 }
Esempio n. 13
0
 /**
  * @param string $string
  * @param string $expected
  * @return void
  *
  * @dataProvider dataForIntegrationTest
  */
 public function testStrikethrough($string, $expected)
 {
     $environment = Environment::createCommonMarkEnvironment();
     $environment->addInlineParser(new StrikethroughParser());
     $environment->addInlineRenderer('CommonMarkExt\\Strikethrough\\Strikethrough', new StrikethroughRenderer());
     $parser = new DocParser($environment);
     $renderer = new HtmlRenderer($environment);
     $document = $parser->parse($string);
     $html = $renderer->renderBlock($document);
     //
     //        $converter = new CommonMarkConverter($environment->getConfig());
     //        $html = $converter->convertToHtml($string);
     $this->assertSame($expected, $html);
 }
Esempio n. 14
0
 /**
  * @param Image $inline
  * @param HtmlRenderer $htmlRenderer
  *
  * @return HtmlElement
  */
 public function render(AbstractInline $inline, HtmlRenderer $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);
 }
Esempio n. 15
0
 public function transform($markdownText)
 {
     $environment = Environment::createCommonMarkEnvironment();
     $beforeParseEvent = Croogo::dispatchEvent('Helper.Markdown.beforeMarkdownParse', $this->_View, array('environment' => $environment, 'markdown' => $markdownText));
     $markdownText = $beforeParseEvent->data['markdown'];
     $environment = $beforeParseEvent->data['environment'];
     $parser = new DocParser($environment);
     $htmlRenderer = new HtmlRenderer($environment);
     $documentAST = $parser->parse($markdownText);
     $beforeRenderEvent = Croogo::dispatchEvent('Helper.Markdown.beforeMarkdownRender', $this->_View, array('ast' => $documentAST));
     $documentAST = $beforeRenderEvent->data['ast'];
     $rendered = $htmlRenderer->renderBlock($documentAST);
     $afterRenderEvent = Croogo::dispatchEvent('Helper.Markdown.afterMarkdownRender', $this->_View, array('rendered' => $rendered));
     return $afterRenderEvent->data['rendered'];
 }
Esempio n. 16
0
 /**
  * @param ListItem $block
  * @param HtmlRenderer $htmlRenderer
  * @param bool $inTightList
  *
  * @return string
  */
 public function render(AbstractBlock $block, HtmlRenderer $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', array(), $contents);
     return trim($li);
 }
Esempio n. 17
0
 /**
  * @param \League\CommonMark\Inline\Element\AbstractInline $inline
  * @param \League\CommonMark\HtmlRenderer $htmlRenderer
  *
  * @return \League\CommonMark\HtmlElement|string
  */
 public function render(\League\CommonMark\Inline\Element\AbstractInline $inline, \League\CommonMark\HtmlRenderer $htmlRenderer)
 {
     if (!$inline instanceof \League\CommonMark\Inline\Element\Link) {
         throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
     }
     $attrs = array();
     $attrs['href'] = $htmlRenderer->escape($inline->getUrl(), true);
     if (isset($inline->attributes['title'])) {
         $attrs['title'] = $htmlRenderer->escape($inline->attributes['title'], true);
     }
     if ($this->isExternalUrl($inline->getUrl())) {
         $attrs['class'] = 'external-link';
     }
     return new HtmlElement('a', $attrs, $htmlRenderer->renderInlines($inline->getLabel()->getInlines()));
 }
Esempio n. 18
0
 /**
  * Returns rendered view
  *
  * @return string
  */
 public function render()
 {
     if ($this->isMarkdown()) {
         $environment = Environment::createCommonMarkEnvironment();
         $environment->addInlineParser(new TwitterHandleParser());
         $parser = new DocParser($environment);
         $htmlRenderer = new HtmlRenderer($environment);
         $document = $parser->parse(file_get_contents($this->filename));
         return $htmlRenderer->renderBlock($document);
     }
     ob_start();
     extract($this->vars);
     require $this->filename;
     return ob_get_clean();
 }
Esempio n. 19
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);
 }
Esempio n. 20
0
 public function renderDocument($ast)
 {
     $renderer = new HtmlRenderer($this->getCommonMarkEnvironment());
     return $renderer->renderBlock($ast);
 }
Esempio n. 21
0
 /**
  * @param AbstractBlock $block
  * @param HtmlRenderer $htmlRenderer
  * @param bool $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, HtmlRenderer $htmlRenderer, $inTightList = false)
 {
     return new HtmlElement('pre', array(), new HtmlElement('code', array(), $htmlRenderer->escape($block->getStringContent())));
 }
Esempio n. 22
0
 /**
  * Render a string of text
  *
  * @param string $text
  * @return string
  */
 public function render($string)
 {
     $document = $this->parser->parse($string);
     return $this->renderer->renderBlock($document);
 }
 public function __construct(Environment $environment, \Twig_Environment $twig, $template = 'commonmark.html.twig')
 {
     parent::__construct($environment);
     $this->twig = $twig;
     $this->defaultTemplate = $template;
 }
Esempio n. 24
0
 /**
  * @param \League\CommonMark\Block\Element\AbstractBlock $block
  * @param \League\CommonMark\HtmlRenderer $htmlRenderer
  * @param bool $inTightList
  *
  * @return \League\CommonMark\HtmlElement|string
  */
 public function render(\League\CommonMark\Block\Element\AbstractBlock $block, \League\CommonMark\HtmlRenderer $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof ExampleElement) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     //look for links
     $examples = [];
     foreach ($block->getInlines() as $inline) {
         if (!$inline instanceof \League\CommonMark\Inline\Element\Link) {
             continue;
         }
         $url = explode('#', $inline->getUrl());
         $file = realpath($block->getPath() . '/' . $url[0]);
         if (!$file) {
             throw new \RuntimeException('could not find example code: ' . $block->getPath() . '/' . $inline->getUrl());
         }
         $code = file($file);
         //check for specific ranges lines
         if (isset($url[1])) {
             //check for valid range
             $range = explode('-', str_replace('L', '', $url[1]));
             if (!isset($code[--$range[0]])) {
                 throw new Exception('invalid line range for example');
             }
             if (isset($range[1]) and !isset($code[--$range[1]])) {
                 throw new Exception('invalid line range for example');
             }
             if (isset($range[1])) {
                 $code = array_slice($code, $range[0], $range[1] - $range[0] + 1);
             } else {
                 $code = array_slice($code, $range[0], 1);
             }
             //find smallest indent
             foreach ($code as $line) {
                 if (!preg_match('#\\s*#', $line, $matches)) {
                     $indent = 0;
                     break;
                 }
                 $current = strlen($matches[0]);
                 if (!isset($indent) or $indent > $current) {
                     $indent = $current;
                 }
             }
             if ($indent) {
                 foreach ($code as $index => $line) {
                     $code[$index] = substr($line, $indent);
                 }
             }
         }
         $code = implode('', $code);
         $examples[$htmlRenderer->renderInlines($inline->getLabel()->getInlines())] = $htmlRenderer->escape($code);
     }
     //just one example with the language 'example' just return a code block
     if (count($examples) == 1 and strtolower(key($examples)) == 'example') {
         return new HtmlElement('pre', array(), new HtmlElement('code', array(), $htmlRenderer->escape($code)));
     }
     ob_start();
     include __DIR__ . '/example.phtml';
     $html = ob_get_clean();
     return $html;
 }
Esempio n. 25
0
 /**
  * Parse markdown to html
  * 
  * @param  string $markdown
  * @return string
  */
 public function parse($markdown)
 {
     $document = $this->parser->parse($markdown);
     $html = $this->renderer->renderBlock($document);
     return $html;
 }