Skip to content

Commit

Permalink
feature(renderer): Tables should honor table-caption attribute (bytes…
Browse files Browse the repository at this point in the history
…paradise#717)

This uses a kind of hack to make Table captions work mostly as
they are documented to, so that {counter:table-number} seems to
act like a substitution.  This hack should be removed when full
attribute value substitution is done.  (This approach has the
benefit of localizing this hack until it can be replaced.)

Of course, this will be incompatible with other uses of the
table-counter counter (because it isn't actually the same counter),
and won't support anything beyond the most trivial substitutions.

While here removed a couple of unused functions.
  • Loading branch information
gdamore authored Jul 12, 2020
1 parent 6774f18 commit a6273dd
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 36 deletions.
2 changes: 0 additions & 2 deletions pkg/renderer/sgml/html5/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ const (

tableRowTmpl = "<tr>\n{{ .Content }}</tr>\n"

tableCaptionTmpl = "Table {{ .TableNumber }}. "

tableHeaderCellTmpl = "<th class=\"tableblock halign-{{ .HAlign }} valign-{{ .VAlign }}\">{{ .Content }}</th>\n"

tableCellTmpl = "<td class=\"tableblock halign-{{ .HAlign }} valign-{{ .VAlign }}\"><p class=\"tableblock\">{{ .Content }}</p></td>\n"
Expand Down
1 change: 0 additions & 1 deletion pkg/renderer/sgml/html5/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ var templates = sgml.Templates{
SuperscriptText: superscriptTextTmpl,
Table: tableTmpl,
TableBody: tableBodyTmpl,
TableCaption: tableCaptionTmpl,
TableCell: tableCellTmpl,
TableHeader: tableHeaderTmpl,
TableHeaderCell: tableHeaderCellTmpl,
Expand Down
2 changes: 0 additions & 2 deletions pkg/renderer/sgml/sgml_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ type sgmlRenderer struct {
superscriptText *textTemplate
table *textTemplate
tableBody *textTemplate
tableCaption *textTemplate
tableCell *textTemplate
tableHeader *textTemplate
tableHeaderCell *textTemplate
Expand Down Expand Up @@ -147,7 +146,6 @@ func (r *sgmlRenderer) prepareTemplates() error {
r.superscriptText, err = r.newTemplate("superscript", tmpls.SuperscriptText, err)
r.table, err = r.newTemplate("table", tmpls.Table, err)
r.tableBody, err = r.newTemplate("table-body", tmpls.TableBody, err)
r.tableCaption, err = r.newTemplate("table-caption", tmpls.TableCaption, err)
r.tableCell, err = r.newTemplate("table-cell", tmpls.TableCell, err)
r.tableHeader, err = r.newTemplate("table-header", tmpls.TableHeader, err)
r.tableHeaderCell, err = r.newTemplate("table-header-cell", tmpls.TableHeaderCell, err)
Expand Down
28 changes: 13 additions & 15 deletions pkg/renderer/sgml/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func (r *sgmlRenderer) renderTable(ctx *renderer.Context, t types.Table) (string
caption := &strings.Builder{}

number := 0
title := r.renderElementTitle(t.Attributes)
fit := "stretch"
frame := t.Attributes.GetAsStringWithDefault(types.AttrFrame, "all")
grid := t.Attributes.GetAsStringWithDefault(types.AttrGrid, "all")
Expand Down Expand Up @@ -42,21 +41,20 @@ func (r *sgmlRenderer) renderTable(ctx *renderer.Context, t types.Table) (string
}

if t.Attributes.Has(types.AttrTitle) {
number = ctx.GetAndIncrementTableCounter()
if s, ok := t.Attributes.GetAsString(types.AttrCaption); ok {
caption.WriteString(s)
} else {
err := r.tableCaption.Execute(caption, struct {
TableNumber int
Title sanitized
}{
TableNumber: number,
Title: title,
})
if err != nil {
return "", errors.Wrap(err, "unable to format table caption")
}
c, ok := t.Attributes.GetAsString(types.AttrCaption)
if !ok {
c, _ = ctx.Attributes.GetAsString(types.AttrTableCaption)
}

// TODO: This is a very primitive and incomplete replacement of the counter attribute only.
// This should be removed when attribute values are allowed to contain attributes.
// Also this expansion should be limited to just singly quoted strings in the Attribute list,
// or the default. Ultimately this should all be done long before it gets into the renderer.
if strings.Contains(c, "{counter:table-counter}") {
number = ctx.GetAndIncrementTableCounter()
c = strings.ReplaceAll(c, "{counter:table-counter}", strconv.Itoa(number))
}
caption.WriteString(c)
}

header, err := r.renderTableHeader(ctx, t.Header, t.Columns)
Expand Down
1 change: 0 additions & 1 deletion pkg/renderer/sgml/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ type Templates struct {
SuperscriptText string
Table string
TableBody string
TableCaption string
TableCell string
TableHeader string
TableHeaderCell string
Expand Down
42 changes: 42 additions & 0 deletions pkg/renderer/sgml/xhtml5/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,48 @@ var _ = Describe("tables", func() {
Expect(RenderXHTML(source)).To(MatchHTML(expected))
})

It("2 tables with no caption label", func() {
source := `:table-caption!:
.Title 1
|===
| foo | bar
|===
.Title 2
|===
| foo | bar
|===`
expected := `<table class="tableblock frame-all grid-all stretch">
<caption class="title">Title 1</caption>
<colgroup>
<col style="width: 50%;"/>
<col style="width: 50%;"/>
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">foo</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">bar</p></td>
</tr>
</tbody>
</table>
<table class="tableblock frame-all grid-all stretch">
<caption class="title">Title 2</caption>
<colgroup>
<col style="width: 50%;"/>
<col style="width: 50%;"/>
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">foo</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">bar</p></td>
</tr>
</tbody>
</table>
`
Expect(RenderXHTML(source)).To(MatchHTML(expected))
})

It("2 tables with 2 counters", func() {
source := `.Title 1
|===
Expand Down
9 changes: 2 additions & 7 deletions pkg/types/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ const (
AttrPositional3 = "@3"
// AttrVersionLabel labels the version number in the document
AttrVersionLabel = "version-label"
// AttrTableCaption is the table caption
AttrTableCaption = "table-caption"
)

// NewElementID initializes a new attribute map with a single entry for the ID using the given value
Expand Down Expand Up @@ -330,13 +332,6 @@ func (a Attributes) AppendString(key string, value interface{}) {
}
}

// NilSafeSet sets the key/value pair unless the value is nil or empty
func (a Attributes) NilSafeSet(key string, value interface{}) {
if value != nil && value != "" {
a[key] = value
}
}

// GetAsString gets the string value for the given key (+ `true`),
// or empty string (+ `false`) if none was found
func (a Attributes) GetAsString(key string) (string, bool) {
Expand Down
1 change: 1 addition & 0 deletions pkg/types/predefined_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ func init() {
"two-semicolons": ";",
"cpp": "C++",
AttrVersionLabel: "version",
AttrTableCaption: "Table {counter:table-counter}. ",
}
}
8 changes: 0 additions & 8 deletions pkg/types/types_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ import (
"github.com/pkg/errors"
)

// NilSafe returns a new slice if the given elements is nil, otherwise it returns the given elements
func NilSafe(elements []interface{}) []interface{} {
if elements != nil {
return elements
}
return make([]interface{}, 0)
}

// Merge merge string elements together
func Merge(elements ...interface{}) []interface{} {
result := make([]interface{}, 0)
Expand Down

0 comments on commit a6273dd

Please sign in to comment.