[prev in list] [next in list] [prev in thread] [next in thread]
List: php-doc-cvs
Subject: [DOC-CVS] [phd] master: Move whitespace formatting to generic XHTML format (#95)
From: haszi via GitHub <noreply () php ! net>
Date: 2024-02-13 22:18:27
Message-ID: DpsmXrzNpvCh9bdoEJjPLCfH2kSukMvlwnjLdG8qO68 () main ! php ! net
[Download RAW message or body]
Author: haszi (haszi)
Committer: GitHub (web-flow)
Pusher: Girgias
Date: 2024-02-13T22:04:40Z
Commit: https://github.com/php/phd/commit/27e3bc3e4a442e16404e36070f12ed33bb231e7d
Raw diff: https://github.com/php/phd/commit/27e3bc3e4a442e16404e36070f12ed33bb231e7d.diff
Move whitespace formatting to generic XHTML format (#95)
Co-authored-by: haszi <haszika80@gmail.com>
Changed paths:
A tests/xhtml/data/whitespace_formatting_001.xml
A tests/xhtml/whitespace_formatting_001.phpt
M phpdotnet/phd/Package/Generic/XHTML.php
M phpdotnet/phd/Render.php
Diff:
diff --git a/phpdotnet/phd/Package/Generic/XHTML.php \
b/phpdotnet/phd/Package/Generic/XHTML.php index 256aa71a7..372e29554 100644
--- a/phpdotnet/phd/Package/Generic/XHTML.php
+++ b/phpdotnet/phd/Package/Generic/XHTML.php
@@ -2088,6 +2088,42 @@ public function format_keycombo($open, $name, $attrs, $props) \
{ return '</kbd>';
}
+ public function format_whitespace($whitespace, $elementStack, $currentDepth) {
+ /* The following if is to skip unnecessary whitespaces in the parameter list \
*/ + if (
+ in_array($elementStack[$currentDepth - 1], ['methodsynopsis', \
'constructorsynopsis', 'destructorsynopsis'], true) + && \
(in_array($elementStack[$currentDepth] ?? "", ["methodname", "methodparam", "type", \
"void"], true) + || count($elementStack) === $currentDepth)
+ ) {
+ return false;
+ }
+
+ /* The following if is to skip whitespace before closing semicolon after \
property/class constant */ + if ($elementStack[$currentDepth - 1] === \
"fieldsynopsis" && (in_array($elementStack[$currentDepth], ["varname", \
"initializer"], true))) { + return false;
+ }
+
+ /*
+ TODO: add trim() in type_text handling method and remove the below
+ as it doesn't work due to XMLReader including all whitespace
+ inside the tag in the text
+ hence no separate significant whitespace here
+ */
+ /* The following if is to skip whitespace inside type elements */
+ if ($elementStack[$currentDepth - 1] === "type") {
+ return false;
+ }
+
+ /* The following if is to skip unnecessary whitespaces in the implements \
list */ + if (
+ ($elementStack[$currentDepth - 1] === 'classsynopsisinfo' && \
$elementStack[$currentDepth] === 'oointerface') || + \
($elementStack[$currentDepth - 1] === 'oointerface' && $elementStack[$currentDepth] \
=== 'interfacename') + ) {
+ return false;
+ }
+
+ return $whitespace;
+ }
}
diff --git a/phpdotnet/phd/Render.php b/phpdotnet/phd/Render.php
index 337009d85..144c56fdc 100644
--- a/phpdotnet/phd/Render.php
+++ b/phpdotnet/phd/Render.php
@@ -176,33 +176,17 @@ public function execute(Reader $r) { /* {{{ */
case \XMLReader::WHITESPACE: /* {{{ */
case \XMLReader::SIGNIFICANT_WHITESPACE:
- /* The following if is to skip unnecessary whitespaces in the \
parameter list */
- if (in_array($this->STACK[$r->depth - 1], ['methodsynopsis', \
'constructorsynopsis', 'destructorsynopsis'], true) &&
- in_array($this->STACK[$r->depth] ?? "", ["methodname", \
"methodparam", "type", "void"], true)
- ) {
- break;
- }
-
- /* The following if is to skip whitespace before closing semicolon \
after property/class constant */
- if ($this->STACK[$r->depth - 1] === "fieldsynopsis" && \
(in_array($this->STACK[$r->depth], ["varname", "initializer"], \
true))) {
- break;
- }
+ $retval = $r->value;
+ foreach($this as $format) {
- /* The following if is to skip whitespace inside type elements */
- if ($this->STACK[$r->depth - 1] === "type") {
- break;
- }
+ if (method_exists($format, 'format_whitespace')) {
+ $retval = $format->format_whitespace($retval, $this->STACK, \
$r->depth);
- /* The following if is to skip unnecessary whitespaces in the \
implements list */
- if (
- ($this->STACK[$r->depth - 1] === 'classsynopsisinfo' && \
$this->STACK[$r->depth] === 'oointerface') ||
- ($this->STACK[$r->depth - 1] === 'oointerface' && \
$this->STACK[$r->depth] === 'interfacename')
- ) {
- break;
- }
+ if ($retval === false) {
+ continue;
+ }
+ }
- $retval = $r->value;
- foreach($this as $format) {
$format->appendData($retval);
}
break;
diff --git a/tests/xhtml/data/whitespace_formatting_001.xml \
b/tests/xhtml/data/whitespace_formatting_001.xml new file mode 100644
index 000000000..41b623b8a
--- /dev/null
+++ b/tests/xhtml/data/whitespace_formatting_001.xml
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="utf-8"?>
+<chapter xml:id="whitespace-formatting-001">
+
+ <section>
+ <para>1. Function/method with whitespace between name, parameters and return \
types</para> + <methodsynopsis>
+ <type class="union"><type>int</type><type>float</type><type>false</type></type>
+ <methodname>function_name</methodname>
+ <methodparam><type \
class="union"><type>iterable</type><type>resource</type><type>callable</type><type>null</type></type><parameter>option</parameter></methodparam>
+ </methodsynopsis>
+ </section>
+
+ <section>
+ <para>2. Constructor with whitespace between name, parameters and return \
types</para> + <constructorsynopsis>
+ <modifier>final</modifier> <modifier>public</modifier> \
<methodname>ClassName::__construct</methodname> + <methodparam><type \
class="union"><type>iterable</type><type>resource</type><type>callable</type><type>null</type></type><parameter>option</parameter></methodparam>
+ <void/>
+ </constructorsynopsis>
+ </section>
+
+ <section>
+ <para>3. Destructor with whitespace between name, parameters and return \
types</para> + <destructorsynopsis>
+ <modifier>final</modifier> <modifier>public</modifier> \
<methodname>ClassName::__construct</methodname> + <methodparam><type \
class="union"><type>iterable</type><type>resource</type><type>callable</type><type>null</type></type><parameter>option</parameter></methodparam>
+ <void/>
+ </destructorsynopsis>
+ </section>
+
+ <section>
+ <para>4. Class constant with whitespace after varname and initializer</para>
+ <fieldsynopsis>
+ <modifier>const</modifier>
+ <type>int</type>
+ <varname>CONSTANT_NAME</varname>
+ <initializer>1</initializer>
+ </fieldsynopsis>
+ </section>
+
+ <section>
+ <para>5. Implements list with whitespace</para>
+ <classsynopsisinfo>
+ <ooclass>
+ <classname>SomeClass</classname>
+ </ooclass>
+ <oointerface>
+ <interfacename>FirstInterface</interfacename>
+ </oointerface>
+ <oointerface>
+ <interfacename>SecondInterface</interfacename>
+ </oointerface>
+ <oointerface>
+ <interfacename>ThirdInterface</interfacename>
+ </oointerface>
+ </classsynopsisinfo>
+ </section>
+
+</chapter>
diff --git a/tests/xhtml/whitespace_formatting_001.phpt \
b/tests/xhtml/whitespace_formatting_001.phpt new file mode 100644
index 000000000..81339724a
--- /dev/null
+++ b/tests/xhtml/whitespace_formatting_001.phpt
@@ -0,0 +1,76 @@
+--TEST--
+Whitespace formatting 001
+--FILE--
+<?php
+namespace phpdotnet\phd;
+
+require_once __DIR__ . "/../setup.php";
+require_once __DIR__ . "/TestChunkedXHTML.php";
+
+$formatclass = "TestChunkedXHTML";
+$xml_file = __DIR__ . "/data/whitespace_formatting_001.xml";
+
+$opts = array(
+ "index" => 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();
+?>
+--EXPECT--
+Filename: whitespace-formatting-001.html
+Content:
+<div id="whitespace-formatting-001" class="chapter">
+
+ <div class="section">
+ <p class="para">1. Function/method with whitespace between name, parameters and \
return types</p> + <div class="methodsynopsis dc-description"><span \
class="type"><span class="type">int</span><span class="type">float</span><span \
class="type">false</span></span> <span class="methodname">function_name</span>(<span \
class="methodparam"><span class="type"><span class="type">iterable</span><span \
class="type">resource</span><span class="type">callable</span><span \
class="type">null</span></span> <code class="parameter">$option</code></span>)</div> \
+ + </div>
+
+ <div class="section">
+ <p class="para">2. Constructor with whitespace between name, parameters and return \
types</p> + <div class="constructorsynopsis dc-description"><span \
class="modifier">final</span> <span class="modifier">public</span> <span \
class="methodname">ClassName::__construct</span>(<span class="methodparam"><span \
class="type"><span class="type">iterable</span><span \
class="type">resource</span><span class="type">callable</span><span \
class="type">null</span></span> <code class="parameter">$option</code></span><span \
class="type"><span class="type void">void</span></span>)</div> +
+ </div>
+
+ <div class="section">
+ <p class="para">3. Destructor with whitespace between name, parameters and return \
types</p> + <div class="destructorsynopsis dc-description"><span \
class="modifier">final</span> <span class="modifier">public</span> <span \
class="methodname">ClassName::__construct</span>(<span class="methodparam"><span \
class="type"><span class="type">iterable</span><span \
class="type">resource</span><span class="type">callable</span><span \
class="type">null</span></span> <code class="parameter">$option</code></span><span \
class="type"><span class="type void">void</span></span>)</div> +
+ </div>
+
+ <div class="section">
+ <p class="para">4. Class constant with whitespace after varname and \
initializer</p> + <div class="fieldsynopsis">
+ <span class="modifier">const</span>
+ <span class="type">int</span>
+ <var class="fieldsynopsis_varname">CONSTANT_NAME</var><span class="initializer"> \
= 1</span>;</div> +
+ </div>
+
+ <div class="section">
+ <p class="para">5. Implements list with whitespace</p>
+ <div class="classsynopsisinfo">
+ <span class="ooclass">
+ <span class="modifier">class</span> SomeClass
+ </span>
+ <span class="oointerface"><span class="modifier">implements</span>
+ FirstInterface</span><span class="oointerface">, SecondInterface</span><span \
class="oointerface">, ThirdInterface</span> {</div> + </div>
+
+</div>
--
PHP Documentation Commits Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic