Skip to content

Commit

Permalink
feature(renderer): version-label not honored
Browse files Browse the repository at this point in the history
This supports the version-label be changed, or even reset
for document attributes.

To support resetting this label, the initial value is located
in the Predefined attributes, and a reset of the attribute is
stored as a nil attribute value instead of deleting it from the
attributes.  This allows us to distinguish a reset as an override.

This approach will be used for other predefined text values in
follow ups.

Fixes #710
  • Loading branch information
gdamore committed Jul 11, 2020
1 parent e756614 commit d054193
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 26 deletions.
4 changes: 3 additions & 1 deletion pkg/parser/document_attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,9 @@ a paragraph written by {author}.`
a paragraph written by {author}.`
expected := types.Document{
Attributes: types.Attributes{
"author": "Xavier",
"author": "Xavier",
"author1": nil,
"author2": nil,
},
Elements: []interface{}{
types.Paragraph{
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/document_processing_apply_substitutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func applyAttributeSubstitutionsOnElement(element interface{}, attrs types.Attri
attrs.Set(e.Name, e.Value)
return e, false, nil
case types.AttributeReset:
attrs.Delete(e.Name)
attrs.Set(e.Name, nil) // This allows us to test for a reset vs. undefined.
return e, false, nil
case types.AttributeSubstitution:
if value, ok := attrs.GetAsString(e.Name); ok {
Expand Down
2 changes: 2 additions & 0 deletions pkg/parser/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ a link to {scheme}://{path} and https://foo.baz`
expected := types.Document{
Attributes: types.Attributes{
"scheme": "https",
"path": nil,
},
Elements: []interface{}{
types.Paragraph{
Expand Down Expand Up @@ -1193,6 +1194,7 @@ a link to {scheme}:{path}[] and https://foo.baz`
expected := types.Document{
Attributes: types.Attributes{
"scheme": "link",
"path": nil,
},
Elements: []interface{}{
types.Paragraph{
Expand Down
5 changes: 5 additions & 0 deletions pkg/renderer/sgml/document_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sgml

import (
"bytes"
"fmt"
"strconv"
"strings"

Expand All @@ -17,20 +18,24 @@ func (r *sgmlRenderer) renderDocumentDetails(ctx *renderer.Context) (*sanitized,
return nil, errors.Wrap(err, "error while rendering the document details")
}
documentDetailsBuff := &bytes.Buffer{}
revLabel, _ := ctx.Attributes.GetAsString("version-label")
revNumber, _ := ctx.Attributes.GetAsString("revnumber")
revDate, _ := ctx.Attributes.GetAsString("revdate")
revRemark, _ := ctx.Attributes.GetAsString("revremark")
err = r.documentDetails.Execute(documentDetailsBuff, struct {
Authors sanitized
RevLabel string
RevNumber string
RevDate string
RevRemark string
}{
Authors: *authors,
RevLabel: revLabel,
RevNumber: revNumber,
RevDate: revDate,
RevRemark: revRemark,
})
fmt.Printf("DEBUG: REVISION: %q\n", revLabel)
if err != nil {
return nil, errors.Wrap(err, "error while rendering the document details")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/renderer/sgml/html5/document_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package html5
const (
documentDetailsTmpl = `<div class="details">{{ if .Authors }}
{{ .Authors }}{{ end }}{{ if .RevNumber }}
<span id="revnumber">version {{ .RevNumber }}{{ if .RevDate }},{{ end }}</span>{{ end }}{{ if .RevDate }}
<span id="revnumber">{{ if .RevLabel }}{{ .RevLabel }} {{ end }}{{ .RevNumber }}{{ if .RevDate }},{{ end }}</span>{{ end }}{{ if .RevDate }}
<span id="revdate">{{ .RevDate }}</span>{{ end }}{{ if .RevRemark }}
<br><span id="revremark">{{ .RevRemark }}</span>{{ end }}
</div>
Expand Down
2 changes: 1 addition & 1 deletion pkg/renderer/sgml/xhtml5/document_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package xhtml5
const (
documentDetailsTmpl = `<div class="details">{{ if .Authors }}
{{ .Authors }}{{ end }}{{ if .RevNumber }}
<span id="revnumber">version {{ .RevNumber }}{{ if .RevDate }},{{ end }}</span>{{ end }}{{ if .RevDate }}
<span id="revnumber">{{ if .RevLabel }}{{ .RevLabel }} {{ end }}{{ .RevNumber }}{{ if .RevDate }},{{ end }}</span>{{ end }}{{ if .RevDate }}
<span id="revdate">{{ .RevDate }}</span>{{ end }}{{ if .RevRemark }}
<br/><span id="revremark">{{ .RevRemark }}</span>{{ end }}
</div>
Expand Down
87 changes: 86 additions & 1 deletion pkg/renderer/sgml/xhtml5/document_details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,92 @@ a paragraph`
)).To(MatchHTMLTemplate(expected, now))
})

It("with author and version label reset", func() {
source := `= Document Title
Joe Blow <[email protected]>
1.5, May 21, 1999
:version-label!:
a paragraph`
expected := `<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="generator" content="libasciidoc"/>
<meta name="author" content="Joe Blow"/>
<title>Document Title</title>
</head>
<body class="article">
<div id="header">
<h1>Document Title</h1>
<div class="details">
<span id="author" class="author">Joe Blow</span><br/>
<span id="email" class="email"><a href="mailto:[email protected]">[email protected]</a></span><br/>
<span id="revnumber">1.5,</span>
<span id="revdate">May 21, 1999</span>
</div>
</div>
<div id="content">
<div class="paragraph">
<p>a paragraph</p>
</div>
</div>
</body>
</html>`
Expect(RenderXHTML(source,
configuration.WithHeaderFooter(true),
configuration.WithLastUpdated(now),
configuration.WithAttributes(map[string]string{
types.AttrNoFooter: "",
}),
)).To(MatchHTMLTemplate(expected, now))
})

It("with author and custom version label only", func() {
source := `= Document Title
Joe Blow <[email protected]>
1.0, May 21, 1999
:version-label: Edition
a paragraph`
expected := `<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="generator" content="libasciidoc"/>
<meta name="author" content="Joe Blow"/>
<title>Document Title</title>
</head>
<body class="article">
<div id="header">
<h1>Document Title</h1>
<div class="details">
<span id="author" class="author">Joe Blow</span><br/>
<span id="email" class="email"><a href="mailto:[email protected]">[email protected]</a></span><br/>
<span id="revnumber">Edition 1.0,</span>
<span id="revdate">May 21, 1999</span>
</div>
</div>
<div id="content">
<div class="paragraph">
<p>a paragraph</p>
</div>
</div>
</body>
</html>`
Expect(RenderXHTML(source,
configuration.WithHeaderFooter(true),
configuration.WithLastUpdated(now),
configuration.WithAttributes(map[string]string{
types.AttrNoFooter: "",
}),
)).To(MatchHTMLTemplate(expected, now))
})

It("without header and with footer", func() {
source := `= Document Title
Expand Down Expand Up @@ -238,6 +324,5 @@ a paragraph`
}),
)).To(MatchHTMLTemplate(expected, now))
})

})
})
24 changes: 16 additions & 8 deletions pkg/types/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ const (
AttrPositional2 = "@2"
// AttrPositional3 positional parameter 3
AttrPositional3 = "@3"
// AttrVersionLabel labels the version number in the document
AttrVersionLabel = "version-label"
)

// NewElementID initializes a new attribute map with a single entry for the ID using the given value
Expand Down Expand Up @@ -338,32 +340,38 @@ func (a Attributes) NilSafeSet(key string, value interface{}) {
// 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) {
// check in predefined attributes
if value, found := Predefined[key]; found {
return value, true
}
if value, found := a[key]; found {
if value == nil {
return "", false // nil here means attribute was reset
}
if value, ok := value.(string); ok {
return value, true
} else if v, ok := a[key]; ok {
return fmt.Sprintf("%v", v), true
}
}
// check in predefined attributes
if value, found := Predefined[key]; found {
return value, true
}
return "", false
}

// GetAsStringWithDefault gets the string value for the given key,
// or returns the given default value
func (a Attributes) GetAsStringWithDefault(key, defaultValue string) string {
// check in predefined attributes
if value, found := Predefined[key]; found {
return value
}
if value, found := a[key]; found {
if value == nil {
return "" // nil present means attribute was reset
}
if value, ok := value.(string); ok {
return value
}
}
// check in predefined attributes
if value, found := Predefined[key]; found {
return value
}
return defaultValue
}

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 @@ -34,5 +34,6 @@ func init() {
"two-colons": "::",
"two-semicolons": ";",
"cpp": "C++",
AttrVersionLabel: "version",
}
}
16 changes: 3 additions & 13 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,20 +272,10 @@ type AttributeDeclaration struct {

// NewAttributeDeclaration initializes a new AttributeDeclaration with the given name and optional value
func NewAttributeDeclaration(name string, value interface{}) (AttributeDeclaration, error) {
var attrName, attrValue string
attrName = Apply(name,
func(s string) string {
return strings.TrimSpace(s)
})
if value, ok := value.(string); ok {
attrValue = Apply(value,
func(s string) string {
return strings.TrimSpace(s)
})
}
log.Debugf("initialized a new AttributeDeclaration: '%s' -> '%s'", attrName, attrValue)
attrValue, _ := value.(string)
log.Debugf("initialized a new AttributeDeclaration: '%s' -> '%s'", name, attrValue)
return AttributeDeclaration{
Name: attrName,
Name: name,
Value: attrValue,
}, nil
}
Expand Down

0 comments on commit d054193

Please sign in to comment.