From 8f18bb354c6827d2d2c3c0e7e65838b0a239a0eb Mon Sep 17 00:00:00 2001 From: Christophe Vidal Date: Sun, 11 Aug 2019 17:27:26 +0700 Subject: [PATCH 1/3] Fixed class template --- templates/class.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/class.twig b/templates/class.twig index e77b0cd..2bf9a6e 100644 --- a/templates/class.twig +++ b/templates/class.twig @@ -42,7 +42,7 @@ Constants {{ constant.signature|raw }} -{{ property.description|raw }} +{{ constant.description|raw }} {% if constant.deprecated %}* **Warning:** this constant is **deprecated**. This means that this constant will likely be removed in a future version. {% endif %} From 27c233dbdfbb16086e28bbc6c7b95a86846f65d3 Mon Sep 17 00:00:00 2001 From: Christophe Vidal Date: Sun, 11 Aug 2019 17:28:29 +0700 Subject: [PATCH 2/3] Added support for multiblock method argument parameters --- composer.json | 1 + src/Parser.php | 56 +++++++++++++++++++++++++++++++++++++++++++- templates/class.twig | 8 ++++++- 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 1c1617d..2447472 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,7 @@ "license" : "MIT", "require" : { "php" : ">=5.3.1", + "league/html-to-markdown" : "~4.8.2", "twig/twig" : "~1.2|2.0" }, "require-dev" : { diff --git a/src/Parser.php b/src/Parser.php index 518673a..bfdc411 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -2,6 +2,7 @@ namespace PHPDocMD; +use League\HTMLToMarkdown\HtmlConverter; use SimpleXMLElement; /** @@ -27,12 +28,24 @@ class Parser */ protected $classDefinitions; + /** + * The HTML to Markdown converter. + * + * @var League\HTMLToMarkdown\HtmlConverter + */ + protected $htmlConverter; + /** * @param string $structureXmlFile */ function __construct($structureXmlFile) { $this->structureXmlFile = $structureXmlFile; + + $this->htmlConverter = new HtmlConverter([ + 'hard_break' => true, + 'strip_tags' => true, + ]); } /** @@ -153,7 +166,7 @@ protected function parseMethods(SimpleXMLElement $class) } if ((string)$tag['description']) { - $nArgument['description'] = (string)$tag['description']; + $nArgument['description'] = $this->escapeHtmlToMarkdownBlocks((string)$tag['description']); } if ((string)$tag['variable']) { @@ -351,4 +364,45 @@ protected function expandProperties($className) return $newProperties; } + + /** + * Converts encoded HTML to Markdown and breaks multi Markdown blocks to + * string arrays of indented Markdown code. + * + * The former is required since PHPDocumentor encodes certain attributes to + * HTML while generating the XML structure. + * + * The latter is required to allow the Twig template to render blocks + * block-by-block. Currently, this method assumes that such blocks are under + * a top-level block: the block indentation it generates is hard-coded to 4 + * spaces. + * + * @param string $escapedHtml + * The escaped HTML, as encoded in structure.xml + * + * @return string|array + * The Markdown code, as a string if it is a single block or an + * array of strings containing indented blocks otherwise. + */ + protected function escapeHtmlToMarkdownBlocks($escapedHtml) + { + $flags = ENT_QUOTES | ENT_HTML5; + $html = htmlspecialchars_decode($escapedHtml, ENT_QUOTES | ENT_HTML5); + $md = $this->htmlConverter->convert($html); + $blocks = explode("\n\n", $md); + + if (count($blocks) == 1) { + return $blocks[0]; + } + + return array_map(function ($block) { + $lines = explode("\n", $block); + + $lines = array_map(function ($line) { + return ' ' . $line; + }, $lines); + + return implode("\n", $lines); + }, $blocks); + } } diff --git a/templates/class.twig b/templates/class.twig index 2bf9a6e..6aa9b1b 100644 --- a/templates/class.twig +++ b/templates/class.twig @@ -99,7 +99,13 @@ Methods {% if method.arguments %} #### Arguments {% for argument in method.arguments %} -* {{ argument.name }} **{% if argument.type %}{{ argument.type|classLink }}{% else %}mixed{% endif %}**{% if argument.description %} - {{ argument.description }}{% endif %} +* {{ argument.name }} **{% if argument.type %}{{ argument.type|classLink }}{% else %}mixed{% endif %}**{% if argument.description %}{% if argument.description is iterable %} + + +{% for block_ in argument.description %} +{{ block_|raw }} + +{% endfor %}{% else %} - {{ argument.description|raw }}{% endif %}{% endif %} {% endfor %} From f8d5c7c107916ae0158857cbbe8deabc3acd8e7b Mon Sep 17 00:00:00 2001 From: Christophe Vidal Date: Sun, 11 Aug 2019 18:15:56 +0700 Subject: [PATCH 3/3] Fixed code style --- src/Parser.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Parser.php b/src/Parser.php index bfdc411..3dcbdfb 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -395,10 +395,10 @@ protected function escapeHtmlToMarkdownBlocks($escapedHtml) return $blocks[0]; } - return array_map(function ($block) { + return array_map(function($block) { $lines = explode("\n", $block); - $lines = array_map(function ($line) { + $lines = array_map(function($line) { return ' ' . $line; }, $lines);