The Unified and Autonomous Development Platform
midwork of tasklist
robin shinebf05f12e5d64
diff --git a/commons.markdown/src/main/java/com/pmease/commons/markdown/DefaultMarkdownManager.java b/commons.markdown/src/main/java/com/pmease/commons/markdown/DefaultMarkdownManager.javaindex f784f115c4..441debb8b3 100644--- a/commons.markdown/src/main/java/com/pmease/commons/markdown/DefaultMarkdownManager.java+++ b/commons.markdown/src/main/java/com/pmease/commons/markdown/DefaultMarkdownManager.java@@ -17,7 +17,12 @@ import org.pegdown.LinkRenderer;import org.pegdown.Parser;import org.pegdown.PegDownProcessor;import org.pegdown.ToHtmlSerializer;+import org.pegdown.ast.ListItemNode;+import org.pegdown.ast.Node;+import org.pegdown.ast.ParaNode;import org.pegdown.ast.RootNode;+import org.pegdown.ast.SuperNode;+import org.pegdown.ast.TaskListNode;import org.pegdown.plugins.PegDownPlugins;import org.pegdown.plugins.ToHtmlSerializerPlugin;@@ -48,26 +53,58 @@ public class DefaultMarkdownManager implements MarkdownManager {builder.withPlugin(each);}}- PegDownPlugins plugins = builder.build();- PegDownProcessor processor = new PegDownProcessor(ALL_WITH_OPTIONALS, plugins);+ PegDownPlugins pegDownPlugins = builder.build();+ PegDownProcessor processor = new PegDownProcessor(ALL_WITH_OPTIONALS, pegDownPlugins);RootNode ast = processor.parseMarkdown(markdown.toCharArray());- List<ToHtmlSerializerPlugin> serializers = new ArrayList<>();+ List<ToHtmlSerializerPlugin> serializerPlugins = new ArrayList<>();for (MarkdownExtension extension: extensions) {if (extension.getHtmlSerializers() != null) {for (ToHtmlSerializerPlugin each: extension.getHtmlSerializers())- serializers.add(each);+ serializerPlugins.add(each);}}- return new ToHtmlSerializer(new LinkRenderer(), serializers).toHtml(ast);+ return new ToHtmlSerializer(new LinkRenderer(), serializerPlugins) {++ @Override+ public void visit(ListItemNode node) {+ if (node instanceof TaskListNode) {+ // vsch: #185 handle GitHub style task list items, these are a bit messy because the <input> checkbox needs to be+ // included inside the optional <p></p> first grand-child of the list item, first child is always RootNode+ // because the list item text is recursively parsed.+ Node firstChild = node.getChildren().get(0).getChildren().get(0);+ boolean firstIsPara = firstChild instanceof ParaNode;+ int indent = node.getChildren().size() > 1 ? 2 : 0;+ boolean startWasNewLine = printer.endsWithNewLine();++ printer.println().print("<li class=\"task-list-item\">").indent(indent);+ if (firstIsPara) {+ printer.println().print("<p>");+ printer.print("<input data-mdstart=" + node.getStartIndex() + " data-mdend=" + node.getEndIndex() + " type='checkbox' class='task-list-item-checkbox'" + (((TaskListNode) node).isDone() ? " checked='checked'" : "") + "></input>");+ visitChildren((SuperNode) firstChild);++ // render the other children, the p tag is taken care of here+ visitChildrenSkipFirst(node);+ printer.print("</p>");+ } else {+ printer.print("<input data-mdstart=" + node.getStartIndex() + " data-mdend=" + node.getEndIndex() + " type='checkbox' class='task-list-item-checkbox'" + (((TaskListNode) node).isDone() ? " checked='checked'" : "") + "></input>");+ visitChildren(node);+ }+ printer.indent(-indent).printchkln(indent != 0).print("</li>")+ .printchkln(startWasNewLine);+ } else {+ printConditionallyIndentedTag(node, "li");+ }+ }++ }.toHtml(ast);}@Overridepublic String parseAndProcess(String markdown) {- String rawHtml = parse(markdown);- return process(rawHtml);+ return process(parse(markdown));}@Overridediff --git a/commons.util/src/main/java/com/pmease/commons/util/JsoupUtils.java b/commons.util/src/main/java/com/pmease/commons/util/JsoupUtils.javaindex 3933c2f2b9..9a69eb4116 100644--- a/commons.util/src/main/java/com/pmease/commons/util/JsoupUtils.java+++ b/commons.util/src/main/java/com/pmease/commons/util/JsoupUtils.java@@ -20,7 +20,7 @@ public class JsoupUtils {"sup", "sub", "p", "ol", "ul", "li", "table", "thead", "tbody", "tfoot","th", "tr", "td", "rt", "rp", "blockquote", "dl", "dt", "dd", "kbd", "q","hr", "strike", "caption", "cite", "col", "colgroup", "small",- "span", "u"+ "span", "u", "input"};private static final String[] SAFE_ATTRIBUTES = new String[] {@@ -40,7 +40,8 @@ public class JsoupUtils {"selected", "shape", "size", "span","start", "style", "summary", "tabindex", "target","title", "type", "usemap", "valign", "value",- "vspace", "width", "itemprop", "class"+ "vspace", "width", "itemprop", "class",+ "data-mdstart", "data-mdend"};private static final String[] SAFE_ANCHOR_SCHEMES = new String[] {diff --git a/gitplex.product/src/test/java/gitplex/product/Test.java b/gitplex.product/src/test/java/gitplex/product/Test.javaindex e04b2bf2a7..484ca18121 100644--- a/gitplex.product/src/test/java/gitplex/product/Test.java+++ b/gitplex.product/src/test/java/gitplex/product/Test.java@@ -2,9 +2,17 @@ package gitplex.product;import java.io.IOException;+import org.pegdown.Extensions;+import org.pegdown.PegDownProcessor;+public class Test {@org.junit.Testpublic void test() throws IOException {+ String markdown = ""+ + "* hello\n"+ + " * item1\n"+ + "* world";+ System.out.println(new PegDownProcessor(Extensions.ALL_WITH_OPTIONALS).markdownToHtml(markdown));}}\ No newline at end of file