Skip to content

Commit

Permalink
Add macro align (#89)
Browse files Browse the repository at this point in the history
* Add macro align

ERM27235

* cc

* cc
  • Loading branch information
DvogelHallowelt authored Nov 28, 2023
1 parent e9f1369 commit 550dfcb
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Converter/ConfluenceConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use HalloWelt\MigrateConfluence\Converter\Processor\Emoticon;
use HalloWelt\MigrateConfluence\Converter\Processor\ExpandMacro;
use HalloWelt\MigrateConfluence\Converter\Processor\Image;
use HalloWelt\MigrateConfluence\Converter\Processor\MacroAlign;
use HalloWelt\MigrateConfluence\Converter\Processor\PageLink;
use HalloWelt\MigrateConfluence\Converter\Processor\PreserveCode;
use HalloWelt\MigrateConfluence\Converter\Processor\StructuredMacroChildren;
Expand Down Expand Up @@ -242,7 +243,8 @@ private function runProcessors( $dom ) {
$currentPageTitle, $this->nsFileRepoCompat
),
new StructuredMacroContenByLabel( $this->currentPageTitle ),
new ExpandMacro()
new ExpandMacro(),
new MacroAlign()
];

/** @var IProcessor $processor */
Expand Down Expand Up @@ -353,7 +355,8 @@ private function processMacro( $sender, $match, $dom, $xpath ) {
'children',
'drawio',
'contentbylabel',
'expand'
'expand',
'align'
]
) ) {
return;
Expand Down
97 changes: 97 additions & 0 deletions src/Converter/Processor/MacroAlign.php
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 );
}
}
}
}
}
34 changes: 34 additions & 0 deletions tests/phpunit/Converter/Processor/MacroAlignTest.php
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 );
}
}
6 changes: 6 additions & 0 deletions tests/phpunit/data/macroalign-input.xml
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>
6 changes: 6 additions & 0 deletions tests/phpunit/data/macroalign-output.xml
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="{&quot;align&quot;:&quot;center&quot;}" style="text-align: center;">lorem ipsum dolor</div>
</p>
<p>sit amet</p>
</xml>

0 comments on commit 550dfcb

Please sign in to comment.