From 74212eb1b0e1757fc0ac3e5d45b0ee18bac491e5 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Fri, 11 Aug 2017 11:56:54 -0700 Subject: Added support for translations (localization) (see #3559). * readDataFile, readDefaultDataFile, getReferenceDocx, getReferenceODT have been removed from Shared and moved into Class. They are now defined in terms of PandocMonad primitives, rather than being primitve methods of the class. * toLang has been moved from BCP47 to Class. * NoTranslation and CouldNotLoudTranslations have been added to LogMessage. * New module, Text.Pandoc.Translations, exporting Term, Translations, readTranslations. * New functions in Class: translateTerm, setTranslations. Note that nothing is loaded from data files until translateTerm is used; setTranslation just sets the language to be used. * Added two translation data files in data/translations. * LaTeX reader: Support `\setmainlanguage` or `\setdefaultlanguage` (polyglossia) and `\figurename`. --- src/Text/Pandoc/App.hs | 341 ++++++++++++++++++------------------ src/Text/Pandoc/BCP47.hs | 14 -- src/Text/Pandoc/Class.hs | 258 ++++++++++++++++++++++----- src/Text/Pandoc/Logging.hs | 14 ++ src/Text/Pandoc/Lua/PandocModule.hs | 6 +- src/Text/Pandoc/Readers/LaTeX.hs | 135 +++++++++++++- src/Text/Pandoc/Shared.hs | 119 +------------ src/Text/Pandoc/Templates.hs | 2 +- src/Text/Pandoc/Translations.hs | 94 ++++++++++ src/Text/Pandoc/Writers/ConTeXt.hs | 2 +- src/Text/Pandoc/Writers/Docx.hs | 4 +- src/Text/Pandoc/Writers/LaTeX.hs | 4 +- src/Text/Pandoc/Writers/ODT.hs | 4 +- 13 files changed, 640 insertions(+), 357 deletions(-) create mode 100644 src/Text/Pandoc/Translations.hs (limited to 'src') diff --git a/src/Text/Pandoc/App.hs b/src/Text/Pandoc/App.hs index 47976a499..99d9aa4cb 100644 --- a/src/Text/Pandoc/App.hs +++ b/src/Text/Pandoc/App.hs @@ -41,7 +41,7 @@ module Text.Pandoc.App ( import Control.Applicative ((<|>)) import qualified Control.Exception as E import Control.Monad -import Control.Monad.Except (throwError) +import Control.Monad.Except (throwError, catchError) import Control.Monad.Trans import Data.Monoid import Data.Aeson (FromJSON (..), ToJSON (..), defaultOptions, eitherDecode', @@ -71,21 +71,22 @@ import System.Environment (getArgs, getEnvironment, getProgName) import System.Exit (ExitCode (..), exitSuccess) import System.FilePath import System.IO (nativeNewline, stdout) -import qualified System.IO as IO (Newline (..)) import System.IO.Error (isDoesNotExistError) +import qualified System.IO as IO (Newline (..)) import Text.Pandoc import Text.Pandoc.Builder (setMeta) import Text.Pandoc.Class (PandocIO, extractMedia, fillMediaBag, getLog, setResourcePath, getMediaBag, setTrace, report, - setUserDataDir) + setUserDataDir, readFileStrict, readDataFile, + readDefaultDataFile, setTranslations) import Text.Pandoc.Highlighting (highlightingStyles) +import Text.Pandoc.BCP47 (parseBCP47, Lang(..)) import Text.Pandoc.Lua (runLuaFilter, LuaException(..)) import Text.Pandoc.Writers.Math (defaultMathJaxURL, defaultKaTeXURL) import Text.Pandoc.PDF (makePDF) import Text.Pandoc.Process (pipeProcess) import Text.Pandoc.SelfContained (makeDataURI, makeSelfContained) -import Text.Pandoc.Shared (headerShift, isURI, openURL, readDataFile, - readDataFileUTF8, readDefaultDataFile, +import Text.Pandoc.Shared (headerShift, isURI, openURL, safeRead, tabFilter, eastAsianLineBreakFilter) import qualified Text.Pandoc.UTF8 as UTF8 import Text.Pandoc.XML (toEntities) @@ -215,84 +216,6 @@ convertWithOpts opts = do _ -> e let standalone = optStandalone opts || not (isTextFormat format) || pdfOutput - - templ <- case optTemplate opts of - _ | not standalone -> return Nothing - Nothing -> do - deftemp <- runIO $ do - setUserDataDir datadir - getDefaultTemplate format - case deftemp of - Left e -> E.throwIO e - Right t -> return (Just t) - Just tp -> do - -- strip off extensions - let tp' = case takeExtension tp of - "" -> tp <.> format - _ -> tp - Just <$> E.catch (UTF8.readFile tp') - (\e -> if isDoesNotExistError e - then E.catch - (readDataFileUTF8 datadir - ("templates" tp')) - (\e' -> let _ = (e' :: E.SomeException) - in E.throwIO e') - else E.throwIO e) - - let addStringAsVariable varname s vars = return $ (varname, s) : vars - - let addContentsAsVariable varname fp vars = do - s <- UTF8.readFile fp - return $ (varname, s) : vars - - -- note: this reverses the list constructed in option parsing, - -- which in turn was reversed from the command-line order, - -- so we end up with the correct order in the variable list: - let withList _ [] vars = return vars - withList f (x:xs) vars = f x vars >>= withList f xs - - variables <- - - withList (addStringAsVariable "sourcefile") - (reverse $ optInputFiles opts) (("outputfile", optOutputFile opts) : optVariables opts) - -- we reverse this list because, unlike - -- the other option lists here, it is - -- not reversed when parsed from CLI arguments. - -- See withList, above. - >>= - withList (addContentsAsVariable "include-before") - (optIncludeBeforeBody opts) - >>= - withList (addContentsAsVariable "include-after") - (optIncludeAfterBody opts) - >>= - withList (addContentsAsVariable "header-includes") - (optIncludeInHeader opts) - >>= - withList (addStringAsVariable "css") (optCss opts) - >>= - maybe return (addStringAsVariable "title-prefix") (optTitlePrefix opts) - >>= - maybe return (addStringAsVariable "epub-cover-image") - (optEpubCoverImage opts) - >>= - (\vars -> case mathMethod of - LaTeXMathML Nothing -> do - s <- readDataFileUTF8 datadir "LaTeXMathML.js" - return $ ("mathml-script", s) : vars - _ -> return vars) - >>= - (\vars -> if format == "dzslides" - then do - dztempl <- readDataFileUTF8 datadir - ("dzslides" "template.html") - let dzline = "