Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to use listings package for code blocks in LaTeX #16

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README
Expand Up @@ -269,6 +269,9 @@ Options
: Number section headings in LaTeX, ConTeXt, or HTML output.
By default, sections are not numbered.

`--listings`
: Use listings package for LaTeX code blocks

`--section-divs`
: Wrap sections in `<div>` tags (or `<section>` tags in HTML5),
and attach identifiers to the enclosing `<div>` (or `<section>`)
Expand Down
2 changes: 2 additions & 0 deletions src/Text/Pandoc/Shared.hs
Expand Up @@ -483,6 +483,7 @@ data WriterOptions = WriterOptions
, writerBiblioFiles :: [FilePath] -- ^ Biblio files to use for citations
, writerHtml5 :: Bool -- ^ Produce HTML5
, writerChapters :: Bool -- ^ Use "chapter" for top-level sects
, writerListings :: Bool -- ^ Use listings package for code
} deriving Show

-- | Default writer options.
Expand Down Expand Up @@ -514,6 +515,7 @@ defaultWriterOptions =
, writerBiblioFiles = []
, writerHtml5 = False
, writerChapters = False
, writerListings = False
}

--
Expand Down
47 changes: 39 additions & 8 deletions src/Text/Pandoc/Writers/LaTeX.hs
Expand Up @@ -117,6 +117,7 @@ pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
[ ("lhs", "yes") | stLHS st ] ++
[ ("graphics", "yes") | stGraphics st ] ++
[ ("book-class", "yes") | stBook st] ++
[ ("listings", "yes") | writerListings options ] ++
citecontext
return $ if writerStandalone options
then renderTemplate context template
Expand Down Expand Up @@ -170,19 +171,47 @@ blockToLaTeX (Para lst) = do
blockToLaTeX (BlockQuote lst) = do
contents <- blockListToLaTeX lst
return $ "\\begin{quote}" $$ contents $$ "\\end{quote}"
blockToLaTeX (CodeBlock (_,classes,_) str) = do
blockToLaTeX (CodeBlock (_,classes,keyvalAttr) str) = do
st <- get
env <- if writerLiterateHaskell (stOptions st) && "haskell" `elem` classes &&
"literate" `elem` classes
then do
modify $ \s -> s{ stLHS = True }
return "code"
else if stInNote st
then do
modify $ \s -> s{ stVerbInNote = True }
return "Verbatim"
else return "verbatim"
return $ "\\begin{" <> text env <> "}" $$ flush (text str) $$
else if writerListings (stOptions st)
then return "lstlisting"
else if stInNote st
then do
modify $ \s -> s{ stVerbInNote = True }
return "Verbatim"
else return "verbatim"
let params = if writerListings (stOptions st)
then take 1
[ "language=" ++ lang | lang <- classes
, lang `elem` ["ABAP","IDL","Plasm","ACSL","inform"
,"POV","Ada","Java","Prolog","Algol"
,"JVMIS","Promela","Ant","ksh","Python"
,"Assembler","Lisp","R","Awk","Logo"
,"Reduce","bash","make","Rexx","Basic"
,"Mathematica","RSL","C","Matlab","Ruby"
,"C++","Mercury","S","Caml","MetaPost"
,"SAS","Clean","Miranda","Scilab","Cobol"
,"Mizar","sh","Comal","ML","SHELXL","csh"
,"Modula-2","Simula","Delphi","MuPAD"
,"SQL","Eiffel","NASTRAN","tcl","Elan"
,"Oberon-2","TeX","erlang","OCL"
,"VBScript","Euphoria","Octave","Verilog"
,"Fortran","Oz","VHDL","GCL","Pascal"
,"VRML","Gnuplot","Perl","XML","Haskell"
,"PHP","XSLT","HTML","PL/I"]
] ++
[ key ++ "=" ++ attr | (key,attr) <- keyvalAttr ]
else []
printParams
| null params = empty
| otherwise = "[" <> hsep (intersperse "," (map text params)) <>
"]"
return $ "\\begin{" <> text env <> "}" <> printParams $$ flush (text str) $$
"\\end{" <> text env <> "}" $$ cr -- final cr needed because of footnotes
blockToLaTeX (RawHtml _) = return empty
blockToLaTeX (BulletList lst) = do
Expand Down Expand Up @@ -334,7 +363,9 @@ inlineToLaTeX (Code str) = do
st <- get
when (stInNote st) $ modify $ \s -> s{ stVerbInNote = True }
let chr = ((enumFromTo '!' '~') \\ str) !! 0
return $ text $ "\\verb" ++ [chr] ++ str ++ [chr]
if writerListings (stOptions st)
then return $ text $ "\\lstinline" ++ [chr] ++ str ++ [chr]
else return $ text $ "\\verb" ++ [chr] ++ str ++ [chr]
inlineToLaTeX (Quoted SingleQuote lst) = do
contents <- inlineListToLaTeX lst
let s1 = if (not (null lst)) && (isQuoted (head lst))
Expand Down
11 changes: 10 additions & 1 deletion src/pandoc.hs
Expand Up @@ -122,6 +122,7 @@ data Opt = Opt
, optCiteMethod :: CiteMethod -- ^ Method to output cites
, optBibliography :: [String]
, optCslFile :: FilePath
, optListings :: Bool -- ^ Use listings package for code blocks
}

-- | Defaults for command-line options.
Expand Down Expand Up @@ -164,6 +165,7 @@ defaultOpts = Opt
, optCiteMethod = Citeproc
, optBibliography = []
, optCslFile = ""
, optListings = False
}

-- | A list of functions, each transforming the options data structure
Expand Down Expand Up @@ -312,6 +314,11 @@ options =
(\opt -> return opt { optNumberSections = True }))
"" -- "Number sections in LaTeX"

, Option "" ["listings"]
(NoArg
(\opt -> return opt { optListings = True }))
"" -- "Use listings package for LaTeX code blocks"

, Option "" ["section-divs"]
(NoArg
(\opt -> return opt { optSectionDivs = True }))
Expand Down Expand Up @@ -667,6 +674,7 @@ main = do
, optBibliography = reffiles
, optCslFile = cslfile
, optCiteMethod = citeMethod
, optListings = listings
} = opts

when dumpArgs $
Expand Down Expand Up @@ -795,7 +803,8 @@ main = do
writerUserDataDir = datadir,
writerHtml5 = html5 &&
"html" `isPrefixOf` writerName',
writerChapters = chapters }
writerChapters = chapters,
writerListings = listings }

when (isNonTextOutput writerName' && outputFile == "-") $
do UTF8.hPutStrLn stderr ("Error: Cannot write " ++ writerName ++ " output to stdout.\n" ++
Expand Down
3 changes: 3 additions & 0 deletions templates/latex.template
Expand Up @@ -69,6 +69,9 @@ $endif$
\usepackage[breaklinks=true,unicode=true,pdfborder={0 0 0}]{hyperref}
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
$if(listings)$
\usepackage{listings}
$endif$
$if(numbersections)$
$else$
\setcounter{secnumdepth}{0}
Expand Down