psb/app/Markdown.hs

50 lines
1.2 KiB
Haskell

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
module Markdown (markdownParser) where
import Data.Text
import IR
import Text.Parsec
import Text.Parsec.Combinator
type Parser a = forall s u m. (Stream s m Char) => ParsecT s u m a
markdownParser :: Parser Document
markdownParser = Doc <$> many block
block :: Parser Element
block = choice [heading, codeBlock, quoteBlock, list, table, htmlBlock, paragraph, blankLine]
heading :: Parser Element
heading = pure $ Heading $ H {level = 1, text = ""}
codeBlock :: Parser Element
codeBlock = pure $ Code $ C {language = "", code = ""}
quoteBlock :: Parser Element
quoteBlock = pure $ BlockQuote $ Q ""
list :: Parser Element
list = pure $ List $ L {list_type = Ordered, items = []}
table :: Parser Element
table = pure $ Table $ T {header = TH [], rows = []}
htmlBlock :: Parser Element
htmlBlock = pure $ HTML $ Raw ""
paragraph :: Parser Element
paragraph = do
first_text <- inlineText
rem_text <- many (endOfLine >> inlineText)
pure $ Paragraph $ P []
inlineText :: Parser InlineText
inlineText = choi
blankline :: Parser Element
blankline = do
endOfLine
pure $ BlankLine BL