Skip to content

Commit

Permalink
feat(renderer): role not honored on h1 titles (#583) (#584)
Browse files Browse the repository at this point in the history
This adds support for setting the role on a level 0 title,
which will cause the class for the document body to be set.

Fixes #583
  • Loading branch information
gdamore authored Jun 6, 2020
1 parent f15b786 commit 79a43b3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/renderer/html5/html5.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
<link type="text/css" rel="stylesheet" href="{{ .CSS }}">{{ end }}
<title>{{ escape .Title }}</title>
</head>
<body class="{{ .Doctype }}">{{ if .IncludeHeader }}
<body class="{{ .Doctype }}{{ if .Role }} {{ .Role }}{{ end }}">{{ if .IncludeHeader }}
{{ .Header }}{{ end }}
<div id="content">
{{ .Content }}
Expand Down Expand Up @@ -87,6 +87,7 @@ func Render(ctx renderer.Context, doc types.Document, output io.Writer) (types.M
Title string
Authors string
Header string
Role string
Content htmltemplate.HTML
RevNumber string
LastUpdated string
Expand All @@ -99,6 +100,7 @@ func Render(ctx renderer.Context, doc types.Document, output io.Writer) (types.M
Title: string(renderedTitle),
Authors: renderAuthors(doc.Attributes.GetAuthors()),
Header: string(renderedHeader),
Role: documentRole(doc),
Content: htmltemplate.HTML(string(renderedContent)), //nolint: gosec
RevNumber: doc.Attributes.GetAsStringWithDefault("revnumber", ""),
LastUpdated: ctx.Config.LastUpdated.Format(configuration.LastUpdatedFormat),
Expand Down Expand Up @@ -192,6 +194,13 @@ func splitAndRenderForManpage(ctx renderer.Context, doc types.Document) ([]byte,
return []byte{}, result.Bytes(), nil
}

func documentRole(doc types.Document) string {
if header, exists := doc.Header(); exists {
return header.Attributes.GetAsString(types.AttrRole)
}
return ""
}

func renderAuthors(authors []types.DocumentAuthor) string {
authorStrs := make([]string, len(authors))
for i, author := range authors {
Expand Down
34 changes: 34 additions & 0 deletions pkg/renderer/html5/html5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,40 @@ var _ = Describe("document header", func() {
</div>
<div id="content">
</div>
<div id="footer">
<div id="footer-text">
Last updated {{.LastUpdated}}
</div>
</div>
</body>
</html>`
now := time.Now()
Expect(RenderHTML(source, configuration.WithHeaderFooter(true),
configuration.WithCSS("/path/to/style.css"),
configuration.WithLastUpdated(now))).
To(MatchHTMLTemplate(expected, now))
})

It("header with role", func() {
source := `[.my_role]
= My Title`
expected := `<!DOCTYPE html>
<html 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">
<link type="text/css" rel="stylesheet" href="/path/to/style.css">
<title>My Title</title>
</head>
<body class="article my_role">
<div id="header">
<h1>My Title</h1>
</div>
<div id="content">
</div>
<div id="footer">
<div id="footer-text">
Expand Down

0 comments on commit 79a43b3

Please sign in to comment.