From 5050e99777df61ab95391c380b0ba7cf11ca0f60 Mon Sep 17 00:00:00 2001 From: Sylry Date: Wed, 16 May 2018 18:26:41 +0200 Subject: [PATCH] Add option to have differnt styles for consecutive lists. --- src/Converter/ListItemConverter.php | 47 +++++++++++++++++++++++++++-- tests/HtmlConverterTest.php | 1 + 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/Converter/ListItemConverter.php b/src/Converter/ListItemConverter.php index f737b4e..f21bca1 100644 --- a/src/Converter/ListItemConverter.php +++ b/src/Converter/ListItemConverter.php @@ -13,12 +13,52 @@ class ListItemConverter implements ConverterInterface, ConfigurationAwareInterfa */ protected $config; + /** + * @var bool + */ + protected $doAlternatePrefixes = FALSE; + + /** + * @var array + */ + protected $prefixes; + + /** + * @var array + */ + protected $standardPrefixes; + + /** + * @var array + */ + protected $alternatePrefixes; + /** * @param Configuration $config */ public function setConfig(Configuration $config) { $this->config = $config; + $list_item_alternate = $this->config->getOption('list_item_alternate'); + if (isset($list_item_alternate)) { + foreach (['standard', 'alternate'] as $item) { + if (!isset($list_item_alternate[$item])) { + throw new \Exception("The '{$item}' property is missing in 'list_item_alternate' config of HtmlConverter."); + } + if (count($list_item_alternate[$item]) != 2) { + throw new \Exception("The '{$item}' property (in 'list_item_alternate' config of HtmlConverter) should have 2 entries."); + } + } + + $this->doAlternatePrefixes = TRUE; + $this->prefixes = $list_item_alternate['alternate']; + $this->standardPrefixes = $list_item_alternate['standard']; + $this->alternatePrefixes = $list_item_alternate['alternate']; + } + else { + $list_item_style = $this->config->getOption('list_item_style', '-'); + $this->prefixes = [$list_item_style, '.']; + } } /** @@ -42,9 +82,12 @@ public function convert(ElementInterface $element) if ($level > 0 && $element->getSiblingPosition() === 1) { $prefix = "\n"; } + elseif ($level == 0 && $element->getSiblingPosition() === 1 && $this->doAlternatePrefixes) { + $this->prefixes = ($this->prefixes == $this->standardPrefixes) ? $this->alternatePrefixes : $this->standardPrefixes; + } if ($list_type === 'ul') { - $list_item_style = $this->config->getOption('list_item_style', '-'); + $list_item_style = $this->prefixes[0]; return $prefix . $list_item_style . ' ' . $value . "\n"; } @@ -54,7 +97,7 @@ public function convert(ElementInterface $element) $number = $element->getSiblingPosition(); } - return $prefix . $number . '. ' . $value . "\n"; + return $prefix . $number . $this->prefixes[1] . ' ' . $value . "\n"; } /** diff --git a/tests/HtmlConverterTest.php b/tests/HtmlConverterTest.php index 24a793b..73d03d5 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('', "- item of first list\n- item of first list\n\n* item of second list", array('list_item_alternate' => array('standard' => array('-', '.'), 'alternate' => array('*', ')')))); } public function test_nested_lists()