hakyll-std/{TableOfContents,hakyll-std}: switch from string-based alignment to sum-type
This commit is contained in:
parent
6b576a7e2d
commit
40934fa72e
@ -7,6 +7,7 @@ module TableOfContents (
|
|||||||
tableOfContents,
|
tableOfContents,
|
||||||
ignoreTOC,
|
ignoreTOC,
|
||||||
collectHeaders,
|
collectHeaders,
|
||||||
|
TOCAlignment(TOCOff,TOCLeft,TOCRight)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Text.Pandoc
|
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 as H
|
||||||
import qualified Text.Blaze.Html5.Attributes as A
|
import qualified Text.Blaze.Html5.Attributes as A
|
||||||
|
|
||||||
|
data TOCAlignment = TOCOff | TOCLeft | TOCRight
|
||||||
|
|
||||||
headerLevel :: Block -> Int
|
headerLevel :: Block -> Int
|
||||||
headerLevel (Header level _ _) = level
|
headerLevel (Header level _ _) = level
|
||||||
headerLevel _ = error "not a header"
|
headerLevel _ = error "not a header"
|
||||||
@ -69,20 +72,18 @@ createTable headers =
|
|||||||
H.p "Contents"
|
H.p "Contents"
|
||||||
H.ol $ markupHeaders headers
|
H.ol $ markupHeaders headers
|
||||||
|
|
||||||
generateTOC :: [Block] -> String -> Block -> Block
|
generateTOC :: [Block] -> TOCAlignment -> Block -> Block
|
||||||
generateTOC [] _ x = x
|
generateTOC [] _ x = x
|
||||||
generateTOC headers alignment x@(BulletList (( (( Plain ((Str "toc"):_)):_)):_))
|
generateTOC headers alignment x@(BulletList (( (( Plain ((Str "toc"):_)):_)):_))
|
||||||
| alignment == "right" = render . (! A.class_ "right-toc") . table $ headers
|
= case alignment of
|
||||||
| alignment == "left" = render . table $ headers
|
TOCRight -> render . (! A.class_ "right-toc") . table $ headers
|
||||||
| otherwise = x
|
TOCLeft -> render . table $ headers
|
||||||
|
_ -> x
|
||||||
where render = (RawBlock "html") . renderHtml
|
where render = (RawBlock "html") . renderHtml
|
||||||
table = createTable . groupByHierarchy
|
table = createTable . groupByHierarchy
|
||||||
generateTOC _ _ x = x
|
generateTOC _ _ x = x
|
||||||
|
|
||||||
tableOfContents :: String -> Pandoc -> Pandoc
|
tableOfContents :: TOCAlignment -> Pandoc -> Pandoc
|
||||||
tableOfContents alignment ast =
|
tableOfContents TOCOff ast = walk ignoreTOC ast
|
||||||
if alignment /= "off"
|
tableOfContents alignment ast = let headers = query collectHeaders ast
|
||||||
then let headers = query collectHeaders ast
|
in walk (generateTOC headers alignment) ast
|
||||||
in walk (generateTOC headers alignment) ast
|
|
||||||
else walk ignoreTOC ast
|
|
||||||
|
|
||||||
|
|||||||
@ -53,7 +53,7 @@ import System.Process (system)
|
|||||||
-- import Text.Highlighting.Kate (pygments, kate, espresso, tango, haddock, monochrome, zenburn)
|
-- import Text.Highlighting.Kate (pygments, kate, espresso, tango, haddock, monochrome, zenburn)
|
||||||
import Text.Pandoc.Options
|
import Text.Pandoc.Options
|
||||||
|
|
||||||
import TableOfContents (tableOfContents)
|
import TableOfContents (tableOfContents, TOCAlignment(TOCRight))
|
||||||
|
|
||||||
import Debug.Trace
|
import Debug.Trace
|
||||||
strace :: Show a => a -> a
|
strace :: Show a => a -> a
|
||||||
@ -118,7 +118,7 @@ pandocWriterOptions = def
|
|||||||
-- ,writerHighlightStyle=tango
|
-- ,writerHighlightStyle=tango
|
||||||
--- }
|
--- }
|
||||||
|
|
||||||
pandocTransform = tableOfContents "right"
|
pandocTransform = tableOfContents TOCRight
|
||||||
|
|
||||||
main = do
|
main = do
|
||||||
args <- getArgs
|
args <- getArgs
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user