forked from nfeske/gosh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
man.gosh
251 lines (212 loc) · 6.16 KB
/
man.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
#######################
# #
# MANUAL PAGE BACKEND #
# #
#######################
proc man_mkboldline {string} {
# Collapse multiple style changes in one line
# First, delete all boldness.
regsub -all -lineanchor -linestop \
{^\.B (.*)$} $string {\1} string
# Collapse non-italic lines.
regsub -all -lineanchor -linestop \
{^([^.].*)\n([^.].*)$} $string {\1 \2} string
# Now, we should have only roman and italic lines left.
# Trim whitespace
regsub -all \
{ *\n *} $string "\n" string
# Collapse.
regsub -all \
{\n(\.I )?} [string trimright $string] \
{" " } string
# escape double quotes
regsub -all {"} $string {\\(dq} string
regsub {^\.I } $string {.IB "} string
regsub {^([^.])} $string {.BI "\1} string
append string {"}
return $string
}
### FILTER TEXTUAL OUTPUT ###
proc out_man {string} {
set string " $string "
# italic style #
while {[regexp {([ \"\(])_(.+?)_([ \)\.\",!?])} $string dummy head_char emph_text tail_char]} {
regsub -all {_} $emph_text " " emph_text
regsub " " $tail_char "" tail_char
regsub {\.} $tail_char {\\\\\\\&.} tail_char
regsub {([ \"\(])_(.+?)_([ \)\.\",!?])} $string "$head_char\n.I $emph_text\n$tail_char" string
}
# bold style #
while {[regexp {([ \"\(])\*(.+?)\*([ \)\.\",!?])} $string dummy head_char bf_text tail_char]} {
regsub -all {\*} $bf_text " " bf_text
regsub " " $tail_char "" tail_char
regsub {\.} $tail_char {\\\\\\\&.} tail_char
regsub {([ \"\(])\*(.+?)\*([ \)\.\",!?])} $string "$head_char\n.B $bf_text\n$tail_char" string
}
# monospace style #
# while {[regexp {([ \(])\'(.+?)\'([ \)\.,!?])} $string dummy head_char code_text tail_char]} {
# regsub {([ \(])\'(.+?)\'([ \)\.,!?])} $string "$head_char\\texttt{$code_text}$tail_char" string
# }
regsub -all {"} $string "\"" string
regsub {^ } $string "" string
regsub { $} $string "" string
regsub {^\.} $string {\\\&.} string
regsub -lineanchor {^'} $string {\(cq} string
return $string
}
### FILTER LABEL ###
proc label_man {string} {
regsub -all {"} $string "" string
return $string
}
### WRITE HEADER ###
proc produce_head_man {} {
global title authors txtfilename config_man_no_date
if {[regexp {^([^(]+)\(([0-9]+)\) *-+ *(.*)$} \
$title dummy name section desc]} {
string trim name
string trim desc
} else {
set section 7
set desc $title
set name $txtfilename
}
set date_string ""
if {$config_man_no_date == 0} {
set date_string [exec date]
}
puts ".TH \"$name\" $section \"$date_string\" \"TU Dresden\" \"DROPS Manual\""
puts ".SH NAME"
puts "$name \\- $desc"
}
### WRITE TAIL OF TEX FILE ###
proc produce_tail_man {} {
global authors
puts ".SH AUTHOR"
puts "$authors"
}
### VERBATIM ###
proc process_verbatim_man {txtblock} {
while {[lindex $txtblock end] == ""} {
set txtblock [lrange $txtblock 0 [expr [llength $txtblock]-2]]
}
puts ".sp"
puts ".in +4n"
puts ".ll +1i"
foreach txtline $txtblock {
set txt [linetxt $txtline]
regsub {^!} $txt "" txt
regsub {\\} $txt {\\\\} txt
regsub {^([.'])} $txt {\\\&\1} txt
puts "$txt\n.br"
}
puts ".ll"
puts ".in -4n"
puts ".PP"
}
### ITEMIZE ###
proc process_itemize_man {txtblock} {
handle_txtblock itemize $txtblock
}
### ITEM ###
proc process_item_man {itemtxtblock} {
puts ".IP \" *\""
# puts ".IP \"\\(bu\""
set txtline [lindex $itemtxtblock 0]
set txtline [lineregsub {^\*\ } $txtline ""]
lappend txtblock $txtline
foreach txtline [lrange $itemtxtblock 1 end] {
set txtline [lineregsub {^\ \ } $txtline ""]
lappend txtblock $txtline
}
handle_txtblock item $txtblock
}
### DESCRIPTION ###
proc process_description_man {txtblock} {
puts ".RS 0.0"
handle_txtblock description $txtblock
puts ".RE"
puts ".sp"
}
### DESCRIPTION ITEM ###
proc process_descitem_man {itemtxtblock} {
set txtline [lindex $itemtxtblock 0]
set desc_name ""
regexp {^\:(.+)\:} [linetxt $txtline] dummy desc_name
set txtline [lineregsub {^\:(.+)\: *} $txtline ""]
puts ".TP\n[man_mkboldline [out_man $desc_name]]"
lappend txtblock $txtline
foreach txtline [lrange $itemtxtblock 1 end] {
set txtline [lineregsub {^\ \ } $txtline ""]
lappend txtblock $txtline
}
handle_txtblock descitem $txtblock
}
set enum_level 0
### ENUMERATION ###
proc process_enumeration_man {txtblock} {
global enum_level enum_cnt
incr enum_level
set enum_cnt($enum_level) 0
puts ".RS"
handle_txtblock enumeration $txtblock
puts ".RE"
puts ""
incr enum_level -1
}
### ENUM ITEM ###
proc process_enum_man {itemtxtblock} {
global enum_level enum_cnt
puts ".TP"
incr enum_cnt($enum_level)
puts ".B $enum_cnt($enum_level)."
set txtline [lindex $itemtxtblock 0]
set txtline [lineregsub {^\#\ } $txtline ""]
lappend txtblock $txtline
foreach txtline [lrange $itemtxtblock 1 end] {
set txtline [lineregsub {^\ \ } $txtline ""]
lappend txtblock $txtline
}
handle_txtblock item $txtblock
}
### PLAIN ###
proc process_plain_man {plaintxtblock} {
while {[lindex $plaintxtblock end] == ""} {
set plaintxtblock [lrange $plaintxtblock 0 [expr [llength $plaintxtblock]-2]]
}
foreach txtline $plaintxtblock {
puts [out_man [linetxt $txtline]]
}
}
### ABSTRACT ###
proc process_abstract_man {txtblock} {
set title [linetxt [lindex $txtblock 0]]
puts ".SH \"[out_man [string toupper $title]]\""
handle_txtblock abstract [lrange $txtblock 2 end]
}
### CHAPTER ###
proc process_chapter_man {txtblock} {
set title [linetxt [lindex $txtblock 0]]
puts ".SH \"[out_man [string toupper $title]]\""
handle_txtblock chapter [lrange $txtblock 2 end]
}
### SECTION ###
proc process_section_man {txtblock} {
set title [linetxt [lindex $txtblock 0]]
puts ".SS \"[out_man $title]\""
handle_txtblock section [lrange $txtblock 2 end]
}
### SUBSECTION ###
proc process_subsection_man {txtblock} {
set title [linetxt [lindex $txtblock 0]]
puts ".PP\n[man_mkboldline [out_man $title]]\n.br"
handle_txtblock subsection [lrange $txtblock 2 end]
}
### PARAGRAPH ###
proc process_paragraph_man {txtblock} {
set title [linetxt [lindex $txtblock 0]]
puts ".PP\n[man_mkboldline [out_man $title.]]"
handle_txtblock paragraph [lrange $txtblock 2 end]
}
set config_man_no_date [regexp {\--man-no-date} $argv dummy]
set outmode man