-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarkdown_1.0.2b8-extra.patch
299 lines (296 loc) · 6.89 KB
/
Markdown_1.0.2b8-extra.patch
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
--- Markdown.pl.orig 2007-07-29 20:58:53.000000000 +0900
+++ Markdown.pl 2007-07-29 22:32:27.000000000 +0900
@@ -483,6 +483,8 @@
$text = _DoLists($text);
$text = _DoCodeBlocks($text);
$text = _DoBlockQuotes($text);
+ $text = _DoTables($text);
+ $text = _DoDefLists($text);
# We already ran _HashHTMLBlocks() before, in Markdown(), but that
# was to escape raw HTML in the original Markdown source. This time,
@@ -830,6 +832,287 @@
return $text;
}
+sub _DoTables {
+#
+# Form HTML tables.
+#
+ my $text = shift;
+ my $less_than_tab = $g_tab_width - 1;
+
+ #
+ # Find tables with leading pipe.
+ #
+ # | Header 1 | Header 2
+ # | -------- | --------
+ # | Cell 1 | Cell 2
+ # | Cell 3 | Cell 4
+ #
+
+ $text =~ s{
+ ^ # Start of a line
+ [ ]{0,$less_than_tab} # Allowed whitespace.
+ [|] # Optional leading pipe (present)
+ (.+) \n # $1: Header row (at least one pipe)
+
+ [ ]{0,$less_than_tab} # Allowed whitespace.
+ [|] ([ ]*[-:]+[-| :]*) \n # $2: Header underline
+
+ ( # $3: Cells
+ (?:
+ [ ]* # Allowed whitespace.
+ [|] .* \n # Row content.
+ )*
+ )
+ (?=\n|\Z) # Stop at final double newline.
+ }{
+ my $head = $1;
+ my $underline = $2;
+ my $content = $3;
+
+ # Remove leading pipe for each row.
+ $content =~ s/^ *\|//mg;
+
+ _ProcessTables($head, $underline, $content);
+ }egmx;
+
+ #
+ # Find tables without leading pipe.
+ #
+ # Header 1 | Header 2
+ # -------- | --------
+ # Cell 1 | Cell 2
+ # Cell 3 | Cell 4
+ #
+ $text =~ s{
+ ^ # Start of a line
+ [ ]{0,$less_than_tab} # Allowed whitespace.
+ (\S.*[|].*) \n # $1: Header row (at least one pipe)
+
+ [ ]{0,$less_than_tab} # Allowed whitespace.
+ ([-:]+[ ]*[|][-| :]*) \n # $2: Header underline
+
+ ( # $3: Cells
+ (?:
+ .* [|] .* \n # Row content
+ )*
+ )
+ (?=\n|\Z) # Stop at final double newline.
+ }{
+ my $head = $1;
+ my $underline = $2;
+ my $content = $3;
+
+ _ProcessTables($head, $underline, $content);
+ }egmx;
+
+ return $text;
+}
+
+sub trim {
+ my @out = @_;
+ for (@out) {
+ s/^\s+//;
+ s/\s+$//;
+ }
+ return wantarray ? @out : $out[0];
+}
+
+sub rtrim {
+ my @out = @_;
+ for (@out) {
+ s/\s+$//;
+ }
+ return wantarray ? @out : $out[0];
+}
+
+sub _ProcessTables {
+ my $head = shift;
+ my $underline = shift;
+ my $content = shift;
+
+ # Remove any tailing pipes for each line.
+ $head =~ s/[|] *$//mgx;
+ $underline =~ s/[|] *$//mgx;
+ $content =~ s/[|] *$//mgx;
+
+ my @attr;
+ my $n = 0;
+
+ # Reading alignement from header underline.
+ my @separators = split(/ *[|] */, $underline);
+ foreach my $s (@separators) {
+ if ($s =~ /^ *-+: *$/){ $attr[$n] = ' align="right"'; }
+ elsif ($s =~ /^ *:-+: *$/){ $attr[$n] = ' align="center"'; }
+ elsif ($s =~ /^ *:-+ *$/){ $attr[$n] = ' align="left"';}
+ else{ $attr[$n] = '';}
+ $n++;
+ }
+
+ # Creating code spans before splitting the row is an easy way to
+ # handle a code span containg pipes.
+ $head = _DoCodeSpans($head);
+ my @headers = split(/ *[|] */, $head);
+ my $col_count = $#headers + 1;
+
+ # Write column headers.
+ my $text = "<table>\n";
+ $text .= "<thead>\n";
+ $text .= "<tr>\n";
+ $n = 0;
+ foreach my $header (@headers){
+ $text .= " <th$attr[$n]>"._RunSpanGamut(trim($header))."</th>\n";
+ $n++;
+ }
+ $text .= "</tr>\n";
+ $text .= "</thead>\n";
+
+ # Split content by row.
+ my $content0 = $content;
+ chomp $content0;
+ my @rows = split(/\n/, $content0);
+
+ $text .= "<tbody>\n";
+ foreach my $row (@rows) {
+ # Creating code spans before splitting the row is an easy way to
+ # handle a code span containg pipes.
+ $row = _DoCodeSpans($row);
+
+
+ # Split row by cell.
+ my @row_cells = split(/ *[|] */, $row, $col_count);
+ ## $row_cells = array_pad($row_cells, $col_count, '');
+
+ $text .= "<tr>\n";
+ $n = 0;
+ foreach my $cell (@row_cells){
+ $text .= " <td$attr[$n]>"._RunSpanGamut(trim($cell))."</td>\n";
+ $n++;
+ }
+ $text .= "</tr>\n";
+ }
+ $text .= "</tbody>\n";
+ $text .= "</table>";
+
+ return $text . "\n";
+}
+
+sub _DoDefLists {
+ #
+ # Form HTML definition lists.
+ #
+
+ my $text = shift;
+ my $less_than_tab = $g_tab_width - 1;
+
+ # Re-usable pattern to match any entire dl list:
+ my $whole_list = qr{
+ ( # $1 = whole list
+ ( # $2
+ [ ]{0,$less_than_tab}
+ ((?>.*\S.*\n)+) # $3 = defined term
+ \n?
+ [ ]{0,$less_than_tab}:[ ]+ # colon starting definition
+ )
+ (?s:.+?)
+ ( # $4
+ \z
+ |
+ \n{2,}
+ (?=\S)
+ (?! # Negative lookahead for another term
+ [ ]{0,$less_than_tab}
+ (?: \S.*\n )+? # defined term
+ \n?
+ [ ]{0,$less_than_tab}:[ ]+ # colon starting definition
+ )
+ (?! # Negative lookahead for another definition
+ [ ]{0,$less_than_tab}:[ ]+ # colon starting definition
+ )
+ )
+ )
+ }mx;
+
+ $text =~ s{
+ (?:(?<=\n\n)|\A\n?)
+ $whole_list
+ }{
+ # Re-usable patterns to match list item bullets and number markers:
+ my $list = $1;
+
+ # Turn double returns into triple returns, so that we can make a
+ # paragraph for the last item in a list, if necessary:
+ my $result = trim(_ProcessDefListItems($list));
+ $result = "<dl>\n" . $result . "\n</dl>";
+ $result . "\n\n";
+ }egmx;
+
+ return $text;
+}
+
+sub _ProcessDefListItems {
+#
+# Process the contents of a single definition list, splitting it
+# into individual term and definition list items.
+#
+ my $list_str = shift;
+ my $less_than_tab = $g_tab_width - 1;
+
+ # trim trailing blank lines:
+ $list_str =~ s/\n{2,}\z/\n/;
+
+
+ # Process definition terms.
+ $list_str =~ s{
+ (?:\n\n+|\A\n?) # leading line
+ ( # definition terms = $1
+ [ ]{0,$less_than_tab} # leading whitespace
+ (?![:][ ]|[ ]) # negative lookahead for a definition
+ # mark (colon) or more whitespace.
+ (?: \S.* \n)+? # actual term (not whitespace).
+ )
+ (?=\n?[ ]{0,3}:[ ]) # lookahead for following line feed
+ # with a definition mark.
+ }{
+
+ my @terms = split(/\n/, trim($1));
+ my $text = '';
+ foreach my $term (@terms) {
+ $term = _RunSpanGamut(trim($term));
+ $text .= "\n<dt>" . $term . "</dt>";
+ }
+ $text . "\n";
+ }egmx;
+
+ # Process actual definitions.
+ $list_str =~ s{
+ \n(\n+)? # leading line = $1
+ [ ]{0,$less_than_tab} # whitespace before colon
+ [:][ ]+ # definition mark (colon)
+ ((?s:.+?)) # definition text = $2
+ (?= \n+ # stop at next definition mark,
+ (?: # next term or end of text
+ [ ]{0,$less_than_tab} [:][ ] |
+ <dt> | \z
+ )
+ )
+ }{
+ my $leading_line = $1;
+ my $def = $2;
+
+ if ($leading_line || $def =~ /\n{2,}/) {
+ $def = _RunBlockGamut(_Outdent($def . "\n\n"));
+ $def = "\n". $def ."\n";
+ }
+ else {
+ $def = rtrim($def);
+ $def = _RunSpanGamut(_Outdent($def));
+ }
+
+ "\n<dd>" . $def . "</dd>\n";
+ }egmx;
+
+ return $list_str;
+}
sub _DoLists {
#