aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Citeproc.hs
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2020-12-02 10:46:23 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2020-12-02 10:46:23 -0800
commit7b11cdee49ec491a33dcade6ea68c3084a425992 (patch)
tree431807d53abe1685e461c0a3f6b543ab449c03dc /src/Text/Pandoc/Citeproc.hs
parent4ca0d590e2c433c6ce99d53be58df4b982a587f6 (diff)
downloadpandoc-7b11cdee49ec491a33dcade6ea68c3084a425992.tar.gz
Citeproc: ensure that BCP47 lang codes can be used.
We ignore the variants and just use the base lang code and country code when passing off to citeproc.
Diffstat (limited to 'src/Text/Pandoc/Citeproc.hs')
-rw-r--r--src/Text/Pandoc/Citeproc.hs19
1 files changed, 17 insertions, 2 deletions
diff --git a/src/Text/Pandoc/Citeproc.hs b/src/Text/Pandoc/Citeproc.hs
index a48f97c3b..0de2882ae 100644
--- a/src/Text/Pandoc/Citeproc.hs
+++ b/src/Text/Pandoc/Citeproc.hs
@@ -16,6 +16,7 @@ import Text.Pandoc.Citeproc.BibTeX (readBibtexString, Variant(..))
import Text.Pandoc.Citeproc.MetaValue (metaValueToReference, metaValueToText)
import Text.Pandoc.Readers.Markdown (yamlToRefs)
import Text.Pandoc.Class (setResourcePath, getResourcePath, getUserDataDir)
+import qualified Text.Pandoc.BCP47 as BCP47
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as L
import Text.Pandoc.Definition as Pandoc
@@ -91,8 +92,9 @@ processCitations (Pandoc meta bs) = do
case styleRes of
Left err -> throwError $ PandocAppError $ prettyCiteprocError err
Right style -> return style{ styleAbbreviations = mbAbbrevs }
- let mblang = parseLang <$>
- ((lookupMeta "lang" meta <|> lookupMeta "locale" meta) >>= metaValueToText)
+ mblang <- maybe (return Nothing) bcp47LangToIETF
+ ((lookupMeta "lang" meta <|> lookupMeta "locale" meta) >>=
+ metaValueToText)
let locale = Citeproc.mergeLocales mblang style
let getCiteId (Cite cs _) = Set.fromList $ map B.citationId cs
getCiteId _ = mempty
@@ -579,3 +581,16 @@ removeFinalPeriod ils =
isRightQuote "\8217" = True
isRightQuote "\187" = True
isRightQuote _ = False
+
+bcp47LangToIETF :: PandocMonad m => Text -> m (Maybe Lang)
+bcp47LangToIETF bcplang =
+ case BCP47.parseBCP47 bcplang of
+ Left _ -> do
+ report $ InvalidLang bcplang
+ return Nothing
+ Right lang ->
+ return $ Just
+ $ Lang (BCP47.langLanguage lang)
+ (if T.null (BCP47.langRegion lang)
+ then Nothing
+ else Just (BCP47.langRegion lang))