git.iko.soy/typeable/pandoc
on master help sign in
LaTeX writer - hlint.
commit8a42ca41cf5fbb4d812a7774d10be1d07cbe0b39 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 27 Nov 2019, 10:04 (7 years ago) parent982d2f6
1 file changed+24−29
src/Text/Pandoc/Writers/LaTeX.hs+24−29
src/Text/Pandoc/Writers/LaTeX.hs+24−29
@@ -21,6 +21,7 @@ module Text.Pandoc.Writers.LaTeX (
2121import Prelude
2222import Control.Applicative ((<|>))
2323import Control.Monad.State.Strict
24+import Control.Monad (when)
2425import Data.Monoid (Any(..))
2526import Data.Char (isAlphaNum, isAscii, isDigit, isLetter, isSpace,
2627 isPunctuation, ord)
@@ -30,7 +31,8 @@ import qualified Data.Map as M
3031import Data.Text (Text)
3132import qualified Data.Text as T
3233import Network.URI (unEscapeString)
33−import Text.DocTemplates (FromContext(lookupContext))
34+import Text.DocTemplates (FromContext(lookupContext), renderTemplate,
35+ Val(..), Context(..))
3436import Text.Pandoc.BCP47 (Lang (..), getLang, renderLang)
3537import Text.Pandoc.Class (PandocMonad, report, toLang)
3638import Text.Pandoc.Definition
@@ -42,8 +44,6 @@ import Text.Pandoc.Options
4244import Text.DocLayout
4345import Text.Pandoc.Shared
4446import Text.Pandoc.Slides
45−import Text.Pandoc.Templates (renderTemplate)
46−import Text.DocTemplates (Val(..), Context(..))
4747import Text.Pandoc.Walk
4848import Text.Pandoc.Writers.Shared
4949import Text.Printf (printf)
@@ -148,8 +148,7 @@ pandocToLaTeX options (Pandoc meta blocks) = do
148148 -- these have \frontmatter etc.
149149 beamer <- gets stBeamer
150150 let documentClass =
151− case (lookupContext "documentclass"
152− (writerVariables options)) `mplus`
151+ case lookupContext "documentclass" (writerVariables options) `mplus`
153152 (stringify <$> lookupMeta "documentclass" meta) of
154153 Just x -> x
155154 Nothing | beamer -> "beamer"
@@ -202,7 +201,7 @@ pandocToLaTeX options (Pandoc meta blocks) = do
202201 let dirs = query (extract "dir") blocks
203202
204203 let context = defField "toc" (writerTableOfContents options) $
205− defField "toc-depth" (tshow $
204+ defField "toc-depth" (tshow
206205 (writerTOCDepth options -
207206 if stHasChapters st
208207 then 1
@@ -333,7 +332,7 @@ stringToLaTeX context zs = do
333332 Just cmd ->
334333 cmd <> "{" <> [c] <> "}" <> drop 1 xs -- drop combining accent
335334 Nothing -> c : xs
336− emitcseq cs = do
335+ emitcseq cs =
337336 case xs of
338337 c:_ | isLetter c
339338 , ctx == TextString
@@ -342,7 +341,7 @@ stringToLaTeX context zs = do
342341 | ctx == TextString
343342 -> cs <> xs
344343 _ -> cs <> "{}" <> xs
345− emitquote cs = do
344+ emitquote cs =
346345 case xs of
347346 '`':_ -> cs <> "\\," <> xs -- add thin space
348347 '\'':_ -> cs <> "\\," <> xs -- add thin space
@@ -521,7 +520,7 @@ blockToLaTeX (Div (identifier,"slide":dclasses,dkvs)
521520 contents $$
522521 "\\end{frame}"
523522blockToLaTeX (Div (identifier@(T.uncons -> Just (_,_)),dclasses,dkvs)
524− (Header lvl ("",hclasses,hkvs) ils : bs)) = do
523+ (Header lvl ("",hclasses,hkvs) ils : bs)) =
525524 -- move identifier from div to header
526525 blockToLaTeX (Div ("",dclasses,dkvs)
527526 (Header lvl (identifier,hclasses,hkvs) ils : bs))
@@ -530,9 +529,8 @@ blockToLaTeX (Div (identifier,classes,kvs) bs) = do
530529 oldIncremental <- gets stIncremental
531530 if beamer && "incremental" `elem` classes
532531 then modify $ \st -> st{ stIncremental = True }
533− else if beamer && "nonincremental" `elem` classes
534− then modify $ \st -> st { stIncremental = False }
535− else return ()
532+ else when (beamer && "nonincremental" `elem` classes) $
533+ modify $ \st -> st { stIncremental = False }
536534 result <- if identifier == "refs"
537535 then do
538536 inner <- blockListToLaTeX bs
@@ -669,8 +667,8 @@ blockToLaTeX (CodeBlock (identifier,classes,keyvalAttr) str) = do
669667 | otherwise -> rawCodeBlock
670668blockToLaTeX b@(RawBlock f x) = do
671669 beamer <- gets stBeamer
672− if (f == Format "latex" || f == Format "tex" ||
673− (f == Format "beamer" && beamer))
670+ if f == Format "latex" || f == Format "tex" ||
671+ (f == Format "beamer" && beamer)
674672 then return $ literal x
675673 else do
676674 report $ BlockNotRendered b
@@ -913,12 +911,12 @@ listItemToLaTeX lst
913911 -- element in an item. This will look ugly in LaTeX regardless, but
914912 -- this will keep the typesetter from throwing an error.
915913 | (Header{} :_) <- lst =
916− blockListToLaTeX lst >>= return . (text "\\item ~" $$) . nest 2
914+ (text "\\item ~" $$) . nest 2 <$> blockListToLaTeX lst
917915 | Plain (Str "☐":Space:is) : bs <- lst = taskListItem False is bs
918916 | Plain (Str "☒":Space:is) : bs <- lst = taskListItem True is bs
919917 | Para (Str "☐":Space:is) : bs <- lst = taskListItem False is bs
920918 | Para (Str "☒":Space:is) : bs <- lst = taskListItem True is bs
921− | otherwise = blockListToLaTeX lst >>= return . (text "\\item" $$) . nest 2
919+ | otherwise = (text "\\item" $$) . nest 2 <$> blockListToLaTeX lst
922920 where
923921 taskListItem checked is bs = do
924922 let checkbox = if checked
@@ -1073,7 +1071,7 @@ hypertarget addnewline ident x = do
10731071 return $ text "\\hypertarget"
10741072 <> braces ref
10751073 <> braces ((if addnewline && not (isEmpty x)
1076− then ("%" <> cr)
1074+ then "%" <> cr
10771075 else empty) <> x)
10781076
10791077labelFor :: PandocMonad m => Text -> LW m (Doc Text)
@@ -1086,9 +1084,8 @@ labelFor ident = do
10861084inlineListToLaTeX :: PandocMonad m
10871085 => [Inline] -- ^ Inlines to convert
10881086 -> LW m (Doc Text)
1089−inlineListToLaTeX lst =
1087+inlineListToLaTeX lst = hcat <$>
10901088 mapM inlineToLaTeX (fixLineInitialSpaces . fixInitialLineBreaks $ lst)
1091− >>= return . hcat
10921089 -- nonbreaking spaces (~) in LaTeX don't work after line breaks,
10931090 -- so we turn nbsps after hard breaks to \hspace commands.
10941091 -- this is mostly used in verse.
@@ -1135,10 +1132,8 @@ inlineToLaTeX (Span (id',classes,kvs) ils) = do
11351132 (if null cmds
11361133 then braces contents
11371134 else foldr inCmd contents cmds)
1138−inlineToLaTeX (Emph lst) =
1139− inlineListToLaTeX lst >>= return . inCmd "emph"
1140−inlineToLaTeX (Strong lst) =
1141− inlineListToLaTeX lst >>= return . inCmd "textbf"
1135+inlineToLaTeX (Emph lst) = inCmd "emph" <$> inlineListToLaTeX lst
1136+inlineToLaTeX (Strong lst) = inCmd "textbf" <$> inlineListToLaTeX lst
11421137inlineToLaTeX (Strikeout lst) = do
11431138 -- we need to protect VERB in an mbox or we get an error
11441139 -- see #1294
@@ -1148,11 +1143,11 @@ inlineToLaTeX (Strikeout lst) = do
11481143 modify $ \s -> s{ stStrikeout = True }
11491144 return $ inCmd "sout" contents
11501145inlineToLaTeX (Superscript lst) =
1151− inlineListToLaTeX lst >>= return . inCmd "textsuperscript"
1146+ inCmd "textsuperscript" <$> inlineListToLaTeX lst
11521147inlineToLaTeX (Subscript lst) =
1153− inlineListToLaTeX lst >>= return . inCmd "textsubscript"
1148+ inCmd "textsubscript" <$> inlineListToLaTeX lst
11541149inlineToLaTeX (SmallCaps lst) =
1155− inlineListToLaTeX lst >>= return . inCmd "textsc"
1150+ inCmd "textsc"<$> inlineListToLaTeX lst
11561151inlineToLaTeX (Cite cits lst) = do
11571152 st <- get
11581153 let opts = stOptions st
@@ -1168,7 +1163,7 @@ inlineToLaTeX (Code (_,classes,kvs) str) = do
11681163 let listingsCode = do
11691164 let listingsopts = (case getListingsLanguage classes of
11701165 Just l -> (("language", mbBraced l):)
1171− Nothing -> id) $
1166+ Nothing -> id)
11721167 [(k,v) | (k,v) <- kvs
11731168 , k `notElem` ["exports","tangle","results"]]
11741169 let listingsopt = if null listingsopts
@@ -1241,8 +1236,8 @@ inlineToLaTeX (Math DisplayMath str) = do
12411236 return $ "\\[" <> literal (handleMathComment str) <> "\\]"
12421237inlineToLaTeX il@(RawInline f str) = do
12431238 beamer <- gets stBeamer
1244− if (f == Format "latex" || f == Format "tex" ||
1245− (f == Format "beamer" && beamer))
1239+ if f == Format "latex" || f == Format "tex" ||
1240+ (f == Format "beamer" && beamer)
12461241 then do
12471242 setEmptyLine False
12481243 return $ literal str