diff options
author | fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> | 2006-12-20 06:50:14 +0000 |
---|---|---|
committer | fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> | 2006-12-20 06:50:14 +0000 |
commit | dc9c6450f3b16592d0ee865feafc17b670e4ad14 (patch) | |
tree | dc29955e1ea518d6652af3d12876863b19819f6d /src/Text/Pandoc/Writers/LaTeX.hs | |
parent | 42d29838960f9aed3a08a4d76fc7e9c3941680a8 (diff) | |
download | pandoc-dc9c6450f3b16592d0ee865feafc17b670e4ad14.tar.gz |
+ Added module data for haddock.
+ Reformatted code consistently.
git-svn-id: https://pandoc.googlecode.com/svn/trunk@252 788f1e2b-df1e-0410-8736-df70ead52e1b
Diffstat (limited to 'src/Text/Pandoc/Writers/LaTeX.hs')
-rw-r--r-- | src/Text/Pandoc/Writers/LaTeX.hs | 155 |
1 files changed, 85 insertions, 70 deletions
diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs index 22a96a423..3a3d249e9 100644 --- a/src/Text/Pandoc/Writers/LaTeX.hs +++ b/src/Text/Pandoc/Writers/LaTeX.hs @@ -1,4 +1,14 @@ --- | Convert Pandoc to LaTeX. +{- | + Module : Text.Pandoc.Writers.LaTeX + Copyright : Copyright (C) 2006 John MacFarlane + License : GNU GPL, version 2 or above + + Maintainer : John MacFarlane <jgm at berkeley dot edu> + Stability : unstable + Portability : portable + +Conversion of 'Pandoc' format into LaTeX. +-} module Text.Pandoc.Writers.LaTeX ( writeLaTeX ) where @@ -9,46 +19,40 @@ import List ( (\\) ) -- | Convert Pandoc to LaTeX. writeLaTeX :: WriterOptions -> Pandoc -> String writeLaTeX options (Pandoc meta blocks) = - let notes = filter isNoteBlock blocks in -- assumes all notes are at outer level - let body = (writerIncludeBefore options) ++ - (concatMap (blockToLaTeX notes) (replaceReferenceLinks blocks)) ++ - (writerIncludeAfter options) in - let head = if writerStandalone options then - latexHeader notes options meta - else - "" in - let foot = if writerStandalone options then "\n\\end{document}\n" else "" in - head ++ body ++ foot + let notes = filter isNoteBlock blocks in -- assumes all notes at outer level + let body = (writerIncludeBefore options) ++ + (concatMap (blockToLaTeX notes) + (replaceReferenceLinks blocks)) ++ + (writerIncludeAfter options) in + let head = if writerStandalone options + then latexHeader notes options meta + else "" in + let foot = if writerStandalone options then "\n\\end{document}\n" else "" in + head ++ body ++ foot -- | Insert bibliographic information into LaTeX header. -latexHeader :: [Block] -- ^ List of note blocks to use in resolving note refs - -> WriterOptions -- ^ Options, including LaTeX header - -> Meta -- ^ Meta with bibliographic information +latexHeader :: [Block] -- ^ List of note blocks to use in resolving note refs + -> WriterOptions -- ^ Options, including LaTeX header + -> Meta -- ^ Meta with bibliographic information -> String latexHeader notes options (Meta title authors date) = - let titletext = if null title then - "" - else - "\\title{" ++ inlineListToLaTeX notes title ++ "}\n" - authorstext = if null authors then - "" - else - "\\author{" ++ (joinWithSep "\\\\" (map stringToLaTeX authors)) ++ "}\n" - datetext = if date == "" then - "" - else - "\\date{" ++ stringToLaTeX date ++ "}\n" - maketitle = if null title then - "" - else - "\\maketitle\n" - secnumline = if (writerNumberSections options) then - "" - else - "\\setcounter{secnumdepth}{0}\n" - header = writerHeader options in - header ++ secnumline ++ titletext ++ authorstext ++ datetext ++ "\\begin{document}\n" ++ maketitle - + let titletext = if null title + then "" + else "\\title{" ++ inlineListToLaTeX notes title ++ "}\n" + authorstext = if null authors + then "" + else "\\author{" ++ (joinWithSep "\\\\" + (map stringToLaTeX authors)) ++ "}\n" + datetext = if date == "" + then "" + else "\\date{" ++ stringToLaTeX date ++ "}\n" + maketitle = if null title then "" else "\\maketitle\n" + secnumline = if (writerNumberSections options) + then "" + else "\\setcounter{secnumdepth}{0}\n" + header = writerHeader options in + header ++ secnumline ++ titletext ++ authorstext ++ datetext ++ + "\\begin{document}\n" ++ maketitle -- escape things as needed for LaTeX (also ldots, dashes, quotes, etc.) @@ -77,7 +81,8 @@ escapeSingleQuotes = gsub "([^[:punct:][:space:]])`(s|S)" "\\1'\\2" . -- catch possessives gsub "^'([^[:punct:][:space:]])" "`\\1" . -- 'word left gsub "([[:space:]])'" "\\1`" . -- never right quote after space - gsub "([[:space:]])'([^[:punct:][:space:]])" "\\1`\\2" -- 'word left (leave possessives) + gsub "([[:space:]])'([^[:punct:][:space:]])" "\\1`\\2" + -- 'word left (leave possessives) escapeEllipses = gsub "\\.\\.\\.|\\. \\. \\." "\\ldots{}" @@ -85,12 +90,14 @@ escapeDashes = gsub "([0-9])-([0-9])" "\\1--\\2" . gsub " *--- *" "---" . gsub "([^-])--([^-])" "\\1---\\2" -escapeSmart = escapeDashes . escapeSingleQuotes . escapeDoubleQuotes . escapeEllipses +escapeSmart = escapeDashes . escapeSingleQuotes . escapeDoubleQuotes . + escapeEllipses -- | Escape string for LaTeX (including smart quotes, dashes, ellipses) stringToLaTeX :: String -> String stringToLaTeX = escapeSmart . escapeGt . escapeLt . escapeBar . escapeHat . - escapeSpecial . fixBackslash . escapeBrackets . escapeBackslash + escapeSpecial . fixBackslash . escapeBrackets . + escapeBackslash -- | Remove all code elements from list of inline elements -- (because it's illegal to have a \\verb inside a command argument) @@ -107,43 +114,47 @@ blockToLaTeX notes Blank = "\n" blockToLaTeX notes Null = "" blockToLaTeX notes (Plain lst) = inlineListToLaTeX notes lst ++ "\n" blockToLaTeX notes (Para lst) = (inlineListToLaTeX notes lst) ++ "\n\n" -blockToLaTeX notes (BlockQuote lst) = - "\\begin{quote}\n" ++ (concatMap (blockToLaTeX notes) lst) ++ "\\end{quote}\n" +blockToLaTeX notes (BlockQuote lst) = "\\begin{quote}\n" ++ + (concatMap (blockToLaTeX notes) lst) ++ "\\end{quote}\n" blockToLaTeX notes (Note ref lst) = "" blockToLaTeX notes (Key _ _) = "" -blockToLaTeX notes (CodeBlock str) = "\\begin{verbatim}\n" ++ str ++ "\n\\end{verbatim}\n" +blockToLaTeX notes (CodeBlock str) = "\\begin{verbatim}\n" ++ str ++ + "\n\\end{verbatim}\n" blockToLaTeX notes (RawHtml str) = "" -blockToLaTeX notes (BulletList lst) = - "\\begin{itemize}\n" ++ (concatMap (listItemToLaTeX notes) lst) ++ "\\end{itemize}\n" -blockToLaTeX notes (OrderedList lst) = - "\\begin{enumerate}\n" ++ (concatMap (listItemToLaTeX notes) lst) ++ "\\end{enumerate}\n" -blockToLaTeX notes HorizontalRule = "\\begin{center}\\rule{3in}{0.4pt}\\end{center}\n\n" +blockToLaTeX notes (BulletList lst) = "\\begin{itemize}\n" ++ + (concatMap (listItemToLaTeX notes) lst) ++ "\\end{itemize}\n" +blockToLaTeX notes (OrderedList lst) = "\\begin{enumerate}\n" ++ + (concatMap (listItemToLaTeX notes) lst) ++ "\\end{enumerate}\n" +blockToLaTeX notes HorizontalRule = + "\\begin{center}\\rule{3in}{0.4pt}\\end{center}\n\n" blockToLaTeX notes (Header level lst) = - if (level > 0) && (level <= 3) then - "\\" ++ (concat (replicate (level - 1) "sub")) ++ "section{" ++ - (inlineListToLaTeX notes (deVerb lst)) ++ "}\n\n" - else - (inlineListToLaTeX notes lst) ++ "\n\n" -listItemToLaTeX notes list = "\\item " ++ (concatMap (blockToLaTeX notes) list) + if (level > 0) && (level <= 3) + then "\\" ++ (concat (replicate (level - 1) "sub")) ++ "section{" ++ + (inlineListToLaTeX notes (deVerb lst)) ++ "}\n\n" + else (inlineListToLaTeX notes lst) ++ "\n\n" +listItemToLaTeX notes list = "\\item " ++ + (concatMap (blockToLaTeX notes) list) -- | Convert list of inline elements to LaTeX. inlineListToLaTeX :: [Block] -- ^ List of note blocks to use in resolving note refs -> [Inline] -- ^ Inlines to convert -> String inlineListToLaTeX notes lst = - -- first, consolidate Str and Space for more effective smartquotes: - let lst' = consolidateList lst in - concatMap (inlineToLaTeX notes) lst' + -- first, consolidate Str and Space for more effective smartquotes: + let lst' = consolidateList lst in + concatMap (inlineToLaTeX notes) lst' -- | Convert inline element to LaTeX inlineToLaTeX :: [Block] -- ^ List of note blocks to use in resolving note refs -> Inline -- ^ Inline to convert -> String -inlineToLaTeX notes (Emph lst) = "\\emph{" ++ (inlineListToLaTeX notes (deVerb lst)) ++ "}" -inlineToLaTeX notes (Strong lst) = "\\textbf{" ++ (inlineListToLaTeX notes (deVerb lst)) ++ "}" +inlineToLaTeX notes (Emph lst) = "\\emph{" ++ + (inlineListToLaTeX notes (deVerb lst)) ++ "}" +inlineToLaTeX notes (Strong lst) = "\\textbf{" ++ + (inlineListToLaTeX notes (deVerb lst)) ++ "}" inlineToLaTeX notes (Code str) = "\\verb" ++ [chr] ++ stuffing ++ [chr] - where stuffing = str - chr = ((enumFromTo '!' '~') \\ stuffing) !! 0 + where stuffing = str + chr = ((enumFromTo '!' '~') \\ stuffing) !! 0 inlineToLaTeX notes (Str str) = stringToLaTeX str inlineToLaTeX notes (TeX str) = str inlineToLaTeX notes (HtmlInline str) = "" @@ -151,18 +162,22 @@ inlineToLaTeX notes (LineBreak) = "\\\\\n" inlineToLaTeX notes Space = " " inlineToLaTeX notes (Link text (Src src tit)) = "\\href{" ++ src ++ "}{" ++ (inlineListToLaTeX notes (deVerb text)) ++ "}" -inlineToLaTeX notes (Link text (Ref [])) = "[" ++ (inlineListToLaTeX notes text) ++ "]" -inlineToLaTeX notes (Link text (Ref ref)) = "[" ++ (inlineListToLaTeX notes text) ++ "][" ++ - (inlineListToLaTeX notes ref) ++ "]" -- this is what markdown does, for better or worse -inlineToLaTeX notes (Image alternate (Src source tit)) = "\\includegraphics{" ++ source ++ "}" +inlineToLaTeX notes (Link text (Ref [])) = "[" ++ + (inlineListToLaTeX notes text) ++ "]" +inlineToLaTeX notes (Link text (Ref ref)) = "[" ++ + (inlineListToLaTeX notes text) ++ "][" ++ (inlineListToLaTeX notes ref) ++ + "]" -- this is what markdown does, for better or worse +inlineToLaTeX notes (Image alternate (Src source tit)) = + "\\includegraphics{" ++ source ++ "}" inlineToLaTeX notes (Image alternate (Ref [])) = "![" ++ (inlineListToLaTeX notes alternate) ++ "]" inlineToLaTeX notes (Image alternate (Ref ref)) = - "![" ++ (inlineListToLaTeX notes alternate) ++ "][" ++ (inlineListToLaTeX notes ref) ++ "]" + "![" ++ (inlineListToLaTeX notes alternate) ++ "][" ++ + (inlineListToLaTeX notes ref) ++ "]" inlineToLaTeX [] (NoteRef ref) = "" inlineToLaTeX ((Note firstref firstblocks):rest) (NoteRef ref) = - if (firstref == ref) then - "\\footnote{" ++ (stripTrailingNewlines (concatMap (blockToLaTeX rest) firstblocks)) ++ "}" - else - inlineToLaTeX rest (NoteRef ref) + if (firstref == ref) + then "\\footnote{" ++ (stripTrailingNewlines + (concatMap (blockToLaTeX rest) firstblocks)) ++ "}" + else inlineToLaTeX rest (NoteRef ref) |