From cc9d36e897065da95f6c85cb8b933dda7afe5ab3 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sat, 13 May 2023 21:44:51 +0100 Subject: Tweak command line a bit --- src/Main.hs | 14 +++++++------- src/Zettel/Render.hs | 28 ++++++++++++++++------------ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/Main.hs b/src/Main.hs index dd77a2a..99d39a2 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -206,7 +206,7 @@ import Zettel data Options = Options { org :: [FilePath], bibliography :: Maybe FilePath, - csl :: Maybe FilePath, + csl :: FilePath, index :: Maybe FilePath, output :: FilePath } @@ -221,14 +221,12 @@ options = def &= name "b" &= help "Bibliography file path (default: references.bib)" - &= typFile - &= opt (Just ("references.bib" :: FilePath)), + &= typFile, csl = - def + "ieee.csl" &= name "c" &= help "Csl file path (default: ieee.csl)" - &= typFile - &= opt (Just ("ieee.csl" :: FilePath)), + &= typFile, index = def &= name "i" @@ -243,8 +241,10 @@ main :: IO () main = do zkOpts <- cmdArgs options let fl = org zkOpts + verbose <- (== Loud) <$> getVerbosity + _quiet <- (== Quiet) <$> getVerbosity graph' <- parseZettelKasten $ zip [1 ..] fl let graph = transcludeMdAll graph' let linkedGraph = linkAll graph let wrappedGraph = wrapZettelGraph linkedGraph - renderZettelGraphFile (output zkOpts) wrappedGraph + renderZettelGraphFile verbose (csl zkOpts) (bibliography zkOpts) (output zkOpts) wrappedGraph diff --git a/src/Zettel/Render.hs b/src/Zettel/Render.hs index b63e0d2..0a052f1 100644 --- a/src/Zettel/Render.hs +++ b/src/Zettel/Render.hs @@ -17,6 +17,7 @@ import Text.Pandoc.Scripting (noEngine) import Text.Pandoc.Templates (WithDefaultPartials (..), compileTemplate) import Text.Pandoc.Walk (query) import Text.Pandoc.Writers (writeMarkdown) +import System.FilePath ((), (<.>)) import Zettel.Types zettelIdToLink :: ZettelId -> Inline @@ -45,8 +46,8 @@ checkCitation :: Inline -> MB checkCitation Cite {} = MB True checkCitation _ = MB False -renderZettel :: ZettelId -> Zettel -> IO Text -renderZettel _ zettel = do +renderZettel :: FilePath -> Maybe FilePath -> ZettelId -> Zettel -> IO Text +renderZettel csl bib _ zettel = do templateFile <- getDataFileName "data/markdown.template" template <- decodeUtf8 <$> readFileBS templateFile Right templ <- runIOorExplode . runWithDefaultPartials $ compileTemplate "" template @@ -58,13 +59,13 @@ renderZettel _ zettel = do } ) runIOorExplode $ - if unMB $ query checkCitation zettel.zettelBody + if unMB (query checkCitation zettel.zettelBody) && isJust bib then let pandoc = - setMeta "csl" ("/Users/ymherklotz/Dropbox/zk/assets/ieee.csl" :: FilePath) $ + setMeta "csl" (csl :: FilePath) $ setMeta "bibliography" - (["/Users/ymherklotz/bibliography/references.bib"] :: [FilePath]) + ([fromMaybe "references.bib" bib] :: [FilePath]) zettel.zettelBody in applyFilters noEngine @@ -84,11 +85,14 @@ renderZettel _ zettel = do >>= writeMarkdown writeOpts else writeMarkdown writeOpts zettel.zettelBody -renderZettelFile :: FilePath -> ZettelId -> Zettel -> IO () -renderZettelFile dir ident zettel = do - t <- renderZettel ident zettel - writeFileText (dir <> "/" <> toString (unZettelId ident <> ".md")) t +renderZettelFile :: Bool -> FilePath -> Maybe FilePath -> FilePath -> ZettelId -> Zettel -> IO () +renderZettelFile v csl bib dir ident zettel = do + when v . putStrLn $ "Writing " <> path + t <- renderZettel csl bib ident zettel + writeFileText path t + where + path = dir toString (unZettelId ident) <.> "md" -renderZettelGraphFile :: FilePath -> ZettelGraph -> IO () -renderZettelGraphFile fp zg = - forM_ (Map.assocs (unZettelGraph zg)) $ uncurry (renderZettelFile fp) +renderZettelGraphFile :: Bool -> FilePath -> Maybe FilePath -> FilePath -> ZettelGraph -> IO () +renderZettelGraphFile v csl bib fp zg = + forM_ (Map.assocs (unZettelGraph zg)) $ uncurry (renderZettelFile v csl bib fp) -- cgit