aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2015-09-26 21:17:25 -0700
committerSean Whitton <spwhitton@spwhitton.name>2015-09-26 21:17:25 -0700
commit8c13c60d65b648d2723c048f176eccd61e245ad6 (patch)
treea183466f922fece5d5708514fee752e14036d72a
parent037af7dbb0d2d16263bd2bf2ee60c792324972f2 (diff)
downloadpandoc-citeproc-preamble-8c13c60d65b648d2723c048f176eccd61e245ad6.tar.gz
script generalised beyond just hardcoded LaTeX
-rw-r--r--pandoc-citeproc-preamble.cabal3
-rw-r--r--src/pandoc-citeproc-preamble.hs63
2 files changed, 44 insertions, 22 deletions
diff --git a/pandoc-citeproc-preamble.cabal b/pandoc-citeproc-preamble.cabal
index 5eb673f..0862ce7 100644
--- a/pandoc-citeproc-preamble.cabal
+++ b/pandoc-citeproc-preamble.cabal
@@ -16,6 +16,9 @@ executable pandoc-citeproc-preamble
main-is: pandoc-citeproc-preamble.hs
build-depends: base
, pandoc-types
+ , process
+ , directory
+ , filepath
default-language: Haskell2010
source-repository head
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