Skip to content

Commit

Permalink
[thephpleague#19] Indented list not correctly converted
Browse files Browse the repository at this point in the history
Add an openElement/closeElement to the converters so that they may track additional information once they're about to be converted/have been converted.

This allows us to track the number of ul/ol opened, such that when we render the li element, we know the current indent level.
  • Loading branch information
tomzx committed Jul 25, 2015
1 parent 16f0fe2 commit e1812e9
Show file tree
Hide file tree
Showing 18 changed files with 94 additions and 18 deletions.
26 changes: 26 additions & 0 deletions src/Converter/BaseConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace League\HTMLToMarkdown\Converter;

use League\HTMLToMarkdown\ElementInterface;

abstract class BaseConverter implements ConverterInterface
{
/**
* @param \League\HTMLToMarkdown\ElementInterface $element
* @return void
*/
public function openElement(ElementInterface $element)
{
// Do nothing
}

/**
* @param \League\HTMLToMarkdown\ElementInterface $element
* @return void
*/
public function closeElement(ElementInterface $element)
{
// Do nothing
}
}
2 changes: 1 addition & 1 deletion src/Converter/BlockquoteConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use League\HTMLToMarkdown\ElementInterface;

class BlockquoteConverter implements ConverterInterface
class BlockquoteConverter extends BaseConverter
{
/**
* @param ElementInterface $element
Expand Down
2 changes: 1 addition & 1 deletion src/Converter/CommentConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use League\HTMLToMarkdown\ElementInterface;

class CommentConverter implements ConverterInterface
class CommentConverter extends BaseConverter
{
/**
* @param ElementInterface $element
Expand Down
12 changes: 12 additions & 0 deletions src/Converter/ConverterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,16 @@ public function convert(ElementInterface $element);
* @return string[]
*/
public function getSupportedTags();

/**
* @param \League\HTMLToMarkdown\ElementInterface $element
* @return void
*/
public function openElement(ElementInterface $element);

/**
* @param \League\HTMLToMarkdown\ElementInterface $element
* @return void
*/
public function closeElement(ElementInterface $element);
}
2 changes: 1 addition & 1 deletion src/Converter/DefaultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use League\HTMLToMarkdown\ConfigurationAwareInterface;
use League\HTMLToMarkdown\ElementInterface;

class DefaultConverter implements ConverterInterface, ConfigurationAwareInterface
class DefaultConverter extends BaseConverter implements ConfigurationAwareInterface
{
const DEFAULT_CONVERTER = '_default';

Expand Down
2 changes: 1 addition & 1 deletion src/Converter/DivConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use League\HTMLToMarkdown\ConfigurationAwareInterface;
use League\HTMLToMarkdown\ElementInterface;

class DivConverter implements ConverterInterface, ConfigurationAwareInterface
class DivConverter extends BaseConverter implements ConfigurationAwareInterface
{
/**
* @var Configuration
Expand Down
2 changes: 1 addition & 1 deletion src/Converter/EmphasisConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use League\HTMLToMarkdown\ConfigurationAwareInterface;
use League\HTMLToMarkdown\ElementInterface;

class EmphasisConverter implements ConverterInterface, ConfigurationAwareInterface
class EmphasisConverter extends BaseConverter implements ConfigurationAwareInterface
{
/**
* @var Configuration
Expand Down
2 changes: 1 addition & 1 deletion src/Converter/HardBreakConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use League\HTMLToMarkdown\ElementInterface;

class HardBreakConverter implements ConverterInterface
class HardBreakConverter extends BaseConverter
{
/**
* @param ElementInterface $element
Expand Down
2 changes: 1 addition & 1 deletion src/Converter/HeaderConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use League\HTMLToMarkdown\ConfigurationAwareInterface;
use League\HTMLToMarkdown\ElementInterface;

class HeaderConverter implements ConverterInterface, ConfigurationAwareInterface
class HeaderConverter extends BaseConverter implements ConfigurationAwareInterface
{
const STYLE_ATX = 'atx';
const STYLE_SETEXT = 'setext';
Expand Down
2 changes: 1 addition & 1 deletion src/Converter/HorizontalRuleConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use League\HTMLToMarkdown\ElementInterface;

class HorizontalRuleConverter implements ConverterInterface
class HorizontalRuleConverter extends BaseConverter
{
/**
* @param ElementInterface $element
Expand Down
2 changes: 1 addition & 1 deletion src/Converter/ImageConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use League\HTMLToMarkdown\ElementInterface;

class ImageConverter implements ConverterInterface
class ImageConverter extends BaseConverter
{
/**
* @param ElementInterface $element
Expand Down
2 changes: 1 addition & 1 deletion src/Converter/LinkConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use League\HTMLToMarkdown\ElementInterface;

class LinkConverter implements ConverterInterface
class LinkConverter extends BaseConverter
{
/**
* @param ElementInterface $element
Expand Down
22 changes: 19 additions & 3 deletions src/Converter/ListBlockConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@

use League\HTMLToMarkdown\ElementInterface;

class ListBlockConverter implements ConverterInterface
class ListBlockConverter extends BaseConverter
{
public static $level = -1;

/**
* @param ElementInterface $element
*
* @return string
*/
public function convert(ElementInterface $element)
{
return $element->getValue() . "\n";
}
if (self::$level >= 0) {
return "\n".$element->getValue() . "\n";
} else {
return $element->getValue() . "\n";
}
}

/**
* @return string[]
Expand All @@ -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--;
}
}
4 changes: 2 additions & 2 deletions src/Converter/ListItemConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use League\HTMLToMarkdown\ElementInterface;

class ListItemConverter implements ConverterInterface
class ListItemConverter extends BaseConverter
{
/**
* @param ElementInterface $element
Expand All @@ -24,7 +24,7 @@ public function convert(ElementInterface $element)
$markdown = $number . '. ' . trim($value) . "\n";
}

return $markdown;
return str_repeat("\t", ListBlockConverter::$level).$markdown;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Converter/ParagraphConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use League\HTMLToMarkdown\ElementInterface;

class ParagraphConverter implements ConverterInterface
class ParagraphConverter extends BaseConverter
{
/**
* @param ElementInterface $element
Expand Down
2 changes: 1 addition & 1 deletion src/Converter/PreformattedConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use League\HTMLToMarkdown\ElementInterface;

class PreformattedConverter implements ConverterInterface
class PreformattedConverter extends BaseConverter
{
/**
* @param ElementInterface $element
Expand Down
2 changes: 1 addition & 1 deletion src/Converter/TextConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use League\HTMLToMarkdown\ElementInterface;

class TextConverter implements ConverterInterface
class TextConverter extends BaseConverter
{
/**
* @param ElementInterface $element
Expand Down
22 changes: 22 additions & 0 deletions src/HtmlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,17 @@ 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) {
$this->convertChildren($child);
}
}

$this->postProcess($element);

// Now that child nodes have been converted, convert the original node
$markdown = $this->convertToMarkdown($element);

Expand All @@ -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
*
Expand Down

0 comments on commit e1812e9

Please sign in to comment.