git.iko.soy/typeable/pandoc
on master help sign in
Write id for code block to label attr in latex when listing is used

The code: ~~~{#test} asdf ~~~ gets compiled to html: <pre id="test"> asdf </pre> So it is possible to link to the identifier `test` But this doesn't happen on latex When using the listings package (`--listings`) it is possible to set the identifier using the `label=test` property: \begin{lstlisting}[label=id] hi \end{lstlisting} And this is exactly what this patch is doing. Modified LaTeX Reader/Writer and added tests for this.

commit5f09cf7ff033ae11c5094fe39f8cd2ac11657229 authorFlorian Eitel <florian.eitel@wooga.net> dateThu 22 Aug 2013, 20:15 (13 years ago) parentf7c14ed
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
@@ -47,6 +47,7 @@ import Text.Pandoc.Builder
4747import Data.Char (isLetter)
4848import Control.Applicative
4949import Data.Monoid
50+import Data.Maybe (fromMaybe)
5051import System.Environment (getEnv)
5152import System.FilePath (replaceExtension, (</>))
5253import Data.List (intercalate, intersperse)
@@ -901,7 +902,7 @@ environments = M.fromList
901902 lookup "numbers" options == Just "left" ]
902903 ++ maybe [] (:[]) (lookup "language" options
903904 >>= fromListingsLanguage)
904− let attr = ("",classes,kvs)
905+ let attr = (fromMaybe "" (lookup "label" options),classes,kvs)
905906 codeBlockWith attr <$> (verbEnv "lstlisting"))
906907 , ("minted", do options <- option [] keyvals
907908 lang <- grouped (many1 $ satisfy (/='}'))
src/Text/Pandoc/Writers/LaTeX.hs+6−2
@@ -313,7 +313,7 @@ blockToLaTeX (BlockQuote lst) = do
313313 _ -> do
314314 contents <- blockListToLaTeX lst
315315 return $ "\\begin{quote}" $$ contents $$ "\\end{quote}"
316−blockToLaTeX (CodeBlock (_,classes,keyvalAttr) str) = do
316+blockToLaTeX (CodeBlock (identifier,classes,keyvalAttr) str) = do
317317 opts <- gets stOptions
318318 case () of
319319 _ | isEnabled Ext_literate_haskell opts && "haskell" `elem` classes &&
@@ -344,7 +344,11 @@ blockToLaTeX (CodeBlock (_,classes,keyvalAttr) str) = do
344344 [ (if key == "startFrom"
345345 then "firstnumber"
346346 else key) ++ "=" ++ attr |
347− (key,attr) <- keyvalAttr ]
347+ (key,attr) <- keyvalAttr ] ++
348+ (if identifier == ""
349+ then []
350+ else [ "label=" ++ identifier ])
351+
348352 else []
349353 printParams
350354 | 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\\}}") =?>