hakyll-std/{TableOfContents,hakyll-std}: switch from string-based alignment to sum-type

This commit is contained in:
Everett Hildenbrandt 2018-04-27 02:40:26 -06:00 committed by Simon Michael
parent 6b576a7e2d
commit 40934fa72e
2 changed files with 14 additions and 13 deletions

View File

@ -7,6 +7,7 @@ module TableOfContents (
tableOfContents,
ignoreTOC,
collectHeaders,
TOCAlignment(TOCOff,TOCLeft,TOCRight)
) where
import Text.Pandoc
@ -28,6 +29,8 @@ import Text.Blaze.Html.Renderer.String (renderHtml)
import qualified Text.Blaze.Html5 as H
import qualified Text.Blaze.Html5.Attributes as A
data TOCAlignment = TOCOff | TOCLeft | TOCRight
headerLevel :: Block -> Int
headerLevel (Header level _ _) = level
headerLevel _ = error "not a header"
@ -69,20 +72,18 @@ createTable headers =
H.p "Contents"
H.ol $ markupHeaders headers
generateTOC :: [Block] -> String -> Block -> Block
generateTOC :: [Block] -> TOCAlignment -> Block -> Block
generateTOC [] _ x = x
generateTOC headers alignment x@(BulletList (( (( Plain ((Str "toc"):_)):_)):_))
| alignment == "right" = render . (! A.class_ "right-toc") . table $ headers
| alignment == "left" = render . table $ headers
| otherwise = x
= case alignment of
TOCRight -> render . (! A.class_ "right-toc") . table $ headers
TOCLeft -> render . table $ headers
_ -> x
where render = (RawBlock "html") . renderHtml
table = createTable . groupByHierarchy
generateTOC _ _ x = x
tableOfContents :: String -> Pandoc -> Pandoc
tableOfContents alignment ast =
if alignment /= "off"
then let headers = query collectHeaders ast
in walk (generateTOC headers alignment) ast
else walk ignoreTOC ast
tableOfContents :: TOCAlignment -> Pandoc -> Pandoc
tableOfContents TOCOff ast = walk ignoreTOC ast
tableOfContents alignment ast = let headers = query collectHeaders ast
in walk (generateTOC headers alignment) ast

View File

@ -53,7 +53,7 @@ import System.Process (system)
-- import Text.Highlighting.Kate (pygments, kate, espresso, tango, haddock, monochrome, zenburn)
import Text.Pandoc.Options
import TableOfContents (tableOfContents)
import TableOfContents (tableOfContents, TOCAlignment(TOCRight))
import Debug.Trace
strace :: Show a => a -> a
@ -118,7 +118,7 @@ pandocWriterOptions = def
-- ,writerHighlightStyle=tango
--- }
pandocTransform = tableOfContents "right"
pandocTransform = tableOfContents TOCRight
main = do
args <- getArgs