feat: contents lexer working?
This commit is contained in:
parent
fcc3fdf969
commit
682f37e49c
30
inline.fnl
30
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,12 +44,13 @@
|
||||||
(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)]
|
||||||
|
(if (> found 1)
|
||||||
(table.insert lexed {:type "raw-text"
|
(table.insert lexed {:type "raw-text"
|
||||||
:contents (string.sub contents current (- found 1))})
|
:contents (string.sub contents current (- found 1))}))
|
||||||
(match char
|
(match char
|
||||||
; This would be better if we had a reverse lookup table
|
; This would be better if we had a reverse lookup table
|
||||||
"_" (let [(block new-end) ((. grammar :Italics) contents current)]
|
"_" (let [(block new-end) ((. grammar :Italics) contents current)]
|
||||||
|
@ -69,19 +70,24 @@
|
||||||
(set current new-end)))) ; Links
|
(set current new-end)))) ; Links
|
||||||
(do
|
(do
|
||||||
(table.insert lexed {:type "raw-text"
|
(table.insert lexed {:type "raw-text"
|
||||||
:contents contents})
|
:text (string.sub contents current (length contents))})
|
||||||
(set current (length contents))))) ; No decoration at all
|
(set current (length contents)))))) ; No decoration at all
|
||||||
lexed)))
|
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