Skip to content

Commit 22a17b0

Browse files
author
james-ball-qualcomm
authored
Merge pull request #138 from riscv/132-convert-adoc-like-table-syntax
Convert adoc tables to html tables
2 parents 19d71fa + 6ceb068 commit 22a17b0

File tree

8 files changed

+127
-35
lines changed

8 files changed

+127
-35
lines changed

converters/tags.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ def node_text_content(node)
144144
cell.content.join("\n")
145145
end
146146
end
147-
# Separate cells by |.
148-
end.join("|")
147+
# Prefix cells with "|"
148+
end.map { |s| '|' + s }.join
149149
# Separate rows by newlines.
150150
end.join("\n")
151151
# Separate table sections by ===.

tests/norm-rule/expected/test-norm-rules.adoc

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ It's got 2 lines.
8989

9090
.1+| table1
9191
| ===
92-
WITH anchor
93-
WITHOUT anchor
92+
|WITH anchor
93+
|WITHOUT anchor
9494
=== a| link:test.html#norm:table:anchors-in-cells:entire-table[norm:table:anchors-in-cells:entire-table]
9595

9696
.1+| table2
97-
| Header 1|Header 2
97+
| |Header 1|Header 2
9898
===
99-
Cell in column 1, row 1|Cell in column 2, row 1
100-
Cell in column 1, row 2|Cell in column 2, row 2
99+
|Cell in column 1, row 1|Cell in column 2, row 1
100+
|Cell in column 1, row 2|Cell in column 2, row 2
101101
=== a| link:test.html#norm:table:no-anchors-in-cells:entire-table[norm:table:no-anchors-in-cells:entire-table]
102102

103103
.1+| table3
@@ -107,19 +107,19 @@ Cell in column 1, row 2|Cell in column 2, row 2
107107
| ABC DEF a| link:test.html#norm:table:anchors-in-cells:entire-table-not-tagged:cell[norm:table:anchors-in-cells:entire-table-not-tagged:cell]
108108

109109
.1+| table5
110-
| Name|Color
110+
| |Name|Color
111111
===
112-
Roses|Red
113-
Violets|Blue
114-
Name1|Color1
115-
Name2|Color2
116-
Name3|Color3
117-
Name4|Color4
118-
Name5|Color5
119-
Name6|Color6
120-
Name7|Color8
121-
Name9|Color9
122-
Name10|Color10
112+
|Roses|Red
113+
|Violets|Blue
114+
|Name1|Color1
115+
|Name2|Color2
116+
|Name3|Color3
117+
|Name4|Color4
118+
|Name5|Color5
119+
|Name6|Color6
120+
|Name7|Color7
121+
|Name8|Color8
122+
|Name9|Color9
123123
... a| link:test.html#norm:table:many-rows[norm:table:many-rows]
124124

125125
.1+| unordered1

tests/norm-rule/expected/test-norm-rules.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,12 @@ <h3>my-chapter_name</h3>
255255
</tr>
256256
<tr>
257257
<td rowspan=1 id="table1">table1</td>
258-
<td>===<br>WITH anchor<br>WITHOUT anchor<br>===</td>
258+
<td><table><tbody><tr><td>WITH anchor</td></tr><tr><td>WITHOUT anchor</td></tr></tbody></table></td>
259259
<td><a href="test.html#norm:table:anchors-in-cells:entire-table">norm:table:anchors-in-cells:entire-table</a></td>
260260
</tr>
261261
<tr>
262262
<td rowspan=1 id="table2">table2</td>
263-
<td>Header 1|Header 2<br>===<br>Cell in column 1, row 1|Cell in column 2, row 1<br>Cell in column 1, row 2|Cell in column 2, row 2<br>===</td>
263+
<td><table><thead><tr><th>Header 1</th><th>Header 2</th></tr></thead><tbody><tr><td>Cell in column 1, row 1</td><td>Cell in column 2, row 1</td></tr><tr><td>Cell in column 1, row 2</td><td>Cell in column 2, row 2</td></tr></tbody></table></td>
264264
<td><a href="test.html#norm:table:no-anchors-in-cells:entire-table">norm:table:no-anchors-in-cells:entire-table</a></td>
265265
</tr>
266266
<tr>
@@ -275,7 +275,7 @@ <h3>my-chapter_name</h3>
275275
</tr>
276276
<tr>
277277
<td rowspan=1 id="table5">table5</td>
278-
<td>Name|Color<br>===<br>Roses|Red<br>Violets|Blue<br>Name1|Color1<br>Name2|Color2<br>Name3|Color3<br>Name4|Color4<br>Name5|Color5<br>Name6|Color6<br>Name7|Color8<br>Name9|Color9<br>Name10|Color10<br>...</td>
278+
<td><table><thead><tr><th>Name</th><th>Color</th></tr></thead><tbody><tr><td>Roses</td><td>Red</td></tr><tr><td>Violets</td><td>Blue</td></tr><tr><td>Name1</td><td>Color1</td></tr><tr><td>Name2</td><td>Color2</td></tr><tr><td>Name3</td><td>Color3</td></tr><tr><td>Name4</td><td>Color4</td></tr><tr><td>Name5</td><td>Color5</td></tr><tr><td>Name6</td><td>Color6</td></tr><tr><td>Name7</td><td>Color7</td></tr><tr><td>Name8</td><td>Color8</td></tr><tr><td>Name9</td><td>Color9</td></tr><tr><td>Name10</td><td>Color10</td></tr><tr><td>...</td><td>...</td></tr></tbody></table></td>
279279
<td><a href="test.html#norm:table:many-rows">norm:table:many-rows</a></td>
280280
</tr>
281281
<tr>

tests/norm-rule/expected/test-norm-rules.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@
333333
"tags": [
334334
{
335335
"name": "norm:table:anchors-in-cells:entire-table",
336-
"text": "===\nWITH anchor\nWITHOUT anchor\n===",
336+
"text": "===\n|WITH anchor\n|WITHOUT anchor\n===",
337337
"tag_filename": "/build/test-norm-tags.json",
338338
"stds_doc_url": "test.html"
339339
}
@@ -346,7 +346,7 @@
346346
"tags": [
347347
{
348348
"name": "norm:table:no-anchors-in-cells:entire-table",
349-
"text": "Header 1|Header 2\n===\nCell in column 1, row 1|Cell in column 2, row 1\nCell in column 1, row 2|Cell in column 2, row 2\n===",
349+
"text": "|Header 1|Header 2\n===\n|Cell in column 1, row 1|Cell in column 2, row 1\n|Cell in column 1, row 2|Cell in column 2, row 2\n===",
350350
"tag_filename": "/build/test-norm-tags.json",
351351
"stds_doc_url": "test.html"
352352
}
@@ -385,7 +385,7 @@
385385
"tags": [
386386
{
387387
"name": "norm:table:many-rows",
388-
"text": "Name|Color\n===\nRoses|Red\nViolets|Blue\nName1|Color1\nName2|Color2\nName3|Color3\nName4|Color4\nName5|Color5\nName6|Color6\nName7|Color8\nName9|Color9\nName10|Color10\nName11|Color11\nName12|Color12\nName13|Color13\nName14|Color14\n===",
388+
"text": "|Name|Color\n===\n|Roses|Red\n|Violets|Blue\n|Name1|Color1\n|Name2|Color2\n|Name3|Color3\n|Name4|Color4\n|Name5|Color5\n|Name6|Color6\n|Name7|Color7\n|Name8|Color8\n|Name9|Color9\n|Name10|Color10\n|Name11|Color11\n|Name12|Color12\n|Name13|Color13\n|Name14|Color14\n===",
389389
"tag_filename": "/build/test-norm-tags.json",
390390
"stds_doc_url": "test.html"
391391
}
5 Bytes
Binary file not shown.

tests/norm-rule/expected/test-norm-tags.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
"norm:hyperlink4": "DEF &lt;&lt;non-norm-anchor,custom text&gt;&gt; GHI",
2525
"norm:hyperlink5": "GHI &lt;&lt;norm:superscript&gt;&gt; and &lt;&lt;norm:subscript&gt;&gt; JKL",
2626
"norm:hyperlink6": "JKL &lt;&lt;norm:superscript,hello&gt;&gt; and &lt;&lt;norm:subscript,goodbye&gt;&gt; MNO",
27-
"norm:table:no-anchors-in-cells:entire-table": "Header 1|Header 2\n===\nCell in column 1, row 1|Cell in column 2, row 1\nCell in column 1, row 2|Cell in column 2, row 2\n===",
27+
"norm:table:no-anchors-in-cells:entire-table": "|Header 1|Header 2\n===\n|Cell in column 1, row 1|Cell in column 2, row 1\n|Cell in column 1, row 2|Cell in column 2, row 2\n===",
2828
"norm:table:anchors-in-cells:entire-table-tagged:cell": "WITH anchor",
29-
"norm:table:anchors-in-cells:entire-table": "===\nWITH anchor\nWITHOUT anchor\n===",
29+
"norm:table:anchors-in-cells:entire-table": "===\n|WITH anchor\n|WITHOUT anchor\n===",
3030
"norm:table:anchors-in-cells:entire-table-not-tagged:cell": "ABC DEF",
31-
"norm:table:many-rows": "Name|Color\n===\nRoses|Red\nViolets|Blue\nName1|Color1\nName2|Color2\nName3|Color3\nName4|Color4\nName5|Color5\nName6|Color6\nName7|Color8\nName9|Color9\nName10|Color10\nName11|Color11\nName12|Color12\nName13|Color13\nName14|Color14\n===",
31+
"norm:table:many-rows": "|Name|Color\n===\n|Roses|Red\n|Violets|Blue\n|Name1|Color1\n|Name2|Color2\n|Name3|Color3\n|Name4|Color4\n|Name5|Color5\n|Name6|Color6\n|Name7|Color7\n|Name8|Color8\n|Name9|Color9\n|Name10|Color10\n|Name11|Color11\n|Name12|Color12\n|Name13|Color13\n|Name14|Color14\n===",
3232
"norm:unordered-list:no-anchors-in-items:entire-list": "Item A\nItem B\nItem C",
3333
"norm:unordered-list:anchors-in-items:item1": "Item 1",
3434
"norm:unordered-list:anchors-in-items:item2": "Item 2",

tests/norm-rule/test.adoc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Here's some text that isn't normative but the normatively tagged text has an exi
118118
[[norm:table:anchors-in-cells:entire-table]]
119119
|===
120120

121-
// FAILS - Want tagged text to be "cell with anchor"
121+
// FAILS - Want tagged text to be "WITH anchor"
122122
| [#norm:table:anchors-in-cells:entire-table-tagged:cell]#WITH anchor#
123123
| WITHOUT anchor
124124
|===
@@ -145,7 +145,8 @@ a| Cell is adoc
145145
|Name4|Color4
146146
|Name5|Color5
147147
|Name6|Color6
148-
|Name7|Color8
148+
|Name7|Color7
149+
|Name8|Color8
149150
|Name9|Color9
150151
|Name10|Color10
151152
|Name11|Color11

tools/create_normative_rules.rb

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
NORM_PREFIX = "norm:"
1717

18+
MAX_TABLE_ROWS = 12 # Max rows of a table displayed in a cell.
19+
1820
###################################
1921
# Classes for Normative Rule Tags #
2022
###################################
@@ -1016,7 +1018,7 @@ def html_chapter_table(f, table_num, chapter_name, nr_defs, tags, tag_fname2url)
10161018

10171019
unless nr.description.nil?
10181020
f.puts(%Q{ <tr>}) unless row_started
1019-
f.puts(%Q{ <td>#{html_convert_newlines(nr.description)}</td>})
1021+
f.puts(%Q{ <td>#{convert_newlines_to_html(nr.description)}</td>})
10201022
f.puts(%Q{ <td>Rule's "description" property</td>})
10211023
f.puts(%Q{ </tr>})
10221024
row_started = false
@@ -1052,8 +1054,7 @@ def html_chapter_table(f, table_num, chapter_name, nr_defs, tags, tag_fname2url)
10521054
html_fname = tag_fname2url[tag.tag_filename]
10531055
fatal("No fname tag to HTML mapping (-tag2url cmd line arg) for tag fname #{tag.tag_filename} for tag name #{tag.name}") if html_fname.nil?
10541056

1055-
tag_text = html_convert_newlines(limit_table_rows(Adoc2HTML::convert(tag.text)))
1056-
1057+
tag_text = convert_newlines_to_html(convert_tags_tables_to_html(Adoc2HTML::convert(tag.text)))
10571058

10581059
# Convert adoc links to normative text in tag text to html links.
10591060
#
@@ -1139,14 +1140,104 @@ def html_script(f)
11391140
)
11401141
end
11411142

1143+
# Convert the tagged text containing entire tables. Uses format created by "tags" Asciidoctor backend.
1144+
#
1145+
# Two possible formats:
1146+
#
1147+
# Without heading:
1148+
#
1149+
# ===
1150+
# | ABC | DEF
1151+
# |GHI |JKL
1152+
# ===
1153+
#
1154+
# Actual string from tags: "===\n| ABC | DEF\n|GHI |JKL\n==="
1155+
#
1156+
# With heading:
1157+
#
1158+
# | H1 | H2
1159+
# ===
1160+
# | GHI | JKL
1161+
# ===
1162+
#
1163+
# Actual string from tags: "| H1 | H2\n===\n| GHI | JKL\n==="
1164+
1165+
def convert_tags_tables_to_html(text)
1166+
raise ArgumentError, "Expected String for text but was passed a #{text}.class" unless text.is_a?(String)
1167+
1168+
ret = text # Default to input
1169+
1170+
text.gsub(/(.*?)===\n(.+)\n===/m) do
1171+
# Found a "tags" formatted table
1172+
heading = $1.chomp # Remove trailing newline
1173+
rows = $2.split("\n") # Split into array of rows
1174+
1175+
ret = "<table>".dup # Start html table
1176+
1177+
# Add heading if present
1178+
heading_cells = extract_tags_table_cells(heading)
1179+
unless heading_cells.empty?
1180+
ret << "<thead>"
1181+
ret << "<tr>"
1182+
ret << heading_cells.map { |cell| "<th>#{cell}</th>" }.join("")
1183+
ret << "</tr>"
1184+
ret << "</thead>"
1185+
end
1186+
1187+
# Add each row
1188+
ret << "<tbody>"
1189+
rows.each_with_index do |row,index|
1190+
if index < MAX_TABLE_ROWS
1191+
ret << "<tr>"
1192+
row_cells = extract_tags_table_cells(row)
1193+
ret << row_cells.map { |cell| "<td>#{cell}</td>" }.join("")
1194+
ret << "</tr>"
1195+
elsif index == MAX_TABLE_ROWS
1196+
ret << "<tr>"
1197+
row_cells = extract_tags_table_cells(row)
1198+
ret << row_cells.map { |cell| "<td>...</td>" }.join("")
1199+
ret << "</tr>"
1200+
end
1201+
end
1202+
1203+
ret << "</tbody>"
1204+
ret << "</table>" # End html table
1205+
end
1206+
1207+
return ret
1208+
end
1209+
1210+
# Return array of table columns from one row/header of a table.
1211+
def extract_tags_table_cells(text)
1212+
raise ArgumentError, "Expected String for text but was passed a #{text}.class" unless text.is_a?(String)
1213+
1214+
# This pattern matches strings that:
1215+
# - Start with a non-pipe, non-whitespace character
1216+
# - Then contain zero or more non-pipe characters (can include internal spaces)
1217+
# - End with a non-pipe, non-whitespace character
1218+
#
1219+
# All leading/trailing whitespace is removed.
1220+
#
1221+
# Examples:
1222+
# "| H1 | H2".scan(/[^|\s][^|]*[^|\s]/)
1223+
# => ["H1", "H2"]
1224+
#
1225+
# "| ABC | DEF GHI |".scan(/[^|\s][^|]*[^|\s]/)
1226+
# => ["ABC", "DEF GHI"] # Note: internal space preserved
1227+
#
1228+
# "| Name | Value |".scan(/[^|\s][^|]*[^|\s]/)
1229+
# => ["Name", "Value"] # Leading/trailing spaces removed
1230+
text.scan(/[^|\s][^|]*[^|\s]/)
1231+
end
1232+
11421233
# Cleanup the tag text to be suitably displayed.
11431234
def limit_table_rows(text)
11441235
raise ArgumentError, "Expected String for text but was passed a #{text}.class" unless text.is_a?(String)
11451236

11461237
# This is the detection pattern for an entire table being tagged from the "tags.rb" AsciiDoctor backend.
11471238
if text.end_with?("\n===")
11481239
# Limit table size displayed.
1149-
truncate_after_newlines(text, 12)
1240+
truncate_after_newlines(text, MAX_TABLE_ROWS)
11501241
else
11511242
text
11521243
end
@@ -1181,7 +1272,7 @@ def count_parameters(defs)
11811272
end
11821273

11831274
# Convert newlines to <br>.
1184-
def html_convert_newlines(text)
1275+
def convert_newlines_to_html(text)
11851276
raise ArgumentError, "Expected String for text but was passed a #{text}.class" unless text.is_a?(String)
11861277

11871278
text.gsub(/\n/, '<br>')

0 commit comments

Comments
 (0)