Initial commit

This commit is contained in:
orosmatthew 2023-09-25 16:29:43 -04:00
commit d9f9c4b268
16 changed files with 294 additions and 0 deletions

29
.gitignore vendored Normal file
View File

@ -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

8
.idea/.gitignore vendored Normal file
View File

@ -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

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="ReplaceUntilWithRangeUntil" enabled="true" level="WEAK WARNING" enabled_by_default="true" />
</profile>
</component>

10
.idea/kotlinc.xml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Kotlin2JvmCompilerArguments">
<option name="jvmTarget" value="1.8" />
</component>
<component name="KotlinCommonCompilerArguments">
<option name="apiVersion" value="1.9" />
<option name="languageVersion" value="1.9" />
</component>
</project>

View File

@ -0,0 +1,26 @@
<component name="libraryTable">
<library name="KotlinJavaRuntime" type="repository">
<properties maven-id="org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.9.0" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.0/kotlin-stdlib-jdk8-1.9.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.0/kotlin-stdlib-1.9.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.0/kotlin-stdlib-common-1.9.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.0/kotlin-stdlib-jdk7-1.9.0.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.0/kotlin-stdlib-jdk8-1.9.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.0/kotlin-stdlib-1.9.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.0/kotlin-stdlib-common-1.9.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.0/kotlin-stdlib-jdk7-1.9.0-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.0/kotlin-stdlib-jdk8-1.9.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.0/kotlin-stdlib-1.9.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.0/kotlin-stdlib-common-1.9.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.0/kotlin-stdlib-jdk7-1.9.0-sources.jar!/" />
</SOURCES>
</library>
</component>

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_20" default="true" project-jdk-name="20" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/markdown-parser.iml" filepath="$PROJECT_DIR$/markdown-parser.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

15
markdown-parser.iml Normal file
View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/kotlin" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/kotlin" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
</component>
</module>

32
output.html Normal file
View File

@ -0,0 +1,32 @@
<h1>Heading 1</h1>
<br />
<h2>Heading 2</h2>
<br />
<h3>Heading 3</h3>
<br />
This is some regular text *ITALICS*<br />
<b>This is some bold text</b><br />
<br />
<br />
<br />
<i>This is some italicized text</i><br />
<blockquote>This is a block quote</blockquote>
<hr />
<hr />
<hr />

View File

@ -0,0 +1,3 @@
interface CodeParser {
fun parseCode(filename: String): List<Node>
}

16
src/main/kotlin/Main.kt Normal file
View File

@ -0,0 +1,16 @@
import java.io.BufferedWriter
import java.io.FileWriter
fun generateHtml(nodes: List<MarkdownNode>) {
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))
}

View File

@ -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 "<h1>${text}</h1>\n"
}
MarkdownNodeType.HEADING_2 -> {
return "<h2>${text}</h2>\n"
}
MarkdownNodeType.HEADING_3 -> {
return "<h3>${text}</h3>\n"
}
MarkdownNodeType.TEXT -> {
return "${text}<br />\n"
}
MarkdownNodeType.BOLD -> {
return "<b>${text}</b><br />\n"
}
MarkdownNodeType.ITALICS -> {
return "<i>${text}</i><br />\n"
}
MarkdownNodeType.BLOCK_QUOTE -> {
return "<blockquote>${text}</blockquote>\n"
}
MarkdownNodeType.LINE -> {
return "<hr />\n"
}
}
}
}

View File

@ -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<MarkdownNode> {
val reader = BufferedReader(FileReader(filename))
var line: String?
val nodes = Vector<MarkdownNode>()
while (reader.readLine().also { line = it } != null) {
line?.let {
nodes.add(parseLine(it))
}
}
return nodes
}
}

3
src/main/kotlin/Node.kt Normal file
View File

@ -0,0 +1,3 @@
abstract class Node(val text: String?) {
abstract fun toHTML(): String
}

16
test.md Normal file
View File

@ -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
- - -
- - -
- - -