From 2641d4e1ac45f5405b99b07553d2b98a0a99761a Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sun, 20 Sep 2015 18:52:13 -0700 Subject: don't depend on my personal config files --- README.md | 16 ++++++++++---- oso2pdf.cabal | 1 + src/oso2pdf.hs | 68 +++++++++++++++++++++++++++++++--------------------------- 3 files changed, 49 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 1c682e8..7bf8bb9 100644 --- a/README.md +++ b/README.md @@ -29,16 +29,24 @@ conversion; the default setting is $ oso2pdf --font="Liberation Serif" my_chapter.html +You may pass additional arguments to pandoc like this: + + $ oso2pdf --font="Times New Roman" my_chapter.html -- -V documentclass=pessay -V classoption=onehalf --template=pessay + +In this example Pandoc is instructed to make use of the files +[~/.pandoc/templates/pessay.latex](https://github.com/spwhitton/dotfiles/blob/master/.pandoc/templates/pessay.latex) +and +[~/texmf/tex/latex/pessay/pessay.cls](https://github.com/spwhitton/dotfiles/blob/master/texmf/tex/latex/pessay/pessay.cls). +Indeed, any arguments passed after `--` will be ignored by oso2pdf and +handed on to invocations of Pandoc. + ## Installation Runtime dependencies: -- a working installation of pandoc +- a working installation of Pandoc - a working LaTeX installation, including XeLaTeX - some XeLaTeX-compatible font, such as Liberation Serif -- my - [~/.pandoc/templates/pessay.latex](https://github.com/spwhitton/dotfiles/blob/master/.pandoc/templates/pessay.latex) -- my [~/texmf/tex/latex/pessay/pessay.cls](https://github.com/spwhitton/dotfiles/blob/master/texmf/tex/latex/pessay/pessay.cls) To build and install, first [install stack](https://github.com/commercialhaskell/stack), and then diff --git a/oso2pdf.cabal b/oso2pdf.cabal index be1c97c..395e122 100644 --- a/oso2pdf.cabal +++ b/oso2pdf.cabal @@ -28,6 +28,7 @@ executable oso2pdf , process , temporary , filepath + , mtl default-language: Haskell2010 source-repository head diff --git a/src/oso2pdf.hs b/src/oso2pdf.hs index 3f9bbbf..766bb2b 100644 --- a/src/oso2pdf.hs +++ b/src/oso2pdf.hs @@ -23,23 +23,25 @@ {-# LANGUAGE TemplateHaskell #-} -import Control.Lens hiding (argument) -import Control.Monad (when) -import Data.List (isInfixOf) +import Control.Lens hiding (argument) +import Control.Monad (when) +import Control.Monad.Reader +import Data.List (isInfixOf) import Options.Applicative -import System.Directory (findExecutable) -import System.Exit (die) -import System.FilePath (takeBaseName, ()) +import System.Directory (findExecutable) +import System.Exit (die) +import System.FilePath (takeBaseName, ()) import System.IO.Temp import System.Process -data Opts = Opts { _font :: String, _input :: FilePath } +data Opts = Opts { _font :: String, _input :: FilePath, _pandocArgs :: [String] } makeLenses ''Opts optsParser :: Parser Opts optsParser = Opts <$> strOption (long "font" <> metavar "FONT" <> value "Liberation Serif") <*> argument str (metavar "INPUT") + <*> many (argument str (metavar "PANDOC_ARGS...")) narrowToContent :: String -> (String, String, String) narrowToContent full = (body, notes ++ "", footer) @@ -68,34 +70,36 @@ main = do opts <- execParser $ info optsParser mempty + runReaderT oso2pdf opts + +oso2pdf :: ReaderT Opts IO () +oso2pdf = ask >>= \opts -> do + -- 3. get HTML content - (body, notes, footer) <- narrowToContent <$> readFile (opts ^. input) + (body, notes, footer) <- narrowToContent <$> (liftIO . readFile) (opts ^. input) -- 4. go go go - let contentFile = (takeBaseName $ opts ^. input) ++ "-content.pdf" - let notesFile = (takeBaseName $ opts ^. input) ++ "-notes.pdf" + let basename = (takeBaseName $ opts ^. input) + contentFile = basename ++ "-content.pdf" + notesFile = basename ++ "-notes.pdf" + withSystemTempDirectory "oso2pdf" $ \dir -> do - writeFile (dir "content.html") (body ++ footer) - writeFile (dir "notes.html") (notes ++ footer) - runPandoc (dir "content.html") contentFile (opts ^. font) - runPandoc (dir "notes.html") notesFile (opts ^. font) - -runPandoc :: String -> String -> String -> IO () -runPandoc input output font = do - readProcess "pandoc" [ "-s" - , input - , "--filter" - , "pandoc-oso2tex" - , "--variable" - , "mainfont=" ++ font - , "--latex-engine=xelatex" - , "-V" - , "documentclass=pessay" - , "-V" - , "classoption=onehalf" - , "--template=pessay" - , "-o" - , output] "" - return () + liftIO $ writeFile (dir "content.html") (body ++ footer) + liftIO $ writeFile (dir "notes.html") (notes ++ footer) + runPandoc (dir "content.html") contentFile + runPandoc (dir "notes.html") notesFile + +runPandoc :: String -> String -> ReaderT Opts IO () +runPandoc input output = ask >>= \opts -> do + liftIO $ readProcess "pandoc" ([ "-s" + , input + , "--filter" + , "pandoc-oso2tex" + , "--variable" + , "mainfont=" ++ (opts ^. font) + , "--latex-engine=xelatex" + , "-o" + , output] ++ (opts ^. pandocArgs)) "" + pure () -- cgit v1.2.3