Text.Latex

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
{-# OPTIONS -fno-warn-unused-do-bind -fno-warn-missing-signatures -fno-warn-name-shadowing
            -fno-warn-type-defaults #-}
{-# LANGUAGE FlexibleContexts, FlexibleInstances #-}

module Text.LaTeX where

import Data.Maybe
import Prelude hiding (and)
import Control.Monad.Writer
import Data.List (intercalate)
import Text.Regex

runLaTeX m = do ((),xs) <- runWriterT m
                return xs

renderLaTeXen :: [LaTeX] -> String
renderLaTeXen = unlines . map renderLaTeX

renderLaTeX :: LaTeX -> [Char]
renderLaTeX (Cmd name props bodies) =
  join ["\\",name,doProps props
       ,maybe "" doBodies bodies]
    where doProps Nothing = ""
          doProps (Just props) = "[" ++ intercalate "," (map renderLaTeX props) ++ "]"
          doBodies = concat . map (braces . intercalate "\n" . map renderLaTeX)
          braces x = "{" ++ x ++ "}"
renderLaTeX Newline = "\\\\"
renderLaTeX (Text txt) = quotes $ concatMap escape txt
renderLaTeX (Comment txt) = "% " ++ quotes (concatMap escape txt)
renderLaTeX (Num n) = show n
renderLaTeX (Raw x) = x

quotes x = subRegex (mkRegex " \"") (subRegex (mkRegex " '") x " `") " ``"

escape :: Char -> String
escape c = fromMaybe [c] $ lookup c specials where
  specials = [('<',"\\textless{}")
             ,('>',"\\textgreater{}")
             ,('~',"\\textasciitilde{}")
             ,('^',"\\textasciicircum{}")
             ,('&',"\\&amp;")
             ,('#',"\\#")
             ,('_',"\\_")
             ,('$',"\\$")
             ,('%',"\\%")
             ,('|',"\\docbooktolatexpipe{}")
             ,('{',"\\{")
             ,('}',"\\}")
             ,('\\',"\\textbackslash{}")]

(!) :: (MonadWriter [LaTeX] m) => (a1 -> m a) -> [WriterT [LaTeX] [] ()] -> a1 -> m a
cmd ! props = censor updateProps . cmd where
  updateProps [Cmd name Nothing content] =
    [Cmd name (Just $ concat $ concat $ map runLaTeX props) content]
  updateProps xs = xs

t :: MonadWriter [LaTeX] m => String -> m ()
t txt = tell [Text txt]

num :: MonadWriter [LaTeX] m => Integer -> m ()
num n = tell [Num n]

data LaTeX = Cmd String (Maybe [LaTeX]) (Maybe [[LaTeX]]) | Newline | Text String | Num Integer
     | Raw String | Comment String
  deriving Show

chapter' :: MonadWriter [LaTeX] m => m () -> m ()
chapter' = cmd "chapter*"

section' :: MonadWriter [LaTeX] m => m () -> m ()
section' = cmd "section*"

vspace :: MonadWriter [LaTeX] m => m () -> m ()
vspace = cmd "vspace"

preface :: MonadWriter [LaTeX] m => m ()
preface = cmdNone "preface"

noindent :: MonadWriter [LaTeX] m => m ()
noindent = cmdNone "noindent"

hfill :: MonadWriter [LaTeX] m => m ()
hfill = cmdNone "hfill"

documentclass :: MonadWriter [LaTeX] m => m () -> m ()
documentclass = cmd "documentclass"

newline :: MonadWriter [LaTeX] m => m ()
newline = tell [Newline]

usepackage :: MonadWriter [LaTeX] m => m () -> m ()
usepackage = cmd "usepackage"

amp :: MonadWriter [LaTeX] m => m ()
amp = raw "&"

begin :: MonadWriter [LaTeX] m => m () -> m () -> m ()
begin x m = do cmd "begin" x; m; cmd "end" x

beginMany :: MonadWriter [LaTeX] m => [m ()] -> m () -> m ()
beginMany xs m = do cmdMany "begin" xs; m; cmd "end" (head xs)

makeindex :: MonadWriter [LaTeX] m => m ()
makeindex = cmdNone "makeindex"

frontmatter :: MonadWriter [LaTeX] m => m ()
frontmatter = cmdNone "frontmatter"

input :: MonadWriter [LaTeX] m => m () -> m ()
input = cmd "input"

tableofcontents :: MonadWriter [LaTeX] m => m ()
tableofcontents = cmdNone "tableofcontents"

mainmatter :: MonadWriter [LaTeX] m => m ()
mainmatter = cmdNone "mainmatter"

addtocmark :: MonadWriter [LaTeX] m => m () -> m ()
addtocmark = cmd "addtocmark"

title :: MonadWriter [LaTeX] m => m () -> m ()
title = cmd "title"

author :: MonadWriter [LaTeX] m => m () -> m ()
author = cmd "author"

tocauthor :: MonadWriter [LaTeX] m => m () -> m ()
tocauthor = cmd "tocauthor"

inst :: MonadWriter [LaTeX] m => Integer -> m ()
inst = cmd "inst" . num

toccauthor :: MonadWriter [LaTeX] m => m () -> m ()
toccauthor = cmd "toccauthor"

institute :: MonadWriter [LaTeX] m => m () -> m ()
institute = cmd "institute"

index :: MonadWriter [LaTeX] m => m () -> m ()
index = cmd "index"

indexname :: MonadWriter [LaTeX] m => m ()
indexname = cmdNone "indexname"

email :: MonadWriter [LaTeX] m => m () -> m ()
email = cmd "email"

comment :: MonadWriter [LaTeX] m => String -> m ()
comment cmt = tell [Comment cmt]

texttt :: MonadWriter [LaTeX] m => m () -> m ()
texttt = cmd "texttt"

renewcommand :: MonadWriter [LaTeX] m => [m ()] -> m ()
renewcommand = cmdMany "renewcommand"

printindex :: MonadWriter [LaTeX] m => m ()
printindex = cmdNone "printindex"

maketitle :: MonadWriter [LaTeX] m => m ()
maketitle = cmdNone "maketitle"

titlerunning :: MonadWriter [LaTeX] m => m () -> m ()
titlerunning = cmd "titlerunning"

setcounter :: MonadWriter [LaTeX] m => [m ()] -> m ()
setcounter = cmdMany "setcounter"

pagenumbering :: MonadWriter [LaTeX] m => m () -> m ()
pagenumbering = cmd "pagenumbering"

clearpage :: MonadWriter [LaTeX] m => m ()
clearpage = do cmdNone "clearpage"; raw "\n"

backmatter :: MonadWriter [LaTeX] m => m ()
backmatter = cmdNone "backmatter"

and :: MonadWriter [LaTeX] m => m ()
and = cmdNone "and"

raw :: MonadWriter [LaTeX] m => String -> m ()
raw x = tell [Raw x]

cmd :: MonadWriter [LaTeX] m => String -> m a -> m a
cmd name = censor wrapCmd where
  wrapCmd cmds = [Cmd name Nothing (Just [cmds])]

cmdNone :: MonadWriter [LaTeX] m => String -> m ()
cmdNone name = tell [Cmd name Nothing Nothing]

cmdMany :: MonadWriter [LaTeX] m => String -> [m a] -> m ()
cmdMany name bodies = do
  censor merge $ mapM_ (censor wrap) bodies
  where wrap cmds = [Cmd name Nothing (Just [cmds])]
        merge [cmd] = [cmd]
        merge cmds  = [foldr1 combine cmds]
        combine (Cmd name props cmds1) (Cmd _ _ cmds2) =
          Cmd name props $ toMaybe $ fromMaybe [] cmds1 ++ fromMaybe [] cmds2
        combine a _ = a
        toMaybe [] = Nothing
        toMaybe xs = Just xs
18:16: Warning: Use String
Found:
LaTeX -> [Char]
Why not:
LaTeX -> String
24:22: Error: Use concatMap
Found:
concat . map (braces . intercalate "\n" . map renderLaTeX)
Why not:
concatMap (braces . intercalate "\n" . map renderLaTeX)
53:32: Error: Use concatMap
Found:
concat $ map runLaTeX props
Why not:
concatMap runLaTeX props
191:23: Error: Redundant do
Found:
do censor merge $ mapM_ (censor wrap) bodies
Why not:
censor merge $ mapM_ (censor wrap) bodies