about summary refs log tree commit diff stats
path: root/adapter
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-03-12 16:38:03 +0100
committerbptato <nincsnevem662@gmail.com>2024-03-12 17:03:50 +0100
commit232d861836993d81f7828a2917e8d242a23194e0 (patch)
treef7de06f515265b2d177e534e5b7ced2814dfccea /adapter
parentc152009ed38a53f68cddef60d9130b117f319b52 (diff)
downloadchawan-232d861836993d81f7828a2917e8d242a23194e0.tar.gz
md2html: convert <https://example.org> to anchor
Diffstat (limited to 'adapter')
-rw-r--r--adapter/format/md2html.nim33
1 files changed, 32 insertions, 1 deletions
diff --git a/adapter/format/md2html.nim b/adapter/format/md2html.nim
index 2c72634e..5fb9bb9c 100644
--- a/adapter/format/md2html.nim
+++ b/adapter/format/md2html.nim
@@ -8,7 +8,8 @@ proc toggle[T](s: var set[T], t: T): bool =
     s.excl(t)
 
 type BracketState = enum
-  bsNone, bsInBracketRef, bsInBracket, bsAfterBracket, bsInParen, bsInImage
+  bsNone, bsInBracketRef, bsInBracket, bsAfterBracket, bsInParen, bsInImage,
+  bsInTag
 
 proc getId(line: openArray[char]): string =
   result = ""
@@ -48,6 +49,14 @@ type InlineState = enum
   isItalic, isBold, isCode, isComment, isDel
 
 const AsciiAlphaNumeric = {'0'..'9', 'A'..'Z', 'a'..'z'}
+func startsWithScheme(s: string): bool =
+  for i, c in s:
+    if i > 0 and c == ':':
+      return true
+    if c notin AsciiAlphaNumeric:
+      break
+  false
+
 proc parseInline(line: openArray[char]) =
   var state: set[InlineState] = {}
   var bs = bsNone
@@ -76,6 +85,25 @@ proc parseInline(line: openArray[char]) =
         i += 2
       else:
         append c
+    elif bs == bsInTag:
+      if c == '>': # done
+        if bracketChars.startsWithScheme(): # link
+          var linkChars = ""
+          for c in bracketChars:
+            if c == '\'':
+              linkChars &= "&apos"
+            else:
+              linkChars &= c
+          stdout.write("<A HREF='" & linkChars & "'>" & bracketChars & "</A>")
+        else: # tag
+          stdout.write('<' & bracketChars & '>')
+        bracketChars = ""
+        bs = bsNone
+      elif c == '<':
+        stdout.write('<' & bracketChars)
+        bracketChars = ""
+      else:
+        bracketChars &= c
     elif isCode in state:
       case c
       of '<': append "&lt;"
@@ -141,6 +169,9 @@ proc parseInline(line: openArray[char]) =
       bs = bsNone
     elif c == '\'' and bs == bsInParen:
       stdout.write("&apos;")
+    elif c == '<' and bs == bsNone:
+      bs = bsInTag
+      bracketChars = ""
     elif i + 4 < line.len and line.toOpenArray(i, i + 3) == "<!--":
       append "<!--"
       i += 3