@@ -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