git.iko.soy/typeable/pandoc
on master help sign in
BCP47: split toLang from getLang, rearranged types.
commit4cbbc9dd587d73d576b4c891f3f37a19f12cf10c authorJohn MacFarlane <jgm@berkeley.edu> dateSun 25 Jun 2017, 23:16 (9 years ago) parentd0d2443
4 files changed+55−48
src/Text/Pandoc/BCP47.hs+16−10 src/Text/Pandoc/Writers/ConTeXt.hs+34−33 src/Text/Pandoc/Writers/Docx.hs+3−3 src/Text/Pandoc/Writers/ODT.hs+2−2
src/Text/Pandoc/BCP47.hs+16−10
@@ -29,6 +29,7 @@ Functions for parsing and rendering BCP47 language identifiers.
2929-}
3030module Text.Pandoc.BCP47 (
3131 getLang
32+ , toLang
3233 , parseBCP47
3334 , Lang(..)
3435 , renderLang
@@ -56,21 +57,26 @@ renderLang lang = intercalate "-" (langLanguage lang : filter (not . null)
5657 ([langScript lang, langRegion lang] ++ langVariants lang))
5758
5859-- | Get the contents of the `lang` metadata field or variable.
59−getLang :: PandocMonad m => WriterOptions -> Meta -> m (Maybe Lang)
60−getLang opts meta = case
61− (case lookup "lang" (writerVariables opts) of
60+getLang :: WriterOptions -> Meta -> Maybe String
61+getLang opts meta =
62+ case lookup "lang" (writerVariables opts) of
6263 Just s -> Just s
6364 _ ->
6465 case lookupMeta "lang" meta of
6566 Just (MetaInlines [Str s]) -> Just s
6667 Just (MetaString s) -> Just s
67− _ -> Nothing) of
68− Nothing -> return Nothing
69− Just s -> case parseBCP47 s of
70− Left _ -> do
71− report $ InvalidLang s
72− return Nothing
73− Right l -> return (Just l)
68+ _ -> Nothing
69+
70+-- | Convert BCP47 string to a Lang, issuing warning
71+-- if there are problems.
72+toLang :: PandocMonad m => Maybe String -> m (Maybe Lang)
73+toLang Nothing = return Nothing
74+toLang (Just s) =
75+ case parseBCP47 s of
76+ Left _ -> do
77+ report $ InvalidLang s
78+ return Nothing
79+ Right l -> return (Just l)
7480
7581-- | Parse a BCP 47 string as a Lang.
7682parseBCP47 :: String -> Either String Lang
src/Text/Pandoc/Writers/ConTeXt.hs+34−33
@@ -89,7 +89,7 @@ pandocToConTeXt options (Pandoc meta blocks) = do
8989 ,("top","margin-top")
9090 ,("bottom","margin-bottom")
9191 ]
92− lang <- maybe "" fromBCP47 <$> getLang options meta
92+ mblang <- fromBCP47 (getLang options meta)
9393 let context = defField "toc" (writerTableOfContents options)
9494 $ defField "placelist" (intercalate ("," :: String) $
9595 take (writerTOCDepth options +
@@ -102,7 +102,7 @@ pandocToConTeXt options (Pandoc meta blocks) = do
102102 $ defField "body" main
103103 $ defField "layout" layoutFromMargins
104104 $ defField "number-sections" (writerNumberSections options)
105− $ defField "context-lang" lang
105+ $ maybe id (defField "context-lang") mblang
106106 $ metadata
107107 let context' = defField "context-dir" (toContextDir
108108 $ getField "dir" context) context
@@ -187,6 +187,7 @@ blockToConTeXt b@(RawBlock _ _ ) = do
187187 return empty
188188blockToConTeXt (Div (ident,_,kvs) bs) = do
189189 let align dir txt = "\\startalignment[" <> dir <> "]" $$ txt $$ "\\stopalignment"
190+ mblang <- fromBCP47 (lookup "lang" kvs)
190191 let wrapRef txt = if null ident
191192 then txt
192193 else ("\\reference" <> brackets (text $ toLabel ident) <>
@@ -195,9 +196,9 @@ blockToConTeXt (Div (ident,_,kvs) bs) = do
195196 Just "rtl" -> align "righttoleft"
196197 Just "ltr" -> align "lefttoright"
197198 _ -> id
198− wrapLang txt = case lookup "lang" kvs of
199+ wrapLang txt = case mblang of
199200 Just lng -> "\\start\\language["
200− <> text (fromBCP47' lng) <> "]" $$ txt $$ "\\stop"
201+ <> text lng <> "]" $$ txt $$ "\\stop"
201202 Nothing -> txt
202203 wrapBlank txt = blankline <> txt <> blankline
203204 fmap (wrapBlank . wrapLang . wrapDir . wrapRef) $ blockListToConTeXt bs
@@ -417,12 +418,13 @@ inlineToConTeXt (Note contents) = do
417418 else text "\\startbuffer " <> nest 2 contents' <>
418419 text "\\stopbuffer\\footnote{\\getbuffer}"
419420inlineToConTeXt (Span (_,_,kvs) ils) = do
421+ mblang <- fromBCP47 (lookup "lang" kvs)
420422 let wrapDir txt = case lookup "dir" kvs of
421423 Just "rtl" -> braces $ "\\righttoleft " <> txt
422424 Just "ltr" -> braces $ "\\lefttoright " <> txt
423425 _ -> txt
424− wrapLang txt = case lookup "lang" kvs of
425− Just lng -> "\\start\\language[" <> text (fromBCP47' lng)
426+ wrapLang txt = case mblang of
427+ Just lng -> "\\start\\language[" <> text lng
426428 <> "]" <> txt <> "\\stop "
427429 Nothing -> txt
428430 fmap (wrapLang . wrapDir) $ inlineListToConTeXt ils
@@ -459,35 +461,34 @@ sectionHeader (ident,classes,_) hdrLevel lst = do
459461 <> blankline
460462 _ -> contents <> blankline
461463
462−fromBCP47' :: String -> String
463−fromBCP47' s = case parseBCP47 s of
464− Right r -> fromBCP47 r
465− Left _ -> ""
464+fromBCP47 :: PandocMonad m => Maybe String -> WM m (Maybe String)
465+fromBCP47 mbs = fromBCP47' <$> toLang mbs
466466
467467-- Takes a list of the constituents of a BCP 47 language code
468468-- and irons out ConTeXt's exceptions
469469-- https://tools.ietf.org/html/bcp47#section-2.1
470470-- http://wiki.contextgarden.net/Language_Codes
471−fromBCP47 :: Lang -> String
472−fromBCP47 (Lang "ar" _ "SY" _) = "ar-sy"
473−fromBCP47 (Lang "ar" _ "IQ" _) = "ar-iq"
474−fromBCP47 (Lang "ar" _ "JO" _) = "ar-jo"
475−fromBCP47 (Lang "ar" _ "LB" _) = "ar-lb"
476−fromBCP47 (Lang "ar" _ "DZ" _) = "ar-dz"
477−fromBCP47 (Lang "ar" _ "MA" _) = "ar-ma"
478−fromBCP47 (Lang "de" _ _ ["1901"]) = "deo"
479−fromBCP47 (Lang "de" _ "DE" _) = "de-de"
480−fromBCP47 (Lang "de" _ "AT" _) = "de-at"
481−fromBCP47 (Lang "de" _ "CH" _) = "de-ch"
482−fromBCP47 (Lang "el" _ _ ["poly"]) = "agr"
483−fromBCP47 (Lang "en" _ "US" _) = "en-us"
484−fromBCP47 (Lang "en" _ "GB" _) = "en-gb"
485−fromBCP47 (Lang "grc"_ _ _) = "agr"
486−fromBCP47 (Lang "el" _ _ _) = "gr"
487−fromBCP47 (Lang "eu" _ _ _) = "ba"
488−fromBCP47 (Lang "he" _ _ _) = "il"
489−fromBCP47 (Lang "jp" _ _ _) = "ja"
490−fromBCP47 (Lang "uk" _ _ _) = "ua"
491−fromBCP47 (Lang "vi" _ _ _) = "vn"
492−fromBCP47 (Lang "zh" _ _ _) = "cn"
493−fromBCP47 (Lang l _ _ _) = l
471+fromBCP47' :: Maybe Lang -> Maybe String
472+fromBCP47' (Just (Lang "ar" _ "SY" _) ) = Just "ar-sy"
473+fromBCP47' (Just (Lang "ar" _ "IQ" _) ) = Just "ar-iq"
474+fromBCP47' (Just (Lang "ar" _ "JO" _) ) = Just "ar-jo"
475+fromBCP47' (Just (Lang "ar" _ "LB" _) ) = Just "ar-lb"
476+fromBCP47' (Just (Lang "ar" _ "DZ" _) ) = Just "ar-dz"
477+fromBCP47' (Just (Lang "ar" _ "MA" _) ) = Just "ar-ma"
478+fromBCP47' (Just (Lang "de" _ _ ["1901"]) ) = Just "deo"
479+fromBCP47' (Just (Lang "de" _ "DE" _) ) = Just "de-de"
480+fromBCP47' (Just (Lang "de" _ "AT" _) ) = Just "de-at"
481+fromBCP47' (Just (Lang "de" _ "CH" _) ) = Just "de-ch"
482+fromBCP47' (Just (Lang "el" _ _ ["poly"]) ) = Just "agr"
483+fromBCP47' (Just (Lang "en" _ "US" _) ) = Just "en-us"
484+fromBCP47' (Just (Lang "en" _ "GB" _) ) = Just "en-gb"
485+fromBCP47' (Just (Lang "grc"_ _ _) ) = Just "agr"
486+fromBCP47' (Just (Lang "el" _ _ _) ) = Just "gr"
487+fromBCP47' (Just (Lang "eu" _ _ _) ) = Just "ba"
488+fromBCP47' (Just (Lang "he" _ _ _) ) = Just "il"
489+fromBCP47' (Just (Lang "jp" _ _ _) ) = Just "ja"
490+fromBCP47' (Just (Lang "uk" _ _ _) ) = Just "ua"
491+fromBCP47' (Just (Lang "vi" _ _ _) ) = Just "vn"
492+fromBCP47' (Just (Lang "zh" _ _ _) ) = Just "cn"
493+fromBCP47' (Just (Lang l _ _ _) ) = Just l
494+fromBCP47' Nothing = Nothing
src/Text/Pandoc/Writers/Docx.hs+3−3
@@ -68,7 +68,7 @@ import qualified Text.Pandoc.UTF8 as UTF8
6868import Text.Pandoc.Walk
6969import Text.Pandoc.Writers.Math
7070import Text.Pandoc.Writers.Shared (fixDisplayMath)
71−import Text.Pandoc.BCP47 (getLang, renderLang)
71+import Text.Pandoc.BCP47 (getLang, renderLang, toLang)
7272import Text.Printf (printf)
7373import Text.TeXMath
7474import Text.XML.Light as XML
@@ -258,9 +258,9 @@ writeDocx opts doc@(Pandoc meta _) = do
258258 )
259259
260260 -- styles
261− lang <- getLang opts meta
261+ mblang <- toLang $ getLang opts meta
262262 let addLang :: Element -> Element
263− addLang e = case lang >>= \l ->
263+ addLang e = case mblang >>= \l ->
264264 (return . XMLC.toTree . go (renderLang l)
265265 . XMLC.fromElement) e of
266266 Just (Elem e') -> e'
src/Text/Pandoc/Writers/ODT.hs+2−2
@@ -51,7 +51,7 @@ import Text.Pandoc.UTF8 (fromStringLazy, fromTextLazy, toStringLazy)
5151import Text.Pandoc.Walk
5252import Text.Pandoc.Writers.OpenDocument (writeOpenDocument)
5353import Text.Pandoc.Writers.Shared (fixDisplayMath)
54−import Text.Pandoc.BCP47 (getLang, Lang(..), renderLang)
54+import Text.Pandoc.BCP47 (getLang, toLang, Lang(..), renderLang)
5555import Text.Pandoc.XML
5656import Text.TeXMath
5757import Text.XML.Light
@@ -80,7 +80,7 @@ pandocToODT :: PandocMonad m
8080pandocToODT opts doc@(Pandoc meta _) = do
8181 let datadir = writerUserDataDir opts
8282 let title = docTitle meta
83− lang <- getLang opts meta
83+ lang <- toLang (getLang opts meta)
8484 refArchive <-
8585 case writerReferenceDoc opts of
8686 Just f -> liftM toArchive $ lift $ P.readFileLazy f