From 8c13c60d65b648d2723c048f176eccd61e245ad6 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sat, 26 Sep 2015 21:17:25 -0700 Subject: script generalised beyond just hardcoded LaTeX --- src/pandoc-citeproc-preamble.hs | 63 +++++++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/pandoc-citeproc-preamble.hs b/src/pandoc-citeproc-preamble.hs index 1bfa2f8..df75ed5 100644 --- a/src/pandoc-citeproc-preamble.hs +++ b/src/pandoc-citeproc-preamble.hs @@ -1,28 +1,47 @@ +import Data.List (isPrefixOf) +import System.Directory (doesFileExist) +import System.FilePath ((<.>), ()) +import System.Process (readProcess) import Text.Pandoc.JSON -preamble = [ RawBlock (Format "latex") "\\section*{References}" - , Para [ RawInline (Format "tex") "\\setlength{\\parindent}{-0.2in}" - , Space - , RawInline (Format "tex") "\\singlespacing" - , Space - , RawInline (Format "tex") "\\small" - , Space - , RawInline (Format "tex") "\\setlength{\\leftskip}{0.2in}" - , Space - , RawInline (Format "tex") "\\setlength{\\parskip}{8pt}" - , Space - , RawInline (Format "tex") "\\vspace*{-0.4in}" - , Space - , RawInline (Format "tex") "\\noindent" - ] - ] - -insertPreamble :: [Block] -> [Block] -insertPreamble = foldr step [] +insertPreamble :: Block -> [Block] -> [Block] +insertPreamble preamble = foldr step [] where - step refs@(Div (_, ["references"], _) _) xs = preamble ++ refs : xs + step refs@(Div (_, ["references"], _) _) xs = preamble : refs : xs step x xs = x:xs -main = toJSONFilter preamble +doPreamble :: Maybe Format -> Pandoc -> IO Pandoc +doPreamble maybeFormat input@(Pandoc meta blocks) = + findPreambleFile format meta + >>= maybe (return input) + (\file -> do + ls <- readFile file + return $ Pandoc meta (insertPreamble (lsBlock ls) blocks)) + where + lsBlock ls = RawBlock format ls + format = maybe (Format "latex") id maybeFormat + +findPreambleFile :: Format -> Meta -> IO (Maybe FilePath) +findPreambleFile (Format format) meta = + case lookupMeta "citeproc-preamble" meta >>= toPath of + metadataPreamble@(Just _) -> return metadataPreamble + Nothing -> tryDatadir where - preamble (Pandoc meta blocks) = Pandoc meta (insertPreamble blocks) + defaultPreamble = ( "citeproc-preamble" "default" <.> format) + <$> getPandocDatadir + tryDatadir = defaultPreamble >>= doesFileExist >>= \exists -> + if exists then Just <$> defaultPreamble else return Nothing + +getPandocDatadir :: IO FilePath +getPandocDatadir = + drop (length prefix) . head . filter (prefix `isPrefixOf`) . lines + <$> readProcess "pandoc" ["--version"] "" + where + prefix = "Default user data directory: " + +toPath :: MetaValue -> Maybe String +toPath (MetaString s) = Just s +toPath _ = Nothing + +main :: IO () +main = toJSONFilter doPreamble -- cgit v1.2.3