diff options
Diffstat (limited to 'jimmy')
-rwxr-xr-x | jimmy | 134 |
1 files changed, 94 insertions, 40 deletions
diff --git a/jimmy b/jimmy index 6e7c244..74cef29 100755 --- a/jimmy +++ b/jimmy | |||
@@ -13,59 +13,81 @@ BEGIN { # configuration | |||
13 | if (!to) to = "html" # default: html | 13 | if (!to) to = "html" # default: html |
14 | 14 | ||
15 | if (to == "html") { | 15 | if (to == "html") { |
16 | # blocks | ||
17 | isblock["paragraph"] = 1 | ||
18 | opener["paragraph"] = "<p>" | ||
19 | closer["paragraph"] = "</p>\n" | ||
20 | linefmt["paragraph"] = "%s\n" | ||
21 | isblock["verbatim"] = 1 | ||
16 | opener["verbatim"] = "<pre><code>" | 22 | opener["verbatim"] = "<pre><code>" |
17 | closer["verbatim"] = "</code></pre>\n" | 23 | closer["verbatim"] = "</code></pre>\n" |
18 | linefmt["verbatim"] = "%s\n" | 24 | linefmt["verbatim"] = "%s\n" |
19 | verbatim_esc[1] = "&" | 25 | isblock["quote"] = 1 |
20 | verbatim_repl[1] = "&" | ||
21 | verbatim_esc[2] = "<" | ||
22 | verbatim_repl[2] = "<" | ||
23 | verbatim_esc[3] = ">" | ||
24 | verbatim_repl[3] = ">" | ||
25 | headerfmt[1] = "<h1>%s</h1>\n" | ||
26 | headerfmt[2] = "<h2>%s</h2>\n" | ||
27 | headerfmt[3] = "<h3>%s</h3>\n" | ||
28 | linkfmt = "<a href=\"%s\">%s</a>\n" | ||
29 | link_block = 0 | ||
30 | opener["quote"] = "<blockquote>" | 26 | opener["quote"] = "<blockquote>" |
31 | closer["quote"] = "</blockquote>\n" | 27 | closer["quote"] = "</blockquote>\n" |
32 | linefmt["quote"] = "%s\n" | 28 | linefmt["quote"] = "%s\n" |
33 | opener["paragraph"] = "<p>" | 29 | isblock["list"] = 1 |
34 | closer["paragraph"] = "</p>\n" | ||
35 | linefmt["paragraph"] = "%s\n" | ||
36 | paragraph_collapse = 0 # turn \n to ' ' in paragraphs | ||
37 | # consider: paragraph_fold-- <w>, 0 (do nothing), -1 (collapse) | ||
38 | collapse_blanks = 1 | ||
39 | opener["list"] = "<ul>\n" | 30 | opener["list"] = "<ul>\n" |
40 | closer["list"] = "</ul>\n" | 31 | closer["list"] = "</ul>\n" |
41 | linefmt["list"] = "<li>%s</li>\n" | 32 | linefmt["list"] = "<li>%s</li>\n" |
33 | isblock["linklist"] = 1 | ||
42 | opener["linklist"] = "<ul class=\"linklist\">\n" | 34 | opener["linklist"] = "<ul class=\"linklist\">\n" |
43 | closer["linklist"] = "</ul>\n" | 35 | closer["linklist"] = "</ul>\n" |
44 | linefmt["linklist"] = "<li><a href=\"%s\">%s</a></li>\n" | 36 | linefmt["linklist"] = "<li><a href=\"%s\">%s</a></li>\n" |
37 | # lines | ||
38 | isblock["header"] = 1 | ||
39 | linefmt["header", 1] = "<h1>%s</h1>\n" | ||
40 | linefmt["header", 2] = "<h2>%s</h2>\n" | ||
41 | linefmt["header", 3] = "<h3>%s</h3>\n" | ||
42 | isblock["link"] = 0 | ||
43 | linefmt["link"] = "<a href=\"%s\">%s</a>\n" | ||
44 | # escapes | ||
45 | esc["verbatim", 0, "&"] = "\\&" | ||
46 | esc["verbatim", 8, "<"] = "\\<" | ||
47 | esc["verbatim", 9, ">"] = "\\>" | ||
48 | # fill -- # fill[BLOCK] can be a width, 0, or -1. | ||
49 | ## 0 => do nothing . -1 => all one line | ||
50 | fill["paragraph"] = 0 | ||
51 | fill["quote"] = 0 | ||
52 | # collapse whitespace | ||
53 | collapse_blanks = 1 | ||
54 | # collapse_blanks is a boolean: collapse >1 blank lines? | ||
45 | } else if (to == "gemini") { | 55 | } else if (to == "gemini") { |
46 | opener["verbatim"] = "```\n" | 56 | # blocks |
47 | closer["verbatim"] = "```" | 57 | isblock["paragraph"] = 1 |
48 | linefmt["verbatim"] = "%s\n" | ||
49 | headerfmt[1] = "# %s\n" | ||
50 | headerfmt[2] = "## %s\n" | ||
51 | headerfmt[3] = "### %s\n" | ||
52 | linkfmt = "=> %s %s\n" | ||
53 | link_block = 1 | ||
54 | opener["quote"] = "" | ||
55 | closer["quote"] = "" | ||
56 | linefmt["quote"] = "> %s\n" | ||
57 | opener["paragraph"] = "" | 58 | opener["paragraph"] = "" |
58 | closer["paragraph"] = "" | 59 | closer["paragraph"] = "\n" |
59 | linefmt["paragraph"] = "%s\n" | 60 | linefmt["paragraph"] = "%s\n" |
60 | paragraph_collapse = 1 # turn \n to ' ' in paragraphs | 61 | isblock["verbatim"] = 1 |
61 | # consider: paragraph_fold-- <w>, 0 (do nothing), -1 (collapse) | 62 | opener["verbatim"] = "```\n" |
62 | collapse_blanks = 0 | 63 | closer["verbatim"] = "```\n" |
63 | opener["list"] = "\n" | 64 | linefmt["verbatim"] = "%s\n" |
65 | isblock["quote"] = 1 | ||
66 | opener["quote"] = "> " | ||
67 | closer["quote"] = "\n" | ||
68 | linefmt["quote"] = "%s" | ||
69 | isblock["list"] = 1 | ||
70 | opener["list"] = "" | ||
64 | closer["list"] = "" | 71 | closer["list"] = "" |
65 | linefmt["list"] = "* %s\n" | 72 | linefmt["list"] = "* %s\n" |
66 | opener["linklist"] = "\n" | 73 | isblock["linklist"] = 1 |
74 | opener["linklist"] = "" | ||
67 | closer["linklist"] = "" | 75 | closer["linklist"] = "" |
68 | linefmt["linklist"] = "=> %s %s\n" | 76 | linefmt["linklist"] = "=> %s %s\n" |
77 | # lines | ||
78 | isblock["header"] = 1 | ||
79 | linefmt["header", 1] = "# %s\n" | ||
80 | linefmt["header", 2] = "## %s\n" | ||
81 | linefmt["header", 3] = "### %s\n" | ||
82 | isblock["link"] = 0 | ||
83 | linefmt["link"] = "@NL@=> %s %s@NL@" | ||
84 | # escapes | ||
85 | # fill -- # fill[BLOCK] can be a width, 0, or -1. | ||
86 | ## 0 => do nothing . -1 => all one line | ||
87 | fill["paragraph"] = -1 | ||
88 | fill["quote"] = -1 | ||
89 | # collapse whitespace | ||
90 | collapse_blanks = 1 | ||
69 | } else die("Unknown `to' type: `" to "'") | 91 | } else die("Unknown `to' type: `" to "'") |
70 | } | 92 | } |
71 | 93 | ||
@@ -86,14 +108,16 @@ BLOCK == "verbatim" { | |||
86 | /^#/ { | 108 | /^#/ { |
87 | close_block() | 109 | close_block() |
88 | match($0, /^#+/) | 110 | match($0, /^#+/) |
89 | bufpush(sprintf(headerfmt[RLENGTH], collect(2))) | 111 | bufpush(sprintf(linefmt["header", RLENGTH], collect(2))) |
90 | BLOCK = "header" | 112 | BLOCK = "header" |
91 | next | 113 | next |
92 | } | 114 | } |
93 | 115 | ||
94 | /^=>/ { | 116 | /^=>/ { |
95 | if (BLOCK == "paragraph" && !link_block) | 117 | if (BLOCK == "paragraph" && !isblock["link"]) { |
96 | bufpush(sprintf(linkfmt, $2, collect(3))) | 118 | bufpush(sprintf(linefmt["link"], $2, collect(3))) |
119 | next | ||
120 | } | ||
97 | if (BLOCK != "linklist") close_block() | 121 | if (BLOCK != "linklist") close_block() |
98 | BLOCK = "linklist" | 122 | BLOCK = "linklist" |
99 | bufpush(sprintf(linefmt[BLOCK], $2, collect(3))) | 123 | bufpush(sprintf(linefmt[BLOCK], $2, collect(3))) |
@@ -134,9 +158,17 @@ function close_block () { | |||
134 | if (collapse_blanks) return | 158 | if (collapse_blanks) return |
135 | else printf "\n" | 159 | else printf "\n" |
136 | } | 160 | } |
137 | if (BLOCK == "paragraph" && paragraph_collapse) | 161 | |
138 | gsub(/\n/, " ", BUFFER) | 162 | if (!isblock[BLOCK]) return |
139 | printf("%s%s%s", opener[BLOCK], BUFFER, closer[BLOCK]) | 163 | if (fill[BLOCK]) BUFFER = buffill(BUFFER, fill[BLOCK]) |
164 | |||
165 | for (e in esc) { | ||
166 | if (BLOCK && match(e, "^" BLOCK ".[0-9]")) { | ||
167 | gsub(substr(e, RLENGTH+2), esc[e], BUFFER) | ||
168 | } | ||
169 | } | ||
170 | |||
171 | printf("%s%s%s\n", opener[BLOCK], BUFFER, closer[BLOCK]) | ||
140 | BUFFER = BLOCK = "" | 172 | BUFFER = BLOCK = "" |
141 | } | 173 | } |
142 | 174 | ||
@@ -149,3 +181,25 @@ function collect (begin, end, out) { | |||
149 | function bufpush (str) { | 181 | function bufpush (str) { |
150 | BUFFER = BUFFER str | 182 | BUFFER = BUFFER str |
151 | } | 183 | } |
184 | |||
185 | function buffill(buf, width, out) { | ||
186 | if (width < 0) { | ||
187 | gsub("\n", " ", buf) | ||
188 | out = buf | ||
189 | } | ||
190 | else { | ||
191 | split(buf, arr) | ||
192 | nline = 0 | ||
193 | for (w=1;w<=length(arr);w++) { | ||
194 | if (nline + length(arr[w]) >= width) { | ||
195 | out = out (out?"\n":"") arr[w] | ||
196 | nline = length(arr[w]) | ||
197 | } else { | ||
198 | out = out (out?" ":"") arr[w] | ||
199 | nline += length(arr[w]) + 1 | ||
200 | } | ||
201 | } | ||
202 | } | ||
203 | gsub("@NL@", "\n", out) | ||
204 | return out | ||
205 | } | ||