forked from nfeske/gosh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rst.gosh
389 lines (342 loc) · 10.2 KB
/
rst.gosh
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#############################
# #
# RESTRUCTURED TEXT BACKEND #
# #
#############################
### FILTER TEXTUAL OUTPUT ###
proc out_rst {string} {
global references
set string " $string "
# bold style #
while {[regexp {([ \"\(])\*([^\*]+?)\*([ \)\.\",!?])} $string dummy head_char bf_text tail_char]} {
regsub -all {\*} [seal_repl $bf_text] " " bf_text
regsub {([ \"\(])\*([^\*]+?)\*([ \)\.\",!?])} $string [seal_repl "$head_char**$bf_text**$tail_char"] string
}
# italic style #
while {[regexp {([ \"\(])_(.+?)_([ \)\.\",!?])} $string dummy head_char emph_text tail_char]} {
regsub -all {_} $emph_text " " emph_text
regsub {([ \"\(])_(.+?)_([ \)\.\",!?])} $string [seal_repl "$head_char*$emph_text*$tail_char"] string
}
# monospace style #
while {[regexp {([ \"\(])\'(.+?)\'([ \-\)\.\",!?])} $string dummy head_char code_text tail_char]} {
regsub -all " " $code_text "\\\ " code_text
regsub {([ \"\(])\'(.+?)\'([ \-\)\.\",!?])} $string [seal_repl "$head_char``$code_text``$tail_char"] string
}
# FIXME: kick out monospace style via hashes #
while {[regexp {\#([^#]+)\#} $string dummy code_text]} {
regsub {\#([^#]+)\#} $string [seal_repl "``$code_text``"] string
puts stderr "Warning: Monospace using #hashes# is deprecated because it looks ugly."
puts stderr " Please use 'apostrophes' instead. Thanks, your GOSH maintainer."
}
# hexadecimal numbers #
regsub -all {0x(([a-fA-F0-9]+)[\+\-\*\/]?(0x)?)+} $string "``&``" string
set linktext ""
# insert references and citations #
while {[regexp {\[([^\]]+)\]} $string dummy ref_text]} {
if {[info exists references($ref_text,type)]} {
if {$references($ref_text,type) == "tabcap"} {
puts stderr "Rst backend does not support table captions as reference"
} elseif {$references($ref_text,type) == "image"} {
regsub {\[([^\]]+)\]} $string [seal_repl "`$references($ref_text,index)`_"] string
} else {
regsub {\[([^\]]+)\]} $string [seal_repl "`$ref_text`_"] string
}
} else {
set url ""
set linktext ""
set tooltip ""
regexp {^(\w+:[^ ]+)} $ref_text url
regexp {^(\w+:[^ ]+) - ([^\(]+)} $ref_text dummy dummy linktext
regexp {\((.*)\)} $ref_text dummy tooltip
if {$url == ""} {
set url $ref_text
}
if {$linktext == ""} {
set linktext $url
}
set tooltiptext " title=\"$tooltip\""
if {$tooltip == ""} {
set tooltiptext ""
}
regsub {^http:([^/])} $url {\1} url
regsub { +$} $linktext "" linktext
regsub -all {&} $url "_§%and%§_" url
regsub -all -- {---} $url "_§%3xminus%§_" url
regsub -all -- {--} $url "_§%2xminus%§_" url
regsub -all -- {---} $linktext "_§%3xminus%§_" alttext
regsub -all -- {--} $linktext "_§%2xminus%§_" alttext
regsub {\[([^\]]+)\]} $string [seal_repl "`$linktext <$url>`_"] string
}
}
regsub -all {_§%and%§_} $string {\&} string
regsub -all -- {\-\-\-} $string {\—} string
regsub -all -- {\-\-} $string {\–} string
regsub -all {_§%3xminus%§_} $string {---} string
regsub -all {_§%2xminus%§_} $string {--} string
# clear spaces at the end of a line #
regsub { +$} $string "" string
regsub {^ } $string "" string
return $string
}
### FILTER LABEL ###
proc label_rst {string} {
regsub -all {"} $string "" string
regsub -all { } $string "_" string
return $string
}
### WRITE HEADER OF TEX FILE ###
proc produce_head_rst {} {
global title authors references toc_refs
global config_select config_marshal
regsub -all {.} $title "=" title_frame
if {$config_select == 0} {
#
# If marshalling is activated, we provide the
# title as "title" tag and ommit the top-level
# headline. Otherwise the title is printed
# using a a nice rst frame style.
#
if {$config_marshal} {
print_marshal_header $title $authors "text/restructured"
} else {
printline "$title_frame"
printline "$title"
printline "$title_frame"
printline ""
}
}
# if {$title != ""} {
# printline "<h1><center>$title</center></h1>"
# }
# if {$authors != ""} {
# printline "<b><center>$authors</center></b>"
# }
# printline "<center>[exec date --iso-8601]</center>"
}
### WRITE TAIL OF TEX FILE ###
proc produce_tail_rst {} {
}
### VERBATIM ###
proc process_verbatim_rst {txtblock} {
global depth
while {[lindex $txtblock end] == ""} {
set txtblock [lrange $txtblock 0 [expr [llength $txtblock]-2]]
}
incr depth -1
printline "::"
printline ""
incr depth 2
foreach txtline $txtblock {
set txt [linetxt $txtline]
regsub {^\!} $txt "" txt
regsub -all {\t} $txt " " txt
regsub -all {&} $txt "_§%and%§_" txt
regsub -all {_§%and%§_} $txt {\&} txt
printline "$txt"
}
incr depth -1
printline ""
}
### ITEMIZE ###
proc process_itemize_rst {txtblock} {
global depth
incr depth -1
handle_txtblock itemize $txtblock
incr depth
}
### ITEM ###
proc process_item_rst {itemtxtblock} {
global strip_space_prefix
global depth
incr depth -1
puts -nonewline "[indent]- "
set strip_space_prefix 2
incr depth 2
handle_txtblock item [extract_item_text $itemtxtblock]
incr depth -1
}
### DESCRIPTION ###
proc process_description_rst {txtblock} {
global depth
incr depth -1
handle_txtblock description $txtblock
incr depth
}
### DESCRIPTION ITEM ###
proc process_descitem_rst {itemtxtblock} {
global depth
set txtline [lindex $itemtxtblock 0]
set desc_name ""
set txt [linetxt $txtline]
regexp {^\:(.+)\:} $txt dummy desc_name
set txtline [lineregsub {^\:(.+)\: *} $txtline ""]
incr depth -1
printline "[out_rst $desc_name]"
incr depth 2
lappend txtblock $txtline
foreach txtline [lrange $itemtxtblock 1 end] {
set txtline [lineregsub {^\ \ } $txtline ""]
lappend txtblock $txtline
}
handle_txtblock descitem $txtblock
incr depth -1
}
### ENUMERATION ###
proc process_enumeration_rst {txtblock} {
global enum_level enum_cnt depth
incr depth -1
incr enum_level
set enum_cnt($enum_level) 0
handle_txtblock enumeration $txtblock
incr enum_level -1
incr depth
}
### ENUM ITEM ###
proc process_enum_rst {itemtxtblock} {
global enum_level enum_cnt depth strip_space_prefix
incr enum_cnt($enum_level)
incr depth -1
set tag "$enum_cnt($enum_level). "
puts -nonewline "[indent]$tag"
incr depth [string length $tag]
set strip_space_prefix [string length $tag]
handle_txtblock item [extract_item_text $itemtxtblock]
incr depth -2
}
### PLAIN ###
proc process_plain_rst {plaintxtblock} {
global depth
incr depth -1
foreach txtline $plaintxtblock {
printline "[out_rst [linetxt $txtline]]"
}
printline ""
incr depth
}
### ABSTRACT ###
proc process_abstract_rst {txtblock} {
global depth
incr depth -1
handle_txtblock abstract [lrange $txtblock 2 end]
incr depth
}
### CHAPTER ###
proc process_chapter_rst {txtblock} {
global references curr_chapter curr_section_type depth
global config_select select_chapter config_marshal
global authors
set curr_section_type chapter
set title [linetxt [lindex $txtblock 0]]
set curr_chapter $references($title,index)
incr depth -1
if {$config_select} {
if {"$select_chapter" == "$curr_chapter" && $config_marshal} {
print_marshal_header $title $authors "text/restructured" }
} else {
printline "$title"
regsub -all {.} $title "#" underline
printline "$underline"
}
printline ""
handle_txtblock chapter [lrange $txtblock 2 end]
incr depth
}
### SECTION ###
proc process_section_rst {txtblock} {
global references curr_chapter curr_section curr_section_type depth
incr depth -1
set curr_section_type section
set title [linetxt [lindex $txtblock 0]]
set curr_section $references($title,index)
printline "$title"
regsub -all {.} $title "=" underline
printline "$underline"
printline ""
handle_txtblock section [lrange $txtblock 2 end]
incr depth
}
### SUBSECTION ###
proc process_subsection_rst {txtblock} {
global depth references curr_chapter curr_section curr_subsection curr_section_type text_color
incr depth -1
set curr_section_type subsection
set title [linetxt [lindex $txtblock 0]]
set curr_subsection $references($title,index)
printline "$title"
regsub -all {.} $title "~" underline
printline "$underline"
printline ""
handle_txtblock subsection [lrange $txtblock 2 end]
incr depth
}
### PARAGRAPH ###
proc process_paragraph_rst {txtblock} {
global curr_section_type text_color
set curr_section_type paragraph
set title [linetxt [lindex $txtblock 0]]
printline "$title"
regsub -all {.} $title "-" underline
printline "$underline"
handle_txtblock paragraph [lrange $txtblock 2 end]
}
### IMAGE ###
proc process_image_rst {txtblock} {
global depth references
set img_info ""
set img_size 80
set img_angle "0"
regexp {\[(image \w+.*)\]} [lindex $txtblock 0] dummy img_info
if {$img_info == ""} return
set img_name [lindex $img_info 1]
regexp { (\d+)%} $img_info dummy img_size
regexp { (\d+)°} $img_info dummy img_angle
incr depth -1
printline ".. _`$references($img_name,index)`:"
printline ".. figure:: $img_name.png"
printline ""
printline " Figure $references($img_name,index):"
foreach img_capline $txtblock {
set txt [linetxt $img_capline]
if {[regexp {^\[.*\]} $txt dummy]} {
regsub {^\[.*\]} $txt "" txt
if {[regexp {^ *$} $txt dummy]} { continue }
}
regsub {^ *} $txt "" txt
printline " $txt"
}
printline ""
incr depth
}
### DETERMINE NUMBER OF TABLE COLUMNS
#
# 'rows' contains the complete table information as a list of rows.
# Each row is a two-element list {<rowtype> <rowtextlines>}.
# Each rowtextline is a two-element list of {<line number> <string>}.
# A table string is contains pipe symbols as column separators.
#
proc table_num_cols {rows} {
return [llength [split [lindex [lindex [lindex [lindex $rows 0] 1] 0] 1] "|"]]
}
### REQUEST LIST OF TEXT LINES OF SPECIFIED TABLE CELL ###
proc table_row_cell_txt {row col_idx} {
set row_type [lindex $row 0]
set row_lines [lindex $row 1]
set cell_txt ""
if {$row_type == "tabrow"} {
foreach row_line $row_lines {
set col_txt_list [split [linetxt $row_line] "|"]
if {$col_idx < [llength $col_txt_list]} {
set col_txt [lindex $col_txt_list $col_idx]
lappend cell_txt $col_txt
}
}
}
return $cell_txt
}
### TABLE ###
proc output_table_rst {col_attr rows caption} {
puts stderr "Table not supported by rst backend"
}
set enum_level 0
set curr_section_type default
set outmode rst