Permalink
Browse files

Fix inline HTML element input #18

  • Loading branch information...
jelovirt committed Jul 19, 2018
1 parent 920d448 commit 03d57f9e04179e0935c0c42a0b2ae9775b696b70
@@ -19,13 +19,18 @@
import com.vladsch.flexmark.ext.yaml.front.matter.YamlFrontMatterBlock;
import com.vladsch.flexmark.util.options.DataHolder;
import com.vladsch.flexmark.util.sequence.BasedSequence;
import nu.validator.htmlparser.common.XmlViolationPolicy;
import nu.validator.htmlparser.sax.HtmlParser;
import org.apache.commons.io.FilenameUtils;
import org.dita.dost.util.DitaClass;
import org.dita.dost.util.SaxCache.SaxEvent;
import org.dita.dost.util.SaxCache.StartElementEvent;
import org.dita.dost.util.SaxCache.EndElementEvent;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.XMLFilterImpl;
import javax.xml.transform.*;
import javax.xml.transform.sax.SAXResult;
@@ -733,22 +738,75 @@ private void render(final HtmlBlock node, final NodeRendererContext context, fin
private void render(final HtmlInline node, final NodeRendererContext context, final DitaWriter html) {
final String text = node.getChars().toString();
final FragmentContentHandler fragmentFilter = new FragmentContentHandler();
fragmentFilter.setContentHandler(html.contentHandler);
final CacheContentHandler cache = new CacheContentHandler();
final TransformerHandler h;
try {
h = tf.newTransformerHandler(t);
} catch (final TransformerConfigurationException e) {
throw new RuntimeException(e);
}
h.setResult(new SAXResult(fragmentFilter));
final HtmlParser parser = new HtmlParser();
h.setResult(new SAXResult(cache));
final HtmlParser parser = new HtmlParser(XmlViolationPolicy.ALLOW);
parser.setContentHandler(h);
try {
parser.parse(new InputSource(new StringReader(text)));
if (text.startsWith("</")) {
final String data = text.replaceAll("/", "") + text;
try (final StringReader in = new StringReader(data)) {
parser.parse(new InputSource(in));
for (SaxEvent event : cache.events) {
if (event instanceof EndElementEvent) {
event.write(html.contentHandler);
}
}
} catch (IOException|SAXException e) {
throw new ParseException("Failed to parse HTML: " + e.getMessage(), e);
}
} else {
try (final StringReader in = new StringReader(text)) {
parser.parse(new InputSource(in));
for (SaxEvent event : cache.events) {
if (event instanceof StartElementEvent) {
event.write(html.contentHandler);
}
}
} catch (IOException|SAXException e) {
throw new ParseException("Failed to parse HTML: " + e.getMessage(), e);
}
}
}
private static class CacheContentHandler extends XMLFilterImpl {
final List<SaxEvent> events = new ArrayList<>();
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
events.add(new StartElementEvent(uri, localName, qName, atts));
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
events.add(new EndElementEvent(uri, localName, qName));
}
}
private StartElementEvent parseHtmlInline(final HtmlInline node) {
final List<StartElementEvent> events = new ArrayList<>();
final HtmlParser parser = new HtmlParser(XmlViolationPolicy.ALLOW);
parser.setContentHandler(new XMLFilterImpl() {
@Override
public void startElement (String uri, String localName, String qName, Attributes atts) throws SAXException {
events.add(new StartElementEvent(uri, localName, qName, atts));
}
});
try (final StringReader in = new StringReader(node.getChars().toString())) {
parser.parse(new InputSource(in));
} catch (IOException|SAXException e) {
throw new ParseException("Failed to parse HTML: " + e.getMessage(), e);
}
if (events.isEmpty()) {
return null;
}
return events.get(0);
}
private void render(final ListItem node, final NodeRendererContext context, final DitaWriter html) {
@@ -394,6 +394,24 @@
<xsl:apply-templates select="@* | node()"/>
</i>
</xsl:template>
<xsl:template match="sup">
<sup>
<xsl:apply-templates select="." mode="class"/>
<xsl:apply-templates select="@* | node()"/>
</sup>
</xsl:template>
<xsl:template match="sup" mode="class">
<xsl:attribute name="class">+ topic/ph hi-d/sup </xsl:attribute>
</xsl:template>
<xsl:template match="sub">
<sub>
<xsl:apply-templates select="." mode="class"/>
<xsl:apply-templates select="@* | node()"/>
</sub>
</xsl:template>
<xsl:template match="sub" mode="class">
<xsl:attribute name="class">+ topic/ph hi-d/sub </xsl:attribute>
</xsl:template>
<xsl:template match="a">
<xsl:variable name="href" select="lower-case(if (contains(@href, '#')) then substring-before(@href, '#') else @href)"/>
<xref>
@@ -175,7 +175,7 @@ public void testEntity() throws Exception {
public void testComment() throws Exception {
run("comment.md");
}
@Ignore
@Test
public void testHtml() throws Exception {
run("html.md");
@@ -28,5 +28,17 @@
<p class="- topic/p ">para</p></dd>
</dlentry>
</dl>
<dl class="- topic/dl ">
<dlentry class="- topic/dlentry ">
<dt class="- topic/dt ">Apple</dt>
<dd class="- topic/dd "><p class="- topic/p ">Pomaceous fruit of plants of the genus Malus in
the family Rosaceae.</p></dd>
</dlentry>
<dlentry class="- topic/dlentry ">
<dt class="- topic/dt ">Orange Nested</dt>
<dd class="- topic/dd "><p class="- topic/p ">The fruit of an evergreen tree of the genus Citrus.</p>
<p class="- topic/p ">para</p></dd>
</dlentry>
</dl>
</body>
</topic>
@@ -5,6 +5,6 @@
<body class="- topic/body ">
<p class="- topic/p ">Plain paragraph.</p>
<p class="- topic/p ">HTML paragraph.</p>
<video class="+ topic/object h5m-d/video " controls="" poster="remote.png" src="remote.mp4"/>
<p class="- topic/p "><video class="+ topic/object h5m-d/video " controls="" poster="remote.png" src="remote.mp4"/></p>
</body>
</topic>
@@ -15,5 +15,11 @@
<p class="- topic/p ">here is a <codeph class="+ topic/ph pr-d/codeph ">code</codeph> (<codeph class="+ topic/ph pr-d/codeph ">code</codeph>) claim</p>
<p class="- topic/p ">here --- mdash --- dash</p>
<p class="- topic/p ">here -- ndash -- dash</p>
<p class="- topic/p ">HTML <b class="+ topic/ph hi-d/b ">bold</b> element</p>
<p class="- topic/p ">HTML <i class="+ topic/ph hi-d/i ">italic</i> element</p>
<p class="- topic/p ">HTML <i class="+ topic/ph hi-d/i ">emphasis</i> element</p>
<p class="- topic/p ">HTML <b class="+ topic/ph hi-d/b ">strong</b> element</p>
<p class="- topic/p ">HTML <sup class="+ topic/ph hi-d/sup ">superscript</sup> element</p>
<p class="- topic/p ">HTML <sub class="+ topic/ph hi-d/sub ">subscript</sub> element</p>
</body>
</topic>
@@ -19,4 +19,12 @@ <h1>Definition list</h1>
<dd>The fruit of an evergreen tree of the genus Citrus.
<p>para</dd>
</dl>
<dl>
<dt>Apple</dt>
<dd>Pomaceous fruit of plants of the genus Malus in
the family Rosaceae.</dd>
<dt>Orange Nested</dt>
<dd>The fruit of an evergreen tree of the genus Citrus.
<p>para</dd>
</dl>
</article>
@@ -4,5 +4,5 @@
<h1>HTML Block</h1>
<p>Plain paragraph.</p>
<p>HTML paragraph.</p>
<video controls poster="remote.png" src="remote.mp4"></video>
<p><video controls poster="remote.png" src="remote.mp4"></video></p>
</article>
@@ -10,4 +10,10 @@ <h1>Test</h1>
<p>here is a <code>code</code> (<code>code</code>) claim</p>
<p>here --- mdash --- dash</p>
<p>here -- ndash -- dash</p>
<p>HTML <b>bold</b> element</p>
<p>HTML <i>italic</i> element</p>
<p>HTML <em>emphasis</em> element</p>
<p>HTML <strong>strong</strong> element</p>
<p>HTML <sup>superscript</sup> element</p>
<p>HTML <sub>subscript</sub> element</p>
</article>
@@ -38,5 +38,21 @@
</dd>
</dlentry>
</dl>
<dl class="- topic/dl ">
<dlentry class="- topic/dlentry ">
<dt class="- topic/dt ">Apple</dt>
<dd class="- topic/dd ">
<p class="- topic/p ">Pomaceous fruit of plants of the genus Malus in the family
Rosaceae.</p>
</dd>
</dlentry>
<dlentry class="- topic/dlentry ">
<dt class="- topic/dt ">Orange Nested</dt>
<dd class="- topic/dd ">
<p class="- topic/p ">The fruit of an evergreen tree of the genus Citrus.</p>
<p class="- topic/p ">para</p>
</dd>
</dlentry>
</dl>
</body>
</topic>
@@ -5,6 +5,6 @@
<shortdesc class="- topic/shortdesc ">Plain paragraph.</shortdesc>
<body class="- topic/body ">
<p class="- topic/p ">HTML paragraph.</p>
<video class="+ topic/object h5m-d/video " controls="" poster="remote.png" src="remote.mp4"/>
<p class="- topic/p "><video class="+ topic/object h5m-d/video " controls="" poster="remote.png" src="remote.mp4"/></p>
</body>
</topic>
@@ -20,5 +20,11 @@
class="+ topic/ph pr-d/codeph ">code</codeph>) claim</p>
<p class="- topic/p ">here --- mdash --- dash</p>
<p class="- topic/p ">here -- ndash -- dash</p>
<p class="- topic/p ">HTML <b class="+ topic/ph hi-d/b ">bold</b> element</p>
<p class="- topic/p ">HTML <i class="+ topic/ph hi-d/i ">italic</i> element</p>
<p class="- topic/p ">HTML <i class="+ topic/ph hi-d/i ">emphasis</i> element</p>
<p class="- topic/p ">HTML <b class="+ topic/ph hi-d/b ">strong</b> element</p>
<p class="- topic/p ">HTML <sup class="+ topic/ph hi-d/sup ">superscript</sup> element</p>
<p class="- topic/p ">HTML <sub class="+ topic/ph hi-d/sub ">subscript</sub> element</p>
</body>
</topic>
@@ -20,3 +20,12 @@ Orange Nested
: The fruit of an evergreen tree of the genus Citrus.
para
<dl>
<dt>Apple</dt>
<dd>Pomaceous fruit of plants of the genus Malus in
the family Rosaceae.</dd>
<dt>Orange Nested</dt>
<dd>The fruit of an evergreen tree of the genus Citrus.
<p>para</p></dd>
</dl>
@@ -14,3 +14,15 @@ here is a `code` (`code`) claim
here --- mdash --- dash
here -- ndash -- dash
HTML <b>bold</b> element
HTML <i>italic</i> element
HTML <em>emphasis</em> element
HTML <strong>strong</strong> element
HTML <sup>superscript</sup> element
HTML <sub>subscript</sub> element
@@ -1,3 +1,3 @@
# Keyref
Foo <span data-keyref="bar"> baz.
Foo <span data-keyref="bar"></span> baz.

0 comments on commit 03d57f9

Please sign in to comment.