Skip to content

Commit fdf533d

Browse files
Fix details macro and add a fix for multiline macros
1 parent 1396aba commit fdf533d

File tree

11 files changed

+103
-71
lines changed

11 files changed

+103
-71
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="macro-details" {{#if:{{{id|}}}|data-id="{{{1|}}}"}} {{#if:{{{hidden|}}}|data-id="{{{1|}}}"}}>
2+
{{{body}}}
3+
</div>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace HalloWelt\MigrateConfluence\Converter\Postprocessor;
4+
5+
use HalloWelt\MigrateConfluence\Converter\IPostprocessor;
6+
7+
class FixMultilineTemplate implements IPostprocessor {
8+
9+
/**
10+
* @inheritDoc
11+
*/
12+
public function postprocess( string $wikiText ): string {
13+
$wikiText = preg_replace_callback(
14+
'/\{\{(.*?)\}\}/s',
15+
function( $match ) {
16+
$lines = explode( "###BREAK###", $match[0] );
17+
for( $index = 0; $index < count( $lines ); $index++ ) {
18+
$line = $lines[$index];
19+
if ( strpos( $line, ' ' ) === 0 ) {
20+
$lines[$index] = substr( $line, 1 );
21+
}
22+
}
23+
return implode( "###BREAK###", $lines );
24+
},
25+
$wikiText
26+
);
27+
28+
return $wikiText;
29+
}
30+
}

src/Converter/Processor/ConvertMacroToTemplateBase.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function process( DOMDocument $dom ): void {
5656
$paramValue = $parameterEl->nodeValue;
5757
// We add a "###BREAK###", as `pandoc` will eat up regular line breaks.
5858
// They will be restored in a "Postprocessor"
59-
$praramString = " |$paramName = $paramValue";
59+
$praramString = "|$paramName = $paramValue";
6060
if ( $this->addLinebreakInsideTemplate() ) {
6161
$praramString .= "###BREAK###\n";
6262
}
@@ -73,7 +73,7 @@ public function process( DOMDocument $dom ): void {
7373
}
7474

7575
if ( !empty( $richTextBodyEls ) ) {
76-
$bodyString = " |body = ";
76+
$bodyString = "|body = ";
7777
if ( $this->addLinebreakInsideTemplate() ) {
7878
$bodyString .= "###BREAK###\n";
7979
}
@@ -85,6 +85,7 @@ public function process( DOMDocument $dom ): void {
8585
// will give children of `$dom->firstChild`.
8686
// Using `iterator_to_array` as an workaround here.
8787
$childNodes = iterator_to_array( $richTextBodyEl->childNodes );
88+
$childNodeCount = count( $childNodes );
8889
foreach ( $childNodes as $richTextBodyChildEl ) {
8990
if ( $richTextBodyChildEl === $actualMacro ) {
9091
continue;

src/Converter/Processor/DetailsMacro.php

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace HalloWelt\MigrateConfluence\Converter\Processor;
44

55
use DOMDocument;
6+
use DOMNode;
67
use HalloWelt\MigrateConfluence\Converter\IProcessor;
78

89
/**
@@ -18,7 +19,7 @@
1819
* ...
1920
* </ac:structured-macro>
2021
*/
21-
class DetailsMacro implements IProcessor {
22+
class DetailsMacro extends ConvertMacroToTemplateBase {
2223

2324
/**
2425
* @return string
@@ -30,61 +31,7 @@ protected function getMacroName(): string {
3031
/**
3132
* @inheritDoc
3233
*/
33-
public function process( DOMDocument $dom ): void {
34-
$macros = $dom->getElementsByTagName( 'structured-macro' );
35-
$requiredMacroName = $this->getMacroName();
36-
37-
// Collect all DOMElements in a non-live list
38-
$actualMacros = [];
39-
foreach ( $macros as $macro ) {
40-
$macroName = $macro->getAttribute( 'ac:name' );
41-
if ( $macroName !== $requiredMacroName ) {
42-
continue;
43-
}
44-
$actualMacros[] = $macro;
45-
}
46-
47-
foreach ( $actualMacros as $actualMacro ) {
48-
$parentNode = $actualMacro->parentNode;
49-
50-
$detailsDiv = $dom->createElement( 'div' );
51-
$detailsDiv->setAttribute( 'class', 'details' );
52-
53-
// Extract scalar parameters, store them as data attributes of 'div'
54-
$parameterEls = $actualMacro->getElementsByTagName( 'parameter' );
55-
foreach ( $parameterEls as $parameterEl ) {
56-
$paramName = $parameterEl->getAttribute( 'ac:name' );
57-
$paramValue = $parameterEl->nodeValue;
58-
59-
$detailsDiv->setAttribute( "data-$paramName", $paramValue );
60-
}
61-
62-
// Extract rich text bodies
63-
/** @var DOMNodeList $richTextBodies */
64-
$richTextBodies = $actualMacro->getElementsByTagName( 'rich-text-body' );
65-
$richTextBodyEls = [];
66-
foreach ( $richTextBodies as $richTextBody ) {
67-
$richTextBodyEls[] = $richTextBody;
68-
}
69-
70-
if ( !empty( $richTextBodyEls ) ) {
71-
foreach ( $richTextBodyEls as $richTextBodyEl ) {
72-
// For some odd reason, iterating `$richTextBodyEl->childNodes` directly
73-
// will give children of `$dom->firstChild`.
74-
// Using `iterator_to_array` as an workaround here.
75-
$childNodes = iterator_to_array( $richTextBodyEl->childNodes );
76-
foreach ( $childNodes as $richTextBodyChildEl ) {
77-
if ( $richTextBodyChildEl === $actualMacro ) {
78-
continue;
79-
}
80-
$detailsDiv->appendChild( $richTextBodyChildEl );
81-
}
82-
}
83-
}
84-
85-
$parentNode->insertBefore( $detailsDiv, $actualMacro );
86-
87-
$parentNode->removeChild( $actualMacro );
88-
}
34+
protected function getWikiTextTemplateName(): string {
35+
return 'Details';
8936
}
9037
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace HalloWelt\MigrateConfluence\Tests\Converter\Preprocessor;
4+
5+
use HalloWelt\MigrateConfluence\Converter\Postprocessor\FixMultilineTemplate;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class FixMultilineTemplateTest extends TestCase {
9+
10+
/**
11+
* @var string
12+
*/
13+
private $dir = '';
14+
15+
/**
16+
* @covers HalloWelt\MigrateConfluence\Converter\Postprocessor\FixMultilineTemplate::postprocess
17+
* @return void
18+
*/
19+
public function testPostprocess() {
20+
$this->dir = dirname( __DIR__, 2 ) . '/data';
21+
22+
$input = $this->getInput();
23+
24+
$preprocessor = new FixMultilineTemplate();
25+
$actualOutput = $preprocessor->postprocess( $input );
26+
27+
$expectedOutput = $this->getExpectedOutput();
28+
29+
$this->assertEquals( $expectedOutput, $actualOutput );
30+
}
31+
32+
protected function getInput(): string {
33+
return file_get_contents( $this->dir . '/fix-multiline-template-input.wikitext' );
34+
}
35+
36+
protected function getExpectedOutput(): string {
37+
return file_get_contents( $this->dir . '/fix-multiline-template-output.wikitext' );
38+
}
39+
}

tests/phpunit/data/convertinfomacrotest-output.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<xml xmlns:ac="sample_namespace" xmlns:ri="sample_second_namespace">
33
{{Info###BREAK###
4-
|title = Some nice title###BREAK###
5-
|body = ###BREAK###
4+
|title = Some nice title###BREAK###
5+
|body = ###BREAK###
66

77
<h3>Some heading:</h3>
88
<ol>
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<xml xmlns:ac="sample_namespace">
2-
<div class="details" data-id="Lorem" data-something="we have never seen before">
3-
<h3>Ipsum dolor</h3>
4-
<h3>There may be multiple rich texts</h3>
5-
</div>
2+
{{Details###BREAK###
3+
|id = Lorem###BREAK###
4+
|something = we have never seen before###BREAK###
5+
|body = ###BREAK###
6+
7+
<h3>Ipsum dolor</h3>
8+
9+
<h3>There may be multiple rich texts</h3>
10+
}}###BREAK###
11+
612
</xml>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<xml xmlns:ac="sample_namespace">
22
{{DetailsSummary###BREAK###
3-
|firstcolumn = Col 1###BREAK###
4-
|headings = Heading 1, Heading 2, Heading 3, Heading 4###BREAK###
5-
|sortBy = Title###BREAK###
6-
|cql = label = "Label 1" and parent = currentContent ( )###BREAK###
3+
|firstcolumn = Col 1###BREAK###
4+
|headings = Heading 1, Heading 2, Heading 3, Heading 4###BREAK###
5+
|sortBy = Title###BREAK###
6+
|cql = label = "Label 1" and parent = currentContent ( )###BREAK###
77
}}###BREAK###
88

99
</xml>

tests/phpunit/data/expand-macro-output.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<xml xmlns:ac="sample_namespace">
22
{{Expand###BREAK###
3-
|title = click here to expand###BREAK###
4-
|body = ###BREAK###
3+
|title = click here to expand###BREAK###
4+
|body = ###BREAK###
55

66
<ul>
77
<li>something
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{Details|id=test-id|hidden=false|body=Lorem ipsum}}
2+
3+
{{Details###BREAK### |id=test-id###BREAK### |hidden=false###BREAK### |body=###BREAK### Lorem ipsum sit amet}}###BREAK###

0 commit comments

Comments
 (0)