From e2e6143f9b55efc71ebf4c40f85e11115355672a Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sat, 19 Sep 2015 19:41:50 -0700 Subject: oso2pdf first attempt --- oso2pdf.cabal | 6 ++++ src/oso2pdf.hs | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/pandoc-oso2tex.hs | 1 + 3 files changed, 85 insertions(+) create mode 100644 src/oso2pdf.hs diff --git a/oso2pdf.cabal b/oso2pdf.cabal index a104aac..be1c97c 100644 --- a/oso2pdf.cabal +++ b/oso2pdf.cabal @@ -22,6 +22,12 @@ executable oso2pdf hs-source-dirs: src main-is: oso2pdf.hs build-depends: base + , directory + , optparse-applicative + , lens + , process + , temporary + , filepath default-language: Haskell2010 source-repository head diff --git a/src/oso2pdf.hs b/src/oso2pdf.hs new file mode 100644 index 0000000..d9251d8 --- /dev/null +++ b/src/oso2pdf.hs @@ -0,0 +1,78 @@ +{-# LANGUAGE TemplateHaskell #-} + +import Control.Lens hiding (argument) +import Control.Monad (when) +import Data.List (isInfixOf) +import Options.Applicative +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 } +makeLenses ''Opts + +optsParser :: Parser Opts +optsParser = Opts + <$> strOption (long "font" <> metavar "FONT" <> value "Liberation Serif") + <*> argument str (metavar "INPUT") + +narrowToContent :: String -> (String, String, String) +narrowToContent full = (body, notes ++ "", footer) + where + ls = lines full + body = unlines + . takeWhile (\x -> not . isInfixOf "
" $ x) + . dropWhile (\x -> not . isInfixOf "
" $ x) + $ ls + notes = unlines + . takeWhile (\x -> not . isInfixOf "
" $ x) + . dropWhile (\x -> not . isInfixOf "
" $ x) + $ ls + footer = unlines . filter ("printFooterCopyright" `isInfixOf`) $ ls + +main = do + + -- 1. check for existence of other programs we're gonna call + + pandoc <- maybe False (const True) <$> findExecutable "pandoc" + pandoc_oso2tex <- maybe False (const True) <$> findExecutable "pandoc-oso2tex" + when (not pandoc) $ die "could not find pandoc executable" + when (not pandoc_oso2tex) $ die "could not find pandoc-oso2tex executable" + + -- 2. parse command line options + + opts <- execParser $ info optsParser mempty + + -- 3. get HTML content + + (body, notes, footer) <- narrowToContent <$> readFile (opts ^. input) + + -- 4. go go go + + let contentFile = (takeBaseName $ opts ^. input) ++ "-content.pdf" + let notesFile = (takeBaseName $ opts ^. input) ++ "-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 () diff --git a/src/pandoc-oso2tex.hs b/src/pandoc-oso2tex.hs index 881ee37..9014392 100644 --- a/src/pandoc-oso2tex.hs +++ b/src/pandoc-oso2tex.hs @@ -32,6 +32,7 @@ instance Monoid Page where (Page n xs) `mappend` (Page _ ys) = Page n (xs ++ ys) pagesToBlocks :: [Page] -> [Block] +pagesToBlocks [Page _ xs] = xs pagesToBlocks ps@( _ : (Page n _) : _) = RawBlock (Format "tex") ("\\setcounter{page}{" ++ show (n - 1) ++ "}") : (drop 1 $ foldr step [] ps) -- drop first \\pagebreak -- cgit v1.2.3