MTable.hs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. module MTable (plugin) where
  2. -- This plugin ideally parses a less irritating table syntax
  3. -- for gitit.
  4. --
  5. -- ~~~ {.mtable}
  6. -- | foo | bar | bop |
  7. -- | and | so | on |
  8. -- ~~~
  9. --
  10. -- Unfortuantely it still can't do colspan
  11. --
  12. -- Simon Heath <icefoxen@gmail.com> 2013
  13. import Data.Default
  14. import Data.List.Split
  15. import Text.Pandoc (readMarkdown)
  16. import Network.Gitit.Interface
  17. import System.IO.Unsafe
  18. plugin :: Plugin
  19. plugin = mkPageTransformM parseTableBlock
  20. parseTableBlock :: Block -> PluginM Block
  21. parseTableBlock (CodeBlock (_, classes, namevals) contents) | "mtable" `elem` classes = do
  22. let lineses = lines contents
  23. splittedLines = map (splitOn "|") lineses
  24. splittedLinesWithEmptiesRemoved = map (filter ((/=) "")) splittedLines
  25. parseCell x =
  26. let res = readMarkdown def x in
  27. case res of
  28. Left _ -> [Para $ [Str "Error parsing markdown in cell?"]]
  29. Right (Pandoc _ blk) -> blk
  30. cellify line = map (\cell -> parseCell cell) line
  31. cells = map cellify splittedLinesWithEmptiesRemoved
  32. alignments = replicate (length (head cells)) AlignDefault
  33. return $ Table [] alignments [] [] cells --(head cells) (tail cells)
  34. parseTableBlock x = return x