aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2017-06-25 12:45:25 +0200
committerJohn MacFarlane <jgm@berkeley.edu>2017-06-25 12:46:26 +0200
commit0c993a6c7b73bfd10c795f45abfed32772790999 (patch)
tree8e8503634276196379ce0d055244cd055a75de10 /src/Text/Pandoc
parent87e6235fe73fef3fd6bc6e374c9372b2254d2764 (diff)
downloadpandoc-0c993a6c7b73bfd10c795f45abfed32772790999.tar.gz
Text.Pandoc.Writers.Shared: export splitLang.
Diffstat (limited to 'src/Text/Pandoc')
-rw-r--r--src/Text/Pandoc/Writers/Shared.hs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/Text/Pandoc/Writers/Shared.hs b/src/Text/Pandoc/Writers/Shared.hs
index 6f7c9f75c..0b35d27f6 100644
--- a/src/Text/Pandoc/Writers/Shared.hs
+++ b/src/Text/Pandoc/Writers/Shared.hs
@@ -30,6 +30,7 @@ Shared utility functions for pandoc writers.
-}
module Text.Pandoc.Writers.Shared (
getLang
+ , splitLang
, metaToJSON
, metaToJSON'
, addVariablesToJSON
@@ -53,8 +54,11 @@ import Data.Maybe (isJust)
import qualified Data.Text as T
import qualified Data.Traversable as Traversable
import Text.Pandoc.Definition
+import Text.Pandoc.Class (PandocMonad, report)
+import Text.Pandoc.Logging
import Text.Pandoc.Options
import Text.Pandoc.Pretty
+import Text.Pandoc.Shared (splitBy)
import Text.Pandoc.UTF8 (toStringLazy)
import Text.Pandoc.XML (escapeStringForXML)
@@ -68,6 +72,21 @@ getLang opts meta =
Just (MetaString s) -> Just s
_ -> Nothing
+-- | Split `lang` field into lang and country, issuing warning
+-- if it doesn't look valid.
+splitLang :: PandocMonad m => String -> m (Maybe String, Maybe String)
+splitLang lang =
+ case splitBy (== '-') lang of
+ [la,co]
+ | length la == 2 && length co == 2
+ -> return (Just la, Just co)
+ [la]
+ | length la == 2
+ -> return (Just la, Nothing)
+ _ -> do
+ report $ InvalidLang lang
+ return (Nothing, Nothing)
+
-- | Create JSON value for template from a 'Meta' and an association list
-- of variables, specified at the command line or in the writer.
-- Variables overwrite metadata fields with the same names.