@@ -20,9 +20,8 @@ module Text.Pandoc.Writers.LaTeX (
21import Control.Applicative ((<|>))
21import Control.Applicative ((<|>))
22import Control.Monad.State.Strict
22import Control.Monad.State.Strict
23−import Data.Char (isAlphaNum, isAscii, isDigit, isLetter, isSpace,
23+import Data.Char (isAlphaNum, isAscii, isDigit, isLetter, isSpace, ord)
25−import Data.List (foldl', intersperse, nubBy, (\\), uncons)
24+import Data.List (intersperse, nubBy, (\\), uncons)
26import Data.Maybe (catMaybes, fromMaybe, isJust, mapMaybe, isNothing)
25import Data.Maybe (catMaybes, fromMaybe, isJust, mapMaybe, isNothing)
27import qualified Data.Map as M
26import qualified Data.Map as M
28import Data.Text (Text)
27import Data.Text (Text)
@@ -44,6 +43,8 @@ import Text.Pandoc.Slides
44import Text.Pandoc.Walk (query, walk, walkM)
43import Text.Pandoc.Walk (query, walk, walkM)
45import Text.Pandoc.Writers.LaTeX.Caption (getCaption)
44import Text.Pandoc.Writers.LaTeX.Caption (getCaption)
46import Text.Pandoc.Writers.LaTeX.Table (tableToLaTeX)
45import Text.Pandoc.Writers.LaTeX.Table (tableToLaTeX)
46+import Text.Pandoc.Writers.LaTeX.Citation (citationsToNatbib,
47import Text.Pandoc.Writers.LaTeX.Types (LW, WriterState (..), startingState)
48import Text.Pandoc.Writers.LaTeX.Types (LW, WriterState (..), startingState)
48import Text.Pandoc.Writers.LaTeX.Lang (toPolyglossiaEnv, toPolyglossia,
49import Text.Pandoc.Writers.LaTeX.Lang (toPolyglossiaEnv, toPolyglossia,
@@ -1026,8 +1027,8 @@ inlineToLaTeX (Cite cits lst) = do
1026 st <- get
1027 st <- get
1027 let opts = stOptions st
1028 let opts = stOptions st
1028 case writerCiteMethod opts of
1029 case writerCiteMethod opts of
1029− Natbib -> citationsToNatbib cits
1030+ Natbib -> citationsToNatbib inlineListToLaTeX cits
1030− Biblatex -> citationsToBiblatex cits
1031+ Biblatex -> citationsToBiblatex inlineListToLaTeX cits
1031 _ -> inlineListToLaTeX lst
1032 _ -> inlineListToLaTeX lst
1033inlineToLaTeX (Code (_,classes,kvs) str) = do
1034inlineToLaTeX (Code (_,classes,kvs) str) = do
@@ -1238,143 +1239,6 @@ protectCode x = [x]
1238setEmptyLine :: PandocMonad m => Bool -> LW m ()
1239setEmptyLine :: PandocMonad m => Bool -> LW m ()
1239setEmptyLine b = modify $ \st -> st{ stEmptyLine = b }
1240setEmptyLine b = modify $ \st -> st{ stEmptyLine = b }
1241−citationsToNatbib :: PandocMonad m => [Citation] -> LW m (Doc Text)
1244− = citeCommand c p s k
1246− Citation { citationId = k
1247− , citationPrefix = p
1248− , citationSuffix = s
1253− AuthorInText -> "citet"
1254− SuppressAuthor -> "citeyearpar"
1255− NormalCitation -> "citep"
1257−citationsToNatbib cits
1258− | noPrefix (tail cits) && noSuffix (init cits) && ismode NormalCitation cits
1259− = citeCommand "citep" p s ks
1261− noPrefix = all (null . citationPrefix)
1262− noSuffix = all (null . citationSuffix)
1263− ismode m = all ((==) m . citationMode)
1264− p = citationPrefix $
1266− s = citationSuffix $
1268− ks = T.intercalate ", " $ map citationId cits
1270−citationsToNatbib (c:cs) | citationMode c == AuthorInText = do
1271− author <- citeCommand "citeauthor" [] [] (citationId c)
1272− cits <- citationsToNatbib (c { citationMode = SuppressAuthor } : cs)
1273− return $ author <+> cits
1275−citationsToNatbib cits = do
1276− cits' <- mapM convertOne cits
1277− return $ text "\\citetext{" <> foldl' combineTwo empty cits' <> text "}"
1279− combineTwo a b | isEmpty a = b
1280− | otherwise = a <> text "; " <> b
1281− convertOne Citation { citationId = k
1282− , citationPrefix = p
1283− , citationSuffix = s
1287− AuthorInText -> citeCommand "citealt" p s k
1288− SuppressAuthor -> citeCommand "citeyear" p s k
1289− NormalCitation -> citeCommand "citealp" p s k
1291−citeCommand :: PandocMonad m
1292− => Text -> [Inline] -> [Inline] -> Text -> LW m (Doc Text)
1293−citeCommand c p s k = do
1294− args <- citeArguments p s k
1295− return $ literal ("\\" <> c) <> args
1297−type Prefix = [Inline]
1298−type Suffix = [Inline]
1300−data CiteGroup = CiteGroup Prefix Suffix [CiteId]
1302−citeArgumentsList :: PandocMonad m
1303− => CiteGroup -> LW m (Doc Text)
1304−citeArgumentsList (CiteGroup _ _ []) = return empty
1305−citeArgumentsList (CiteGroup pfxs sfxs ids) = do
1306− pdoc <- inlineListToLaTeX pfxs
1307− sdoc <- inlineListToLaTeX sfxs'
1308− return $ optargs pdoc sdoc <>
1309− braces (literal (T.intercalate "," (reverse ids)))
1310− where sfxs' = stripLocatorBraces $ case sfxs of
1311− (Str t : r) -> case T.uncons t of
1314− , isPunctuation x -> dropWhile (== Space) r
1315− | isPunctuation x -> Str xs : r
1318− optargs pdoc sdoc = case (isEmpty pdoc, isEmpty sdoc) of
1319− (True, True ) -> empty
1320− (True, False) -> brackets sdoc
1321− (_ , _ ) -> brackets pdoc <> brackets sdoc
1323−citeArguments :: PandocMonad m
1324− => [Inline] -> [Inline] -> Text -> LW m (Doc Text)
1325−citeArguments p s k = citeArgumentsList (CiteGroup p s [k])
1327−-- strip off {} used to define locator in pandoc-citeproc; see #5722
1328−stripLocatorBraces :: [Inline] -> [Inline]
1329−stripLocatorBraces = walk go
1330− where go (Str xs) = Str $ T.filter (\c -> c /= '{' && c /= '}') xs
1333−citationsToBiblatex :: PandocMonad m => [Citation] -> LW m (Doc Text)
1336− = citeCommand cmd p s k
1338− Citation { citationId = k
1339− , citationPrefix = p
1340− , citationSuffix = s
1344− SuppressAuthor -> "autocite*"
1345− AuthorInText -> "textcite"
1346− NormalCitation -> "autocite"
1348−citationsToBiblatex (c:cs)
1349− | all (\cit -> null (citationPrefix cit) && null (citationSuffix cit)) (c:cs)
1351− let cmd = case citationMode c of
1352− SuppressAuthor -> "\\autocite*"
1353− AuthorInText -> "\\textcite"
1354− NormalCitation -> "\\autocite"
1355− return $ text cmd <>
1356− braces (literal (T.intercalate "," (map citationId (c:cs))))
1359− let cmd = case citationMode c of
1360− SuppressAuthor -> "\\autocites*"
1361− AuthorInText -> "\\textcites"
1362− NormalCitation -> "\\autocites"
1364− groups <- mapM citeArgumentsList (reverse (foldl' grouper [] (c:cs)))
1366− return $ text cmd <> mconcat groups
1368− where grouper prev cit = case prev of
1369− ((CiteGroup oPfx oSfx ids):rest)
1370− | null oSfx && null pfx -> CiteGroup oPfx sfx (cid:ids) : rest
1371− _ -> CiteGroup pfx sfx [cid] : prev
1372− where pfx = citationPrefix cit
1373− sfx = citationSuffix cit
1374− cid = citationId cit
1376−citationsToBiblatex _ = return empty
1378-- Extract a key from divs and spans
1242-- Extract a key from divs and spans
1379extract :: Text -> Block -> [Text]
1243extract :: Text -> Block -> [Text]
1380extract key (Div attr _) = lookKey key attr
1244extract key (Div attr _) = lookKey key attr