Source file bracetax_transform.ml
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
(** This module is the {b main entry point} to use bracetax as a library. *)
(** The functions here are used to transform {i Bracetax} input to the
formats currently handled by the library (XHTML, LaTeX, or a
bracetax table of contents). *)
(**/**)
module Signatures = Bracetax_signatures
module Parser = Bracetax_parser
let opt_may ~f = function None -> () | Some o -> f o
(**/**)
open Bracetax_signatures
(** Transformation from {i Bracetax} to HTML (actually {i almost}
XHTML). Most optional parameters correspond to command line
options of [brtx -html].
@param writer The {!type:Signatures.writer} to use for output.
@param doc If [true], output a whole document with header and
footer (Default: [false])).
@param title If [doc = true], use [title] as HTML header title.
@param css_link Add a "meta" link to a CSS URL.
@param print_comments If [true], output Bracetax comments as HTML
comments (Default: [false]).
@param filename Filename used in error messages (Default: ["<IN>"]))
@param class_hook Add a {v class="class_hook" v} to all tags in
the HTML (it is the [-css-hook] option of [brtx]).
@param img_hook A "hook" function to transform each image path.
@param url_hook A "hook" function to transform each URL.
@param input_char The "input" function.
@param separate_header If this reference is provided, after the
call to [brtx_to_html], this reference will contain the {i title},
{i authors}, and {i subtitle} fields; and they won't be output to
the [writer].
@param deny_bypass If [true], treat [\{bypass\}] commands as
[\{code\}] (Default: [false]).
*)
let brtx_to_html ~writer
?make_section_links ?(doc=false) ?title ?css_link ?(=false)
?(filename="<IN>") ?class_hook ?img_hook ?url_hook ~input_char
? ?(deny_bypass=false) () =
if doc then (
writer.w_write (Bracetax_HTML_printer.header
~comment:"Generated with BraceTax" ?title
?stylesheet_link:css_link ());
);
let printer =
Bracetax_HTML_printer.build
?make_section_links ?class_hook ?img_hook ?url_hook ?separate_header
~writer ~print_comments () in
Parser.do_transformation ~deny_bypass printer input_char filename;
if doc then writer.w_write (Bracetax_HTML_printer.footer ());
()
(** Transform from {i Bracetax} to LaTeX. Most parameters have the
same meaning as for {!val:brtx_to_html}, except the following ones.
@param title The [title] will be used for the PDF meta-data (for
[doc = true]).
@param use_package Add a [\\usepackage\{<package>\}] call in the
LaTeX header (doc = true).
@param href_is_footnote If [true], render links as footnotes
(Default: [false]).
*)
let brtx_to_latex ~writer ?(doc=false) ?title ?use_package ?(deny_bypass=false)
?(=false) ?(=false) ?img_hook ?url_hook
? ?table_caption_after
?(filename="<IN>") ~input_char () =
if doc then (
writer.w_write (Bracetax_latex_printer.header
~comment:"Generated with BraceTax" ?title
?stylesheet_link:use_package ());
);
let printer =
Bracetax_latex_printer.build ~writer ~print_comments ~href_is_footnote
?table_caption_after ?separate_header ?img_hook ?url_hook () in
Parser.do_transformation ~deny_bypass printer input_char filename;
if doc then (writer.w_write (Bracetax_latex_printer.footer ()));
()
(** Retrieve a table of contents from a {i Bracetax} input. The TOC is
itself in {i Bracetax}; it is a set of lists with links. *)
let get_TOC ~writer ?(filename="<IN>")
?make_links ?list_type ?numbering ~input_char () =
let output_funs =
Bracetax_TOC_output.create ?list_type ?numbering ?make_links () in
let printer = Bracetax_generic_printer.build ~writer ~output_funs () in
Parser.do_transformation printer input_char filename;
()
(** Create a [writer] and a [input_char] from a [string] and two
[Buffer.t]. With [let w, ic = (string_io input_str output_buffer
error_buffer)], the writer [w] will write to [output_buffer],
transform errors to strings, and output them to [error_buffer]; [ic]
will take characters from [input_str]. [w] and [ic] can then be used
with {!val:brtx_to_html} and {!val:brtx_to_latex}. *)
let string_io in_string out_buf err_buf =
let error = function
| `undefined s ->
Buffer.add_string err_buf (s ^ "\n");
| `message ((_, gravity, _) as msg) ->
Buffer.add_string err_buf ((Error.to_string msg) ^ "\n"); in
let write = Buffer.add_string out_buf in
let read_char_opt =
let cpt = ref (-1) in
(fun () ->
try Some (incr cpt; in_string.[!cpt]) with e -> None) in
let writer = make_writer ~write ~error in
(writer, read_char_opt)
(** This function is a convenience replacement for {!val:brtx_to_html},
to transform a [string]
into another [string] together with a list of {!type:Error.error}s. *)
let str_to_html
?make_section_links ?(doc=false) ?title ?css_link ?(=false)
?(filename="<IN>") ?class_hook ?img_hook ?url_hook
? ?(deny_bypass=false) in_str =
let errors = ref [] in
let error = fun e -> errors := e :: !errors in
let out_buf = Buffer.create 42 in
let write = Buffer.add_string out_buf in
let input_char =
let cpt = ref (-1) in
(fun () -> try Some (incr cpt; in_str.[!cpt]) with e -> None) in
let writer = make_writer ~write ~error in
if doc then (
writer.w_write
(Bracetax_HTML_printer.header ~comment:"Generated with BraceTax" ?title
?stylesheet_link:css_link ());
);
let printer =
Bracetax_HTML_printer.build ?class_hook ?img_hook ?url_hook ?separate_header
?make_section_links
~writer ~print_comments () in
Parser.do_transformation ~deny_bypass printer input_char filename;
if doc then (
writer.w_write (Bracetax_HTML_printer.footer ());
);
(Buffer.contents out_buf, List.rev !errors)
(** This function is a convenience replacement for
{!val:brtx_to_latex}, to transform a [string] into another [string]
together with a list of {!type:Error.error}s. *)
let str_to_latex ?(doc=false) ?title ?use_package ?(deny_bypass=false)
?(=false) ?(=false) ?img_hook ?url_hook
?table_caption_after
? ?(filename="<IN>") in_str =
let errors = ref [] in
let error = fun e -> errors := e :: !errors in
let out_buf = Buffer.create 42 in
let write = Buffer.add_string out_buf in
let input_char =
let cpt = ref (-1) in
(fun () -> try Some (incr cpt; in_str.[!cpt]) with e -> None) in
let writer = make_writer ~write ~error in
if doc then
writer.w_write
(Bracetax_latex_printer.header
~comment:"Generated with BraceTax" ?title
?stylesheet_link:use_package ());
let printer =
Bracetax_latex_printer.build
~writer ~print_comments ~href_is_footnote ?table_caption_after
?separate_header ?img_hook ?url_hook () in
Parser.do_transformation ~deny_bypass printer input_char filename;
if doc then (
writer.w_write (Bracetax_latex_printer.footer ());
);
(Buffer.contents out_buf, List.rev !errors)
(** This function is a convenience replacement for
{!val:get_TOC}, to get the table of contents from a [string]
into another [string]
together with a list of {!type:Error.error}s. *)
let str_to_TOC ?make_links ?list_type ?numbering
?(filename="<IN>") in_str =
let errors = ref [] in
let error = fun e -> errors := e :: !errors in
let out_buf = Buffer.create 42 in
let write = Buffer.add_string out_buf in
let input_char =
let cpt = ref (-1) in
(fun () -> try Some (incr cpt; in_str.[!cpt]) with e -> None) in
let writer = make_writer ~write ~error in
let output_funs =
Bracetax_TOC_output.create ?list_type ?numbering ?make_links () in
let printer = Bracetax_generic_printer.build ~writer ~output_funs () in
Parser.do_transformation printer input_char filename;
(Buffer.contents out_buf, List.rev !errors)