Skip to content

Instantly share code, notes, and snippets.

@mpickering
Created June 20, 2015 17:23
Show Gist options
  • Save mpickering/4e3fa135d9e5a908688d to your computer and use it in GitHub Desktop.
Save mpickering/4e3fa135d9e5a908688d to your computer and use it in GitHub Desktop.
filter
module Main where
import Text.Pandoc.JSON
import Text.Pandoc.Walk
import Text.Pandoc.Generic
import Text.Pandoc.Definition
main = toJSONFilter (listFilter . breaksFilter)
breaksFilter :: Pandoc -> Pandoc
breaksFilter = bottomUp (concatMap hardBreaks)
listFilter :: Pandoc -> Pandoc
listFilter (Pandoc m bs) = Pandoc m (map (toList 0) bs)
hardBreaks :: Block -> [Block]
hardBreaks (Para ys) = findBreaks [] ys
hardBreaks (Plain ys) = findBreaks [] ys
hardBreaks x = [x]
findBreaks :: [Inline] -> [Inline] -> [Block]
findBreaks [] [] = []
findBreaks acc [] = [Para (reverse acc)]
findBreaks acc (LineBreak : ys) = Para (reverse acc) : findBreaks [] ys
findBreaks acc (x:xs) = findBreaks (x:acc) xs
toList :: Int -> Block -> Block
toList depth (BulletList xs) = BulletList (map (makeHeaders (depth + 1)) xs)
toList depth (OrderedList opts xs) = OrderedList opts (map (makeHeaders (depth + 1)) xs)
toList depth e = e
makeHeaders :: Int -> [Block] -> [Block]
makeHeaders d (Para is:xs) = Header d nullAttr is : map (toList d) xs
makeHeaders d (Plain is:xs) = Header d nullAttr is : map (toList d) xs
makeHeaders d xs = map (toList d) xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment