Skip to content

Commit

Permalink
Merge pull request #195 from jelovirt/feature/tight-list-task
Browse files Browse the repository at this point in the history
Fix task lists that use tight lists
  • Loading branch information
jelovirt authored Nov 2, 2023
2 parents a1a1a56 + 102567b commit 338a249
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 8 deletions.
30 changes: 30 additions & 0 deletions src/main/java/com/elovirta/dita/markdown/CleanerFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.elovirta.dita.markdown;

import static com.elovirta.dita.markdown.renderer.TopicRenderer.TIGHT_LIST_P;

import java.util.ArrayDeque;
import java.util.Deque;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.XMLFilterImpl;

public class CleanerFilter extends XMLFilterImpl {

private Deque<Boolean> retainElementStack = new ArrayDeque<>();

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
boolean retain = !localName.equals(TIGHT_LIST_P);
retainElementStack.push(retain);
if (retain) {
getContentHandler().startElement(uri, localName, qName, atts);
}
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (retainElementStack.pop()) {
getContentHandler().endElement(uri, localName, qName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public void setErrorHandler(ErrorHandler errorHandler) {
*/
protected void render(final Document root) {
ContentHandler res = contentHandler;
final XMLFilterImpl cleanerFilter = new CleanerFilter();
cleanerFilter.setContentHandler(res);
res = cleanerFilter;
if (DitaRenderer.SPECIALIZATION.get(options)) {
final XMLFilterImpl specialize = new SpecializeFilter();
specialize.setContentHandler(res);
Expand Down
42 changes: 34 additions & 8 deletions src/main/java/com/elovirta/dita/markdown/SpecializeFilter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.elovirta.dita.markdown;

import static com.elovirta.dita.markdown.renderer.TopicRenderer.TIGHT_LIST_P;
import static javax.xml.XMLConstants.NULL_NS_URI;
import static org.dita.dost.util.Constants.*;

Expand Down Expand Up @@ -179,20 +180,33 @@ private void startElementTask(String uri, String localName, String qName, Attrib
if (depth == DEPTH_IN_BODY) {
if (taskState == TaskState.BODY) {
AttributesImpl sectionAtts = new AttributesImpl();
sectionAtts.addAttribute(NULL_NS_URI, "class", "class", "CDATA", TASK_CONTEXT.toString());
sectionAtts.addAttribute(
NULL_NS_URI,
ATTRIBUTE_NAME_CLASS,
ATTRIBUTE_NAME_CLASS,
"CDATA",
TASK_CONTEXT.toString()
);
doStartElement(uri, TASK_CONTEXT.localName, TASK_CONTEXT.localName, sectionAtts);
taskState = TaskState.CONTEXT;
}
doStartElement(uri, localName, qName, atts);
} else if ((taskState == TaskState.STEP || taskState == TaskState.INFO) && depth == 5) {
switch (localName) {
case "p":
case TIGHT_LIST_P:
paragraphCountInStep++;
if (paragraphCountInStep == 1) {
renameStartElement(TASK_CMD, atts);
} else if (paragraphCountInStep == 2 && taskState != TaskState.INFO) {
AttributesImpl res = new AttributesImpl(atts);
res.addAttribute(NULL_NS_URI, "class", "class", "CDATA", TASK_INFO.toString());
res.addAttribute(
NULL_NS_URI,
ATTRIBUTE_NAME_CLASS,
ATTRIBUTE_NAME_CLASS,
"CDATA",
TASK_INFO.toString()
);
doStartElement(NULL_NS_URI, TASK_INFO.localName, TASK_INFO.localName, res);
taskState = TaskState.INFO;
doStartElement(uri, localName, qName, atts);
Expand All @@ -203,7 +217,13 @@ private void startElementTask(String uri, String localName, String qName, Attrib
default:
if (taskState != TaskState.INFO) {
AttributesImpl res = new AttributesImpl(atts);
res.addAttribute(NULL_NS_URI, "class", "class", "CDATA", TASK_INFO.toString());
res.addAttribute(
NULL_NS_URI,
ATTRIBUTE_NAME_CLASS,
ATTRIBUTE_NAME_CLASS,
"CDATA",
TASK_INFO.toString()
);
doStartElement(NULL_NS_URI, TASK_INFO.localName, TASK_INFO.localName, res);
taskState = TaskState.INFO;
}
Expand Down Expand Up @@ -264,8 +284,14 @@ private void startElementReference(String uri, String localName, String qName, A
default:
if (referenceState == ReferenceState.BODY) {
AttributesImpl sectionAtts = new AttributesImpl();
sectionAtts.addAttribute(NULL_NS_URI, "class", "class", "CDATA", "- topic/section ");
doStartElement(uri, "section", "section", sectionAtts);
sectionAtts.addAttribute(
NULL_NS_URI,
ATTRIBUTE_NAME_CLASS,
ATTRIBUTE_NAME_CLASS,
"CDATA",
"- topic/section "
);
doStartElement(uri, TOPIC_SECTION.localName, TOPIC_SECTION.localName, sectionAtts);
referenceState = ReferenceState.SECTION;
}
break;
Expand Down Expand Up @@ -305,16 +331,16 @@ public void doEndElement(String uri, String localName, String qName) throws SAXE

private void renameStartElement(DitaClass cls, Attributes atts) throws SAXException {
AttributesImpl res = new AttributesImpl(atts);
res.addAttribute(NULL_NS_URI, "class", "class", "CDATA", cls.toString());
final int i = res.getIndex(NULL_NS_URI, "outputclass");
res.addAttribute(NULL_NS_URI, ATTRIBUTE_NAME_CLASS, ATTRIBUTE_NAME_CLASS, "CDATA", cls.toString());
final int i = res.getIndex(NULL_NS_URI, ATTRIBUTE_NAME_OUTPUTCLASS);
if (i != -1) {
res.removeAttribute(i);
}
doStartElement(NULL_NS_URI, cls.localName, cls.localName, res);
}

private Collection<String> getOutputclass(Attributes atts) {
final String outputclass = atts.getValue("outputclass");
final String outputclass = atts.getValue(ATTRIBUTE_NAME_OUTPUTCLASS);
if (outputclass == null) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import nu.validator.htmlparser.common.XmlViolationPolicy;
import nu.validator.htmlparser.sax.HtmlParser;
import org.dita.dost.util.DitaClass;
import org.dita.dost.util.XMLUtils;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -106,6 +107,8 @@ public class TopicRenderer extends AbstractRenderer {
private static final Attributes FIG_ATTS = buildAtts(TOPIC_FIG);
private static final Attributes REQUIRED_CLEANUP_ATTS = buildAtts(TOPIC_REQUIRED_CLEANUP);

public static final String TIGHT_LIST_P = "tight-list-p";

private static final Map<String, DitaClass> sections = new HashMap<>();

static {
Expand Down Expand Up @@ -838,7 +841,9 @@ private void render(final Paragraph node, final NodeRendererContext context, fin
ListItem.class.isAssignableFrom(parent.getClass()) &&
((ListItem) parent).isTight()
) {
html.startElement(node, TIGHT_LIST_P, P_ATTS);
context.renderChildren(node);
html.endElement();
return;
}
final Attributes atts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public String getSrc() {
"table-width.md",
"table.md",
"task.md",
"taskTight.md",
"taskOneStep.md",
"testBOM.md",
"testNoBOM.md",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.xml.sax.*;
import org.xml.sax.helpers.XMLFilterImpl;

public class MarkdownReaderTest extends AbstractReaderTest {

Expand Down Expand Up @@ -78,6 +79,7 @@ public String getSrc() {
"table-width.md",
"table.md",
"task.md",
"taskTight.md",
"taskOneStep.md",
"testBOM.md",
"testNoBOM.md",
Expand Down Expand Up @@ -140,6 +142,7 @@ public void test_missingHeader(String file) throws Exception {
@Test
public void test_schemaParseFailure() throws Exception {
final XMLReader reader = getReader();
reader.setContentHandler(new XMLFilterImpl());
final TestErrorHandler errorHandler = new TestErrorHandler();
reader.setErrorHandler(errorHandler);

Expand All @@ -160,6 +163,7 @@ public void test_schemaParseFailure() throws Exception {
@Test
public void test_schemaParseFailure_withoutErrorHandler() throws Exception {
final XMLReader reader = getReader();
reader.setContentHandler(new XMLFilterImpl());

try (final InputStream in = getClass().getResourceAsStream("/" + getSrc() + "schema/unrecognized.md")) {
final InputSource input = new InputSource(in);
Expand Down
16 changes: 16 additions & 0 deletions src/test/resources/dita/taskTight.dita
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<task 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 task/task " id="task">
<title class="- topic/title ">Task </title>
<taskbody class="- topic/body task/taskbody ">
<steps class="- topic/ol task/steps ">
<step class="- topic/li task/step ">
<cmd class="- topic/ph task/cmd ">Command</cmd>
</step>
<step class="- topic/li task/step ">
<cmd class="- topic/ph task/cmd ">Command</cmd>
</step>
</steps>
</taskbody>
</task>
4 changes: 4 additions & 0 deletions src/test/resources/markdown/taskTight.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Task {.task}

1. Command
1. Command
17 changes: 17 additions & 0 deletions src/test/resources/xdita/taskTight.dita
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<topic xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/"
class="- topic/topic " ditaarch:DITAArchVersion="2.0"
specializations="(topic hi-d)(topic em-d)"
id="task-task">
<title class="- topic/title ">Task {.task}</title>
<body class="- topic/body ">
<ol class="- topic/ol ">
<li class="- topic/li ">
<p class="- topic/p ">Command</p>
</li>
<li class="- topic/li ">
<p class="- topic/p ">Command</p>
</li>
</ol>
</body>
</topic>

0 comments on commit 338a249

Please sign in to comment.