Added a content type system to be able to extend the conversion mechanism
Dieser Commit ist enthalten in:
20
libs/Format/Confluence/ContentTypes/Markdown/CommonMarkConverter.php
Normale Datei
20
libs/Format/Confluence/ContentTypes/Markdown/CommonMarkConverter.php
Normale Datei
@ -0,0 +1,20 @@
|
||||
<?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
|
||||
|
||||
use League\CommonMark\Environment;
|
||||
|
||||
class CommonMarkConverter extends \Todaymade\Daux\Format\Base\ContentTypes\Markdown\CommonMarkConverter
|
||||
{
|
||||
protected function getLinkRenderer(Environment $environment)
|
||||
{
|
||||
return new LinkRenderer($environment->getConfig('daux'));
|
||||
}
|
||||
|
||||
protected function extendEnvironment(Environment $environment)
|
||||
{
|
||||
parent::extendEnvironment($environment);
|
||||
|
||||
//Add code renderer
|
||||
$environment->addBlockRenderer('FencedCode', new FencedCodeRenderer());
|
||||
$environment->addBlockRenderer('IndentedCode', new IndentedCodeRenderer());
|
||||
}
|
||||
}
|
12
libs/Format/Confluence/ContentTypes/Markdown/ContentType.php
Normale Datei
12
libs/Format/Confluence/ContentTypes/Markdown/ContentType.php
Normale Datei
@ -0,0 +1,12 @@
|
||||
<?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
|
||||
|
||||
use Todaymade\Daux\Config;
|
||||
|
||||
class ContentType extends \Todaymade\Daux\Format\Base\ContentTypes\Markdown\ContentType
|
||||
{
|
||||
public function __construct(Config $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->converter = new CommonMarkConverter(['daux' => $config]);
|
||||
}
|
||||
}
|
85
libs/Format/Confluence/ContentTypes/Markdown/FencedCodeRenderer.php
Normale Datei
85
libs/Format/Confluence/ContentTypes/Markdown/FencedCodeRenderer.php
Normale Datei
@ -0,0 +1,85 @@
|
||||
<?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
|
||||
|
||||
use League\CommonMark\Block\Element\AbstractBlock;
|
||||
use League\CommonMark\Block\Element\FencedCode;
|
||||
use League\CommonMark\Block\Renderer\BlockRendererInterface;
|
||||
use League\CommonMark\HtmlElement;
|
||||
use League\CommonMark\HtmlRendererInterface;
|
||||
|
||||
class FencedCodeRenderer implements BlockRendererInterface
|
||||
{
|
||||
|
||||
protected $supported_languages = [
|
||||
'actionscript3',
|
||||
'bash',
|
||||
'csharp',
|
||||
'coldfusion',
|
||||
'cpp',
|
||||
'css',
|
||||
'delphi',
|
||||
'diff',
|
||||
'erlang',
|
||||
'groovy',
|
||||
'html/xml',
|
||||
'java',
|
||||
'javafx',
|
||||
'javascript',
|
||||
'none',
|
||||
'perl',
|
||||
'php',
|
||||
'powershell',
|
||||
'python',
|
||||
'ruby',
|
||||
'scala',
|
||||
'sql',
|
||||
'vb'
|
||||
];
|
||||
protected $known_conversions = ['html' => 'html/xml', 'xml' => 'html/xml', 'js' => 'javascript'];
|
||||
|
||||
/**
|
||||
* @param AbstractBlock $block
|
||||
* @param HtmlRendererInterface $htmlRenderer
|
||||
* @param bool $inTightList
|
||||
*
|
||||
* @return HtmlElement|string
|
||||
*/
|
||||
public function render(AbstractBlock $block, HtmlRendererInterface $htmlRenderer, $inTightList = false)
|
||||
{
|
||||
if (!($block instanceof FencedCode)) {
|
||||
throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
|
||||
}
|
||||
|
||||
$content = [];
|
||||
|
||||
if ($language = $this->getLanguage($block->getInfoWords(), $htmlRenderer)) {
|
||||
$content[] = new HtmlElement('ac:parameter', ['ac:name' => 'language'], $language);
|
||||
}
|
||||
|
||||
$content[] = new HtmlElement('ac:plain-text-body', [], '<![CDATA[' . $block->getStringContent() . ']]>');
|
||||
|
||||
return new HtmlElement(
|
||||
'ac:structured-macro',
|
||||
['ac:name' => 'code'],
|
||||
$content
|
||||
);
|
||||
}
|
||||
|
||||
public function getLanguage($infoWords, HtmlRendererInterface $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;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
|
||||
|
||||
use League\CommonMark\Block\Element\AbstractBlock;
|
||||
use League\CommonMark\Block\Element\IndentedCode;
|
||||
use League\CommonMark\Block\Renderer\BlockRendererInterface;
|
||||
use League\CommonMark\HtmlElement;
|
||||
use League\CommonMark\HtmlRendererInterface;
|
||||
|
||||
class IndentedCodeRenderer implements BlockRendererInterface
|
||||
{
|
||||
/**
|
||||
* @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(
|
||||
'ac:structured-macro',
|
||||
['ac:name' => 'code'],
|
||||
new HtmlElement('ac:plain-text-body', [], '<![CDATA[' . $block->getStringContent() . ']]>')
|
||||
);
|
||||
}
|
||||
}
|
54
libs/Format/Confluence/ContentTypes/Markdown/LinkRenderer.php
Normale Datei
54
libs/Format/Confluence/ContentTypes/Markdown/LinkRenderer.php
Normale Datei
@ -0,0 +1,54 @@
|
||||
<?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
|
||||
|
||||
use League\CommonMark\HtmlElement;
|
||||
use League\CommonMark\HtmlRendererInterface;
|
||||
use League\CommonMark\Inline\Element\AbstractInline;
|
||||
use League\CommonMark\Inline\Element\Link;
|
||||
|
||||
class LinkRenderer extends \Todaymade\Daux\Format\Base\ContentTypes\Markdown\LinkRenderer
|
||||
{
|
||||
/**
|
||||
* @param Link $inline
|
||||
* @param HtmlRendererInterface $htmlRenderer
|
||||
*
|
||||
* @return HtmlElement
|
||||
*/
|
||||
public function render(AbstractInline $inline, HtmlRendererInterface $htmlRenderer)
|
||||
{
|
||||
// This can't be in the method type as
|
||||
// the method is an abstract and should
|
||||
// have the same interface
|
||||
if (!$inline instanceof Link) {
|
||||
throw new \RuntimeException(
|
||||
"Wrong type passed to " . __CLASS__ . "::" . __METHOD__ .
|
||||
" the expected type was 'League\\CommonMark\\Inline\\Element\\Link' but '" .
|
||||
get_class($inline) . "' was provided"
|
||||
);
|
||||
}
|
||||
|
||||
// Default handling
|
||||
$element = parent::render($inline, $htmlRenderer);
|
||||
$url = $inline->getUrl();
|
||||
if (empty($url) || $url[0] != '!') {
|
||||
return $element;
|
||||
}
|
||||
|
||||
//Internal links
|
||||
$file = $this->resolveInternalFile(ltrim($url, "!"));
|
||||
|
||||
$link_props = [
|
||||
'ri:content-title' => trim($this->daux['confluence']['prefix']) . " " . $file->getTitle(),
|
||||
'ri:space-key' => $this->daux['confluence']['space_id']
|
||||
];
|
||||
|
||||
$page = strval(new HtmlElement('ri:page', $link_props, '', true));
|
||||
$children = $htmlRenderer->renderInlines($inline->getChildren());
|
||||
if (strpos($children, "<") !== false) {
|
||||
$children = '<ac:link-body>' . $children . '</ac:link-body>';
|
||||
} else {
|
||||
$children = '<ac:plain-text-link-body><![CDATA[' . $children . ']]></ac:plain-text-link-body>';
|
||||
}
|
||||
|
||||
return new HtmlElement('ac:link', [], $page . $children);
|
||||
}
|
||||
}
|
In neuem Issue referenzieren
Einen Benutzer sperren