git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #961 from nougad/add_latex_listings_label

Write id for code block to label attr in latex when listing is used

commite7a4bcc6fed37afbba423e4c41df6043d7158b3f authorJohn MacFarlane <jgm@berkeley.edu> dateSun 25 Aug 2013, 20:48 (13 years ago) parenta68805b5f09cf7
4 files changed+22−3
src/Text/Pandoc/Readers/LaTeX.hs+2−1 src/Text/Pandoc/Writers/LaTeX.hs+6−2 tests/Tests/Readers/LaTeX.hs+7 tests/Tests/Writers/LaTeX.hs+7
src/Text/Pandoc/Readers/LaTeX.hs+2−1
@@ -46,6 +46,7 @@ import Text.Pandoc.Builder
4646import Data.Char (isLetter)
4747import Control.Applicative
4848import Data.Monoid
49+import Data.Maybe (fromMaybe)
4950import System.Environment (getEnv)
5051import System.FilePath (replaceExtension, (</>))
5152import Data.List (intercalate, intersperse)
@@ -898,7 +899,7 @@ environments = M.fromList
898899 lookup "numbers" options == Just "left" ]
899900 ++ maybe [] (:[]) (lookup "language" options
900901 >>= fromListingsLanguage)
901− let attr = ("",classes,kvs)
902+ let attr = (fromMaybe "" (lookup "label" options),classes,kvs)
902903 codeBlockWith attr <$> (verbEnv "lstlisting"))
903904 , ("minted", do options <- option [] keyvals
904905 lang <- grouped (many1 $ satisfy (/='}'))
src/Text/Pandoc/Writers/LaTeX.hs+6−2
@@ -309,7 +309,7 @@ blockToLaTeX (BlockQuote lst) = do
309309 _ -> do
310310 contents <- blockListToLaTeX lst
311311 return $ "\\begin{quote}" $$ contents $$ "\\end{quote}"
312−blockToLaTeX (CodeBlock (_,classes,keyvalAttr) str) = do
312+blockToLaTeX (CodeBlock (identifier,classes,keyvalAttr) str) = do
313313 opts <- gets stOptions
314314 case () of
315315 _ | isEnabled Ext_literate_haskell opts && "haskell" `elem` classes &&
@@ -340,7 +340,11 @@ blockToLaTeX (CodeBlock (_,classes,keyvalAttr) str) = do
340340 [ (if key == "startFrom"
341341 then "firstnumber"
342342 else key) ++ "=" ++ attr |
343− (key,attr) <- keyvalAttr ]
343+ (key,attr) <- keyvalAttr ] ++
344+ (if identifier == ""
345+ then []
346+ else [ "label=" ++ identifier ])
347+
344348 else []
345349 printParams
346350 | null params = empty
tests/Tests/Readers/LaTeX.hs+7
@@ -55,6 +55,13 @@ tests = [ testGroup "basic"
5555 "hi % this is a comment\nthere\n" =?> para "hi there"
5656 ]
5757
58+ , testGroup "code blocks"
59+ [ "identifier" =:
60+ "\\begin{lstlisting}[label=test]\\end{lstlisting}" =?> codeBlockWith ("test", [], [("label","test")]) ""
61+ , "no identifier" =:
62+ "\\begin{lstlisting}\\end{lstlisting}" =?> codeBlock ""
63+ ]
64+
5865 , testGroup "citations"
5966 [ natbibCitations
6067 , biblatexCitations
tests/Tests/Writers/LaTeX.hs+7
@@ -10,6 +10,9 @@ import Tests.Arbitrary()
1010latex :: (ToString a, ToPandoc a) => a -> String
1111latex = writeLaTeX def . toPandoc
1212
13+latexListing :: (ToString a, ToPandoc a) => a -> String
14+latexListing = writeLaTeX def{ writerListings = True } . toPandoc
15+
1316{-
1417 "my test" =: X =?> Y
1518
@@ -31,6 +34,10 @@ tests :: [Test]
3134tests = [ testGroup "code blocks"
3235 [ "in footnotes" =: note (para "hi" <> codeBlock "hi") =?>
3336 "\\footnote{hi\n\n\\begin{Verbatim}\nhi\n\\end{Verbatim}\n}"
37+ , test latexListing "identifier" $ codeBlockWith ("id",[],[]) "hi" =?>
38+ ("\\begin{lstlisting}[label=id]\nhi\n\\end{lstlisting}" :: String)
39+ , test latexListing "no identifier" $ codeBlock "hi" =?>
40+ ("\\begin{lstlisting}\nhi\n\\end{lstlisting}" :: String)
3441 ]
3542 , testGroup "math"
3643 [ "escape |" =: para (math "\\sigma|_{\\{x\\}}") =?>