From d9f9c4b2685e12ba6537efc27f10c02a4c2a2ccd Mon Sep 17 00:00:00 2001 From: orosmatthew Date: Mon, 25 Sep 2023 16:29:43 -0400 Subject: [PATCH] Initial commit --- .gitignore | 29 ++++++++++ .idea/.gitignore | 8 +++ .idea/inspectionProfiles/Project_Default.xml | 6 ++ .idea/kotlinc.xml | 10 ++++ .idea/libraries/KotlinJavaRuntime.xml | 26 +++++++++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ markdown-parser.iml | 15 +++++ output.html | 32 ++++++++++ src/main/kotlin/CodeParser.kt | 3 + src/main/kotlin/Main.kt | 16 +++++ src/main/kotlin/MarkdownNode.kt | 49 ++++++++++++++++ src/main/kotlin/MarkdownParser.kt | 61 ++++++++++++++++++++ src/main/kotlin/Node.kt | 3 + test.md | 16 +++++ 16 files changed, 294 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/kotlinc.xml create mode 100644 .idea/libraries/KotlinJavaRuntime.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 markdown-parser.iml create mode 100644 output.html create mode 100644 src/main/kotlin/CodeParser.kt create mode 100644 src/main/kotlin/Main.kt create mode 100644 src/main/kotlin/MarkdownNode.kt create mode 100644 src/main/kotlin/MarkdownParser.kt create mode 100644 src/main/kotlin/Node.kt create mode 100644 test.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..df543e3 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..5cba058 --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/KotlinJavaRuntime.xml b/.idea/libraries/KotlinJavaRuntime.xml new file mode 100644 index 0000000..30d5a17 --- /dev/null +++ b/.idea/libraries/KotlinJavaRuntime.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..862d09b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..50d006d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/markdown-parser.iml b/markdown-parser.iml new file mode 100644 index 0000000..4eba30b --- /dev/null +++ b/markdown-parser.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/output.html b/output.html new file mode 100644 index 0000000..fd4583d --- /dev/null +++ b/output.html @@ -0,0 +1,32 @@ +

Heading 1

+ +
+ +

Heading 2

+ +
+ +

Heading 3

+ +
+ +This is some regular text *ITALICS*
+ +This is some bold text
+ +
+ +
+ +
+ +This is some italicized text
+ +
This is a block quote
+ +
+ +
+ +
+ diff --git a/src/main/kotlin/CodeParser.kt b/src/main/kotlin/CodeParser.kt new file mode 100644 index 0000000..220da59 --- /dev/null +++ b/src/main/kotlin/CodeParser.kt @@ -0,0 +1,3 @@ +interface CodeParser { + fun parseCode(filename: String): List +} \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt new file mode 100644 index 0000000..15f71c7 --- /dev/null +++ b/src/main/kotlin/Main.kt @@ -0,0 +1,16 @@ +import java.io.BufferedWriter +import java.io.FileWriter + +fun generateHtml(nodes: List) { + val writer = BufferedWriter(FileWriter("output.html")) + for (node in nodes) { + writer.appendLine(node.toHTML()) + } + writer.flush() +} + +fun main() { + val parser = MarkdownParser() + val nodes = parser.parseCode("test.md") + print(generateHtml(nodes)) +} \ No newline at end of file diff --git a/src/main/kotlin/MarkdownNode.kt b/src/main/kotlin/MarkdownNode.kt new file mode 100644 index 0000000..3844a25 --- /dev/null +++ b/src/main/kotlin/MarkdownNode.kt @@ -0,0 +1,49 @@ +enum class MarkdownNodeType { + HEADING_1, + HEADING_2, + HEADING_3, + TEXT, + BOLD, + ITALICS, + BLOCK_QUOTE, + LINE +} + +class MarkdownNode(private val type: MarkdownNodeType, text: String?) : Node(text) { + + override fun toHTML(): String { + when (type) { + MarkdownNodeType.HEADING_1 -> { + return "

${text}

\n" + } + + MarkdownNodeType.HEADING_2 -> { + return "

${text}

\n" + } + + MarkdownNodeType.HEADING_3 -> { + return "

${text}

\n" + } + + MarkdownNodeType.TEXT -> { + return "${text}
\n" + } + + MarkdownNodeType.BOLD -> { + return "${text}
\n" + } + + MarkdownNodeType.ITALICS -> { + return "${text}
\n" + } + + MarkdownNodeType.BLOCK_QUOTE -> { + return "
${text}
\n" + } + + MarkdownNodeType.LINE -> { + return "
\n" + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/MarkdownParser.kt b/src/main/kotlin/MarkdownParser.kt new file mode 100644 index 0000000..a672f85 --- /dev/null +++ b/src/main/kotlin/MarkdownParser.kt @@ -0,0 +1,61 @@ +import java.io.BufferedReader +import java.io.FileReader +import java.util.* + +class MarkdownParser : CodeParser { + private val regexHeading1 = Regex("#\\s+(.*)") + private val regexHeading2 = Regex("##\\s+(.*)") + private val regexHeading3 = Regex("###\\s+(.*)") + private val regexTextBold = Regex("\\*\\*(.*?)\\*\\*") + private val regexTextItalics = Regex("\\*(.*?)\\*") + private val regexBlockQuote = Regex(">\\s*(.*)") + private val regexLine = Regex("\\s*-\\s*-\\s*-.*") + + private fun parseHeadings(str: String): MarkdownNode? { + regexHeading3.matchEntire(str)?.let { + return MarkdownNode(MarkdownNodeType.HEADING_3, it.groups[1]?.value) + } + regexHeading2.matchEntire(str)?.let { + return MarkdownNode(MarkdownNodeType.HEADING_2, it.groups[1]?.value) + } + regexHeading1.matchEntire(str)?.let { + return MarkdownNode(MarkdownNodeType.HEADING_1, it.groups[1]?.value) + } + return null + } + + private fun parseText(str: String): MarkdownNode { + regexTextBold.matchEntire(str)?.let { + return MarkdownNode(MarkdownNodeType.BOLD, it.groups[1]?.value) + } + regexTextItalics.matchEntire(str)?.let { + return MarkdownNode(MarkdownNodeType.ITALICS, it.groups[1]?.value) + } + return MarkdownNode(MarkdownNodeType.TEXT, str) + } + + private fun parseLine(line: String): MarkdownNode { + parseHeadings(line)?.let { + return it + } + regexBlockQuote.matchEntire(line)?.let { + return MarkdownNode(MarkdownNodeType.BLOCK_QUOTE, it.groups[1]?.value) + } + regexLine.matchEntire(line)?.let { + return MarkdownNode(MarkdownNodeType.LINE, null) + } + return parseText(line) + } + + override fun parseCode(filename: String): List { + val reader = BufferedReader(FileReader(filename)) + var line: String? + val nodes = Vector() + while (reader.readLine().also { line = it } != null) { + line?.let { + nodes.add(parseLine(it)) + } + } + return nodes + } +} \ No newline at end of file diff --git a/src/main/kotlin/Node.kt b/src/main/kotlin/Node.kt new file mode 100644 index 0000000..f2f6963 --- /dev/null +++ b/src/main/kotlin/Node.kt @@ -0,0 +1,3 @@ +abstract class Node(val text: String?) { + abstract fun toHTML(): String +} \ No newline at end of file diff --git a/test.md b/test.md new file mode 100644 index 0000000..d0aea4e --- /dev/null +++ b/test.md @@ -0,0 +1,16 @@ +# Heading 1 + +## Heading 2 + +### Heading 3 + +This is some regular text *ITALICS* +**This is some bold text** + + + +*This is some italicized text* +> This is a block quote +- - - +- - - +- - -