From 2e3dc238275073a5c7a2e5a14c79337d12492dad Mon Sep 17 00:00:00 2001 From: Ilya Ryzhenkov Date: Fri, 26 Sep 2014 21:04:33 +0400 Subject: Continue markdown processing --- src/Markdown/MarkdownProcessor.kt | 124 +++++++++++++++++++++++++++++++------- 1 file changed, 102 insertions(+), 22 deletions(-) (limited to 'src/Markdown/MarkdownProcessor.kt') diff --git a/src/Markdown/MarkdownProcessor.kt b/src/Markdown/MarkdownProcessor.kt index 4453e750..fe6e8436 100644 --- a/src/Markdown/MarkdownProcessor.kt +++ b/src/Markdown/MarkdownProcessor.kt @@ -26,31 +26,111 @@ public class MarkdownProcessor { } public class MarkdownTree(private val text: String, private val structure: FlyweightCapableTreeStructure) { - public fun dump(): String { - val sb = StringBuilder() - visit(sb, "", structure.getRoot(), structure, text) - return sb.toString() + fun visit(action: (LighterASTNode, String, visitChildren: () -> Unit) -> Unit) { + visit(structure.getRoot(), action) + } + + fun visit(node: LighterASTNode, action: (LighterASTNode, String, visitChildren: () -> Unit) -> Unit) { + action(node, text) { + val ref = Ref.create?>() + val count = structure.getChildren(node, ref) + val children = ref.get() + if (children != null) { + for (index in 0..count - 1) { + val child = children[index] + visit(child, action) + } + } + } } -} -fun markdownToHtml(markdown : String) : String { - return MarkdownProcessor().parse(markdown).dump() } +public fun MarkdownTree.dump(): String { + val sb = StringBuilder() + var level = 0 + visit {(node, text, visitChildren) -> + val nodeText = text.substring(node.getStartOffset(), node.getEndOffset()) + sb.append(" ".repeat(level * 2)) + sb.append(node.getTokenType().toString()) + sb.append(":" + nodeText.replace("\n", "\u23CE")) + sb.appendln() + level++ + visitChildren() + level-- + } + return sb.toString() +} -fun visit(sb: StringBuilder, indent: String, node: LighterASTNode, structure: FlyweightCapableTreeStructure, markdown: String) { - sb.append(indent) - sb.append(node.getTokenType().toString()) - val nodeText = markdown.substring(node.getStartOffset(), node.getEndOffset()) - sb.append(":" + nodeText.replace("\n","\u23CE")) - sb.appendln() - val ref = Ref.create?>() - val count = structure.getChildren(node, ref) - val children = ref.get() - if (children == null) - return - for (index in 0..count - 1) { - val child = children[index] - visit(sb, indent + " ", child, structure, markdown) +public fun MarkdownTree.toHtml(): String { + val sb = StringBuilder() + var level = 0 + visit {(node, text, processChildren) -> + val nodeType = node.getTokenType() + val nodeText = text.substring(node.getStartOffset(), node.getEndOffset()) + val indent = " ".repeat(level * 2) + when (nodeType) { + MarkdownElementTypes.BULLET_LIST -> { + sb.appendln("$indent
    ") + level++ + processChildren() + level-- + sb.appendln("$indent
") + } + MarkdownElementTypes.HORIZONTAL_RULE -> { + sb.appendln("$indent
") + } + MarkdownElementTypes.ORDERED_LIST -> { + sb.appendln("$indent
    ") + processChildren() + sb.appendln("$indent
") + } + MarkdownElementTypes.LIST_BLOCK -> { + sb.append("$indent
  • ") + processChildren() + sb.appendln("
  • ") + } + MarkdownElementTypes.EMPH -> { + sb.append("") + processChildren() + sb.append("") + } + MarkdownElementTypes.STRONG -> { + sb.append("") + processChildren() + sb.append("") + } + MarkdownElementTypes.PLAIN_TEXT -> { + sb.append(nodeText) + } + MarkdownElementTypes.END_LINE -> { + sb.appendln() + } + MarkdownElementTypes.BLANK_LINE -> { + sb.appendln() + } + MarkdownElementTypes.PARA -> { + sb.appendln("$indent

    ") + processChildren() + sb.appendln("$indent

    ") + } + MarkdownElementTypes.VERBATIM -> { + sb.appendln("$indent
    ")
    +                processChildren()
    +                sb.appendln("$indent
    ") + } + else -> { + processChildren() + } + } } -} \ No newline at end of file + return sb.toString() +} + + +fun markdownToHtml(markdown: String): String { + val markdownTree = MarkdownProcessor().parse(markdown) + val ast = markdownTree.dump() + return markdownTree.toHtml() +} + -- cgit