From 89e4f1bf9ad48e3fdefeb5c921e9af62ebc24601 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Mon, 5 Oct 2020 17:23:50 -0700 Subject: Improve searching for CSL files... ...and CSL abbreviation files. Use resource path to search in both USERDATADIR/csl and USERDATADIR/csl/dependent. Also, add .csl or .json extension as needed, so you can just do --csl zoology. --- src/Text/Pandoc/Citeproc.hs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src/Text') diff --git a/src/Text/Pandoc/Citeproc.hs b/src/Text/Pandoc/Citeproc.hs index feb9be4f1..8274e35d7 100644 --- a/src/Text/Pandoc/Citeproc.hs +++ b/src/Text/Pandoc/Citeproc.hs @@ -19,6 +19,7 @@ import Text.Pandoc.Citeproc.CslJson (cslJsonToReferences) import Text.Pandoc.Citeproc.BibTeX (readBibtexString, Variant(..)) import Text.Pandoc.Citeproc.MetaValue (metaValueToReference, metaValueToText) import Text.Pandoc.Readers.Markdown (yamlToMeta) +import Text.Pandoc.Class (setResourcePath, getResourcePath, getUserDataDir) import Data.ByteString (ByteString) import qualified Data.ByteString.Lazy as L import Text.Pandoc.Definition as Pandoc @@ -53,21 +54,29 @@ processCitations (Pandoc meta bs) = do let cslfile = (lookupMeta "csl" meta <|> lookupMeta "citation-style" meta) >>= metaValueToText - let getFile fp = catchError (fst <$> fetchItem fp) - (\e -> catchError (readDataFile - (T.unpack $ "csl/" <> fp)) - (\_ -> throwError e)) + let getFile defaultExtension fp = do + oldRp <- getResourcePath + mbUdd <- getUserDataDir + setResourcePath $ oldRp ++ maybe [] + (\u -> [u <> "/csl", + u <> "/csl/dependent"]) mbUdd + let fp' = if T.any (=='.') fp + then fp + else fp <> defaultExtension + (result, _) <- fetchItem fp' + setResourcePath oldRp + return result let getCslDefault = readDataFile "default.csl" - cslContents <- UTF8.toText <$> maybe getCslDefault getFile cslfile + cslContents <- UTF8.toText <$> maybe getCslDefault (getFile ".csl") cslfile let abbrevFile = lookupMeta "citation-abbreviations" meta >>= metaValueToText mbAbbrevs <- case abbrevFile of Nothing -> return Nothing Just fp -> do - rawAbbr <- getFile fp + rawAbbr <- getFile ".json" fp case eitherDecode (L.fromStrict rawAbbr) of Left err -> throwError $ PandocCiteprocError $ CiteprocParseError $ -- cgit v1.2.3