diff options
| author | Shulhan <m.shulhan@gmail.com> | 2020-11-24 02:07:56 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2020-11-24 02:07:56 +0700 |
| commit | d5502ca1b06f72b56709f24fae4fc2d77184be5d (patch) | |
| tree | 7e721bd5b23eda9df2023558304c1065b794b686 | |
| parent | f14e27f9be408ae34ac7f3d6d15eaa9fbede8cee (diff) | |
| download | asciidoctor-go-d5502ca1b06f72b56709f24fae4fc2d77184be5d.tar.xz | |
all: support all column style "a", "e", "h", "l", "m", "d", "s", "v"
| -rw-r--r-- | README.adoc | 3 | ||||
| -rw-r--r-- | adoc_node.go | 2 | ||||
| -rw-r--r-- | adoc_table.go | 2 | ||||
| -rw-r--r-- | document_parser.go | 17 | ||||
| -rw-r--r-- | html_backend.go | 61 | ||||
| -rw-r--r-- | testdata/got.test.html | 51 | ||||
| -rw-r--r-- | testdata/test.adoc | 12 | ||||
| -rw-r--r-- | testdata/test.html | 48 |
8 files changed, 174 insertions, 22 deletions
diff --git a/README.adoc b/README.adoc index 0886764..a56a896 100644 --- a/README.adoc +++ b/README.adoc @@ -51,6 +51,9 @@ https://asciidoctor.org/docs/user-manual/[AsciiDoc user manual] ** 21.2. Numbering Styles * 22. Description List ** 22.1. Question and Answer Style List +* 23. Tables +** 23.1. Columns +** 23.2. Column formatting * 24. Horizontal Rules ** 24.1. Markdown-style horizontal rules * 25. Page Break diff --git a/adoc_node.go b/adoc_node.go index cec0fca..718f740 100644 --- a/adoc_node.go +++ b/adoc_node.go @@ -1006,7 +1006,7 @@ func (node *adocNode) toHTML(doc *Document, w io.Writer, isForToC bool) { fmt.Fprintf(w, "<sup>%s</sup>", node.raw) case nodeKindTable: - htmlWriteTable(node, w) + htmlWriteTable(doc, node, w) } if node.child != nil { diff --git a/adoc_table.go b/adoc_table.go index c66cdc4..8e18a8a 100644 --- a/adoc_table.go +++ b/adoc_table.go @@ -30,7 +30,7 @@ func newTable(attrCols string, content []byte) (table *adocTable) { var ( row tableRow ) - if len(rawRows) == 1 || (len(rawRows[0]) > 0 && len(rawRows[1]) == 0) { + if len(rawRows) >= 2 && len(rawRows[0]) > 1 && len(rawRows[1]) == 0 { table.header = parseTableRow(rawRows[0]) if table.ncols == 0 { table.ncols = len(table.header) diff --git a/document_parser.go b/document_parser.go index 6028880..71588ff 100644 --- a/document_parser.go +++ b/document_parser.go @@ -52,6 +52,23 @@ func Parse(content []byte) (doc *Document) { return doc } +func parseSub(parentDoc *Document, content []byte) (subdoc *Document) { + subdoc = newDocument() + + for k, v := range parentDoc.Attributes { + subdoc.Attributes[k] = v + } + + docp := &documentParser{ + doc: subdoc, + p: parser.New(string(content), "\n"), + } + + docp.parseBlock(subdoc.content, 0) + + return subdoc +} + func (docp *documentParser) consumeLinesUntil( node *adocNode, term int, terms []int, ) ( diff --git a/html_backend.go b/html_backend.go index 156e3d9..b98ba0e 100644 --- a/html_backend.go +++ b/html_backend.go @@ -17,6 +17,7 @@ const ( classNameHalignLeft = "halign-left" classNameHalignRight = "halign-right" classNameListingBlock = "listingblock" + classNameLiteral = "literal" classNameLiteralBlock = "literalblock" classNameTableBlock = "tableblock" classNameToc = "toc" @@ -516,7 +517,7 @@ func hmltWriteSectionDiscrete(doc *Document, node *adocNode, out io.Writer) { fmt.Fprintf(out, "</%s>", tag) } -func htmlWriteTable(node *adocNode, out io.Writer) { +func htmlWriteTable(doc *Document, node *adocNode, out io.Writer) { if node.table == nil { return } @@ -530,39 +531,79 @@ func htmlWriteTable(node *adocNode, out io.Writer) { fmt.Fprint(out, "\n</colgroup>") if len(node.table.header) > 0 { - htmlWriteTableHeader(node.table.header, out) + htmlWriteTableHeader(doc, node.table.header, out) } fmt.Fprint(out, "\n<tbody>") for _, row := range node.table.rows { - htmlWriteTableRow(node.table, row, out) + htmlWriteTableRow(doc, node.table, row, out) } fmt.Fprint(out, "\n</tbody>") fmt.Fprint(out, "\n</table>") } -func htmlWriteTableHeader(header tableRow, out io.Writer) { +func htmlWriteTableHeader(doc *Document, header tableRow, out io.Writer) { classRow := "tableblock halign-left valign-top" fmt.Fprint(out, "\n<thead>\n<tr>") for _, col := range header { - fmt.Fprintf(out, "\n<th class=%q>%s</th>", classRow, col) + fmt.Fprintf(out, "\n<th class=%q>", classRow) + cont := parseInlineMarkup(doc, []byte(col)) + cont.toHTML(doc, out, false) + fmt.Fprint(out, "</th>") } fmt.Fprint(out, "\n</tr>\n</thead>") } -func htmlWriteTableRow(table *adocTable, row tableRow, out io.Writer) { - classP := "tableblock" - +func htmlWriteTableRow(doc *Document, table *adocTable, row tableRow, out io.Writer) { fmt.Fprint(out, "\n<tr>") for x, col := range row { if x == table.ncols { break } + format := table.formats[x] - fmt.Fprintf(out, "\n<td class=%q><p class=%q>%s</p></td>", - format.htmlClasses(), classP, col) + tag := "td" + if format.style == colStyleHeader { + tag = "th" + } + + fmt.Fprintf(out, "\n<%s class=%q>", tag, format.htmlClasses()) + + switch format.style { + case colStyleAsciidoc: + subdoc := parseSub(doc, []byte(col)) + _ = subdoc.ToEmbeddedHTML(out) + + case colStyleDefault: + fmt.Fprintf(out, "<p class=%q>", classNameTableBlock) + container := parseInlineMarkup(doc, []byte(col)) + container.toHTML(doc, out, false) + fmt.Fprint(out, "</p>") + + case colStyleHeader, colStyleVerse: + fmt.Fprintf(out, "<p class=%q>%s</p>", + classNameTableBlock, col) + + case colStyleEmphasis: + fmt.Fprintf(out, "<p class=%q><em>%s</em></p>", + classNameTableBlock, col) + + case colStyleLiteral: + fmt.Fprintf(out, "<div class=%q><pre>%s</pre></div>", + classNameLiteral, col) + + case colStyleMonospaced: + fmt.Fprintf(out, "<p class=%q><code>%s</code></p>", + classNameTableBlock, col) + + case colStyleStrong: + fmt.Fprintf(out, "<p class=%q><strong>%s</strong></p>", + classNameTableBlock, col) + } + + fmt.Fprintf(out, "</%s>", tag) } fmt.Fprint(out, "\n</tr>") } diff --git a/testdata/got.test.html b/testdata/got.test.html index 98ce40d..0002074 100644 --- a/testdata/got.test.html +++ b/testdata/got.test.html @@ -77,7 +77,6 @@ <li><a href="#_table">19. Table</a> <ul class="sectlevel2"> <li><a href="#_column_formatting">19.1. Column Formatting</a></li> -<li><a href="#_column_width">19.2. Column width</a></li> </ul> </li> <li><a href="#_horizontal_rules">20. Horizontal rules</a></li> @@ -1392,9 +1391,9 @@ Hard drive </tr> </tbody> </table> +<div class="paragraph"> +<p>With horizontal and vertical alignment; and width,</p> </div> -<div class="sect2"> -<h3 id="_column_width"><a class="anchor" href="#_column_width"></a><a class="link" href="#_column_width">19.2. Column width</a></h3> <table class="tableblock frame-all grid-all stretch"> <colgroup> <col style="width: 10%;"> @@ -1414,6 +1413,50 @@ Hard drive </tr> </tbody> </table> +<div class="paragraph"> +<p>With column styles,</p> +</div> +<table class="tableblock frame-all grid-all stretch"> +<colgroup> +<col style="width: 12.5%;"> +<col style="width: 12.5%;"> +<col style="width: 12.5%;"> +<col style="width: 12.5%;"> +<col style="width: 12.5%;"> +<col style="width: 12.5%;"> +<col style="width: 12.5%;"> +<col style="width: 12.5%;"> +</colgroup> +<thead> +<tr> +<th class="tableblock halign-left valign-top">Asciidoc</th> +<th class="tableblock halign-left valign-top"><code>emphasis</code></th> +<th class="tableblock halign-left valign-top">header</th> +<th class="tableblock halign-left valign-top">literal</th> +<th class="tableblock halign-left valign-top">mono</th> +<th class="tableblock halign-left valign-top">default</th> +<th class="tableblock halign-left valign-top">strong</th> +<th class="tableblock halign-left valign-top">verse</th> +</tr> +</thead> +<tbody> +<tr> +<td class="tableblock halign-left valign-top"> +<div id="content"> +<div class="paragraph"> +<p>Asciidoc</p> +</div> +</div></td> +<td class="tableblock halign-left valign-top"><p class="tableblock"><em>emphasis</em></p></td> +<th class="tableblock halign-left valign-top"><p class="tableblock">header</p></th> +<td class="tableblock halign-left valign-top"><div class="literal"><pre>literal</pre></div></td> +<td class="tableblock halign-left valign-top"><p class="tableblock"><code>mono</code></p></td> +<td class="tableblock halign-left valign-top"><p class="tableblock">default</p></td> +<td class="tableblock halign-left valign-top"><p class="tableblock"><strong>strong</strong></p></td> +<td class="tableblock halign-left valign-top"><p class="tableblock">verse</p></td> +</tr> +</tbody> +</table> </div> </div> </div> @@ -2155,7 +2198,7 @@ this sidebar.</p> <div id="footer"> <div id="footer-text"> 1.1.1<br> -Last updated 2020-11-23 00:19:13 +0700 +Last updated 2020-11-24 02:04:53 +0700 </div> </div> </body> diff --git a/testdata/test.adoc b/testdata/test.adoc index c35dc6d..c700e90 100644 --- a/testdata/test.adoc +++ b/testdata/test.adoc @@ -566,8 +566,7 @@ With row columns greater than header, |Cell in column 3, row 2 |=== - -=== Column width +With horizontal and vertical alignment; and width, [cols="<.<,^.^3,>.>6"] |=== @@ -580,6 +579,15 @@ With row columns greater than header, |Cell in column 3, row 2 |=== +With column styles, + +[cols="a,e,h,l,m,d,s,v"] +|=== +|Asciidoc | `emphasis` | header | literal | mono | default | strong | verse + +|Asciidoc | emphasis | header | literal | mono | default | strong | verse +|=== + == Horizontal rules diff --git a/testdata/test.html b/testdata/test.html index 8d06cb5..4c2e3a6 100644 --- a/testdata/test.html +++ b/testdata/test.html @@ -77,7 +77,6 @@ <li><a href="#_table">19. Table</a> <ul class="sectlevel2"> <li><a href="#_column_formatting">19.1. Column Formatting</a></li> -<li><a href="#_column_width">19.2. Column width</a></li> </ul> </li> <li><a href="#_horizontal_rules">20. Horizontal rules</a></li> @@ -1396,9 +1395,9 @@ D</p></td> </tr> </tbody> </table> +<div class="paragraph"> +<p>With horizontal and vertical alignment; and width,</p> </div> -<div class="sect2"> -<h3 id="_column_width"><a class="anchor" href="#_column_width"></a><a class="link" href="#_column_width">19.2. Column width</a></h3> <table class="tableblock frame-all grid-all stretch"> <colgroup> <col style="width: 10%;"> @@ -1418,6 +1417,47 @@ D</p></td> </tr> </tbody> </table> +<div class="paragraph"> +<p>With column styles,</p> +</div> +<table class="tableblock frame-all grid-all stretch"> +<colgroup> +<col style="width: 12.5%;"> +<col style="width: 12.5%;"> +<col style="width: 12.5%;"> +<col style="width: 12.5%;"> +<col style="width: 12.5%;"> +<col style="width: 12.5%;"> +<col style="width: 12.5%;"> +<col style="width: 12.5%;"> +</colgroup> +<thead> +<tr> +<th class="tableblock halign-left valign-top">Asciidoc</th> +<th class="tableblock halign-left valign-top"><code>emphasis</code></th> +<th class="tableblock halign-left valign-top">header</th> +<th class="tableblock halign-left valign-top">literal</th> +<th class="tableblock halign-left valign-top">mono</th> +<th class="tableblock halign-left valign-top">default</th> +<th class="tableblock halign-left valign-top">strong</th> +<th class="tableblock halign-left valign-top">verse</th> +</tr> +</thead> +<tbody> +<tr> +<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph"> +<p>Asciidoc</p> +</div></div></td> +<td class="tableblock halign-left valign-top"><p class="tableblock"><em>emphasis</em></p></td> +<th class="tableblock halign-left valign-top"><p class="tableblock">header</p></th> +<td class="tableblock halign-left valign-top"><div class="literal"><pre> literal</pre></div></td> +<td class="tableblock halign-left valign-top"><p class="tableblock"><code>mono</code></p></td> +<td class="tableblock halign-left valign-top"><p class="tableblock">default</p></td> +<td class="tableblock halign-left valign-top"><p class="tableblock"><strong>strong</strong></p></td> +<td class="tableblock halign-left valign-top"><p class="tableblock">verse</p></td> +</tr> +</tbody> +</table> </div> </div> </div> @@ -2156,7 +2196,7 @@ this sidebar.</p> <div id="footer"> <div id="footer-text"> 1.1.1<br> -Last updated 2020-11-23 00:19:13 +0700 +Last updated 2020-11-24 02:04:52 +0700 </div> </div> </body> |
