• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

A Markdown shard for the Crystal programming language


Commit MetaInfo

Révision1ec665f4fddae9acb7f979e2849803b595d108d8 (tree)
l'heure2023-04-07 11:07:45
Auteursupercell <stigma@disr...>
Commitersupercell

Message de Log

Optimize TableSyntax

Change Summary

Modification

--- a/spec/extensions/tables.unit
+++ b/spec/extensions/tables.unit
@@ -4,7 +4,20 @@ head | cells
44 body | cells
55
66 <<<
7-<table><thead><tr><th>head</th><th>cells</th></tr></thead><tbody><tr><td>body</td><td>cells</td></tr></tbody></table>
7+<table>
8+<thead>
9+<tr>
10+<th>head</th>
11+<th>cells</th>
12+</tr>
13+</thead>
14+<tbody>
15+<tr>
16+<td>body</td>
17+<td>cells</td>
18+</tr>
19+</tbody>
20+</table>
821 >>> multiple rows
922 head | cells
1023 -----|------
@@ -12,40 +25,124 @@ body | cells
1225 more | cells
1326
1427 <<<
15-<table><thead><tr><th>head</th><th>cells</th></tr></thead><tbody><tr><td>body</td><td>cells</td></tr><tr><td>more</td><td>cells</td></tr></tbody></table>
28+<table>
29+<thead>
30+<tr>
31+<th>head</th>
32+<th>cells</th>
33+</tr>
34+</thead>
35+<tbody>
36+<tr>
37+<td>body</td>
38+<td>cells</td>
39+</tr>
40+<tr>
41+<td>more</td>
42+<td>cells</td>
43+</tr>
44+</tbody>
45+</table>
1646 >>> rows wrapped in pipes
1747 | head | cells |
1848 |------|-------|
1949 | body | cells |
2050
2151 <<<
22-<table><thead><tr><th>head</th><th>cells</th></tr></thead><tbody><tr><td>body</td><td>cells</td></tr></tbody></table>
52+<table>
53+<thead>
54+<tr>
55+<th>head</th>
56+<th>cells</th>
57+</tr>
58+</thead>
59+<tbody>
60+<tr>
61+<td>body</td>
62+<td>cells</td>
63+</tr>
64+</tbody>
65+</table>
2366 >>> rows wrapped in pipes, whitespace alignment row
2467 | head | cells |
2568 | -- | --- |
2669 | body | cells |
2770
2871 <<<
29-<table><thead><tr><th>head</th><th>cells</th></tr></thead><tbody><tr><td>body</td><td>cells</td></tr></tbody></table>
72+<table>
73+<thead>
74+<tr>
75+<th>head</th>
76+<th>cells</th>
77+</tr>
78+</thead>
79+<tbody>
80+<tr>
81+<td>body</td>
82+<td>cells</td>
83+</tr>
84+</tbody>
85+</table>
3086 >>> rows wrapped in pipes, tabs in whitespace
3187 | head | cells |
3288 | -- | --- |
3389 | body | cells |
3490
3591 <<<
36-<table><thead><tr><th>head</th><th>cells</th></tr></thead><tbody><tr><td>body</td><td>cells</td></tr></tbody></table>
92+<table>
93+<thead>
94+<tr>
95+<th>head</th>
96+<th>cells</th>
97+</tr>
98+</thead>
99+<tbody>
100+<tr>
101+<td>body</td>
102+<td>cells</td>
103+</tr>
104+</tbody>
105+</table>
37106 >>> cells with inline syntax
38107 head `code` | _cells_
39108 ------------|--------
40109 *text* | <span>text</span>
41110 <<<
42-<table><thead><tr><th>head <code>code</code></th><th><em>cells</em></th></tr></thead><tbody><tr><td><em>text</em></td><td><span>text</span></td></tr></tbody></table>
111+<table>
112+<thead>
113+<tr>
114+<th>head <code>code</code></th>
115+<th><em>cells</em></th>
116+</tr>
117+</thead>
118+<tbody>
119+<tr>
120+<td><em>text</em></td>
121+<td><span>text</span></td>
122+</tr>
123+</tbody>
124+</table>
43125 >>> cells are parsed before inline syntax
44126 header | _foo | bar_
45127 -------|------------|---
46128 text | text
47129 <<<
48-<table><thead><tr><th>header</th><th>_foo</th><th>bar_</th></tr></thead><tbody><tr><td>text</td><td>text</td><td /></tr></tbody></table>
130+<table>
131+<thead>
132+<tr>
133+<th>header</th>
134+<th>_foo</th>
135+<th>bar_</th>
136+</tr>
137+</thead>
138+<tbody>
139+<tr>
140+<td>text</td>
141+<td>text</td>
142+<td></td>
143+</tr>
144+</tbody>
145+</table>
49146 >>> cells contain reference links
50147 header | header
51148 -------|--------
@@ -53,20 +150,64 @@ text | [link][here]
53150
54151 [here]: http://url
55152 <<<
56-<table><thead><tr><th>header</th><th>header</th></tr></thead><tbody><tr><td>text</td><td><a href="http://url">link</a></td></tr></tbody></table>
153+<table>
154+<thead>
155+<tr>
156+<th>header</th>
157+<th>header</th>
158+</tr>
159+</thead>
160+<tbody>
161+<tr>
162+<td>text</td>
163+<td><a href="http://url">link</a></td>
164+</tr>
165+</tbody>
166+</table>
57167 >>> one column tables
58168 head
59169 -----|
60170 body
61171 <<<
62-<table><thead><tr><th>head</th></tr></thead><tbody><tr><td>body</td></tr></tbody></table>
172+<table>
173+<thead>
174+<tr>
175+<th>head</th>
176+</tr>
177+</thead>
178+<tbody>
179+<tr>
180+<td>body</td>
181+</tr>
182+</tbody>
183+</table>
63184 >>> varying cells per row
64185 head | foo | bar
65186 -----|-----|-----
66187 body
67188 row with | two cells
68189 <<<
69-<table><thead><tr><th>head</th><th>foo</th><th>bar</th></tr></thead><tbody><tr><td>body</td><td /><td /></tr><tr><td>row with</td><td>two cells</td><td /></tr></tbody></table>
190+<table>
191+<thead>
192+<tr>
193+<th>head</th>
194+<th>foo</th>
195+<th>bar</th>
196+</tr>
197+</thead>
198+<tbody>
199+<tr>
200+<td>body</td>
201+<td></td>
202+<td></td>
203+</tr>
204+<tr>
205+<td>row with</td>
206+<td>two cells</td>
207+<td></td>
208+</tr>
209+</tbody>
210+</table>
70211 >>> left, center, and right alignment
71212 head | cells | here
72213 :----|:-----:|----:
@@ -74,7 +215,27 @@ body | cells | here
74215 too | many | cells | here
75216
76217 <<<
77-<table><thead><tr><th style="text-align: left;">head</th><th style="text-align: center;">cells</th><th style="text-align: right;">here</th></tr></thead><tbody><tr><td style="text-align: left;">body</td><td style="text-align: center;">cells</td><td style="text-align: right;">here</td></tr><tr><td style="text-align: left;">too</td><td style="text-align: center;">many</td><td style="text-align: right;">cells</td></tr></tbody></table>
218+<table>
219+<thead>
220+<tr>
221+<th align="left">head</th>
222+<th align="center">cells</th>
223+<th align="right">here</th>
224+</tr>
225+</thead>
226+<tbody>
227+<tr>
228+<td align="left">body</td>
229+<td align="center">cells</td>
230+<td align="right">here</td>
231+</tr>
232+<tr>
233+<td align="left">too</td>
234+<td align="center">many</td>
235+<td align="right">cells</td>
236+</tr>
237+</tbody>
238+</table>
78239 >>> left, center, and right alignment, with whitespace
79240 head | cells | here
80241 :-- | :---: | ---:
@@ -82,7 +243,27 @@ body | cells | here
82243 too | many | cells | here
83244
84245 <<<
85-<table><thead><tr><th style="text-align: left;">head</th><th style="text-align: center;">cells</th><th style="text-align: right;">here</th></tr></thead><tbody><tr><td style="text-align: left;">body</td><td style="text-align: center;">cells</td><td style="text-align: right;">here</td></tr><tr><td style="text-align: left;">too</td><td style="text-align: center;">many</td><td style="text-align: right;">cells</td></tr></tbody></table>
246+<table>
247+<thead>
248+<tr>
249+<th align="left">head</th>
250+<th align="center">cells</th>
251+<th align="right">here</th>
252+</tr>
253+</thead>
254+<tbody>
255+<tr>
256+<td align="left">body</td>
257+<td align="center">cells</td>
258+<td align="right">here</td>
259+</tr>
260+<tr>
261+<td align="left">too</td>
262+<td align="center">many</td>
263+<td align="right">cells</td>
264+</tr>
265+</tbody>
266+</table>
86267 >>> escape pipe
87268 | Name | Character |
88269 | --- | --- |
@@ -90,17 +271,60 @@ too | many | cells | here
90271 | Pipe | \| |
91272
92273 <<<
93-<table><thead><tr><th>Name</th><th>Character</th></tr></thead><tbody><tr><td>Backtick</td><td>`</td></tr><tr><td>Pipe</td><td>|</td></tr></tbody></table>
274+<table>
275+<thead>
276+<tr>
277+<th>Name</th>
278+<th>Character</th>
279+</tr>
280+</thead>
281+<tbody>
282+<tr>
283+<td>Backtick</td>
284+<td>`</td>
285+</tr>
286+<tr>
287+<td>Pipe</td>
288+<td>|</td>
289+</tr>
290+</tbody>
291+</table>
94292 >>> escape pipe, preserve trailing whitespace
95293 | Name | Character |
96294 | --- | --- |
97295 | Pipe | \| abcdef |
98296
99297 <<<
100-<table><thead><tr><th>Name</th><th>Character</th></tr></thead><tbody><tr><td>Pipe</td><td>| abcdef</td></tr></tbody></table>
298+<table>
299+<thead>
300+<tr>
301+<th>Name</th>
302+<th>Character</th>
303+</tr>
304+</thead>
305+<tbody>
306+<tr>
307+<td>Pipe</td>
308+<td>| abcdef</td>
309+</tr>
310+</tbody>
311+</table>
101312 >>> trailing whitespace after final pipe
102313 | Name | Character |
103314 | --- | --- |
104315 | Pipe | abcdef |
105316 <<<
106-<table><thead><tr><th>Name</th><th>Character</th></tr></thead><tbody><tr><td>Pipe</td><td>abcdef</td></tr></tbody></table>
317+<table>
318+<thead>
319+<tr>
320+<th>Name</th>
321+<th>Character</th>
322+</tr>
323+</thead>
324+<tbody>
325+<tr>
326+<td>Pipe</td>
327+<td>abcdef</td>
328+</tr>
329+</tbody>
330+</table>
--- a/spec/gfm/tables_extension.unit
+++ b/spec/gfm/tables_extension.unit
@@ -3,27 +3,80 @@
33 | --- | --- |
44 | baz | bim |
55 <<<
6-<table><thead><tr><th>foo</th><th>bar</th></tr></thead><tbody><tr><td>baz</td><td>bim</td></tr></tbody></table>
6+<table>
7+<thead>
8+<tr>
9+<th>foo</th>
10+<th>bar</th>
11+</tr>
12+</thead>
13+<tbody>
14+<tr>
15+<td>baz</td>
16+<td>bim</td>
17+</tr>
18+</tbody>
19+</table>
720 >>> Tables (extension) - 199
821 | abc | defghi |
922 :-: | -----------:
1023 bar | baz
1124 <<<
12-<table><thead><tr><th style="text-align: center;">abc</th><th style="text-align: right;">defghi</th></tr></thead><tbody><tr><td style="text-align: center;">bar</td><td style="text-align: right;">baz</td></tr></tbody></table>
25+<table>
26+<thead>
27+<tr>
28+<th align="center">abc</th>
29+<th align="right">defghi</th>
30+</tr>
31+</thead>
32+<tbody>
33+<tr>
34+<td align="center">bar</td>
35+<td align="right">baz</td>
36+</tr>
37+</tbody>
38+</table>
1339 >>> Tables (extension) - 200
1440 | f\|oo |
1541 | ------ |
1642 | b `\|` az |
1743 | b **\|** im |
1844 <<<
19-<table><thead><tr><th>f|oo</th></tr></thead><tbody><tr><td>b <code>|</code> az</td></tr><tr><td>b <strong>|</strong> im</td></tr></tbody></table>
45+<table>
46+<thead>
47+<tr>
48+<th>f|oo</th>
49+</tr>
50+</thead>
51+<tbody>
52+<tr>
53+<td>b <code>|</code> az</td>
54+</tr>
55+<tr>
56+<td>b <strong>|</strong> im</td>
57+</tr>
58+</tbody>
59+</table>
2060 >>> Tables (extension) - 201
2161 | abc | def |
2262 | --- | --- |
2363 | bar | baz |
2464 > bar
2565 <<<
26-<table><thead><tr><th>abc</th><th>def</th></tr></thead><tbody><tr><td>bar</td><td>baz</td></tr></tbody></table>
66+<table>
67+<thead>
68+<tr>
69+<th>abc</th>
70+<th>def</th>
71+</tr>
72+</thead>
73+<tbody>
74+<tr>
75+<td>bar</td>
76+<td>baz</td>
77+</tr>
78+</tbody>
79+</table>
2780 <blockquote>
2881 <p>bar</p>
2982 </blockquote>
@@ -35,14 +88,32 @@ bar
3588
3689 bar
3790 <<<
38-<table><thead><tr><th>abc</th><th>def</th></tr></thead><tbody><tr><td>bar</td><td>baz</td></tr><tr><td>bar</td><td /></tr></tbody></table>
91+<table>
92+<thead>
93+<tr>
94+<th>abc</th>
95+<th>def</th>
96+</tr>
97+</thead>
98+<tbody>
99+<tr>
100+<td>bar</td>
101+<td>baz</td>
102+</tr>
103+<tr>
104+<td>bar</td>
105+<td></td>
106+</tr>
107+</tbody>
108+</table>
39109 <p>bar</p>
40110 >>> Tables (extension) - 203
41111 | abc | def |
42112 | --- |
43113 | bar |
44114 <<<
45-<p>| --- |
115+<p>| abc | def |
116+| --- |
46117 | bar |</p>
47118 >>> Tables (extension) - 204
48119 | abc | def |
@@ -50,9 +121,33 @@ bar
50121 | bar |
51122 | bar | baz | boo |
52123 <<<
53-<table><thead><tr><th>abc</th><th>def</th></tr></thead><tbody><tr><td>bar</td><td /></tr><tr><td>bar</td><td>baz</td></tr></tbody></table>
124+<table>
125+<thead>
126+<tr>
127+<th>abc</th>
128+<th>def</th>
129+</tr>
130+</thead>
131+<tbody>
132+<tr>
133+<td>bar</td>
134+<td></td>
135+</tr>
136+<tr>
137+<td>bar</td>
138+<td>baz</td>
139+</tr>
140+</tbody>
141+</table>
54142 >>> Tables (extension) - 205
55143 | abc | def |
56144 | --- | --- |
57145 <<<
58-<table><thead><tr><th>abc</th><th>def</th></tr></thead></table>
146+<table>
147+<thead>
148+<tr>
149+<th>abc</th>
150+<th>def</th>
151+</tr>
152+</thead>
153+</table>
--- a/src/luce/block_parser.cr
+++ b/src/luce/block_parser.cr
@@ -88,6 +88,10 @@ module Luce
8888 @pos += 1
8989 end
9090
91+ def retreat : Nil
92+ @pos -= 1
93+ end
94+
9195 def done? : Bool
9296 @pos >= @lines.size
9397 end
@@ -106,11 +110,23 @@ module Luce
106110
107111 def parse_lines : Array(Node)
108112 blocks = [] of Node
113+
114+ # If the @pos does not change before and after `parse()`, never try to
115+ # parse the line at @pos with the same syntax again.
116+ # For example, the `TableSyntax` might not advance the @pos in `parse`
117+ # method, because the header row does not match the delimiter row in the
118+ # number of cells, which makes the table like structure not be recognized.
119+ never_match : BlockSyntax? = nil
120+
109121 until done?
110122 block_syntaxes.each do |syntax|
123+ next if never_match == syntax
124+
111125 if syntax.can_parse? self
126+ position_before = @pos
112127 block = syntax.parse self
113128 blocks << block unless block.nil?
129+ never_match = @pos != position_before ? nil : syntax
114130 break
115131 end
116132 end
--- a/src/luce/block_syntaxes/table_syntax.cr
+++ b/src/luce/block_syntaxes/table_syntax.cr
@@ -28,7 +28,10 @@ module Luce
2828 alignments = parse_alignments(parser.next.not_nil!)
2929 column_count = alignments.size
3030 head_row = parse_row(parser, alignments, "th")
31- return nil if head_row.children.not_nil!.size != column_count
31+ if head_row.children.not_nil!.size != column_count
32+ parser.retreat
33+ return nil
34+ end
3235
3336 head = Element.new("thead", [head_row.as Node])
3437
@@ -42,7 +45,7 @@ module Luce
4245 if !children.nil?
4346 while children.size < column_count
4447 # Insert synthetic empty cells.
45- children << Element.empty("td").as Node
48+ children << Element.new("td", [] of Node)
4649 end
4750 while children.size > column_count
4851 children.pop
@@ -62,29 +65,37 @@ module Luce
6265 end
6366
6467 private def parse_alignments(line : String) : Array(String?)
65- start_index = walk_past_opening_pipe(line)
66-
67- end_index = line.size - 1
68- while end_index > 0
69- ch = line.codepoint_at(end_index)
70- if ch == Charcode::PIPE
71- end_index -= 1
72- break
68+ columns = [] of String?
69+ # Set the value to `true` when hitting a non whitespace character other
70+ # than the first pipe character.
71+ started = false
72+ hit_dash = false
73+ alignment : String? = nil
74+
75+ line.each_codepoint do |char|
76+ next if char == Charcode::SPACE || char == Charcode::TAB || (!started && char == Charcode::PIPE)
77+ started = true
78+
79+ if char == Charcode::COLON
80+ if hit_dash
81+ alignment = alignment == "left" ? "center" : "right"
82+ else
83+ alignment = "left"
84+ end
7385 end
74- if ch != Charcode::SPACE && ch != Charcode::TAB
75- break
86+
87+ if char == Charcode::PIPE
88+ columns << alignment
89+ hit_dash = false
90+ alignment = nil
91+ else
92+ hit_dash = true
7693 end
77- end_index -= 1
7894 end
7995
80- # Optimization: We walk *line* too many times. One lap should do it.
81- line[start_index..end_index].split('|').map do |column|
82- column = column.strip
83- next "center" if column.starts_with?(":") && column.ends_with?(":")
84- next "left" if column.starts_with?(":")
85- next "right" if column.ends_with?(":")
86- nil
87- end
96+ columns << alignment if hit_dash
97+
98+ columns
8899 end
89100
90101 # Parses a table row at the current line into a table row element, with
@@ -152,7 +163,7 @@ module Luce
152163 next
153164 end
154165
155- (row[i].as Element).attributes["style"] = "text-align: #{alignments[i]};"
166+ (row[i].as Element).attributes["align"] = "#{alignments[i]}"
156167 i += 1
157168 end
158169
--- a/src/luce/html_renderer.cr
+++ b/src/luce/html_renderer.cr
@@ -36,6 +36,11 @@ module Luce
3636 "nav",
3737 "section",
3838 "table",
39+ "thead",
40+ "tbody",
41+ "th",
42+ "tr",
43+ "td",
3944 ]
4045
4146 protected def self.block_tags : Array(String)
--- a/tools/gfm_stats.json
+++ b/tools/gfm_stats.json
@@ -219,14 +219,14 @@
219219 "197": "strict"
220220 },
221221 "Tables (extension)": {
222- "198": "fail",
223- "199": "fail",
224- "200": "fail",
225- "201": "fail",
226- "202": "fail",
227- "203": "loose",
228- "204": "fail",
229- "205": "fail"
222+ "198": "strict",
223+ "199": "strict",
224+ "200": "strict",
225+ "201": "strict",
226+ "202": "strict",
227+ "203": "strict",
228+ "204": "strict",
229+ "205": "strict"
230230 },
231231 "Block quotes": {
232232 "206": "strict",
--- a/tools/gfm_stats.txt
+++ b/tools/gfm_stats.txt
@@ -9,7 +9,7 @@
99 21 of 28 - 75.0% Link reference definitions
1010 8 of 8 - 100.0% Paragraphs
1111 1 of 1 - 100.0% Blank lines
12- 1 of 8 - 12.5% Tables (extension)
12+ 8 of 8 - 100.0% Tables (extension)
1313 23 of 25 - 92.0% Block quotes
1414 45 of 48 - 93.8% List items
1515 19 of 26 - 73.1% Lists
@@ -28,5 +28,5 @@
2828 15 of 15 - 100.0% Hard line breaks
2929 2 of 2 - 100.0% Soft line breaks
3030 3 of 3 - 100.0% Textual content
31- 614 of 671 - 91.5% TOTAL
32- 561 of 614 - 91.4% TOTAL Strict
31+ 621 of 671 - 92.5% TOTAL
32+ 569 of 621 - 91.6% TOTAL Strict