-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php namespace Todaymade\Daux\Format\Confluence; | ||
|
||
class DetailsToExpand | ||
{ | ||
public function convert(string $content): string | ||
{ | ||
$dom = new \DOMDocument(); | ||
libxml_use_internal_errors(true); | ||
$dom->loadHTML($content, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); | ||
libxml_clear_errors(); | ||
|
||
$detailElements = $dom->getElementsByTagName('details'); | ||
|
||
$count = $detailElements->length; | ||
for ($i = $count - 1; $i >= 0; --$i) { | ||
$this->convertOne($detailElements->item($i)); | ||
} | ||
|
||
return $dom->saveHTML(); | ||
} | ||
|
||
protected function findSummary(\DOMElement $element): ?\DOMElement | ||
{ | ||
if ($element->childElementCount == 0) { | ||
return null; | ||
} | ||
|
||
foreach ($element->childNodes as $child) { | ||
if ($child->nodeName == 'summary') { | ||
return $child; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected function convertOne(\DOMElement $element) | ||
{ | ||
/* | ||
<ac:structured-macro ac:name="expand"> | ||
<ac:parameter ac:name="">This is my message</ac:parameter> | ||
<ac:rich-text-body> | ||
<p>This text is <em>hidden</em> until you expand it.</p> | ||
</ac:rich-text-body> | ||
</ac:structured-macro> | ||
*/ | ||
|
||
$summary = $this->findSummary($element); | ||
if (!$summary) { | ||
// If we can't find a title we don't try to convert to expandable | ||
return; | ||
} | ||
|
||
// Create new title node | ||
$title = $element->ownerDocument->createElement('ac:parameter'); | ||
$title->setAttribute('ac:name', ''); | ||
$titleChildNodes = $summary->childNodes; | ||
while ($titleChildNodes->length > 0) { | ||
$title->appendChild($titleChildNodes->item(0)); | ||
} | ||
$element->removeChild($summary); | ||
|
||
// Create body node | ||
$body = $element->ownerDocument->createElement('ac:rich-text-body'); | ||
$childNodes = $element->childNodes; | ||
while ($childNodes->length > 0) { | ||
$body->appendChild($childNodes->item(0)); | ||
} | ||
|
||
// Assemble the macro | ||
$macro = $element->ownerDocument->createElement('ac:structured-macro'); | ||
$macro->setAttribute('ac:name', 'expand'); | ||
$macro->appendChild($title); | ||
$macro->appendChild($body); | ||
|
||
$element->parentNode->replaceChild($macro, $element); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php namespace Todaymade\Daux\Format\Confluence; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class DetailsToExpandTest extends TestCase | ||
{ | ||
public function provideExpandData() | ||
{ | ||
return [ | ||
[ | ||
// Convert simple case | ||
<<<'EOD' | ||
<details> | ||
<summary>Title !</summary> | ||
<ul> | ||
<li>Item 1</li> | ||
<li>Item 2</li> | ||
</ul> | ||
</details> | ||
EOD, | ||
<<<'EOD' | ||
<ac:structured-macro ac:name="expand"><ac:parameter ac:name="">Title !</ac:parameter><ac:rich-text-body> | ||
<ul> | ||
<li>Item 1</li> | ||
<li>Item 2</li> | ||
</ul> | ||
</ac:rich-text-body></ac:structured-macro> | ||
EOD | ||
], | ||
[ | ||
// Don't convert without title | ||
<<<'EOD' | ||
<details> | ||
<ul> | ||
<li>Item 1</li> | ||
<li>Item 2</li> | ||
</ul> | ||
</details> | ||
EOD, | ||
<<<'EOD' | ||
<details> | ||
<ul> | ||
<li>Item 1</li> | ||
<li>Item 2</li> | ||
</ul> | ||
</details> | ||
EOD | ||
], | ||
[ | ||
// Convert nested | ||
<<<'EOD' | ||
<details> | ||
<summary>Title !</summary> | ||
<ul> | ||
<li>Item 1</li> | ||
<li>Item 2</li> | ||
</ul> | ||
<details> | ||
<summary>Inner title</summary> | ||
<p>Some Text</p> | ||
</details> | ||
</details> | ||
EOD, | ||
<<<'EOD' | ||
<ac:structured-macro ac:name="expand"><ac:parameter ac:name="">Title !</ac:parameter><ac:rich-text-body> | ||
<ul> | ||
<li>Item 1</li> | ||
<li>Item 2</li> | ||
</ul> | ||
<ac:structured-macro ac:name="expand"><ac:parameter ac:name="">Inner title</ac:parameter><ac:rich-text-body> | ||
<p>Some Text</p> | ||
</ac:rich-text-body></ac:structured-macro> | ||
</ac:rich-text-body></ac:structured-macro> | ||
EOD | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideExpandData | ||
* | ||
* @param mixed $input | ||
* @param mixed $expected | ||
*/ | ||
public function testDetailsToExpand($input, $expected) | ||
{ | ||
$expander = new DetailsToExpand(); | ||
|
||
$this->assertEquals($expected, preg_replace("/\n\n/", "\n", trim($expander->convert($input)))); | ||
} | ||
} |