Finally added TOC generation

This commit is contained in:
Yann Esposito (Yogsototh) 2020-08-23 14:06:08 +02:00
parent 15e6289220
commit 2d8c04894d
Signed by untrusted user who does not match committer: yogsototh
GPG key ID: 7B19A4C650D59646
2 changed files with 35 additions and 9 deletions

View file

@ -4,15 +4,13 @@
import Protolude import Protolude
import Development.Shake import Development.Shake
-- import Development.Shake.Command
import Development.Shake.FilePath import Development.Shake.FilePath
import Data.Time.Format.ISO8601 (iso8601Show) import Data.Time.Format.ISO8601 (iso8601Show)
import qualified Data.Time.Clock as Clock import qualified Data.Time.Clock as Clock
import Control.Monad.Fail import Control.Monad.Fail
import Data.Aeson import Data.Aeson
-- import qualified Text.Megaparsec as Megaparsec
import Data.Default ( Default(def) ) import Data.Default ( Default(def) )
import qualified Data.Text as T import qualified Data.Text as T
import Text.Mustache import Text.Mustache
@ -35,8 +33,9 @@ import Text.Pandoc.Options ( ReaderOptions(..)
) )
import qualified Text.Pandoc.Readers as Readers import qualified Text.Pandoc.Readers as Readers
import Text.Pandoc.Walk (Walkable(..)) import Text.Pandoc.Walk (Walkable(..))
import qualified Text.Pandoc.Writers as Writers import qualified Text.Pandoc.Writers as Writers
import qualified Text.Pandoc.Templates as Templates
main :: IO () main :: IO ()
main = shakeArgs shOpts buildRules main = shakeArgs shOpts buildRules
@ -220,7 +219,8 @@ orgContentToText org = do
pandoc <- case eitherResult of pandoc <- case eitherResult of
Left _ -> fail "BAD" Left _ -> fail "BAD"
Right p -> return p Right p -> return p
eitherHtml <- liftIO $ Pandoc.runIO $ Writers.writeHtml5String (def {writerEmailObfuscation = ReferenceObfuscation}) pandoc eitherHtml <- liftIO $ Pandoc.runIO $
Writers.writeHtml5String (def {writerEmailObfuscation = ReferenceObfuscation}) pandoc
case eitherHtml of case eitherHtml of
Left _ -> fail "BAD" Left _ -> fail "BAD"
Right innerHtml -> return innerHtml Right innerHtml -> return innerHtml
@ -242,13 +242,35 @@ postamble now bp =
, "@@html:</footer>@@" , "@@html:</footer>@@"
] ]
tpltxt = T.unlines [
"$if(toc)$"
, "<nav id=\"$idprefix$TOC\" role=\"doc-toc\">"
, "$if(toc-title)$"
, "<h2 id=\"$idprefix$toc-title\">$toc-title$</h2>"
, "$endif$"
, "$table-of-contents$"
, "</nav>"
, "$endif$"
, "$body$"
]
getPostTpl :: IO (Templates.Template Text)
getPostTpl = do
etpl <- Templates.compileTemplate "blog.template" tpltxt
case etpl of
Left e -> fail e
Right tpl -> return tpl
genHtml :: (MonadIO m, MonadFail m) => BlogPost -> m Text genHtml :: (MonadIO m, MonadFail m) => BlogPost -> m Text
genHtml bp = do genHtml bp = do
let htmlBody = replaceLinks (postBody bp) let htmlBody = replaceLinks (postBody bp)
eitherHtml <- liftIO $ eitherHtml <- liftIO $ do
Pandoc.runIO $ tpl <- getPostTpl
Pandoc.runIO $ do
Writers.writeHtml5String Writers.writeHtml5String
(def { writerTableOfContents = postToc bp (def { writerTableOfContents = postToc bp
, writerTemplate = Just tpl
, writerTOCDepth = 3
, writerEmailObfuscation = ReferenceObfuscation , writerEmailObfuscation = ReferenceObfuscation
, writerHTMLMathMethod = MathML , writerHTMLMathMethod = MathML
}) })
@ -262,7 +284,7 @@ genHtml bp = do
origin :: Text origin :: Text
origin = "https://her.esy.fun" origin = "https://her.esy.fun"
genHtmlAction genHtmlAction
:: (FilePath -> Action BlogPost) :: (FilePath -> Action BlogPost)
-> (FilePath -> Action Template) -> [Char] -> Action () -> (FilePath -> Action Template) -> [Char] -> Action ()
@ -316,7 +338,8 @@ genAsciiAction getPost out = do
<> toS origin <> toS (postUrl bp) <> "\n\n" <> toS origin <> toS (postUrl bp) <> "\n\n"
writeFile' out (toS (preamble <> toS innerAscii)) writeFile' out (toS (preamble <> toS innerAscii))
genPdfAction getPost out = do genPdfAction :: p -> [Char] -> Action ()
genPdfAction _getPost out = do
let srcFile = srcDir </> (dropDirectory1 (out -<.> "org")) let srcFile = srcDir </> (dropDirectory1 (out -<.> "org"))
need [srcFile] need [srcFile]
command_ [] "pandoc" command_ [] "pandoc"
@ -367,6 +390,7 @@ compressImage img = do
, dst ] , dst ]
needFast :: Action ()
needFast = do needFast = do
allAssets <- filter (/= ".DS_Store") <$> getDirectoryFiles srcDir ["**"] allAssets <- filter (/= ".DS_Store") <$> getDirectoryFiles srcDir ["**"]
need (map build $ allAssets <> ["archive.html"]) need (map build $ allAssets <> ["archive.html"])
@ -378,6 +402,7 @@ fastRule =
phony "fast" $ phony "fast" $
needFast needFast
needAll :: Action ()
needAll = do needAll = do
needFast needFast
allAsciiAction allAsciiAction

View file

@ -2,6 +2,7 @@ html {
font-family: Courier,monospace; font-family: Courier,monospace;
font-size: 14px; font-size: 14px;
line-height: calc(1ex/0.37); } line-height: calc(1ex/0.37); }
#TOC {text-align: left;}
html,body { margin: 0; padding: 0; border: 0; } html,body { margin: 0; padding: 0; border: 0; }
.main { min-height: calc(100vh - 1em); } .main { min-height: calc(100vh - 1em); }
h1,h2,h3,h4,h5,h6 { h1,h2,h3,h4,h5,h6 {