{-# LANGUAGE OverloadedStrings #-} module Gitea.Preview.Markdown.Meta (renderMeta, renderMeta') where import Data.List (singleton) import Data.Map (toList) import Text.Pandoc.Definition renderMeta' :: Pandoc -> Pandoc renderMeta' (Pandoc meta blocks) = Pandoc meta (renderMeta (MetaMap $ unMeta meta) <> blocks) renderMeta :: MetaValue -> [Block] renderMeta (MetaMap metaMap) = singleton $ plainTable (map (singleton . Plain . singleton . Str . fst)) (singleton . plainRow . map (renderMeta . snd)) length (toList metaMap) renderMeta (MetaList metaValues) = singleton $ plainTable (const []) (map (plainRow . singleton . renderMeta)) (const 1) metaValues renderMeta (MetaBool bool) = [Plain [Str $ if bool then "true" else "false"]] renderMeta (MetaString text) = [Plain [Str text]] renderMeta (MetaInlines inlines) = [Plain inlines] renderMeta (MetaBlocks blocks) = blocks plainTable :: ([a] -> [[Block]]) -> ([a] -> [Row]) -> ([a] -> Int) -> [a] -> Block plainTable mkHead mkBody columns xs = Table nullAttr (Caption Nothing []) (replicate (columns xs) (AlignDefault, ColWidthDefault)) (plainHead $ mkHead xs) [plainBody $ mkBody xs] (TableFoot nullAttr []) plainCell :: [Block] -> Cell plainCell = Cell nullAttr AlignDefault (RowSpan 1) (ColSpan 1) plainRow :: [[Block]] -> Row plainRow = Row nullAttr . map plainCell plainHead :: [[Block]] -> TableHead plainHead = TableHead nullAttr . singleton . plainRow plainBody :: [Row] -> TableBody plainBody = TableBody nullAttr (RowHeadColumns 0) []