From 6d6bd5d63aaf5c3a7b135702f670fd04f5b2a0e7 Mon Sep 17 00:00:00 2001 From: Sylry Date: Thu, 17 May 2018 12:28:15 +0200 Subject: [PATCH 1/2] Add option to have different styles for consecutive lists. --- src/Converter/ListItemConverter.php | 11 ++++++++++- tests/HtmlConverterTest.php | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Converter/ListItemConverter.php b/src/Converter/ListItemConverter.php index f737b4e..e1fdeea 100644 --- a/src/Converter/ListItemConverter.php +++ b/src/Converter/ListItemConverter.php @@ -45,7 +45,16 @@ public function convert(ElementInterface $element) if ($list_type === 'ul') { $list_item_style = $this->config->getOption('list_item_style', '-'); - return $prefix . $list_item_style . ' ' . $value . "\n"; + $list_item_style_alternate = $this->config->getOption('list_item_style_alternate'); + if (!isset($this->list_item_style)) { + $this->list_item_style = $list_item_style_alternate ? $list_item_style_alternate : $list_item_style; + } + + if ($list_item_style_alternate && $level == 0 && $element->getSiblingPosition() === 1) { + $this->list_item_style = $this->list_item_style == $list_item_style ? $list_item_style_alternate : $list_item_style; + } + + return $prefix . $this->list_item_style . ' ' . $value . "\n"; } if ($list_type === 'ol' && $start = $element->getParent()->getAttribute('start')) { diff --git a/tests/HtmlConverterTest.php b/tests/HtmlConverterTest.php index 24a793b..4f9e1fa 100644 --- a/tests/HtmlConverterTest.php +++ b/tests/HtmlConverterTest.php @@ -137,6 +137,7 @@ public function test_lists() $this->html_gives_markdown('
  1. Item A
  2. Item B
', "1. Item A\n2. Item B"); $this->html_gives_markdown('
  1. Item A

    Description

  2. Item B
', "1. ### Item A\n \n Description\n2. Item B"); $this->html_gives_markdown('
  1. Item A
  2. Item B
', "120. Item A\n121. Item B"); + $this->html_gives_markdown('', "- first item of first list\n- second item of first list\n\n* first item of second list", array('list_item_style_alternate' => '*')); } public function test_nested_lists() From f29bad3b9e36fdf79b659b415d94462e71af8ccf Mon Sep 17 00:00:00 2001 From: Sylry Date: Thu, 17 May 2018 12:48:21 +0200 Subject: [PATCH 2/2] Fix coding standards. --- src/Converter/ListItemConverter.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Converter/ListItemConverter.php b/src/Converter/ListItemConverter.php index e1fdeea..c56ab89 100644 --- a/src/Converter/ListItemConverter.php +++ b/src/Converter/ListItemConverter.php @@ -13,6 +13,11 @@ class ListItemConverter implements ConverterInterface, ConfigurationAwareInterfa */ protected $config; + /** + * @var string + */ + protected $listItemStyle; + /** * @param Configuration $config */ @@ -46,15 +51,15 @@ public function convert(ElementInterface $element) if ($list_type === 'ul') { $list_item_style = $this->config->getOption('list_item_style', '-'); $list_item_style_alternate = $this->config->getOption('list_item_style_alternate'); - if (!isset($this->list_item_style)) { - $this->list_item_style = $list_item_style_alternate ? $list_item_style_alternate : $list_item_style; + if (!isset($this->listItemStyle)) { + $this->listItemStyle = $list_item_style_alternate ? $list_item_style_alternate : $list_item_style; } if ($list_item_style_alternate && $level == 0 && $element->getSiblingPosition() === 1) { - $this->list_item_style = $this->list_item_style == $list_item_style ? $list_item_style_alternate : $list_item_style; + $this->listItemStyle = $this->listItemStyle == $list_item_style ? $list_item_style_alternate : $list_item_style; } - return $prefix . $this->list_item_style . ' ' . $value . "\n"; + return $prefix . $this->listItemStyle . ' ' . $value . "\n"; } if ($list_type === 'ol' && $start = $element->getParent()->getAttribute('start')) {