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