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
|
{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{- |
Module : Text.Pandoc.Lua.Marshaling.WriterOptions
Copyright : © 2021-2024 Albert Krewinkel, John MacFarlane
License : GNU GPL, version 2 or above
Maintainer : Albert Krewinkel <[email protected]>
Stability : alpha
Marshaling instance for WriterOptions and its components.
-}
module Text.Pandoc.Lua.Marshal.WriterOptions
( peekWriterOptions
, pushWriterOptions
) where
import Control.Applicative (optional)
import Data.Default (def)
import HsLua as Lua
import Text.Pandoc.Error (PandocError)
import Text.Pandoc.Lua.Marshal.Context (peekContext, pushContext)
import Text.Pandoc.Lua.Marshal.Format (peekExtensions, pushExtensions)
import Text.Pandoc.Lua.Marshal.List (pushPandocList)
import Text.Pandoc.Lua.Marshal.Template (peekTemplate, pushTemplate)
import Text.Pandoc.Options (WriterOptions (..))
--
-- Writer Options
--
-- | Retrieve a WriterOptions value, either from a normal WriterOptions
-- value, from a read-only object, or from a table with the same
-- keys as a WriterOptions object.
peekWriterOptions :: Peeker PandocError WriterOptions
peekWriterOptions = retrieving "WriterOptions" . \idx ->
liftLua (ltype idx) >>= \case
TypeUserdata -> peekUD typeWriterOptions idx
TypeTable -> peekWriterOptionsTable idx
_ -> failPeek =<<
typeMismatchMessage "WriterOptions userdata or table" idx
-- | Pushes a WriterOptions value as userdata object.
pushWriterOptions :: Pusher PandocError WriterOptions
pushWriterOptions = pushUD typeWriterOptions
-- | 'WriterOptions' object type.
typeWriterOptions :: DocumentedType PandocError WriterOptions
typeWriterOptions = deftype "WriterOptions"
[ operation Tostring $ lambda
### liftPure show
<#> udparam typeWriterOptions "opts" "options to print in native format"
=#> functionResult pushString "string" "Haskell representation"
]
[ property "chunk_template"
"Templates used to generate chunked HTML filenames (string)"
(pushViaJSON, writerChunkTemplate)
(peekViaJSON, \opts x -> opts{ writerChunkTemplate = x })
, property "cite_method"
"How to print cites"
(pushViaJSON, writerCiteMethod)
(peekViaJSON, \opts x -> opts{ writerCiteMethod = x })
, property "columns"
"Characters in a line (for text wrapping)"
(pushIntegral, writerColumns)
(peekIntegral, \opts x -> opts{ writerColumns = x })
, property "dpi"
"DPI for pixel to/from inch/cm conversions"
(pushIntegral, writerDpi)
(peekIntegral, \opts x -> opts{ writerDpi = x })
, property "email_obfuscation"
"How to obfuscate emails"
(pushViaJSON, writerEmailObfuscation)
(peekViaJSON, \opts x -> opts{ writerEmailObfuscation = x })
, property "split_level"
"Level at which EPUB or chunked HTML documents are split into files"
(pushIntegral, writerSplitLevel)
(peekIntegral, \opts x -> opts{ writerSplitLevel = x })
, property "epub_chapter_level"
"Deprecated synonym for split_level"
(pushIntegral, writerSplitLevel)
(peekIntegral, \opts x -> opts{ writerSplitLevel = x })
, property "epub_fonts"
"Paths to fonts to embed"
(pushPandocList pushString, writerEpubFonts)
(peekList peekString, \opts x -> opts{ writerEpubFonts = x })
, property "epub_title_page"
"Determines whether a title page is included in EPUB"
(pushBool, writerEpubTitlePage)
(peekBool, \opts x -> opts{ writerEpubTitlePage = x })
, property "epub_metadata"
"Metadata to include in EPUB"
(maybe pushnil pushText, writerEpubMetadata)
(optional . peekText, \opts x -> opts{ writerEpubMetadata = x })
, property "epub_subdirectory"
"Subdir for epub in OCF"
(pushText, writerEpubSubdirectory)
(peekText, \opts x -> opts{ writerEpubSubdirectory = x })
, property "extensions"
"Markdown extensions that can be used"
(pushExtensions, writerExtensions)
(peekExtensions, \opts x -> opts{ writerExtensions = x })
, property "highlight_style"
"Style to use for highlighting (nil = no highlighting)"
(maybe pushnil pushViaJSON, writerHighlightStyle)
(optional . peekViaJSON, \opts x -> opts{ writerHighlightStyle = x })
, property "html_math_method"
"How to print math in HTML"
(pushViaJSON, writerHTMLMathMethod)
(peekViaJSON, \opts x -> opts{ writerHTMLMathMethod = x })
, property "link_images"
"Include links to images instead of embedding in ODT"
(pushBool, writerLinkImages)
(peekBool, \opts x -> opts{ writerLinkImages = x })
, property "html_q_tags"
"Use @<q>@ tags for quotes in HTML"
(pushBool, writerHtmlQTags)
(peekBool, \opts x -> opts{ writerHtmlQTags = x })
, property "identifier_prefix"
"Prefix for section & note ids in HTML and for footnote marks in markdown"
(pushText, writerIdentifierPrefix)
(peekText, \opts x -> opts{ writerIdentifierPrefix = x })
, property "incremental"
"True if lists should be incremental"
(pushBool, writerIncremental)
(peekBool, \opts x -> opts{ writerIncremental = x })
, property "listings"
"Use listings package for code"
(pushBool, writerListings)
(peekBool, \opts x -> opts{ writerListings = x })
, property "number_offset"
"Starting number for section, subsection, ..."
(pushPandocList pushIntegral, writerNumberOffset)
(peekList peekIntegral, \opts x -> opts{ writerNumberOffset = x })
, property "number_sections"
"Number sections in LaTeX"
(pushBool, writerNumberSections)
(peekBool, \opts x -> opts{ writerNumberSections = x })
, property "prefer_ascii"
"Prefer ASCII representations of characters when possible"
(pushBool, writerPreferAscii)
(peekBool, \opts x -> opts{ writerPreferAscii = x })
, property "reference_doc"
"Path to reference document if specified"
(maybe pushnil pushString, writerReferenceDoc)
(optional . peekString, \opts x -> opts{ writerReferenceDoc = x })
, property "reference_links"
"Use reference links in writing markdown, rst"
(pushBool, writerReferenceLinks)
(peekBool, \opts x -> opts{ writerReferenceLinks = x })
, property "reference_location"
"Location of footnotes and references for writing markdown"
(pushViaJSON, writerReferenceLocation)
(peekViaJSON, \opts x -> opts{ writerReferenceLocation = x })
, property "figure_caption_position"
"Location of caption relative to the figure"
(pushViaJSON, writerFigureCaptionPosition)
(peekViaJSON, \opts x -> opts{ writerFigureCaptionPosition = x })
, property "table_caption_position"
"Location of caption relative to the table"
(pushViaJSON, writerTableCaptionPosition)
(peekViaJSON, \opts x -> opts{ writerTableCaptionPosition = x })
, property "section_divs"
"Put sections in div tags in HTML"
(pushBool, writerSectionDivs)
(peekBool, \opts x -> opts{ writerSectionDivs = x })
, property "setext_headers"
"Use setext headers for levels 1-2 in markdown"
(pushBool, writerSetextHeaders)
(peekBool, \opts x -> opts{ writerSetextHeaders = x })
, property "list_tables"
"Render tables using list tables in RST output"
(pushBool, writerListTables)
(peekBool, \opts x -> opts{ writerListTables = x })
, property "slide_level"
"Force header level of slides"
(maybe pushnil pushIntegral, writerSlideLevel)
(optional . peekIntegral, \opts x -> opts{ writerSlideLevel = x })
-- , property "syntax_map" "Syntax highlighting definition"
-- (pushViaJSON, writerSyntaxMap)
-- (peekViaJSON, \opts x -> opts{ writerSyntaxMap = x })
-- :: SyntaxMap
, property "tab_stop"
"Tabstop for conversion btw spaces and tabs"
(pushIntegral, writerTabStop)
(peekIntegral, \opts x -> opts{ writerTabStop = x })
, property "table_of_contents"
"Include table of contents"
(pushBool, writerTableOfContents)
(peekBool, \opts x -> opts{ writerTableOfContents = x })
, property "template"
"Template to use"
(maybe pushnil pushTemplate, writerTemplate)
(optional . peekTemplate, \opts x -> opts{ writerTemplate = x })
-- :: Maybe (Template Text)
, property "toc_depth"
"Number of levels to include in TOC"
(pushIntegral, writerTOCDepth)
(peekIntegral, \opts x -> opts{ writerTOCDepth = x })
, property "top_level_division"
"Type of top-level divisions"
(pushViaJSON, writerTopLevelDivision)
(peekViaJSON, \opts x -> opts{ writerTopLevelDivision = x })
, property "variables"
"Variables to set in template"
(pushContext, writerVariables)
(peekContext, \opts x -> opts{ writerVariables = x })
, property "wrap_text"
"Option for wrapping text"
(pushViaJSON, writerWrapText)
(peekViaJSON, \opts x -> opts{ writerWrapText = x })
]
-- | Retrieves a 'WriterOptions' object from a table on the stack, using
-- the default values for all missing fields.
--
-- Internally, this pushes the default writer options, sets each
-- key/value pair of the table in the userdata value, then retrieves the
-- object again. This will update all fields and complain about unknown
-- keys.
peekWriterOptionsTable :: Peeker PandocError WriterOptions
peekWriterOptionsTable idx = retrieving "WriterOptions (table)" $ do
liftLua $ do
absidx <- absindex idx
pushUD typeWriterOptions def
let setFields = do
next absidx >>= \case
False -> return () -- all fields were copied
True -> do
pushvalue (nth 2) *> insert (nth 2)
settable (nth 4) -- set in userdata object
setFields
pushnil -- first key
setFields
peekUD typeWriterOptions top `lastly` pop 1
|