0 | module Text.Markdown.Tokens
4 | import Data.String.Extra
5 | import Derive.Prelude
7 | import Text.Markdown.String
8 | import public Text.Token
11 | %language ElabReflection
14 | data MarkdownTokenKind
29 | | OpeningParenthesis
30 | | ClosingParenthesis
34 | %runElab derive "MarkdownTokenKind" [Eq, Show]
37 | MarkdownToken : Type
38 | MarkdownToken = Token MarkdownTokenKind
40 | %runElab derive "Token" [Eq, Show]
46 | dropEnds : Nat -> String -> String
47 | dropEnds n = drop n . dropLast n
53 | dropLast : List a -> List a
55 | dropLast (x :: []) = []
56 | dropLast (x :: xs) = x :: dropLast xs
59 | TokenKind MarkdownTokenKind where
60 | TokType BlankLine = ()
61 | TokType HeadingSym = Nat
62 | TokType UListSym = String
63 | TokType UListSepSym = String
64 | TokType MdText = String
65 | TokType MdPre = String
66 | TokType MdCodeBlock = (String, Maybe String)
67 | TokType MdHorizontalRules = ()
68 | TokType MdNewLine = ()
69 | TokType ItalicsSym = String
70 | TokType BoldSym = String
71 | TokType ImageStart = String
72 | TokType OpeningBracket = String
73 | TokType ClosingBracket = String
74 | TokType OpeningParenthesis = String
75 | TokType ClosingParenthesis = String
76 | TokType HtmlOpenTag = String
77 | TokType HtmlCloseTag = String
79 | tokValue BlankLine _ = ()
80 | tokValue HeadingSym x = length x
81 | tokValue UListSym txt = txt
82 | tokValue UListSepSym txt = txt
83 | tokValue MdText txt = txt
84 | tokValue MdPre txt = dropEnds 1 txt
85 | tokValue MdCodeBlock txt =
86 | let ls = lines txt in
87 | ( dropLast 1 $
unlines $
dropLast $
drop 1 ls
88 | , rtrim . drop 3 <$> head' ls
91 | tokValue MdHorizontalRules _ = ()
92 | tokValue MdNewLine _ = ()
93 | tokValue ItalicsSym txt = txt
94 | tokValue BoldSym txt = txt
95 | tokValue ImageStart txt = txt
96 | tokValue OpeningBracket txt = txt
97 | tokValue ClosingBracket txt = txt
98 | tokValue OpeningParenthesis txt = txt
99 | tokValue ClosingParenthesis txt = txt
100 | tokValue HtmlOpenTag tag = dropEnds 1 tag
101 | tokValue HtmlCloseTag tag = dropLast 1 (drop 2 tag)