From 3930e3f92fe243a3cc9c269dda0e7ce23ee00c64 Mon Sep 17 00:00:00 2001 From: haszi Date: Tue, 13 Feb 2024 12:01:38 +0100 Subject: [PATCH 1/5] Implement simplelist attributes --- phpdotnet/phd/Package/Generic/XHTML.php | 125 +++++++++++++++++++++++- phpdotnet/phd/Render.php | 3 - tests/xhtml/data/simplelist.xml | 56 +++++++++++ tests/xhtml/simplelist_001.phpt | 100 +++++++++++++++++++ 4 files changed, 277 insertions(+), 7 deletions(-) create mode 100644 tests/xhtml/data/simplelist.xml create mode 100644 tests/xhtml/simplelist_001.phpt diff --git a/phpdotnet/phd/Package/Generic/XHTML.php b/phpdotnet/phd/Package/Generic/XHTML.php index 40a26e75..77bdf9fb 100644 --- a/phpdotnet/phd/Package/Generic/XHTML.php +++ b/phpdotnet/phd/Package/Generic/XHTML.php @@ -143,7 +143,7 @@ abstract class Package_Generic_XHTML extends Format_Abstract_XHTML { 'methodparam' => 'format_methodparam', 'methodsynopsis' => 'format_methodsynopsis', 'methodname' => 'format_methodname', - 'member' => 'li', + 'member' => 'format_member', 'modifier' => 'span', 'note' => 'format_note', 'orgname' => 'span', @@ -269,7 +269,7 @@ abstract class Package_Generic_XHTML extends Format_Abstract_XHTML { 'setindex' => 'format_chunk', 'shortaffil' => 'format_suppressed_tags', 'sidebar' => 'format_note', - 'simplelist' => 'format_itemizedlist', /* FIXME: simplelists has few attributes that need to be implemented */ + 'simplelist' => 'format_simplelist', /* FIXME: simplelists has few attributes that need to be implemented */ 'simplesect' => 'div', 'simpara' => array( /* DEFAULT */ 'p', @@ -446,6 +446,7 @@ abstract class Package_Generic_XHTML extends Format_Abstract_XHTML { 'literal' => 'format_literal_text', 'email' => 'format_email_text', 'titleabbrev' => 'format_suppressed_text', + 'member' => 'format_member_text', ); /** @var array */ @@ -501,6 +502,11 @@ abstract class Package_Generic_XHTML extends Format_Abstract_XHTML { "varlistentry" => array( "listitems" => array(), ), + "simplelist" => array( + "members" => array(), + "type" => null, + "columns" => null, + ), ); protected $pihandlers = array( @@ -2040,6 +2046,110 @@ public function format_itemizedlist($open, $name, $attrs, $props) { return ''; } + public function format_simplelist($open, $name, $attrs, $props) { + if ($open) { + $this->cchunk["simplelist"]["type"] = $attrs[Reader::XMLNS_DOCBOOK]["type"] ?? ""; + $this->cchunk["simplelist"]["columns"] = $attrs[Reader::XMLNS_DOCBOOK]["columns"] ?? 1; + + if ($this->cchunk["simplelist"]["columns"] < 1) { + $this->cchunk["simplelist"]["columns"] = 1; + } + + if ($this->cchunk["simplelist"]["type"] === "inline") { + return ''; + } else if ($this->cchunk["simplelist"]["type"] === "vert" || $this->cchunk["simplelist"]["type"] === "horiz") { + return '' . "\n" . str_repeat(" ", $props["depth"] + 1) . "\n"; + } + + return '\n"; + } + + $table .= str_repeat(" ", $props["depth"] + 3) . "\n"; + + if ($i % $this->cchunk["simplelist"]["columns"] === $this->cchunk["simplelist"]["columns"] - 1) { + $table .= str_repeat(" ", $props["depth"] + 2) . "\n"; + } + } + if ($i % $this->cchunk["simplelist"]["columns"] !== 0) { + $numOfMissingCells = $this->cchunk["simplelist"]["columns"] - ($i % $this->cchunk["simplelist"]["columns"]); + $oneRow = str_repeat(" ", $props["depth"] + 3) . "\n"; + $table .= str_repeat($oneRow, $numOfMissingCells); + $table .= str_repeat(" ", $props["depth"] + 2) . "\n"; + } + + $this->cchunk["simplelist"] = $this->dchunk["simplelist"]; + + return $table . str_repeat(" ", $props["depth"] + 1) . "\n" . str_repeat(" ", $props["depth"]) . "
" . $this->cchunk["simplelist"]["members"][$i] . "
"; + + } + + if ($this->cchunk["simplelist"]["type"] === "vert") { + + $table = ""; + $numOfRows = ceil(count($this->cchunk["simplelist"]["members"]) / $this->cchunk["simplelist"]["columns"]); + for ($row = 0; $row < $numOfRows; $row++) { + $table .= str_repeat(" ", $props["depth"] + 2) . "\n"; + for ($col = 0; $col < $this->cchunk["simplelist"]["columns"]; $col++) { + $memberIndex = ($numOfRows * $col) + $row; + $table .= str_repeat(" ", $props["depth"] + 3) . ""; + if ($memberIndex < count($this->cchunk["simplelist"]["members"])) { + $table .= $this->cchunk["simplelist"]["members"][$memberIndex]; + } + $table .= "\n"; + } + $table .= str_repeat(" ", $props["depth"] + 2) . "\n"; + } + $this->cchunk["simplelist"] = $this->dchunk["simplelist"]; + + return $table . str_repeat(" ", $props["depth"] + 1) . "\n" . str_repeat(" ", $props["depth"]) . ""; + } + + $this->cchunk["simplelist"] = $this->dchunk["simplelist"]; + return ''; + } + + public function format_member($open, $name, $attrs, $props) { + if ($this->cchunk["simplelist"]["type"] === "inline" + || $this->cchunk["simplelist"]["type"] === "vert" + || $this->cchunk["simplelist"]["type"] === "horiz") { + return ''; + } + if ($open) { + return '
  • '; + } + return '
  • '; + } + + public function format_member_text($value, $tag) { + if ($this->cchunk["simplelist"]["type"] === "inline" + || $this->cchunk["simplelist"]["type"] === "vert" + || $this->cchunk["simplelist"]["type"] === "horiz") { + $this->cchunk["simplelist"]["members"][] = $value; + return ''; + } + return $value; + } + public function format_orderedlist($open, $name, $attrs, $props) { if ($open) { $numeration = "1"; @@ -2115,6 +2225,15 @@ public function format_whitespace($whitespace, $elementStack, $currentDepth) { return false; } + if ( + $elementStack[$currentDepth - 1] === "simplelist" + && ($this->cchunk["simplelist"]["type"] === "inline" + || $this->cchunk["simplelist"]["type"] === "vert" + || $this->cchunk["simplelist"]["type"] === "horiz") + ) { + return false; + } + /* The following if is to skip unnecessary whitespaces in the implements list */ if ( ($elementStack[$currentDepth - 1] === 'classsynopsisinfo' && $elementStack[$currentDepth] === 'oointerface') || @@ -2127,5 +2246,3 @@ public function format_whitespace($whitespace, $elementStack, $currentDepth) { } } - - diff --git a/phpdotnet/phd/Render.php b/phpdotnet/phd/Render.php index 144c56fd..e3a17cc3 100644 --- a/phpdotnet/phd/Render.php +++ b/phpdotnet/phd/Render.php @@ -216,6 +216,3 @@ public function execute(Reader $r) { /* {{{ */ } /* }}} */ } - - - diff --git a/tests/xhtml/data/simplelist.xml b/tests/xhtml/data/simplelist.xml new file mode 100644 index 00000000..ce3e5d5b --- /dev/null +++ b/tests/xhtml/data/simplelist.xml @@ -0,0 +1,56 @@ + + + +
    + 1. Simplelist with no type + + First + Second + Third + Fourth + Fifth + Sixth + Seventh + +
    + +
    + 2. Simplelist with "inline" type + + First + Second + Third + Fourth + Fifth + Sixth + Seventh + +
    + +
    + 3. Simplelist with "vert" type, 3 columns + + First + Second + Third + Fourth + Fifth + Sixth + Seventh + +
    + +
    + 4. Simplelist with "horiz" type, 4 columns + + First + Second + Third + Fourth + Fifth + Sixth + Seventh + +
    + +
    diff --git a/tests/xhtml/simplelist_001.phpt b/tests/xhtml/simplelist_001.phpt new file mode 100644 index 00000000..ad461a05 --- /dev/null +++ b/tests/xhtml/simplelist_001.phpt @@ -0,0 +1,100 @@ +--TEST-- +Simplelist rendering 001 - Types and columns +--FILE-- + true, + "xml_root" => dirname($xml_file), + "xml_file" => $xml_file, + "output_dir" => __DIR__ . "/output/", +); + +$extra = array( + "lang_dir" => __PHDDIR__ . "phpdotnet/phd/data/langs/", + "phpweb_version_filename" => dirname($xml_file) . '/version.xml', + "phpweb_acronym_filename" => dirname($xml_file) . '/acronyms.xml', +); + +$render = new TestRender($formatclass, $opts, $extra); + +if (Index::requireIndexing() && !file_exists($opts["output_dir"])) { + mkdir($opts["output_dir"], 0755); +} + +$render->run(); +?> +--EXPECTF-- +Filename: simpelist.html +Content: +
    + +
    +

    1. Simplelist with no type

    +
      +
    • First
    • +
    • Second
    • +
    • Third
    • +
    • Fourth
    • +
    • Fifth
    • +
    • Sixth
    • +
    • Seventh
    • +
    +
    + +
    +

    2. Simplelist with "inline" type

    + First, Second, Third, Fourth, Fifth, Sixth, Seventh +
    + +
    +

    3. Simplelist with "vert" type, 3 columns

    + + + + + + + + + + + + + + + + + + +
    FirstFourthSeventh
    SecondFifth
    ThirdSixth
    +
    + +
    +

    4. Simplelist with "horiz" type, 4 columns

    + + + + + + + + + + + + + + + +
    FirstSecondThirdFourth
    FifthSixthSeventh
    +
    + +
    From eb6a7c6639f804b30a32730e4b27609c09f20bd8 Mon Sep 17 00:00:00 2001 From: haszi Date: Thu, 15 Feb 2024 10:55:43 +0100 Subject: [PATCH 2/5] Move formatting into separate methods --- phpdotnet/phd/Package/Generic/XHTML.php | 94 ++++++++++++------------- 1 file changed, 45 insertions(+), 49 deletions(-) diff --git a/phpdotnet/phd/Package/Generic/XHTML.php b/phpdotnet/phd/Package/Generic/XHTML.php index 77bdf9fb..ebfe896c 100644 --- a/phpdotnet/phd/Package/Generic/XHTML.php +++ b/phpdotnet/phd/Package/Generic/XHTML.php @@ -2057,75 +2057,71 @@ public function format_simplelist($open, $name, $attrs, $props) { if ($this->cchunk["simplelist"]["type"] === "inline") { return ''; - } else if ($this->cchunk["simplelist"]["type"] === "vert" || $this->cchunk["simplelist"]["type"] === "horiz") { + } + + if ($this->cchunk["simplelist"]["type"] === "vert" + || $this->cchunk["simplelist"]["type"] === "horiz") { return '' . "\n" . str_repeat(" ", $props["depth"] + 1) . "\n"; } return '
      '; } - if ($this->cchunk["simplelist"]["type"] === "inline") { - $list = ""; - foreach ($this->cchunk["simplelist"]["members"] as $member) { - $list .= $member . ", "; - } - $list = rtrim($list, ", "); + $list = match ($this->cchunk["simplelist"]["type"]) { + "inline" => $this->format_inline_simplelist(), + "horiz" => $this->format_horizontal_simplelist($props), + "vert" => $this->format_vertical_simplelist($props), + default => "
    ", + }; - $this->cchunk["simplelist"] = $this->dchunk["simplelist"]; - return $list . ''; + $this->cchunk["simplelist"] = $this->dchunk["simplelist"]; - } + return $list; + } - if ($this->cchunk["simplelist"]["type"] === "horiz") { + private function format_inline_simplelist() { + return implode(", ", $this->cchunk["simplelist"]["members"]) . ''; + } - $table = ""; - for ($i = 0; $i < count($this->cchunk["simplelist"]["members"]); $i++) { - if ($i % $this->cchunk["simplelist"]["columns"] === 0) { - $table .= str_repeat(" ", $props["depth"] + 2) . "\n"; - } + private function format_horizontal_simplelist($props) { + $this->cchunk["simplelist"]["members"] = array_merge($this->cchunk["simplelist"]["members"], $this->get_simplelist_members_padding()); - $table .= str_repeat(" ", $props["depth"] + 3) . "\n"; + $table = ""; + $trPadding = str_repeat(" ", $props["depth"] + 2); + $tdPadding = str_repeat(" ", $props["depth"] + 3); + for ($i = 0; $i < count($this->cchunk["simplelist"]["members"]); $i++) { + $trOpen = ($i % $this->cchunk["simplelist"]["columns"] === 0) ? ($trPadding . "\n") : ""; + $trClose = ($i % $this->cchunk["simplelist"]["columns"] === $this->cchunk["simplelist"]["columns"] - 1) ? ($trPadding . "\n") : ""; - if ($i % $this->cchunk["simplelist"]["columns"] === $this->cchunk["simplelist"]["columns"] - 1) { - $table .= str_repeat(" ", $props["depth"] + 2) . "\n"; - } - } - if ($i % $this->cchunk["simplelist"]["columns"] !== 0) { - $numOfMissingCells = $this->cchunk["simplelist"]["columns"] - ($i % $this->cchunk["simplelist"]["columns"]); - $oneRow = str_repeat(" ", $props["depth"] + 3) . "\n"; - $table .= str_repeat($oneRow, $numOfMissingCells); - $table .= str_repeat(" ", $props["depth"] + 2) . "\n"; - } + $table .= $trOpen . $tdPadding . "\n" . $trClose; + } - $this->cchunk["simplelist"] = $this->dchunk["simplelist"]; + return $table . str_repeat(" ", $props["depth"] + 1) . "\n" . str_repeat(" ", $props["depth"]) . "
    " . $this->cchunk["simplelist"]["members"][$i] . "
    " . $this->cchunk["simplelist"]["members"][$i] . "
    "; + } - return $table . str_repeat(" ", $props["depth"] + 1) . "\n" . str_repeat(" ", $props["depth"]) . ""; + private function get_simplelist_members_padding() { + $numOfRows = ceil(count($this->cchunk["simplelist"]["members"]) / $this->cchunk["simplelist"]["columns"]); - } + return array_fill(0, ($this->cchunk["simplelist"]["columns"] * $numOfRows) - count($this->cchunk["simplelist"]["members"]), ""); + } - if ($this->cchunk["simplelist"]["type"] === "vert") { + private function format_vertical_simplelist($props) { + $numOfRows = ceil(count($this->cchunk["simplelist"]["members"]) / $this->cchunk["simplelist"]["columns"]); - $table = ""; - $numOfRows = ceil(count($this->cchunk["simplelist"]["members"]) / $this->cchunk["simplelist"]["columns"]); - for ($row = 0; $row < $numOfRows; $row++) { - $table .= str_repeat(" ", $props["depth"] + 2) . "\n"; - for ($col = 0; $col < $this->cchunk["simplelist"]["columns"]; $col++) { - $memberIndex = ($numOfRows * $col) + $row; - $table .= str_repeat(" ", $props["depth"] + 3) . ""; - if ($memberIndex < count($this->cchunk["simplelist"]["members"])) { - $table .= $this->cchunk["simplelist"]["members"][$memberIndex]; - } - $table .= "\n"; - } - $table .= str_repeat(" ", $props["depth"] + 2) . "\n"; - } - $this->cchunk["simplelist"] = $this->dchunk["simplelist"]; + $this->cchunk["simplelist"]["members"] = array_merge($this->cchunk["simplelist"]["members"], $this->get_simplelist_members_padding()); - return $table . str_repeat(" ", $props["depth"] + 1) . "\n" . str_repeat(" ", $props["depth"]) . ""; + $table = ""; + $trPadding = str_repeat(" ", $props["depth"] + 2); + $tdPadding = str_repeat(" ", $props["depth"] + 3); + for ($row = 0; $row < $numOfRows; $row++) { + $table .= $trPadding . "\n"; + for ($col = 0; $col < $this->cchunk["simplelist"]["columns"]; $col++) { + $table .= $tdPadding . "" . $this->cchunk["simplelist"]["members"][($numOfRows * $col) + $row] . "\n"; + } + $table .= $trPadding . "\n"; } - $this->cchunk["simplelist"] = $this->dchunk["simplelist"]; - return ''; + return $table . str_repeat(" ", $props["depth"] + 1) . "\n" . str_repeat(" ", $props["depth"]) . ""; } public function format_member($open, $name, $attrs, $props) { From 573cf5d5398b84864a7ef83cbbf43b0d08bdae23 Mon Sep 17 00:00:00 2001 From: haszi Date: Thu, 15 Feb 2024 11:27:27 +0100 Subject: [PATCH 3/5] Simplelist vertical and horizontal formatting Authored-by: Gina Peter Banyard --- phpdotnet/phd/Package/Generic/XHTML.php | 87 ++++++++++++++----------- 1 file changed, 50 insertions(+), 37 deletions(-) diff --git a/phpdotnet/phd/Package/Generic/XHTML.php b/phpdotnet/phd/Package/Generic/XHTML.php index ebfe896c..642495f1 100644 --- a/phpdotnet/phd/Package/Generic/XHTML.php +++ b/phpdotnet/phd/Package/Generic/XHTML.php @@ -2069,8 +2069,8 @@ public function format_simplelist($open, $name, $attrs, $props) { $list = match ($this->cchunk["simplelist"]["type"]) { "inline" => $this->format_inline_simplelist(), - "horiz" => $this->format_horizontal_simplelist($props), - "vert" => $this->format_vertical_simplelist($props), + "horiz" => $this->format_horizontal_simplelist($props["depth"]), + "vert" => $this->format_vertical_simplelist($props["depth"]), default => "", }; @@ -2083,45 +2083,58 @@ private function format_inline_simplelist() { return implode(", ", $this->cchunk["simplelist"]["members"]) . '
    '; } - private function format_horizontal_simplelist($props) { - $this->cchunk["simplelist"]["members"] = array_merge($this->cchunk["simplelist"]["members"], $this->get_simplelist_members_padding()); - - $table = ""; - $trPadding = str_repeat(" ", $props["depth"] + 2); - $tdPadding = str_repeat(" ", $props["depth"] + 3); - for ($i = 0; $i < count($this->cchunk["simplelist"]["members"]); $i++) { - $trOpen = ($i % $this->cchunk["simplelist"]["columns"] === 0) ? ($trPadding . "\n") : ""; - $trClose = ($i % $this->cchunk["simplelist"]["columns"] === $this->cchunk["simplelist"]["columns"] - 1) ? ($trPadding . "\n") : ""; - - $table .= $trOpen . $tdPadding . "" . $this->cchunk["simplelist"]["members"][$i] . "\n" . $trClose; - } - - return $table . str_repeat(" ", $props["depth"] + 1) . "\n" . str_repeat(" ", $props["depth"]) . ""; + private function format_horizontal_simplelist($depth) { + return $this->chunkReduceTable( + $this->processTabular( + $this->cchunk["simplelist"]["members"], + $this->cchunk["simplelist"]["columns"], + $depth), + $this->cchunk["simplelist"]["columns"], + $depth + ); } - private function get_simplelist_members_padding() { - $numOfRows = ceil(count($this->cchunk["simplelist"]["members"]) / $this->cchunk["simplelist"]["columns"]); - - return array_fill(0, ($this->cchunk["simplelist"]["columns"] * $numOfRows) - count($this->cchunk["simplelist"]["members"]), ""); + /** Return formatted table */ + private function chunkReduceTable(array $members, int $cols, int $depth): string + { + $trPadding = str_repeat(' ', $depth + 2); + return array_reduce( + array_chunk( + $members, + $cols, + ), + fn (string $carry, array $entry) => $carry . $trPadding . "\n" . implode('', $entry) . $trPadding . "\n", + '' + ) + . str_repeat(' ', $depth + 1) . "\n" . str_repeat(' ', $depth) . ""; + } + + /** Pads $members so that number of members = columns x rows */ + private function processTabular(array $members, int $cols, int $depth): array + { + $tdPadding = str_repeat(' ', $depth + 3); + return array_map( + fn (string $member) => $tdPadding . "$member\n", + /** The padding is done by getting the additive modular inverse which is + * ``-nb_arr mod columns`` but because PHP gives us the mod in negative we need to + * add $cols back to get the positive + */ + [...$members, ...array_fill(0, (-\count($members) % $cols) + $cols, '')] + ); } - private function format_vertical_simplelist($props) { - $numOfRows = ceil(count($this->cchunk["simplelist"]["members"]) / $this->cchunk["simplelist"]["columns"]); - - $this->cchunk["simplelist"]["members"] = array_merge($this->cchunk["simplelist"]["members"], $this->get_simplelist_members_padding()); - - $table = ""; - $trPadding = str_repeat(" ", $props["depth"] + 2); - $tdPadding = str_repeat(" ", $props["depth"] + 3); - for ($row = 0; $row < $numOfRows; $row++) { - $table .= $trPadding . "\n"; - for ($col = 0; $col < $this->cchunk["simplelist"]["columns"]; $col++) { - $table .= $tdPadding . "" . $this->cchunk["simplelist"]["members"][($numOfRows * $col) + $row] . "\n"; - } - $table .= $trPadding . "\n"; - } - - return $table . str_repeat(" ", $props["depth"] + 1) . "\n" . str_repeat(" ", $props["depth"]) . ""; + private function format_vertical_simplelist($depth) { + $members = $this->processTabular( + $this->cchunk["simplelist"]["members"], + $this->cchunk["simplelist"]["columns"], + $depth + ); + // Sort elements so that we get each correct element for the rows to display vertically + uksort( + $members, + fn (int $l, int $r) => $l % $this->cchunk["simplelist"]["columns"] <=> $r % $this->cchunk["simplelist"]["columns"] + ); + return $this->chunkReduceTable($members, $this->cchunk["simplelist"]["columns"], $depth); } public function format_member($open, $name, $attrs, $props) { From 88da6ba1abfef5d1e94a195135a414f889319417 Mon Sep 17 00:00:00 2001 From: haszi Date: Sun, 18 Feb 2024 21:23:39 +0100 Subject: [PATCH 4/5] Fix tests after rebase --- tests/php/type_rendering_001.phpt | 3 +-- tests/php/type_rendering_002.phpt | 3 +-- tests/php/type_rendering_003.phpt | 3 +-- tests/xhtml/whitespace_formatting_001.phpt | 4 ++-- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/tests/php/type_rendering_001.phpt b/tests/php/type_rendering_001.phpt index 3e0d2545..e9edc78d 100644 --- a/tests/php/type_rendering_001.phpt +++ b/tests/php/type_rendering_001.phpt @@ -38,8 +38,7 @@ Content:

    1. Function/method with no return type

    -
    - function_name()
    +
    function_name()
    diff --git a/tests/php/type_rendering_002.phpt b/tests/php/type_rendering_002.phpt index e44a1c80..6309b11f 100644 --- a/tests/php/type_rendering_002.phpt +++ b/tests/php/type_rendering_002.phpt @@ -38,8 +38,7 @@ Content:

    1. Function/method with no parameters

    -
    - function_name()
    +
    function_name()
    diff --git a/tests/php/type_rendering_003.phpt b/tests/php/type_rendering_003.phpt index 03a30c7d..f37b33ed 100644 --- a/tests/php/type_rendering_003.phpt +++ b/tests/php/type_rendering_003.phpt @@ -38,8 +38,7 @@ Content:

    1. Constructor with no parameters, no return type

    -
    - final public ClassName::__construct()
    +
    final public ClassName::__construct()
    diff --git a/tests/xhtml/whitespace_formatting_001.phpt b/tests/xhtml/whitespace_formatting_001.phpt index 81339724..5c1aa1d6 100644 --- a/tests/xhtml/whitespace_formatting_001.phpt +++ b/tests/xhtml/whitespace_formatting_001.phpt @@ -44,13 +44,13 @@ Content:

    2. Constructor with whitespace between name, parameters and return types

    -
    final public ClassName::__construct(iterableresourcecallablenull $optionvoid)
    +
    final public ClassName::__construct(iterableresourcecallablenull $optionvoid)

    3. Destructor with whitespace between name, parameters and return types

    -
    final public ClassName::__construct(iterableresourcecallablenull $optionvoid)
    +
    final public ClassName::__construct(iterableresourcecallablenull $optionvoid)
    From 886147071da0783b59fe9e2a8209d0319d920bfb Mon Sep 17 00:00:00 2001 From: haszi Date: Mon, 19 Feb 2024 11:36:17 +0100 Subject: [PATCH 5/5] Address review comments Remove FIXME comment from simpelist entry of element map. Add dedicated method for indentation. Rename simplelist type formatting methods. Move tbody and table tags from tabular formatting method to the main simplelist formatting method. --- phpdotnet/phd/Package/Generic/XHTML.php | 35 +++++++++++++++---------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/phpdotnet/phd/Package/Generic/XHTML.php b/phpdotnet/phd/Package/Generic/XHTML.php index 642495f1..20581b43 100644 --- a/phpdotnet/phd/Package/Generic/XHTML.php +++ b/phpdotnet/phd/Package/Generic/XHTML.php @@ -269,7 +269,7 @@ abstract class Package_Generic_XHTML extends Format_Abstract_XHTML { 'setindex' => 'format_chunk', 'shortaffil' => 'format_suppressed_tags', 'sidebar' => 'format_note', - 'simplelist' => 'format_simplelist', /* FIXME: simplelists has few attributes that need to be implemented */ + 'simplelist' => 'format_simplelist', 'simplesect' => 'div', 'simpara' => array( /* DEFAULT */ 'p', @@ -2061,16 +2061,20 @@ public function format_simplelist($open, $name, $attrs, $props) { if ($this->cchunk["simplelist"]["type"] === "vert" || $this->cchunk["simplelist"]["type"] === "horiz") { - return '' . "\n" . str_repeat(" ", $props["depth"] + 1) . "\n"; + return '
    ' . "\n" . $this->indent($props["depth"] + 1) . "\n"; } return '
      '; } $list = match ($this->cchunk["simplelist"]["type"]) { - "inline" => $this->format_inline_simplelist(), - "horiz" => $this->format_horizontal_simplelist($props["depth"]), - "vert" => $this->format_vertical_simplelist($props["depth"]), + "inline" => $this->simplelist_format_inline(), + "horiz" => $this->simplelist_format_horizontal($props["depth"]) + . $this->indent($props["depth"] + 1) . "
    \n" + . $this->indent($props["depth"]) . "
    ", + "vert" => $this->simplelist_format_vertical($props["depth"]) + . $this->indent($props["depth"] + 1) . "\n" + . $this->indent($props["depth"]) . "", default => "", }; @@ -2079,11 +2083,15 @@ public function format_simplelist($open, $name, $attrs, $props) { return $list; } - private function format_inline_simplelist() { + private function indent($depth): string { + return $depth > 0 ? str_repeat(' ', $depth) : ''; + } + + private function simplelist_format_inline() { return implode(", ", $this->cchunk["simplelist"]["members"]) . '
    '; } - private function format_horizontal_simplelist($depth) { + private function simplelist_format_horizontal($depth) { return $this->chunkReduceTable( $this->processTabular( $this->cchunk["simplelist"]["members"], @@ -2094,10 +2102,10 @@ private function format_horizontal_simplelist($depth) { ); } - /** Return formatted table */ + /** Return formatted rows */ private function chunkReduceTable(array $members, int $cols, int $depth): string { - $trPadding = str_repeat(' ', $depth + 2); + $trPadding = $this->indent($depth + 2); return array_reduce( array_chunk( $members, @@ -2105,25 +2113,24 @@ private function chunkReduceTable(array $members, int $cols, int $depth): string ), fn (string $carry, array $entry) => $carry . $trPadding . "\n" . implode('', $entry) . $trPadding . "\n", '' - ) - . str_repeat(' ', $depth + 1) . "\n" . str_repeat(' ', $depth) . ""; + ); } /** Pads $members so that number of members = columns x rows */ private function processTabular(array $members, int $cols, int $depth): array { - $tdPadding = str_repeat(' ', $depth + 3); + $tdPadding = $this->indent($depth + 3); return array_map( fn (string $member) => $tdPadding . "$member\n", /** The padding is done by getting the additive modular inverse which is - * ``-nb_arr mod columns`` but because PHP gives us the mod in negative we need to + * ``-\count($members) % $cols`` but because PHP gives us the mod in negative we need to * add $cols back to get the positive */ [...$members, ...array_fill(0, (-\count($members) % $cols) + $cols, '')] ); } - private function format_vertical_simplelist($depth) { + private function simplelist_format_vertical($depth) { $members = $this->processTabular( $this->cchunk["simplelist"]["members"], $this->cchunk["simplelist"]["columns"],