diff --git a/src/Converter/BaseConverter.php b/src/Converter/BaseConverter.php new file mode 100644 index 0000000..992d6ef --- /dev/null +++ b/src/Converter/BaseConverter.php @@ -0,0 +1,26 @@ +getValue() . "\n"; - } + if (self::$level >= 0) { + return "\n".$element->getValue() . "\n"; + } else { + return $element->getValue() . "\n"; + } + } /** * @return string[] @@ -23,4 +29,14 @@ public function getSupportedTags() { return array('ol', 'ul'); } + + public function openElement(ElementInterface $element) + { + self::$level++; + } + + public function closeElement(ElementInterface $element) + { + self::$level--; + } } diff --git a/src/Converter/ListItemConverter.php b/src/Converter/ListItemConverter.php index 625bf42..b385d44 100644 --- a/src/Converter/ListItemConverter.php +++ b/src/Converter/ListItemConverter.php @@ -4,7 +4,7 @@ use League\HTMLToMarkdown\ElementInterface; -class ListItemConverter implements ConverterInterface +class ListItemConverter extends BaseConverter { /** * @param ElementInterface $element @@ -24,7 +24,7 @@ public function convert(ElementInterface $element) $markdown = $number . '. ' . trim($value) . "\n"; } - return $markdown; + return str_repeat("\t", ListBlockConverter::$level).$markdown; } /** diff --git a/src/Converter/ParagraphConverter.php b/src/Converter/ParagraphConverter.php index 8a72e8a..9e1f102 100644 --- a/src/Converter/ParagraphConverter.php +++ b/src/Converter/ParagraphConverter.php @@ -4,7 +4,7 @@ use League\HTMLToMarkdown\ElementInterface; -class ParagraphConverter implements ConverterInterface +class ParagraphConverter extends BaseConverter { /** * @param ElementInterface $element diff --git a/src/Converter/PreformattedConverter.php b/src/Converter/PreformattedConverter.php index 5b5e662..4769452 100644 --- a/src/Converter/PreformattedConverter.php +++ b/src/Converter/PreformattedConverter.php @@ -4,7 +4,7 @@ use League\HTMLToMarkdown\ElementInterface; -class PreformattedConverter implements ConverterInterface +class PreformattedConverter extends BaseConverter { /** * @param ElementInterface $element diff --git a/src/Converter/TextConverter.php b/src/Converter/TextConverter.php index 0f8b75f..eba3be9 100644 --- a/src/Converter/TextConverter.php +++ b/src/Converter/TextConverter.php @@ -4,7 +4,7 @@ use League\HTMLToMarkdown\ElementInterface; -class TextConverter implements ConverterInterface +class TextConverter extends BaseConverter { /** * @param ElementInterface $element diff --git a/src/HtmlConverter.php b/src/HtmlConverter.php index ae5ecd4..9a8dea6 100644 --- a/src/HtmlConverter.php +++ b/src/HtmlConverter.php @@ -133,6 +133,8 @@ private function convertChildren(ElementInterface $element) return; } + $this->preProcess($element); + // If the node has children, convert those to Markdown first if ($element->hasChildren()) { foreach ($element->getChildren() as $child) { @@ -140,6 +142,8 @@ private function convertChildren(ElementInterface $element) } } + $this->postProcess($element); + // Now that child nodes have been converted, convert the original node $markdown = $this->convertToMarkdown($element); @@ -149,6 +153,24 @@ private function convertChildren(ElementInterface $element) $element->setFinalMarkdown($markdown); } + /** + * @param \League\HTMLToMarkdown\ElementInterface $element + */ + protected function preProcess(ElementInterface $element) + { + $converter = $this->environment->getConverterByTag($element->getTagName()); + $converter->openElement($element); + } + + /** + * @param \League\HTMLToMarkdown\ElementInterface $element + */ + protected function postProcess(ElementInterface $element) + { + $converter = $this->environment->getConverterByTag($element->getTagName()); + $converter->closeElement($element); + } + /** * Convert to Markdown *