Skip to content

Commit

Permalink
Disable Wiki support by default in Markdown
Browse files Browse the repository at this point in the history
Signed-off-by: Jarno Elovirta <jarno@elovirta.com>
  • Loading branch information
jelovirt committed May 14, 2024
1 parent e1e5fc5 commit 7ebdee7
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 38 deletions.
47 changes: 30 additions & 17 deletions src/main/java/com/elovirta/dita/markdown/MarkdownParserImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.elovirta.dita.markdown;

import com.elovirta.dita.markdown.renderer.HeaderIdGenerator;
import com.vladsch.flexmark.ast.Heading;
import com.vladsch.flexmark.ast.Text;
import com.vladsch.flexmark.ext.anchorlink.AnchorLink;
Expand All @@ -11,6 +12,7 @@
import com.vladsch.flexmark.util.data.DataSet;
import com.vladsch.flexmark.util.sequence.BasedSequence;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
Expand Down Expand Up @@ -130,16 +132,17 @@ protected void validate(Document root) {
* If document doesn't start with H1, generate H1 from YAML metadata or file name.
*/
protected Document preprocess(Document root, URI input) throws SAXException {
if (isWiki(root)) {
if (isWiki(root) && !DitaRenderer.MAP.get(options)) {
final Map<String, String> header = parseYamlHeader(root);
if (DitaRenderer.WIKI.get(options)) {
generateRootHeading(root, input);
generateRootHeading(root, header.get("id"), header.getOrDefault("title", getTextFromFile(input)));
} else if (DitaRenderer.FIX_ROOT_HEADING.get(options)) {
if (errorHandler != null) {
errorHandler.warning(
new SAXParseException(MESSAGES.getString("error.missing_title"), null, input.toString(), 1, 1)
);
}
generateRootHeading(root, input);
generateRootHeading(root, header.get("id"), header.getOrDefault("title", getTextFromFile(input)));
} else if (MarkdownReader.PROCESSING_MODE.get(options)) {
if (errorHandler != null) {
errorHandler.error(
Expand All @@ -152,43 +155,51 @@ protected Document preprocess(Document root, URI input) throws SAXException {
new SAXParseException(MESSAGES.getString("error.missing_title"), null, input.toString(), 1, 1)
);
}
generateRootHeading(root, getTextFromFile(input).toLowerCase().trim().replaceAll("[\\s_]+", "-"), null);
String id = header.get("id");
if (id == null) {
id =
HeaderIdGenerator.generateId(
getTextFromFile(input),
DitaRenderer.HEADER_ID_GENERATOR_TO_DASH_CHARS.get(root),
DitaRenderer.HEADER_ID_GENERATOR_NO_DUPED_DASHES.get(root)
);
}
generateRootHeading(root, id, header.get("title"));
}
}
return root;
}

/**
* Generate root heading using YAML header or input file name. If neither YAML or file name is not available, create empty heading.
*
*/
private void generateRootHeading(Document root, URI input) {
private static String getId(final String contents) {
return contents.toLowerCase().replaceAll("[^\\w\\s]", "").trim().replaceAll("\\s+", "-");
}

private Map<String, String> parseYamlHeader(Document root) {
Map<String, String> res = new HashMap<>();
final YamlFrontMatterBlock yaml = root.getFirstChild() instanceof YamlFrontMatterBlock
? (YamlFrontMatterBlock) root.getFirstChild()
: null;
String title = getTextFromFile(input);
String id = null;
if (yaml != null) {
final AbstractYamlFrontMatterVisitor v = new AbstractYamlFrontMatterVisitor();
v.visit(root);
final Map<String, List<String>> metadata = v.getData();
final List<String> ids = metadata.get("id");
if (ids != null && !ids.isEmpty()) {
id = ids.get(0);
res.put("id", ids.get(0));
}
final List<String> titles = metadata.get("title");
if (titles != null && !titles.isEmpty()) {
title = titles.get(0);
String title = titles.get(0);
if (
(title.charAt(0) == '\'' && title.charAt(title.length() - 1) == '\'') ||
(title.charAt(0) == '"' && title.charAt(title.length() - 1) == '"')
) {
title = title.substring(1, title.length() - 1);
}
res.put("title", title);
}
}

generateRootHeading(root, id, title);
return res;
}

private void generateRootHeading(Document root, String id, String title) {
Expand All @@ -197,10 +208,12 @@ private void generateRootHeading(Document root, String id, String title) {
heading.setAnchorRefId(id);
}
heading.setLevel(1);
if (title != null) {
if (id == null) {
final AnchorLink anchorLink = new AnchorLink();
anchorLink.appendChild(new Text(title));
anchorLink.appendChild(new Text(title != null ? title : ""));
heading.appendChild(anchorLink);
} else {
heading.appendChild(new Text(title != null ? title : ""));
}
root.prependChild(heading);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/elovirta/dita/markdown/MarkdownReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class MarkdownReader implements XMLReader {
features.put("http://lwdita.org/sax/features/specialization-concept", DitaRenderer.SPECIALIZATION_CONCEPT);
features.put("http://lwdita.org/sax/features/specialization-task", DitaRenderer.SPECIALIZATION_TASK);
features.put("http://lwdita.org/sax/features/specialization-reference", DitaRenderer.SPECIALIZATION_REFERENCE);
features.put("http://lwdita.org/sax/features/wiki", DitaRenderer.WIKI);
features.put("http://lwdita.org/sax/features/fix-root-heading", DitaRenderer.FIX_ROOT_HEADING);
features.put("http://lwdita.org/sax/features/map", DitaRenderer.MAP);
FEATURES = Collections.unmodifiableMap(features);
Expand Down Expand Up @@ -132,7 +133,7 @@ public MarkdownReader() {
.set(TablesExtension.DISCARD_EXTRA_COLUMNS, true)
.set(TablesExtension.HEADER_SEPARATOR_COLUMN_MATCH, true)
.set(DitaRenderer.SPECIALIZATION, true)
.set(DitaRenderer.WIKI, true)
.set(DitaRenderer.ID_FROM_YAML, true)
);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<parser format="mditamap" class="com.elovirta.dita.markdown.MDitamapReader"/>
<parser format="wikidocs" class="com.elovirta.dita.markdown.MarkdownReader">
<feature name="http://lwdita.org/sax/features/shortdesc-paragraph" value="true"/>
<feature name="http://lwdita.org/sax/features/wiki" value="true"/>
</parser>
</feature>
<extension-point id="dita.xsl.markdown" name="Markdown overrides XSLT import"/>
Expand Down
13 changes: 4 additions & 9 deletions src/test/java/com/elovirta/dita/markdown/MarkdownReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,26 +136,21 @@ public void test_missingHeader(String file) throws Exception {
new MutableDataSet()
.set(Parser.EXTENSIONS, singletonList(YamlFrontMatterExtension.create()))
.set(DitaRenderer.FIX_ROOT_HEADING, true)
.set(DitaRenderer.ID_FROM_YAML, true)
.set(DitaRenderer.WIKI, true)
);
final TestErrorHandler errorHandler = new TestErrorHandler();
reader.setErrorHandler(errorHandler);

run(getSrc() + file, getExp() + "wiki/" + file.replaceAll("\\.md$", ".dita"));

assertEquals(1, errorHandler.warnings.size());
final SAXParseException e = errorHandler.warnings.get(0);
assertEquals("Document content doesn't start with heading", e.getMessage());
assertEquals("classpath:/markdown/" + file, e.getSystemId());
assertNull(e.getPublicId());
assertEquals(1, e.getLineNumber());
assertEquals(1, e.getColumnNumber());
assertEquals(0, errorHandler.warnings.size());
}

@ParameterizedTest
@ValueSource(strings = { "missing_root_header.md", "missing_root_header_with_yaml.md" })
public void test_emptyHeader(String file) throws Exception {
reader =
new MarkdownReader(new MutableDataSet().set(Parser.EXTENSIONS, singletonList(YamlFrontMatterExtension.create())));
reader = new MarkdownReader();
final TestErrorHandler errorHandler = new TestErrorHandler();
reader.setErrorHandler(errorHandler);

Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/dita/missing_root_header_with_yaml.dita
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<topic xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" class="- topic/topic "
specializations="@props/audience @props/deliveryTarget @props/otherprops @props/platform @props/product"
id="missing-root-header-with-yaml" ditaarch:DITAArchVersion="2.0">
<title class="- topic/title "></title>
id="yaml-id" ditaarch:DITAArchVersion="2.0">
<title class="- topic/title ">YAML Title</title>
<body class="- topic/body ">
<p class="- topic/p ">Root topic content.</p>
</body>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<topic xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" class="- topic/topic "
specializations="@props/audience @props/deliveryTarget @props/otherprops @props/platform @props/product"
id="yaml-title" ditaarch:DITAArchVersion="2.0">
id="yaml-id" ditaarch:DITAArchVersion="2.0">
<title class="- topic/title ">YAML Title</title>
<body class="- topic/body ">
<p class="- topic/p ">Root topic content.</p>
Expand Down
3 changes: 1 addition & 2 deletions src/test/resources/dita/yaml.dita
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<topic xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" class="- topic/topic "
specializations="@props/audience @props/deliveryTarget @props/otherprops @props/platform @props/product"
id="yaml-header" ditaarch:DITAArchVersion="2.0">
id="extended-profile" ditaarch:DITAArchVersion="2.0">
<title class="- topic/title ">YAML Header</title>
<prolog class="- topic/prolog ">
<author class="- topic/author ">Author One</author>
Expand All @@ -22,7 +22,6 @@
<data class="- topic/data " name="bar" value="bar"/>
<data class="- topic/data " name="bar" value="baz"/>
<data class="- topic/data " name="foo" value="foo"/>
<data class="- topic/data " name="id" value="extended-profile"/>
<data class="- topic/data " name="tags" value="nothing"/>
<data class="- topic/data " name="tags" value="nothingness"/>
<data class="- topic/data " name="title" value="'This is the title: it contains a colon'"/>
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/output/ast/missing_root_header.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?xml version="1.0" encoding="UTF-8"?><pandoc><div><header id="missing-root-header" level="1">missing root header</header><div><para>Root topic content.</para></div><div id="nested-topic"><header id="nested-topic" level="2">Nested Topic</header><div><para>Nested topic content.</para></div></div></div></pandoc>
<?xml version="1.0" encoding="UTF-8"?><pandoc><div><header id="missing-root-header" level="1"/><div><para>Root topic content.</para></div><div id="nested-topic"><header id="nested-topic" level="2">Nested Topic</header><div><para>Nested topic content.</para></div></div></div></pandoc>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?xml version="1.0" encoding="UTF-8"?><pandoc><div><header id="yaml-title" level="1">YAML Title</header><div><para>Root topic content.</para></div></div></pandoc>
<?xml version="1.0" encoding="UTF-8"?><pandoc><div><header id="yaml-id" level="1">YAML Title</header><div><para>Root topic content.</para></div></div></pandoc>
2 changes: 1 addition & 1 deletion src/test/resources/output/ast/yaml.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?xml version="1.0" encoding="UTF-8"?><pandoc><div><header id="yaml-header" level="1">YAML Header</header><div/></div></pandoc>
<?xml version="1.0" encoding="UTF-8"?><pandoc><div><header id="extended-profile" level="1">YAML Header</header><div/></div></pandoc>
2 changes: 1 addition & 1 deletion src/test/resources/output/markdown/missing_root_header.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# missing root header {#missing-root-header}
# {#missing-root-header}

Root topic content.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# YAML Title {#yaml-title}
# YAML Title {#yaml-id}

Root topic content.

2 changes: 1 addition & 1 deletion src/test/resources/output/markdown/yaml.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# YAML Header {#yaml-header}
# YAML Header {#extended-profile}

0 comments on commit 7ebdee7

Please sign in to comment.