From 9a4b0d12d8bc6695400ce84b196b21f530fa05db Mon Sep 17 00:00:00 2001 From: Haryto <37344295+Haryto@users.noreply.github.com> Date: Thu, 31 Aug 2023 14:52:58 +0300 Subject: [PATCH] Fix tables (#84) * Disable masking of table attributes Previously table attributes like "class" were masked before processing by "pandoc" because such attributes broke whole table convertion process. Newer "pandoc" versions handle such attributes corrently, so no need to do masking anymore. * Remove redundant use statements * Remove unused unit tests * Fix readme pandoc note * Update README.md * Update README.md --------- Co-authored-by: akulbii Co-authored-by: DvogelHallowelt <32163064+DvogelHallowelt@users.noreply.github.com> --- README.md | 4 +- src/Converter/ConfluenceConverter.php | 4 - .../Postprocessor/RestoreTableAttributes.php | 85 ------------ .../Processor/PreserveTableAttributes.php | 124 ------------------ .../RestoreTableAttributesTest.php | 102 -------------- .../Processor/PreserveTableAttributesTest.php | 31 ----- .../preservetableattributestest-input.xml | 42 ------ .../preservetableattributestest-output.xml | 53 -------- 8 files changed, 2 insertions(+), 443 deletions(-) delete mode 100644 src/Converter/Postprocessor/RestoreTableAttributes.php delete mode 100644 src/Converter/Processor/PreserveTableAttributes.php delete mode 100644 tests/phpunit/Converter/Postprocessor/RestoreTableAttributesTest.php delete mode 100644 tests/phpunit/Converter/Processor/PreserveTableAttributesTest.php delete mode 100644 tests/phpunit/data/preservetableattributestest-input.xml delete mode 100644 tests/phpunit/data/preservetableattributestest-output.xml diff --git a/README.md b/README.md index 05875e4..008068d 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This is a command line tool to convert the contents of a Confluence space into a ## Prerequisites 1. PHP >= 7.4 with the `xml` extension must be installed -2. The `pandoc` tool must be installed and available in the `PATH` (https://pandoc.org/installing.html) +2. `pandoc` => 3.1.6. The `pandoc` tool must be installed and available in the `PATH` (https://pandoc.org/installing.html). ## Installation 1. Download `migrate-confluence.phar` from https://github.com/hallowelt/migrate-confluence/releases/tag/latest @@ -135,7 +135,7 @@ In the case that the tool can not migrate content or functionality it will creat ## Creating a build 1. Clone this repo 2. Run `composer update --no-dev` -3. Run `box build` to actually create the PHAR file in `dist/`. See also https://github.com/humbug/box +3. Run `box compile` to actually create the PHAR file in `dist/`. See also https://github.com/humbug/box # TODO * Reduce multiple linebreaks (`
`) to one diff --git a/src/Converter/ConfluenceConverter.php b/src/Converter/ConfluenceConverter.php index dfc1948..895266e 100644 --- a/src/Converter/ConfluenceConverter.php +++ b/src/Converter/ConfluenceConverter.php @@ -14,7 +14,6 @@ use HalloWelt\MigrateConfluence\Converter\Postprocessor\FixLineBreakInHeadings; use HalloWelt\MigrateConfluence\Converter\Postprocessor\NestedHeadings; use HalloWelt\MigrateConfluence\Converter\Postprocessor\RestoreCode; -use HalloWelt\MigrateConfluence\Converter\Postprocessor\RestoreTableAttributes; use HalloWelt\MigrateConfluence\Converter\Preprocessor\CDATAClosingFixer; use HalloWelt\MigrateConfluence\Converter\Processor\AttachmentLink; use HalloWelt\MigrateConfluence\Converter\Processor\ConvertInfoMacro; @@ -30,7 +29,6 @@ use HalloWelt\MigrateConfluence\Converter\Processor\Image; use HalloWelt\MigrateConfluence\Converter\Processor\PageLink; use HalloWelt\MigrateConfluence\Converter\Processor\PreserveCode; -use HalloWelt\MigrateConfluence\Converter\Processor\PreserveTableAttributes; use HalloWelt\MigrateConfluence\Converter\Processor\StructuredMacroChildren; use HalloWelt\MigrateConfluence\Converter\Processor\StructuredMacroColumn; use HalloWelt\MigrateConfluence\Converter\Processor\StructuredMacroContenByLabel; @@ -202,7 +200,6 @@ private function runProcessors( $dom ) { $currentPageTitle = $this->getCurrentPageTitle(); $processors = [ - new PreserveTableAttributes(), new ConvertPlaceholderMacro(), new ConvertInlineCommentMarkerMacro(), new ConvertTipMacro(), @@ -251,7 +248,6 @@ private function runProcessors( $dom ) { */ private function runPostProcessors() { $postProcessors = [ - new RestoreTableAttributes(), new FixLineBreakInHeadings(), new FixImagesWithExternalUrl(), new RestoreCode(), diff --git a/src/Converter/Postprocessor/RestoreTableAttributes.php b/src/Converter/Postprocessor/RestoreTableAttributes.php deleted file mode 100644 index 88730ea..0000000 --- a/src/Converter/Postprocessor/RestoreTableAttributes.php +++ /dev/null @@ -1,85 +0,0 @@ -isPreservedTableAttributesLine( $trimmedLine ) ) { - $tableStart = false; - $inPreservedTableAttributesTableRow = true; - $preserverdAttributes = - $this->extractPreservedTableAttributes( $trimmedLine ); - $preserverdAttributes = trim( $preserverdAttributes ); - $newWikiText[] = "{| $preserverdAttributes"; - $hasTableAttributes = true; - continue; - } - - $linesAfterTableStart[] = $line; - continue; - } - if ( !$tableStart ) { - if ( !empty( $linesAfterTableStart ) && !$hasTableAttributes ) { - $newWikiText[] = '{|'; - } - foreach ( $linesAfterTableStart as $lineAfterTableStart ) { - $newWikiText[] = $lineAfterTableStart; - } - $linesAfterTableStart = []; - } - $newWikiText[] = $line; - } - return implode( "\n", $newWikiText ); - } - - /** - * - * @param string $line - * @return bool - */ - private function isPreservedTableAttributesLine( $line ) { - return preg_match( "/\|.*?###PRESERVEDTABLEATTRIBUTES###<\/span>/", $line ) === 1; - } - - /** - * - * @param string $line - * @return string - */ - private function extractPreservedTableAttributes( $line ) { - return preg_replace( - "/\|.*?###PRESERVEDTABLEATTRIBUTES###<\/span>/", - '$1', - $line - ); - } -} diff --git a/src/Converter/Processor/PreserveTableAttributes.php b/src/Converter/Processor/PreserveTableAttributes.php deleted file mode 100644 index 865ae5e..0000000 --- a/src/Converter/Processor/PreserveTableAttributes.php +++ /dev/null @@ -1,124 +0,0 @@ -` - * - * becomes - * - * `{|` - * - * instead of - * - * `{| data-layout="full-width" class="someotherclass"` - * - * Therefore we preserve the information in the DOM and restore it in the post processing. - * @see HalloWelt\MigrateConfluence\Converter\Postprocessor\RestoreTableAttributes - */ -class PreserveTableAttributes implements IProcessor { - - /** - * @inheritDoc - */ - public function process( DOMDocument $dom ): void { - $tables = $dom->getElementsByTagName( 'table' ); - /** @var DOMElement $table */ - foreach ( $tables as $table ) { - $rowContainer = $table; - $tbody = $table->getElementsByTagName( 'tbody' )->item( 0 ); - if ( $tbody instanceof DOMElement ) { - $rowContainer = $tbody; - } - - $attributes = []; - if ( $table->hasAttributes() ) { - foreach ( $table->attributes as $attr ) { - $name = $attr->nodeName; - $value = $attr->nodeValue; - $attributes[$name] = $value; - } - } - - $attributes = $this->ensureWikiTableClass( $attributes ); - - if ( !empty( $attributes ) ) { - $newRow = $dom->createElement( 'tr' ); - $newCell = $dom->createElement( 'td' ); - $newSpan = $dom->createElement( 'span' ); - $newSpanContent = $dom->createTextNode( '###PRESERVEDTABLEATTRIBUTES###' ); - foreach ( $attributes as $attrName => $attrValue ) { - $newSpan->setAttribute( $attrName, $attrValue ); - } - $newSpan->appendChild( $newSpanContent ); - $newCell->appendChild( $newSpan ); - $newRow->appendChild( $newCell ); - - // Fill row with remaining numbers of td to avoid broken table after - // conversion with pandoc - // See https://github.com/hallowelt/migrate-confluence/issues/52 - $colgroup = $table->getElementsByTagName( 'colgroup' )->item( 0 ); - if ( $colgroup ) { - $cols = []; - $columns = $colgroup->childNodes; - foreach ( $columns as $column ) { - if ( ( $column instanceof DOMElement ) === false - || ( $column->tagName !== 'col' ) ) { - continue; - } - $cols[] = $column; - } - - for ( $index = 1; $index < count( $cols ); $index++ ) { - if ( ( $cols[$index] instanceof DOMElement ) - && ( $cols[$index]->tagName === 'col' ) ) { - - $newCell = $dom->createElement( 'td' ); - $newRow->appendChild( $newCell ); - } - } - } - - if ( $rowContainer->firstChild instanceof DOMNode ) { - $rowContainer->insertBefore( $newRow, $rowContainer->firstChild ); - } else { - $rowContainer->appendChild( $newRow ); - } - } - } - } - - /** - * - * @param array $attributes - * @return array - */ - private function ensureWikiTableClass( $attributes ) { - $newAttributes = []; - $noClass = true; - foreach ( $attributes as $name => $value ) { - if ( $name === 'class' ) { - $noClass = false; - $classes = explode( ' ', $value ); - if ( !in_array( 'wikitable', $classes ) ) { - $classes[] = 'wikitable'; - } - $value = implode( ' ', $classes ); - } - $newAttributes[$name] = $value; - } - - if ( $noClass ) { - $newAttributes['class'] = 'wikitable'; - } - - return $newAttributes; - } -} diff --git a/tests/phpunit/Converter/Postprocessor/RestoreTableAttributesTest.php b/tests/phpunit/Converter/Postprocessor/RestoreTableAttributesTest.php deleted file mode 100644 index 2ac78b3..0000000 --- a/tests/phpunit/Converter/Postprocessor/RestoreTableAttributesTest.php +++ /dev/null @@ -1,102 +0,0 @@ -###PRESERVEDTABLEATTRIBUTES### -| -|- -| Some -| [[DEF]] -|- -| Table -| -|} -[[Ipsum]] -{| -|width="50%"| ###PRESERVEDTABLEATTRIBUTES### -|width="50%"| -|- -| Some -| [[DEF]] -|- -| Table -| -|} -{{sit amet}} -{| -! Table head 1 -! Table head 2 -|- -|width="50%"| ###PRESERVEDTABLEATTRIBUTES### -|width="50%"| -|- -| Some -| [[DEF]] -|- -| Table -| -|} -HERE; - - private $expectedOutput = <<postprocess( $this->input ); - $this->assertEquals( $this->expectedOutput, $actualOutput ); - } -} diff --git a/tests/phpunit/Converter/Processor/PreserveTableAttributesTest.php b/tests/phpunit/Converter/Processor/PreserveTableAttributesTest.php deleted file mode 100644 index 6937734..0000000 --- a/tests/phpunit/Converter/Processor/PreserveTableAttributesTest.php +++ /dev/null @@ -1,31 +0,0 @@ -loadXML( $input ); - - $preprocessor = new PreserveTableAttributes(); - $preprocessor->process( $dom ); - - $this->assertXmlStringEqualsXmlString( - $expectedOutput, - $dom->saveXML() - ); - } -} diff --git a/tests/phpunit/data/preservetableattributestest-input.xml b/tests/phpunit/data/preservetableattributestest-input.xml deleted file mode 100644 index 2f2b934..0000000 --- a/tests/phpunit/data/preservetableattributestest-input.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - - - - - - -
-

Link

-
-

Description

-
-

- Confluence 101: organize your work in spaces -

-
-

Chances are, the information you need to do your job lives in multiple places. Word docs, Evernote files, email, PDFs, even Post-it notes. It's scattered among different systems. And to make matters worse, - the stuff your teammates need is equally siloed. If information had feelings, it would be lonely. -

-

But with Confluence, you can bring all that information into one place.

-
-

Some Text

- - - - - -
-

Dummy

-
-

Content

-
- \ No newline at end of file diff --git a/tests/phpunit/data/preservetableattributestest-output.xml b/tests/phpunit/data/preservetableattributestest-output.xml deleted file mode 100644 index f12ff73..0000000 --- a/tests/phpunit/data/preservetableattributestest-output.xml +++ /dev/null @@ -1,53 +0,0 @@ - - - - - - - - - - - - - - - - - - - -
- ###PRESERVEDTABLEATTRIBUTES### - -
-

Link

-
-

Description

-
-

- Confluence 101: organize your work in spaces -

-
-

Chances are, the information you need to do your job lives in multiple places. Word docs, Evernote files, email, PDFs, even Post-it notes. It's scattered among different systems. And to make matters worse, - the stuff your teammates need is equally siloed. If information had feelings, it would be lonely. -

-

But with Confluence, you can bring all that information into one place.

-
-

Some Text

- - - - - - - - -
- ###PRESERVEDTABLEATTRIBUTES### -
-

Dummy

-
-

Content

-
- \ No newline at end of file