Skip to content

Commit

Permalink
Merge pull request #200 from jelovirt/feature/table-block-content
Browse files Browse the repository at this point in the history
Table block content
  • Loading branch information
jelovirt authored Dec 25, 2023
2 parents 2f10acd + 0263891 commit 10bd041
Show file tree
Hide file tree
Showing 9 changed files with 450 additions and 41 deletions.
99 changes: 99 additions & 0 deletions src/main/resources/ast2markdown.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,105 @@
<xsl:value-of select="$linefeed"/>
</xsl:template>

<xsl:template match="*" mode="render-html">
<xsl:text>&lt;</xsl:text>
<xsl:value-of select="name()"/>
<xsl:for-each select="@*">
<xsl:text> </xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>="</xsl:text>
<xsl:value-of select="replace(replace(., '&amp;', '&amp;amp;'), '&quot;', '&amp;quot;')"/>
<xsl:text>"</xsl:text>
</xsl:for-each>
<xsl:text>></xsl:text>
<xsl:apply-templates mode="#current"/>
<xsl:text>&lt;/</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>></xsl:text>
</xsl:template>

<xsl:template match="text()" mode="render-html">
<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="table[*/tr/tablecell/(para | plain | codeblock | rawblock | blockquote | orderedlist | bulletlist | definitionlist | header | table | div)]" mode="ast" priority="10">
<xsl:variable name="html" as="element()*">
<table>
<xsl:copy-of select="@*"/>
<xsl:for-each select="thead">
<thead>
<xsl:copy-of select="@*"/>
<xsl:for-each select="tr">
<tr>
<xsl:copy-of select="@*"/>
<xsl:for-each select="tablecell">
<th>
<xsl:copy-of select="@*"/>
<xsl:value-of select="$linefeed"/>
<xsl:value-of select="$linefeed"/>
<xsl:variable name="contents" as="xs:string">
<xsl:value-of separator="">
<xsl:apply-templates mode="#current">
<xsl:with-param name="indent" tunnel="yes" select="''"/>
</xsl:apply-templates>
</xsl:value-of>
</xsl:variable>
<xsl:value-of select="$contents" separator="|"/>
<xsl:choose>
<xsl:when test="not(ends-with($contents, '&#xA;&#xA;'))">
<xsl:value-of select="$linefeed"/>
<xsl:value-of select="$linefeed"/>
</xsl:when>
<xsl:when test="not(ends-with($contents, '&#xA;'))">
<xsl:value-of select="$linefeed"/>
</xsl:when>
</xsl:choose>
</th>
</xsl:for-each>
</tr>
</xsl:for-each>
</thead>
</xsl:for-each>
<xsl:for-each select="tbody">
<tbody>
<xsl:copy-of select="@*"/>
<xsl:for-each select="tr">
<tr>
<xsl:copy-of select="@*"/>
<xsl:for-each select="tablecell">
<td>
<xsl:copy-of select="@*"/>
<xsl:value-of select="$linefeed"/>
<xsl:value-of select="$linefeed"/>
<xsl:variable name="contents" as="xs:string">
<xsl:value-of separator="">
<xsl:apply-templates mode="#current">
<xsl:with-param name="indent" tunnel="yes" select="''"/>
</xsl:apply-templates>
</xsl:value-of>
</xsl:variable>
<xsl:value-of select="$contents" separator="|"/>
<xsl:choose>
<xsl:when test="not(ends-with($contents, '&#xA;&#xA;'))">
<xsl:value-of select="$linefeed"/>
<xsl:value-of select="$linefeed"/>
</xsl:when>
<xsl:when test="not(ends-with($contents, '&#xA;'))">
<xsl:value-of select="$linefeed"/>
</xsl:when>
</xsl:choose>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</xsl:for-each>
<xsl:value-of select="$linefeed"/>
</table>
</xsl:variable>
<xsl:apply-templates select="$html" mode="render-html"/>
</xsl:template>

<xsl:template name="process-tablecell-contents">
<xsl:choose>
<xsl:when test="para and count(*) eq 1 and not(text()[normalize-space()])">
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/tables.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
<xsl:call-template name="find-colspan"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="@align">
<xsl:attribute name="align" select="@align"/>
</xsl:if>

<!-- Add any flags from tgroup, thead or tbody, and row -->
<xsl:apply-templates select="../../../*[contains(@class, ' ditaot-d/ditaval-startprop ')]" mode="out-of-line"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.elovirta.dita.markdown;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.elovirta.dita.utils.ClasspathURIResolver;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.xml.transform.Transformer;
Expand Down Expand Up @@ -55,6 +56,7 @@ public class AstToMarkdownTest {
"reference",
"short",
"shortdesc",
"table-block",
"table-width",
"table",
"task",
Expand All @@ -71,7 +73,7 @@ public class AstToMarkdownTest {
public void testAst(String name) throws Exception {
final byte[] act = run("output/ast/" + name + ".xml");
final byte[] exp = read("output/markdown/" + name + ".md");
assertArrayEquals(exp, act);
assertEquals(new String(exp, StandardCharsets.UTF_8), new String(act, StandardCharsets.UTF_8));
}

private byte[] run(final String input) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public DitaToAstTest() {
"reference",
"short",
"shortdesc",
"table-block",
"table-width",
"table",
"task",
Expand Down
70 changes: 70 additions & 0 deletions src/test/resources/dita/table-block.dita
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<topic xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/"
ditaarch:DITAArchVersion="2.0"
specializations="@props/audience @props/deliveryTarget @props/otherprops @props/platform @props/product"
class="- topic/topic "
id="tables">
<title class="- topic/title ">Table with block content</title>
<body class="- topic/body ">
<table class="- topic/table ">
<tgroup class="- topic/tgroup " cols="3">
<colspec class="- topic/colspec " colname="col1"/>
<colspec class="- topic/colspec " colname="col2"/>
<colspec class="- topic/colspec " colname="col3"/>
<thead class="- topic/thead ">
<row class="- topic/row ">
<entry class="- topic/entry ">A</entry>
<entry class="- topic/entry ">B</entry>
<entry class="- topic/entry ">C</entry>
</row>
</thead>
<tbody class="- topic/tbody ">
<row class="- topic/row ">
<entry class="- topic/entry ">
<p class="- topic/p ">A1 <b class="+ topic/ph hi-d/b ">bold</b></p>
<p class="- topic/p ">A1</p>
</entry>
<entry class="- topic/entry ">
<p class="- topic/p ">B1</p>
<p class="- topic/p "><b class="+ topic/ph hi-d/b ">bold</b> B1</p>
</entry>
<entry class="- topic/entry ">
<p class="- topic/p "><b class="+ topic/ph hi-d/b ">C1</b></p>
<p class="- topic/p ">C1</p>
</entry>
</row>
<row class="- topic/row ">
<entry class="- topic/entry " align="left">
<p class="- topic/p ">A2 <b class="+ topic/ph hi-d/b ">bold</b></p>
</entry>
<entry class="- topic/entry " align="center">
<p class="- topic/p ">B2</p>
</entry>
<entry class="- topic/entry " align="right">
<p class="- topic/p "><b class="+ topic/ph hi-d/b ">C2</b></p>
</entry>
</row>
<row class="- topic/row ">
<entry class="- topic/entry " namest="col1" nameend="col2">
<p class="- topic/p ">A3</p>
<p class="- topic/p ">B3</p>
</entry>
<entry class="- topic/entry " morerows="1">
<p class="- topic/p ">C3</p>
</entry>
</row>
<row class="- topic/row ">
<entry class="- topic/entry ">
<codeblock class="+ topic/pre pr-d/codeblock ">A4</codeblock>
</entry>
<entry class="- topic/entry ">
<ol class="- topic/ol ">
<li class="- topic/li ">B4</li>
</ol>
</entry>
</row>
</tbody>
</tgroup>
</table>
</body>
</topic>
85 changes: 47 additions & 38 deletions src/test/resources/output/ast/ast.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="../../main/resources/ast.rnc" type="application/relax-ng-compact-syntax"?>
<pandoc>
<div>
<header id="test" level="1">Test</header>
<div>
<para>Paragraph <emph>test</emph> and <strong>list list list list list list list list list list list list list
list list list list list list list list list list</strong> or <code>code</code>. This is a very long line.
This is a very long line. This is a very long line. This is a very long line. This is a very long line. This is
a very long line. This is a very long line. This is a very long line. This is a very long line. This is a very
long line. This is a very long line.</para>
list list list list list list list list list list
</strong> or <code>code</code>. This is a very long line. This is a very long line. This is a very long line. This
is a very long line. This is a very long line. This is a very long line. This is a very long line. This is a
very long line. This is a very long line. This is a very long line. This is a very long line.
</para>
<para>Paragraph with block content</para>
<codeblock xml:space="preserve">codeblock
codeblock
Expand All @@ -20,16 +20,20 @@ codeblock</codeblock>
</li>
</orderedlist>
<para>Asterisk \* bracket \[foo\].</para>
<para>\\\`\*\_\{\}\[\]\(\)\&gt;\#+-.!&lt;></para>
<para>\\\`\*\_\{\}\[\]\(\)\&gt;\#+-.!&lt;&gt;</para>
<para>Hyphen:</para>
<bulletlist>
<li>
<para>hyphen list list list list list list list list list list list list list list list list list list list
list list list list</para>
list list list list
</para>
</li>
<li>
<para><strong>start</strong> with <emph>inline</emph>
<strong>elements</strong></para>
<para>
<strong>start</strong>
with <emph>inline</emph>
<strong>elements</strong>
</para>
<para>para</para>
</li>
<li>
Expand Down Expand Up @@ -57,7 +61,8 @@ codeblock</codeblock>
<para>Asterix:</para>
<bulletlist>
<li>
<para>inline <emph>bold</emph> normal</para>
<para>inline <emph>bold</emph> normal
</para>
<bulletlist>
<li>
<para>inline</para>
Expand Down Expand Up @@ -156,7 +161,8 @@ codeblock
</orderedlist>
<div class="section" id="example">
<header class="section" id="example" level="2">Example</header>
<para>Code example on <code>for</code> loop:</para>
<para>Code example on <code>for</code> loop:
</para>
<codeblock xml:space="preserve">for i in items:
println(i)
</codeblock>
Expand All @@ -168,7 +174,8 @@ codeblock
<div id="images">
<header id="images" level="2">Images</header>
<div>
<para>An inline <image alt="Alt" href="test.jpg"/>.</para>
<para>An inline <image alt="Alt" href="test.jpg"/>.
</para>
<para>
<image alt="Alt" href="test.jpg"/>
</para>
Expand Down Expand Up @@ -223,30 +230,30 @@ codeblock
<col/>
<thead>
<tr>
<tablecell>Right</tablecell>
<tablecell>Left</tablecell>
<tablecell align="right">Right</tablecell>
<tablecell align="left">Left</tablecell>
<tablecell>Default</tablecell>
<tablecell>Center</tablecell>
<tablecell align="center">Center</tablecell>
</tr>
</thead>
<tbody>
<tr>
<tablecell align="right">12</tablecell>
<tablecell align="left">12</tablecell>
<tablecell>12</tablecell>
<tablecell>12</tablecell>
<tablecell>12</tablecell>
<tablecell>12</tablecell>
<tablecell align="center">12</tablecell>
</tr>
<tr>
<tablecell align="right">123</tablecell>
<tablecell align="left">123</tablecell>
<tablecell>123</tablecell>
<tablecell>123</tablecell>
<tablecell>123</tablecell>
<tablecell>123</tablecell>
<tablecell align="center">123</tablecell>
</tr>
<tr>
<tablecell align="right">1</tablecell>
<tablecell align="left">1</tablecell>
<tablecell>1</tablecell>
<tablecell>1</tablecell>
<tablecell>1</tablecell>
<tablecell>1</tablecell>
<tablecell align="center">1</tablecell>
</tr>
</tbody>
</table>
Expand All @@ -257,27 +264,29 @@ codeblock
<col/>
<thead>
<tr>
<tablecell>First Header</tablecell>
<tablecell>Second Header Very long data entry Very long data entry Very long data entry Very long data
entry Very long data entry Very long data entry Very long data entry Very long data entry Very long data
entry</tablecell>
<tablecell>Third Header</tablecell>
<tablecell align="left">First Header</tablecell>
<tablecell align="center">Second Header Very long data entry Very long data entry Very long data entry
Very long data entry Very long data entry Very long data entry Very long data entry Very long data entry
Very long data entry
</tablecell>
<tablecell align="right">Third Header</tablecell>
</tr>
</thead>
<tbody>
<tr>
<tablecell>First row</tablecell>
<tablecell>Data</tablecell>
<tablecell>Very long data entry Very long data entry Very long data entry Very long data entry Very long
data entry Very long data entry Very long data entry Very long data entry Very long data entry Very long
data entry</tablecell>
<tablecell align="left">First row</tablecell>
<tablecell align="center">Data</tablecell>
<tablecell align="right">Very long data entry Very long data entry Very long data entry Very long data
entry Very long data entry Very long data entry Very long data entry Very long data entry Very long data
entry Very long data entry
</tablecell>
</tr>
<tr>
<tablecell>Second row</tablecell>
<tablecell>
<tablecell align="left">Second row</tablecell>
<tablecell align="center">
<strong>Cell</strong>
</tablecell>
<tablecell>
<tablecell align="right">
<emph>Cell</emph>
</tablecell>
</tr>
Expand Down Expand Up @@ -322,4 +331,4 @@ codeblock
</div>
</div>
</div>
</pandoc>
</pandoc>
1 change: 1 addition & 0 deletions src/test/resources/output/ast/table-block.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><pandoc><div><header id="tables" level="1">Table with block content</header><div><table><col/><col/><col/><thead><tr><tablecell>A</tablecell><tablecell>B</tablecell><tablecell>C</tablecell></tr></thead><tbody><tr><tablecell><para>A1 <strong>bold</strong></para> <para>A1</para></tablecell><tablecell><para>B1</para> <para><strong>bold</strong> B1</para></tablecell><tablecell><para><strong>C1</strong></para> <para>C1</para></tablecell></tr><tr><tablecell align="left"><para>A2 <strong>bold</strong></para></tablecell><tablecell align="center"><para>B2</para></tablecell><tablecell align="right"><para><strong>C2</strong></para></tablecell></tr><tr><tablecell colspan="2"><para>A3</para> <para>B3</para></tablecell><tablecell rowspan="2"><para>C3</para></tablecell></tr><tr><tablecell><codeblock xml:space="preserve">A4</codeblock></tablecell><tablecell><orderedlist><li><plain>B4</plain></li></orderedlist></tablecell></tr></tbody></table></div></div></pandoc>
Loading

0 comments on commit 10bd041

Please sign in to comment.