-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
148 additions
and
2 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,97 @@ | ||
<?php | ||
|
||
namespace HalloWelt\MigrateConfluence\Converter\Processor; | ||
|
||
use DOMDocument; | ||
use DOMNode; | ||
use HalloWelt\MigrateConfluence\Converter\IProcessor; | ||
|
||
class MacroAlign implements IProcessor { | ||
|
||
/** | ||
* | ||
* @return string | ||
*/ | ||
protected function getMacroName(): string { | ||
return 'align'; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function process( DOMDocument $dom ): void { | ||
$macrosTags = $dom->getElementsByTagName( 'macro' ); | ||
|
||
$macros = []; | ||
foreach ( $macrosTags as $macrosTag ) { | ||
$macros[] = $macrosTag; | ||
} | ||
|
||
$macroName = $this->getMacroName(); | ||
foreach ( $macros as $macro ) { | ||
if ( $macro->getAttribute( 'ac:name' ) === $macroName ) { | ||
$this->doProcessMacro( $macro ); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param DOMNode $node | ||
* @return void | ||
*/ | ||
protected function doProcessMacro( $node ): void { | ||
$macroName = $node->getAttribute( 'ac:name' ); | ||
|
||
$macroReplacement = $node->ownerDocument->createElement( 'div' ); | ||
|
||
$macroReplacement->setAttribute( 'class', "ac-macro-$macroName" ); | ||
|
||
$macroParams = $this->getMacroParams( $node, $macroReplacement ); | ||
if ( !empty( $macroParams ) ) { | ||
$macroReplacement->setAttribute( 'data-params', json_encode( $macroParams ) ); | ||
} | ||
|
||
if ( isset( $macroParams['align'] ) ) { | ||
$style = 'text-align: ' . $macroParams['align'] . ';'; | ||
$macroReplacement->setAttribute( 'style', $style ); | ||
} | ||
|
||
$this->macroBody( $node, $macroReplacement ); | ||
$node->parentNode->replaceChild( $macroReplacement, $node ); | ||
} | ||
|
||
/** | ||
* | ||
* @param DOMNode $macro | ||
* @param DOMElement $macroReplacement | ||
* @return array | ||
*/ | ||
private function getMacroParams( $macro, $macroReplacement ): array { | ||
$params = []; | ||
foreach ( $macro->childNodes as $childNode ) { | ||
if ( $childNode->nodeName === 'ac:parameter' ) { | ||
$paramName = $childNode->getAttribute( 'ac:name' ); | ||
$params[$paramName] = $childNode->nodeValue; | ||
} | ||
} | ||
|
||
return $params; | ||
} | ||
|
||
/** | ||
* | ||
* @param DOMNode $macro | ||
* @param DOMElement $macroReplacement | ||
* @return void | ||
*/ | ||
private function macroBody( $macro, $macroReplacement ): void { | ||
foreach ( $macro->childNodes as $childNode ) { | ||
if ( $childNode->nodeName === 'ac:rich-text-body' ) { | ||
foreach ( $childNode->childNodes as $node ) { | ||
$newNode = $node->cloneNode( true ); | ||
$macroReplacement->appendChild( $newNode ); | ||
} | ||
} | ||
} | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace HalloWelt\MigrateConfluence\Tests\Converter\Processor; | ||
|
||
use DOMDocument; | ||
use HalloWelt\MigrateConfluence\Converter\Processor\MacroAlign; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class MacroAlignTest extends TestCase { | ||
|
||
/** | ||
* @covers HalloWelt\MigrateConfluence\Converter\Processor\MacroAlignTest::preprocess | ||
* @return void | ||
*/ | ||
public function testPreprocess() { | ||
$dir = dirname( dirname( __DIR__ ) ) . '/data'; | ||
$input = file_get_contents( "$dir/macroalign-input.xml" ); | ||
|
||
$dom = new DOMDocument(); | ||
$dom->loadXML( $input ); | ||
|
||
$processor = new MacroAlign(); | ||
$processor->process( $dom ); | ||
|
||
$actualOutput = $dom->saveXML( $dom->documentElement ); | ||
|
||
$input = file_get_contents( "$dir/macroalign-output.xml" ); | ||
$expectedDom = new DOMDocument(); | ||
$expectedDom->loadXML( $input ); | ||
$expectedOutput = $expectedDom->saveXML( $expectedDom->documentElement ); | ||
|
||
$this->assertEquals( $expectedOutput, $actualOutput ); | ||
} | ||
} |
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,6 @@ | ||
<xml xmlns:ac="sample_namespace" xmlns:ri="sample_second_namespace"> | ||
<p> | ||
<ac:macro ac:name="align"><ac:parameter ac:name="align">center</ac:parameter><ac:rich-text-body>lorem ipsum dolor</ac:rich-text-body></ac:macro> | ||
</p> | ||
<p>sit amet</p> | ||
</xml> |
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,6 @@ | ||
<xml xmlns:ac="sample_namespace" xmlns:ri="sample_second_namespace"> | ||
<p> | ||
<div class="ac-macro-align" data-params="{"align":"center"}" style="text-align: center;">lorem ipsum dolor</div> | ||
</p> | ||
<p>sit amet</p> | ||
</xml> |