-
Notifications
You must be signed in to change notification settings - Fork 0
/
abulleted2ltx.awk
executable file
·150 lines (125 loc) · 3.29 KB
/
abulleted2ltx.awk
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
#!/usr/bin/awk -f
#
# Look for the first occurrence -- at a given indentation
# level -- of a dash-bullet, used to make an unordered list
# in an ASCII text file. Once found, output typesetting
# commands for a LaTeX itemization environment to begin,
# transforming each subsequent bullet encountered into a
# LaTeX item. If the indentation level is reduced, end the
# itemization environment. Catch if multiple levels of
# indentation have ended at once, and handle arbitrary
# nesting (iteratively).
#
BEGIN {
BULLET = "- ";
sz_bullet = length(BULLET);
indent_level = 0;
last_was_blank = 0;
bigskip = "1em";
smallskip = "0.2em";
indentations[0] = "";
indentations[0,"skiplen"] = "\\savedskip";
for (i = 0; i < sz_bullet; i++)
stretch = stretch sprintf(" ");
}
/^[[:space:]]*- / {
off_bullet = index($0, BULLET) - 1;
whitespace = substr($0, 0, off_bullet);
#print("whitelen: " length(whitespace))
rest_of_line = substr($0, off_bullet + sz_bullet + 1);
len = indentations[indent_level,"length"];
if (off_bullet > len || len == 0) {
increment_indentation(whitespace);
} else if (off_bullet < len)
decrement_indentation(whitespace);
make_bullet(whitespace);
last_was_blank = 0;
print(rest_of_line);
next;
}
/^[[:space:]]*[[:graph:]]/ {
off = index($0, $1) - 1;
len = indentations[indent_level,"length"];
whitespace = substr($0, 0, off);
if (off < len)
decrement_indentation(whitespace);
#print("hitoff " off " ilvl " indent_level);
if (off == 0 && indent_level) {
#print("hitbig");
do_itemization_end();
}
}
{
print($0);
}
/^$/ {
last_was_blank = 1;
next;
}
{
last_was_blank = 0;
}
##############################################################################
function \
make_itemization_start ()
{
printf("\n")
if (last_was_blank)
skiplen = bigskip;
else
skiplen = smallskip;
indentations[indent_level,"skiplen"] = skiplen;
leader = indentations[indent_level,"leader"];
printf("%s\\setlength{\\parskip}{%s}\n", leader, skiplen);
printf("%s\\begin{itemize}[itemsep=%s,parsep=0em]\n\n",
leader, smallskip);
}
function \
make_itemization_end ()
{
leader = indentations[indent_level+1,"leader"];
skiplen = indentations[indent_level,"skiplen"];
printf("%s\\end{itemize}\n\n", leader);
printf("%s\\setlength{\\parskip}{%s}\n\n", leader, skiplen);
}
function \
do_itemization_end ()
{
#print("inlvl " indent_level);
indentations[indent_level,"itemsep"] = 0;
indent_level--;
make_itemization_end();
}
function \
make_bullet (leader)
{
if (last_was_blank)
itemsep = bigskip
else
itemsep = smallskip
if (indentations[indent_level,"itemsep"] != itemsep) {
indentations[indent_level,"itemsep"] = itemsep;
printf("%s%s\\setlength{\\itemsep}{%s}\n",
leader, stretch, itemsep);
}
printf("%s%s\\item\n%s%s", leader, stretch,
leader, stretch);
}
function \
increment_indentation (leader)
{
printf(indentations[indent_level++,"leader"]);
indentations[indent_level,"length"] = length(leader);
indentations[indent_level,"leader"] = leader;
make_itemization_start();
}
function \
decrement_indentation (leader)
{
len = length(leader);
#print("hit on indent level " indent_level \
# ", len " len \
# ", inlen " indentations[indent_level,"length"]);
while (indentations[indent_level,"length"] > len && indent_level)
do_itemization_end();
}