about summary refs log tree commit diff stats
path: root/adapter/format/md2html.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-19 13:57:06 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-19 13:57:06 +0100
commit3c7528f3039fe068ce8760a5878e7c0916cfef7c (patch)
tree3d29f08cd6963ebfa22148dfa638bd67cc205ee1 /adapter/format/md2html.nim
parenta0e98329d8dd2dc5492d0cb9d902d67ab7a94cdc (diff)
downloadchawan-3c7528f3039fe068ce8760a5878e7c0916cfef7c.tar.gz
md2html: add indentation-based code blocks
Diffstat (limited to 'adapter/format/md2html.nim')
-rw-r--r--adapter/format/md2html.nim54
1 files changed, 52 insertions, 2 deletions
diff --git a/adapter/format/md2html.nim b/adapter/format/md2html.nim
index 8e623585..25c9cf6b 100644
--- a/adapter/format/md2html.nim
+++ b/adapter/format/md2html.nim
@@ -240,7 +240,8 @@ proc matchHTMLPreEnd(line: string): bool =
 
 type
   BlockType = enum
-    btNone, btPar, btList, btPre, btHTML, btHTMLPre, btComment
+    btNone, btPar, btList, btPre, btTabPre, btSpacePre, btHTML, btHTMLPre,
+    btComment
 
   ParseState = object
     blockType: BlockType
@@ -249,6 +250,7 @@ type
     lists: seq[ListType]
     hasp: bool
     reprocess: bool
+    numPreLines: int
 
 proc pushList(state: var ParseState, t: ListType) =
   case t
@@ -272,9 +274,23 @@ proc parseNone(state: var ParseState, line: string) =
   elif line[0] == '<' and line.find('>') == line.high:
     state.blockType = if line.matchHTMLPreStart(): btHTMLPre else: btHTML
     state.reprocess = true
-  elif line.len >= 3 and line.startsWith("```"):
+  elif line.startsWith("```"):
     state.blockType = btPre
     stdout.write("<PRE>")
+  elif line.startsWith("    "):
+    state.blockType = btSpacePre
+    if state.hasp:
+      state.hasp = false
+      stdout.write("</P>\n")
+    stdout.write("<PRE>")
+    state.blockData = line.substr(4) & "\n"
+  elif line.startsWith("\t"):
+    state.blockType = btTabPre
+    if state.hasp:
+      state.hasp = false
+      stdout.write("</P>\n")
+    stdout.write("<PRE>")
+    state.blockData = line.substr(1) & "\n"
   elif (let (n, len, t) = line.getListDepth(); n != -1):
     state.blockType = btList
     state.listDepth = n
@@ -361,6 +377,38 @@ proc parseHTMLPre(state: var ParseState, line: string) =
   else:
     state.blockData &= line & "\n"
 
+proc parseTabPre(state: var ParseState, line: string) =
+  if line.len == 0:
+    inc state.numPreLines
+  elif line[0] != '\t':
+    state.numPreLines = 0
+    stdout.write(state.blockData)
+    stdout.write("</PRE>")
+    state.blockData = ""
+    state.reprocess = true
+    state.blockType = btNone
+  else:
+    while state.numPreLines > 0:
+      state.blockData &= '\n'
+      dec state.numPreLines
+    state.blockData &= line.substr(1) & "\n"
+
+proc parseSpacePre(state: var ParseState, line: string) =
+  if line.len == 0:
+    inc state.numPreLines
+  elif not line.startsWith("    "):
+    state.numPreLines = 0
+    stdout.write(state.blockData)
+    stdout.write("</PRE>")
+    state.blockData = ""
+    state.reprocess = true
+    state.blockType = btNone
+  else:
+    while state.numPreLines > 0:
+      state.blockData &= '\n'
+      dec state.numPreLines
+    state.blockData &= line.substr(4) & "\n"
+
 proc parseComment(state: var ParseState, line: string) =
   let i = line.find("-->")
   if i != -1:
@@ -378,6 +426,8 @@ proc main() =
     case state.blockType
     of btNone: state.parseNone(line)
     of btPre: state.parsePre(line)
+    of btTabPre: state.parseTabPre(line)
+    of btSpacePre: state.parseSpacePre(line)
     of btList: state.parseList(line)
     of btPar: state.parsePar(line)
     of btHTML: state.parseHTML(line)