Skip to content

Commit

Permalink
Fix doc-en GH-3428
Browse files Browse the repository at this point in the history
Replace static example element counter with class properties in Index and generic XHTML.
Add tests.
Refactor TestRender so that it can render multiple formats.
  • Loading branch information
haszi committed Jun 5, 2024
1 parent e18fd3e commit 7ffbc90
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 7 deletions.
7 changes: 3 additions & 4 deletions phpdotnet/phd/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class Index extends Format
private $commit = array();
private $POST_REPLACEMENT_INDEXES = array();
private $POST_REPLACEMENT_VALUES = array();
private int $exampleCounter = 0;

public function __construct(IndexRepository $indexRepository) {
$this->indexRepository = $indexRepository;
Expand Down Expand Up @@ -377,16 +378,14 @@ public function format_short_desc($open, $name, $attrs, $props) {
}

public function format_example($open, $name, $attrs, $props) {
static $n = 0;

if ($open) {
++$n;
++$this->exampleCounter;

if(isset($attrs[Reader::XMLNS_XML]["id"])) {
$id = $attrs[Reader::XMLNS_XML]["id"];
}
else {
$id = "example-" . $n;
$id = "example-" . $this->exampleCounter;
}

$this->storeInfo($name, $id, $this->currentchunk, false);
Expand Down
7 changes: 4 additions & 3 deletions phpdotnet/phd/Package/Generic/XHTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ abstract class Package_Generic_XHTML extends Format_Abstract_XHTML {
protected $isSectionChunk = array();
protected $params = array();

protected int $exampleCounter = 0;

public function __construct() {
parent::__construct();
$this->registerPIHandlers($this->pihandlers);
Expand Down Expand Up @@ -1711,13 +1713,12 @@ public function format_note_title($open, $name, $attrs)
return '</strong><br />';
}
public function format_example($open, $name, $attrs, $props) {
static $n = 0;
if ($open) {
++$n;
++$this->exampleCounter;
if (isset($props["id"])) {
return '<div class="' . $name . '" id="' . $props["id"] . '">';
}
return '<div class="' . $name . '" id="' . $this->getGeneratedExampleId($n-1) . '">';
return '<div class="' . $name . '" id="' . $this->getGeneratedExampleId($this->exampleCounter - 1) . '">';
}
return '</div>';
}
Expand Down
3 changes: 3 additions & 0 deletions phpdotnet/phd/TestRender.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public function run() {

if ($this->format !== null) {
$this->attach($this->format);
}

if (count($this) > 0) {
$this->reader->open($this->config->xml_file());
$this->execute($this->reader);
}
Expand Down
159 changes: 159 additions & 0 deletions tests/bug_doc-en_GH-3428.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
--TEST--
Example numbering 001 - indexing and rendering examples with and without an xml:id
--FILE--
<?php
namespace phpdotnet\phd;

require_once __DIR__ . "/setup.php";

$xml_file = __DIR__ . "/data/example_numbering_001.xml";

Config::init([
"force_index" => true,
"xml_file" => $xml_file,
]);

$indexRepository = new IndexRepository(new \SQLite3(":memory:"));
$indexRepository->init();
Config::set_indexcache($indexRepository);

$index = new TestIndex($indexRepository);

$render = new TestRender(new Reader, new Config, null, $index);

$render->run();

$indexes = array_keys($index->getNfo());

echo "Indexes stored:\n";

var_dump($indexes);

$render = new TestRender(new Reader, new Config);

$format1 = new TestGenericChunkedXHTML;
$format2 = new TestGenericChunkedXHTML;
$format3 = new TestGenericChunkedXHTML;

$render->attach($format1);
$render->attach($format2);
$render->attach($format3);

$render->run();

?>
--EXPECT--
Indexes stored:
array(6) {
[0]=>
string(17) "example-numbering"
[1]=>
string(9) "example-1"
[2]=>
string(9) "example-2"
[3]=>
string(16) "third-example-id"
[4]=>
string(9) "example-4"
[5]=>
string(9) "example-5"
}
Filename: example-numbering.html
Content:
<div id="example-numbering" class="chapter">
<div class="section">
<div class="example" id="example-1">
<p><strong>Example #1 - 1. example without an xml:id</strong></p>
</div>
</div>

<div class="section">
<div class="example" id="example-2">
<p><strong>Example #2 - 2. example without an xml:id</strong></p>
</div>
</div>

<div class="section">
<div class="example" id="third-example-id">
<p><strong>Example #3 - 3. example with an xml:id</strong></p>
</div>
</div>

<div class="section">
<div class="example" id="example-4">
<p><strong>Example #4 - 4. example without an xml:id</strong></p>
</div>
</div>

<div class="section">
<div class="example" id="example-5">
<p><strong>Example #5 - 5. example without an xml:id</strong></p>
</div>
</div>
</div>
Filename: example-numbering.html
Content:
<div id="example-numbering" class="chapter">
<div class="section">
<div class="example" id="example-1">
<p><strong>Example #1 - 1. example without an xml:id</strong></p>
</div>
</div>

<div class="section">
<div class="example" id="example-2">
<p><strong>Example #2 - 2. example without an xml:id</strong></p>
</div>
</div>

<div class="section">
<div class="example" id="third-example-id">
<p><strong>Example #3 - 3. example with an xml:id</strong></p>
</div>
</div>

<div class="section">
<div class="example" id="example-4">
<p><strong>Example #4 - 4. example without an xml:id</strong></p>
</div>
</div>

<div class="section">
<div class="example" id="example-5">
<p><strong>Example #5 - 5. example without an xml:id</strong></p>
</div>
</div>
</div>
Filename: example-numbering.html
Content:
<div id="example-numbering" class="chapter">
<div class="section">
<div class="example" id="example-1">
<p><strong>Example #1 - 1. example without an xml:id</strong></p>
</div>
</div>

<div class="section">
<div class="example" id="example-2">
<p><strong>Example #2 - 2. example without an xml:id</strong></p>
</div>
</div>

<div class="section">
<div class="example" id="third-example-id">
<p><strong>Example #3 - 3. example with an xml:id</strong></p>
</div>
</div>

<div class="section">
<div class="example" id="example-4">
<p><strong>Example #4 - 4. example without an xml:id</strong></p>
</div>
</div>

<div class="section">
<div class="example" id="example-5">
<p><strong>Example #5 - 5. example without an xml:id</strong></p>
</div>
</div>
</div>
31 changes: 31 additions & 0 deletions tests/data/bug_doc-en_GH-3428.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<chapter xml:id="example-numbering">
<section>
<example>
<title>- 1. example without an xml:id</title>
</example>
</section>

<section>
<example>
<title>- 2. example without an xml:id</title>
</example>
</section>

<section>
<example xml:id="third-example-id">
<title>- 3. example with an xml:id</title>
</example>
</section>

<section>
<example>
<title>- 4. example without an xml:id</title>
</example>
</section>

<section>
<example>
<title>- 5. example without an xml:id</title>
</example>
</section>
</chapter>

0 comments on commit 7ffbc90

Please sign in to comment.