feat: contents lexer working?
This commit is contained in:
parent
fcc3fdf969
commit
682f37e49c
76
inline.fnl
76
inline.fnl
|
@ -6,8 +6,8 @@
|
||||||
(let [(start end contents) (string.find str pattern current)]
|
(let [(start end contents) (string.find str pattern current)]
|
||||||
(values
|
(values
|
||||||
{:type typ
|
{:type typ
|
||||||
:contents contents}
|
:text contents}
|
||||||
end))))
|
(+ 1 end)))))
|
||||||
|
|
||||||
(local contents-grammar
|
(local contents-grammar
|
||||||
{:match-first "[%*%_%~%[%>%`]"
|
{:match-first "[%*%_%~%[%>%`]"
|
||||||
|
@ -44,44 +44,50 @@
|
||||||
(let [grammar (or ?grammar contents-grammar)]
|
(let [grammar (or ?grammar contents-grammar)]
|
||||||
(var current 1)
|
(var current 1)
|
||||||
(var lexed [])
|
(var lexed [])
|
||||||
(while (> (length contents) current)
|
(while (and (not (= nil current)) (> (length contents) current))
|
||||||
(let [found (string.find contents (. grammar :match-first) current)]
|
(let [found (string.find contents (. grammar :match-first) current)]
|
||||||
(if (not (= nil found))
|
(if (not (= nil found))
|
||||||
(let [char (string.sub contents found found)]
|
(let [char (string.sub contents found found)]
|
||||||
(table.insert lexed {:type "raw-text"
|
(if (> found 1)
|
||||||
:contents (string.sub contents current (- found 1))})
|
(table.insert lexed {:type "raw-text"
|
||||||
(match char
|
:contents (string.sub contents current (- found 1))}))
|
||||||
; This would be better if we had a reverse lookup table
|
(match char
|
||||||
"_" (let [(block new-end) ((. grammar :Italics) contents current)]
|
; This would be better if we had a reverse lookup table
|
||||||
(table.insert lexed block)
|
"_" (let [(block new-end) ((. grammar :Italics) contents current)]
|
||||||
(set current new-end)) ; Italics
|
(table.insert lexed block)
|
||||||
"*" (let [(block new-end) ((. grammar :Bold) contents current)]
|
(set current new-end)) ; Italics
|
||||||
(table.insert lexed block)
|
"*" (let [(block new-end) ((. grammar :Bold) contents current)]
|
||||||
(set current new-end)) ; Bold
|
(table.insert lexed block)
|
||||||
"~" (let [(block new-end) ((. grammar :Strikethrough) contents current)]
|
(set current new-end)) ; Bold
|
||||||
(table.insert lexed block)
|
"~" (let [(block new-end) ((. grammar :Strikethrough) contents current)]
|
||||||
(set current new-end)) ; Strikethrough
|
(table.insert lexed block)
|
||||||
"`" (let [(block new-end) ((. grammar :Monospace) contents current)]
|
(set current new-end)) ; Strikethrough
|
||||||
(table.insert lexed block)
|
"`" (let [(block new-end) ((. grammar :Monospace) contents current)]
|
||||||
(set current new-end)); Monospace
|
(table.insert lexed block)
|
||||||
"[" (let [(block new-end) ((. grammar :Link) contents current)]
|
(set current new-end)); Monospace
|
||||||
(table.insert lexed block)
|
"[" (let [(block new-end) ((. grammar :Link) contents current)]
|
||||||
(set current new-end)))) ; Links
|
(table.insert lexed block)
|
||||||
(do
|
(set current new-end)))) ; Links
|
||||||
(table.insert lexed {:type "raw-text"
|
(do
|
||||||
:contents contents})
|
(table.insert lexed {:type "raw-text"
|
||||||
(set current (length contents))))) ; No decoration at all
|
:text (string.sub contents current (length contents))})
|
||||||
lexed)))
|
(set current (length contents)))))) ; No decoration at all
|
||||||
|
lexed))
|
||||||
; ">" (let [(block new-end) ((. grammar :Spoiler) contents current)]
|
; ">" (let [(block new-end) ((. grammar :Spoiler) contents current)]
|
||||||
; (table.insert lexed block)
|
; (table.insert lexed block)
|
||||||
; (set current new-end))))
|
; (set current new-end))))
|
||||||
|
|
||||||
|
; (string.match "*This* other thing" "%*(.*)%*")
|
||||||
|
; (general-contents-fn "%*(.-)%*" "Bold" "*This* other thing" 1)
|
||||||
|
; ((. contents-grammar :Bold) "*This* other thing" 1)
|
||||||
|
; (contents-lexer "*This* is a paragraph.")
|
||||||
|
|
||||||
(fn list-rule [str ?regex]
|
(fn list-rule [str ?regex]
|
||||||
(let [regex (or ?regex "(%s*)%-%s*(.-)\n")]
|
(let [regex (or ?regex "(%s*)%-%s*(.-)\n")]
|
||||||
(var list-elems [])
|
(var list-elems [])
|
||||||
(each [indents line (string.gmatch str regex)]
|
(each [indents line (string.gmatch str regex)]
|
||||||
(table.insert list-elems {:indent-level (length indents)
|
(table.insert list-elems {:indent-level (length indents)
|
||||||
:contents line}))
|
:contents (contents-lexer line)}))
|
||||||
{:type "List"
|
{:type "List"
|
||||||
:elements list-elems}))
|
:elements list-elems}))
|
||||||
|
|
||||||
|
@ -103,12 +109,12 @@
|
||||||
(header-size contents) (string.match str regex)]
|
(header-size contents) (string.match str regex)]
|
||||||
{:type "Header"
|
{:type "Header"
|
||||||
:size (- 4 (length header-size))
|
:size (- 4 (length header-size))
|
||||||
: contents}))}
|
:contents (contents-lexer contents)}))}
|
||||||
:Image {:rule (fn image-rule [str ?regex]
|
:Image {:rule (fn image-rule [str ?regex]
|
||||||
(let [regex (or ?regex "%!%[(.-)%]%((.-)%)\n")
|
(let [regex (or ?regex "%!%[(.-)%]%((.-)%)\n")
|
||||||
(alt link) (string.match str regex)]
|
(alt link) (string.match str regex)]
|
||||||
{:type "Image"
|
{:type "Image"
|
||||||
:contents alt
|
:contents (contents-lexer alt)
|
||||||
: link}))}
|
: link}))}
|
||||||
:List {:rule list-rule}
|
:List {:rule list-rule}
|
||||||
:Footnote {:rule (fn footnote-rule [str ?regex]
|
:Footnote {:rule (fn footnote-rule [str ?regex]
|
||||||
|
@ -129,10 +135,10 @@
|
||||||
(each [m (string.gmatch str regex)]
|
(each [m (string.gmatch str regex)]
|
||||||
(set total (.. total m "\n"))))
|
(set total (.. total m "\n"))))
|
||||||
{:type "Quote"
|
{:type "Quote"
|
||||||
:contents total})}
|
:contents (contents-lexer total)})}
|
||||||
:Paragraph {:rule (fn paragraph-rule [str]
|
:Paragraph {:rule (fn paragraph-rule [str]
|
||||||
{:type "Paragraph"
|
{:type "Paragraph"
|
||||||
:contents str})}})
|
:contents (contents-lexer str)})}})
|
||||||
|
|
||||||
; TODO
|
; TODO
|
||||||
; All :contents entries must go through the contents parser
|
; All :contents entries must go through the contents parser
|
||||||
|
|
Loading…
Reference in a new issue