Skip to content

Commit

Permalink
#349 Add support for confluence
Browse files Browse the repository at this point in the history
  • Loading branch information
onigoetz committed Dec 14, 2022
1 parent 46f4ed0 commit 1e349c6
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
5 changes: 5 additions & 0 deletions libs/Format/Confluence/ContentPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ function ($src, array $attributes, Entry $file) {
}
);

if (str_contains($content, '<details')) {
$detailsToExpand = new DetailsToExpand();
$content = $detailsToExpand->convert($content);
}

$intro = '';
if ($this->config->getConfluenceConfiguration()->hasHeader()) {
$intro = '<ac:structured-macro ac:name="info"><ac:rich-text-body>' . $this->config->getConfluenceConfiguration()->getHeader() . '</ac:rich-text-body></ac:structured-macro>';
Expand Down
78 changes: 78 additions & 0 deletions libs/Format/Confluence/DetailsToExpand.php
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);
}
}
102 changes: 102 additions & 0 deletions tests/Format/Confluence/DetailsToExpandTest.php
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))));
}
}

0 comments on commit 1e349c6

Please sign in to comment.