/
Builder.hs
340 lines (273 loc) · 10.2 KB
/
Builder.hs
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
{-
Copyright (C) 2010 John MacFarlane <jgm@berkeley.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.Builder
Copyright : Copyright (C) 2010 John MacFarlane
License : GNU GPL, version 2 or above
Maintainer : John MacFarlane <jgm@berkeley.edu>
Stability : alpha
Portability : portable
Convenience functions for building pandoc documents programmatically.
Example of use (requires the @OverloadedStrings@ language extension):
> import Text.Pandoc.Builder
>
> myDoc :: Pandoc
> myDoc = setTitle "My title" $ doc $
> para "This is the first paragraph" +++
> para ("And " +++ emph "another" +++ ".") +++
> bulletList [ para "item one" +++ para "continuation"
> , plain ("item two and a " +++
> link "/url" "go to url" "link")
> ]
Isn't that nicer than writing the following?
> import Text.Pandoc.Definition
>
> myDoc :: Pandoc
> myDoc = Pandoc (Meta {docTitle = [Str "My",Space,Str "title"]
> , docAuthors = []
> , docDate = []})
> [Para [Str "This",Space,Str "is",Space,Str "the",Space,Str "first",
> Space,Str "paragraph"]
> ,Para [Str "And",Space,Emph [Str "another"],Str "."]
> ,BulletList [[Para [Str "item",Space,Str "one"]
> ,Para [Str "continuation"]]
> ,[Plain [Str "item",Space,Str "two",Space,Str "and", Space,
> Str "a",Space,Link [Str "link"] ("/url","go to url")]]]]
And of course, you can use Haskell to define your own builders:
> import Text.Pandoc.Builder
> import Text.JSON
> import Control.Arrow ((***))
>
> -- | Converts a JSON document into 'Blocks'.
> json :: String -> Blocks
> json x =
> case decode x of
> Ok y -> jsValueToBlocks y
> Error y -> error y
> where jsValueToBlocks x =
> case x of
> JSNull -> empty
> JSBool x -> plain $ text $ show x
> JSRational _ x -> plain $ text $ show x
> JSString x -> plain $ text $ fromJSString x
> JSArray xs -> bulletList $ map jsValueToBlocks xs
> JSObject x -> definitionList $
> map (text *** (:[]) . jsValueToBlocks) $
> fromJSObject x
-}
module Text.Pandoc.Builder ( module Text.Pandoc.Definition
, Inlines
, Blocks
, toList
, fromList
, empty
, (+++)
-- * Document builders
, doc
, setTitle
, setAuthors
, setDate
-- * Inline list builders
, text
, str
, emph
, strong
, strikeout
, superscript
, subscript
, smallcaps
, singleQuoted
, doubleQuoted
, cite
, codeWith
, code
, space
, ellipses
, emdash
, endash
, apostrophe
, linebreak
, math
, displayMath
, rawInline
, link
, image
, note
-- * Block list builders
, para
, plain
, codeBlockWith
, codeBlock
, rawBlock
, blockQuote
, bulletList
, orderedListWith
, orderedList
, definitionList
, header
, horizontalRule
, table
, simpleTable
)
where
import Text.Pandoc.Definition
import Data.String
import Data.Monoid
import Data.Maybe (fromMaybe)
import Data.Sequence (Seq, fromList, singleton, empty, (><))
import Data.Foldable (Foldable, toList)
import Data.List (groupBy)
import Control.Arrow ((***))
type Inlines = Seq Inline
-- Foldable gives us toList
-- Monoid gives us mappend, mempty
instance IsString Inlines where
fromString = text
type Blocks = Seq Block
-- | Concatenate two 'Inlines's or two 'Blocks's.
(+++) :: Monoid a => a -> a -> a
(+++) = mappend
-- Document builders
doc :: Blocks -> Pandoc
doc = Pandoc (Meta [] [] []) . toList
setTitle :: Inlines -> Pandoc -> Pandoc
setTitle t (Pandoc m bs) = Pandoc m{ docTitle = toList t } bs
setAuthors :: [Inlines] -> Pandoc -> Pandoc
setAuthors as (Pandoc m bs) = Pandoc m{ docAuthors = map toList as } bs
setDate :: Inlines -> Pandoc -> Pandoc
setDate d (Pandoc m bs) = Pandoc m{ docDate = toList d } bs
-- Inline list builders
-- | Convert a 'String' to 'Inlines', treating interword spaces as 'Space's.
-- If you want a 'Str' with literal spaces, use 'str'.
text :: String -> Inlines
text = fromList . map conv . breakBySpaces
where breakBySpaces = groupBy sameCategory
sameCategory x y = (is_space x && is_space y) ||
(not $ is_space x || is_space y)
conv xs | all is_space xs = Space
conv xs = Str xs
is_space ' ' = True
is_space '\n' = True
is_space '\t' = True
is_space _ = False
str :: String -> Inlines
str = singleton . Str
emph :: Inlines -> Inlines
emph = singleton . Emph . toList
strong :: Inlines -> Inlines
strong = singleton . Strong . toList
strikeout :: Inlines -> Inlines
strikeout = singleton . Strikeout . toList
superscript :: Inlines -> Inlines
superscript = singleton . Superscript . toList
subscript :: Inlines -> Inlines
subscript = singleton . Subscript . toList
smallcaps :: Inlines -> Inlines
smallcaps = singleton . SmallCaps . toList
singleQuoted :: Inlines -> Inlines
singleQuoted = quoted SingleQuote
doubleQuoted :: Inlines -> Inlines
doubleQuoted = quoted DoubleQuote
quoted :: QuoteType -> Inlines -> Inlines
quoted qt = singleton . Quoted qt . toList
cite :: [Citation] -> Inlines -> Inlines
cite cts = singleton . Cite cts . toList
-- | Inline code with attributes.
codeWith :: Attr -> String -> Inlines
codeWith attrs = singleton . Code attrs
-- | Plain inline code.
code :: String -> Inlines
code = codeWith nullAttr
space :: Inlines
space = singleton Space
emdash :: Inlines
emdash = singleton EmDash
endash :: Inlines
endash = singleton EnDash
apostrophe :: Inlines
apostrophe = singleton Apostrophe
ellipses :: Inlines
ellipses = singleton Ellipses
linebreak :: Inlines
linebreak = singleton LineBreak
-- | Inline math
math :: String -> Inlines
math = singleton . Math InlineMath
-- | Display math
displayMath :: String -> Inlines
displayMath = singleton . Math DisplayMath
rawInline :: Format -> String -> Inlines
rawInline format = singleton . RawInline format
link :: String -- ^ URL
-> String -- ^ Title
-> Inlines -- ^ Label
-> Inlines
link url title x = singleton $ Link (toList x) (url, title)
image :: String -- ^ URL
-> String -- ^ Title
-> Inlines -- ^ Alt text
-> Inlines
image url title x = singleton $ Image (toList x) (url, title)
note :: Blocks -> Inlines
note = singleton . Note . toList
-- Block list builders
para :: Inlines -> Blocks
para = singleton . Para . toList
plain :: Inlines -> Blocks
plain = singleton . Plain . toList
-- | A code block with attributes.
codeBlockWith :: Attr -> String -> Blocks
codeBlockWith attrs = singleton . CodeBlock attrs
-- | A plain code block.
codeBlock :: String -> Blocks
codeBlock = codeBlockWith nullAttr
rawBlock :: Format -> String -> Blocks
rawBlock format = singleton . RawBlock format
blockQuote :: Blocks -> Blocks
blockQuote = singleton . BlockQuote . toList
-- | Ordered list with attributes.
orderedListWith :: ListAttributes -> [Blocks] -> Blocks
orderedListWith attrs = singleton . OrderedList attrs . map toList
-- | Ordered list with default attributes.
orderedList :: [Blocks] -> Blocks
orderedList = orderedListWith (1, DefaultStyle, DefaultDelim)
bulletList :: [Blocks] -> Blocks
bulletList = singleton . BulletList . map toList
definitionList :: [(Inlines, [Blocks])] -> Blocks
definitionList = singleton . DefinitionList . map (toList *** map toList)
header :: Int -- ^ Level
-> Inlines
-> Blocks
header level = singleton . Header level . toList
horizontalRule :: Blocks
horizontalRule = singleton HorizontalRule
table :: Inlines -- ^ Caption
-> [(Alignment, Double)] -- ^ Column alignments and fractional widths
-> [Blocks] -- ^ Headers
-> [[Blocks]] -- ^ Rows
-> Blocks
table caption cellspecs headers rows = singleton $
Table (toList caption) aligns widths
(map toList headers) (map (map toList) rows)
where (aligns, widths) = unzip cellspecs
-- | A simple table without a caption.
simpleTable :: [Blocks] -- ^ Headers
-> [[Blocks]] -- ^ Rows
-> Blocks
simpleTable headers = table empty (mapConst defaults headers) headers
where defaults = (AlignDefault, 0)
mapConst :: Functor f => b -> f a -> f b
mapConst = fmap . const