18ec9a8f2SSungbin Jeon<?php 28ec9a8f2SSungbin Jeon 38ec9a8f2SSungbin Jeonnamespace Dokuwiki\Plugin\Commonmark; 48ec9a8f2SSungbin Jeon 594a075eeSSungbin Jeonuse League\CommonMark\Environment\Environment; 694a075eeSSungbin Jeonuse League\CommonMark\Parser\MarkdownParser; 71e536badSSungbin Jeonuse Dokuwiki\Plugin\Commonmark\Extension\CommonmarkToDokuwikiExtension; 8f46768a8SSungbin Jeonuse Dokuwiki\Plugin\Commonmark\Extension\FootnoteToDokuwikiExtension; 978c882e7SSungbin Jeonuse League\CommonMark\Extension\Strikethrough\StrikethroughExtension; 10656793f4SSungbin Jeonuse Dokuwiki\Plugin\Commonmark\Extension\TableExtension; 1181da5a38SSungbin Jeonuse League\CommonMark\Extension\FrontMatter\FrontMatterExtension; 1281da5a38SSungbin Jeonuse League\CommonMark\Extension\FrontMatter\Output\RenderedContentWithFrontMatter; 138ec9a8f2SSungbin Jeon 148ec9a8f2SSungbin Jeonclass Commonmark { 157569cca4SSungbin Jeon public static function RendtoDW($markdown, $frontmatter_tag = 'off'): string { 168ec9a8f2SSungbin Jeon # create environment 178ec9a8f2SSungbin Jeon $environment = self::createDWEnvironment(); 188ec9a8f2SSungbin Jeon 198ec9a8f2SSungbin Jeon # create parser 2094a075eeSSungbin Jeon $parser = new MarkdownParser($environment); 218ec9a8f2SSungbin Jeon # create Dokuwiki Renderer 228ec9a8f2SSungbin Jeon $DWRenderer = new DWRenderer($environment); 238ec9a8f2SSungbin Jeon 2481da5a38SSungbin Jeon # separate frontmatter and main text 2581da5a38SSungbin Jeon $FMresult = self::ExtractFrontmatter($markdown); 2681da5a38SSungbin Jeon $frontmatter = $FMresult->getFrontMatter(); 2781da5a38SSungbin Jeon $markdownOnly = $FMresult->getContent(); 288bcf583bSSungbin Jeon $tagStr = ''; # initialize tag string 2981da5a38SSungbin Jeon //print_r($frontmatter); 3081da5a38SSungbin Jeon 3181da5a38SSungbin Jeon # extract tags only 32022ce692SSungbin Jeon if(!empty($frontmatter) && gettype($frontmatter) == "array") { // frontmatter must be array if valid 33022ce692SSungbin Jeon if (array_key_exists('tags', $frontmatter)) { 3481da5a38SSungbin Jeon $tags = $frontmatter['tags']; 357569cca4SSungbin Jeon $tagStr = "{{tag>"; 3681da5a38SSungbin Jeon foreach ($tags as $tag) { 3781da5a38SSungbin Jeon $tagStr = $tagStr. "\"". $tag. "\" "; 3881da5a38SSungbin Jeon } 394384789bSSungbin Jeon $tagStr = $tagStr. "}}"; 404384789bSSungbin Jeon //echo $tagStr; 418bcf583bSSungbin Jeon } 42022ce692SSungbin Jeon } 4381da5a38SSungbin Jeon 44*ac4825fcSSungbin Jeon // pre-processing: convert slash inside wikilink to colon & image wikilinks 45*ac4825fcSSungbin Jeon $markdownOnly = self::ParseDokuwikiWikilinks($markdownOnly); 4681da5a38SSungbin Jeon $document = $parser->parse($markdownOnly); 4781da5a38SSungbin Jeon $renderResult = $DWRenderer->renderNode($document); 4881da5a38SSungbin Jeon 497569cca4SSungbin Jeon if($frontmatter_tag == 'off') { 507569cca4SSungbin Jeon return $renderResult; 517569cca4SSungbin Jeon } elseif($frontmatter_tag == 'upper') { 527569cca4SSungbin Jeon return $tagStr."\n\n".$renderResult; 537569cca4SSungbin Jeon } else { 547569cca4SSungbin Jeon return $renderResult."\n\n".$tagStr; 557569cca4SSungbin Jeon } 5681da5a38SSungbin Jeon } 5781da5a38SSungbin Jeon 5881da5a38SSungbin Jeon // Temporary implementation: separate method for frontmatter extraction 5981da5a38SSungbin Jeon // Since som parsed frontmatter info must be included in main text, it should be merged 6081da5a38SSungbin Jeon public static function ExtractFrontmatter($markdown) { 6181da5a38SSungbin Jeon $frontMatterExtension = new FrontMatterExtension(); 6281da5a38SSungbin Jeon $result = $frontMatterExtension->getFrontMatterParser()->parse($markdown); 6381da5a38SSungbin Jeon 6481da5a38SSungbin Jeon return $result; 658ec9a8f2SSungbin Jeon } 668ec9a8f2SSungbin Jeon 67*ac4825fcSSungbin Jeon // replace slash in MD wikilink to colon to match DW syntax 68*ac4825fcSSungbin Jeon public static function ParseDokuwikiWikilinks($text) { 69*ac4825fcSSungbin Jeon $pattern = "/(?:\[\[\b|(?!^)\G)[^\/|\]]*\K\/+/"; 70*ac4825fcSSungbin Jeon $result = preg_replace($pattern, ":", $text); 71*ac4825fcSSungbin Jeon $pattern = "/!\[\[(.*)\]\]/"; 72*ac4825fcSSungbin Jeon $result = preg_replace($pattern, '{{$1}}', $result); 73*ac4825fcSSungbin Jeon return $result; 74*ac4825fcSSungbin Jeon } 75*ac4825fcSSungbin Jeon 768ec9a8f2SSungbin Jeon public static function createDWEnvironment(): Environment { 77b0a36678SSungbin Jeon $config = []; 78b0a36678SSungbin Jeon $environment = new Environment($config); 798ec9a8f2SSungbin Jeon $environment->addExtension(new CommonMarkToDokuWikiExtension()); 80f46768a8SSungbin Jeon $environment->addExtension(new FootnoteToDokuwikiExtension()); 8178c882e7SSungbin Jeon $environment->addExtension(new StrikethroughExtension()); 82656793f4SSungbin Jeon $environment->addExtension(new TableExtension()); 8381da5a38SSungbin Jeon $environment->addExtension(new FrontMatterExtension()); 84f46768a8SSungbin Jeon 85f46768a8SSungbin Jeon $environment->mergeConfig([ 86f46768a8SSungbin Jeon 'html_input' => 'strip', 87f46768a8SSungbin Jeon ]); 888ec9a8f2SSungbin Jeon 898ec9a8f2SSungbin Jeon return $environment; 908ec9a8f2SSungbin Jeon } 918ec9a8f2SSungbin Jeon} 928ec9a8f2SSungbin Jeon 938ec9a8f2SSungbin Jeon?>