aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Writers/LaTeX.hs
blob: de1b7e207f261e1603f168089ff7dcc418b78abf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
{-
Copyright (C) 2006 John MacFarlane <jgm at berkeley dot edu>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-}

{- |
   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   : alpha 
   Portability : portable

Conversion of 'Pandoc' format into LaTeX.
-}
module Text.Pandoc.Writers.LaTeX ( 
                                  writeLaTeX 
                                 ) where
import Text.Pandoc.Definition
import Text.Pandoc.Shared
import Text.Printf ( printf )
import List ( (\\) )

-- | Convert Pandoc to LaTeX.
writeLaTeX :: WriterOptions -> Pandoc -> String
writeLaTeX options (Pandoc meta blocks) = 
  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
            -> 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

-- escape things as needed for LaTeX (also ldots, dashes, quotes, etc.) 

escapeBrackets  = backslashEscape "{}"
escapeSpecial   = backslashEscape "$%&~_#"

escapeBackslash = substitute "\\" "\\textbackslash{}" 
fixBackslash    = substitute "\\textbackslash\\{\\}" "\\textbackslash{}"
escapeHat       = substitute "^" "\\^{}"
escapeBar       = substitute "|" "\\textbar{}"
escapeLt        = substitute "<" "\\textless{}"
escapeGt        = substitute ">" "\\textgreater{}"

-- | Escape string for LaTeX
stringToLaTeX :: String -> String
stringToLaTeX = escapeGt . escapeLt . escapeBar . escapeHat . 
                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)
deVerb :: [Inline] -> [Inline]
deVerb [] = []
deVerb ((Code str):rest) = (Str str):(deVerb rest)
deVerb (other:rest) = other:(deVerb rest)

-- | Convert Pandoc block element to LaTeX.
blockToLaTeX :: [Block]   -- ^ List of note blocks to use in resolving note refs
             -> Block     -- ^ Block to convert
             -> String 
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 (Note ref lst) = ""
blockToLaTeX notes (Key _ _) = ""
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 (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"
blockToLaTeX notes (Table caption aligns widths heads rows) =
    let colWidths = map printDecimal widths
        colDescriptors = concat $ zipWith
                                  (\width align -> ">{\\PBS" ++ 
                                  (case align of 
                                         AlignLeft -> "\\raggedright"
                                         AlignRight -> "\\raggedleft"
                                         AlignCenter -> "\\centering"
                                         AlignDefault -> "\\raggedright") ++
                                  "\\hspace{0pt}}p{" ++ width ++ 
                                  "\\textwidth}")
                                  colWidths aligns
        headers        = tableRowToLaTeX notes heads 
        captionText    = inlineListToLaTeX notes caption 
        tableBody      = "\\begin{tabular}{" ++ colDescriptors ++ "}\n" ++
                         headers ++ "\\hline\n" ++ 
                         (concatMap (tableRowToLaTeX notes) rows) ++ 
                         "\\end{tabular}\n" 
        centered str   = "\\begin{center}\n" ++ str ++ "\\end{center}\n" in
    if null captionText
      then centered tableBody ++ "\n"
      else "\\begin{table}[h]\n" ++ centered tableBody ++ "\\caption{" ++
           captionText ++ "}\n" ++ "\\end{table}\n\n" 
      

printDecimal :: Float -> String
printDecimal = printf "%.2f" 

tableColumnWidths notes cols = map (length . (concatMap (blockToLaTeX notes))) cols

tableRowToLaTeX notes cols = joinWithSep " & " (map (concatMap (blockToLaTeX notes)) cols) ++ "\\\\\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 = 
  concatMap (inlineToLaTeX notes) lst

isQuoted :: Inline -> Bool
isQuoted (Quoted _ _) = True
isQuoted Apostrophe = True
isQuoted _ = False

-- | 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 (Code str) = "\\verb" ++ [chr] ++ stuffing ++ [chr]
                     where stuffing = str 
                           chr      = ((enumFromTo '!' '~') \\ stuffing) !! 0
inlineToLaTeX notes (Quoted SingleQuote lst) =
  let s1 = if (not (null lst)) && (isQuoted (head lst)) then "\\," else ""
      s2 = if (not (null lst)) && (isQuoted (last lst)) then "\\," else "" in
  "`" ++ s1 ++ inlineListToLaTeX notes lst ++ s2 ++ "'"
inlineToLaTeX notes (Quoted DoubleQuote lst) =
  let s1 = if (not (null lst)) && (isQuoted (head lst)) then "\\," else ""
      s2 = if (not (null lst)) && (isQuoted (last lst)) then "\\," else "" in
  "``" ++ s1 ++ inlineListToLaTeX notes lst ++ s2 ++ "''"
inlineToLaTeX notes Apostrophe = "'"
inlineToLaTeX notes EmDash = "---"
inlineToLaTeX notes EnDash = "--"
inlineToLaTeX notes Ellipses = "\\ldots{}"
inlineToLaTeX notes (Str str) = stringToLaTeX str
inlineToLaTeX notes (TeX str) = str
inlineToLaTeX notes (HtmlInline str) = ""
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 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 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)