Skip to content

Commit

Permalink
feat(renderer): include authors in 'meta' tag (#544)
Browse files Browse the repository at this point in the history
Include authors (semicolon-separated list) in a
`<meta name="author">` tag in the document head

Fixes #543

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Apr 19, 2020
1 parent 7d51b9e commit 6bdca28
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion libasciidoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ a paragraph with _italic content_`
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<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">
Expand Down
20 changes: 11 additions & 9 deletions pkg/renderer/html5/document_details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ v1.0, March 22, 2020: Containment
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<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="Xavier">
<title>Document Title</title>
</head>
<body class="article">
Expand Down Expand Up @@ -61,16 +62,17 @@ Last updated {{.LastUpdated}}

It("header with 2 authors and no revision", func() {
source := `= Document Title
John Foo Doe <[email protected]>; Lazarus het_Draeke <[email protected]>`
John Foo Doe <[email protected]>; Jane Doe <[email protected]>`
// top-level section is not rendered per-say,
// but the section will be used to set the HTML page's <title> element
expected := `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<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="John Foo Doe; Jane Doe">
<title>Document Title</title>
</head>
<body class="article">
Expand All @@ -79,8 +81,8 @@ John Foo Doe <[email protected]>; Lazarus het_Draeke <[email protected]>
<div class="details">
<span id="author" class="author">John Foo Doe</span><br>
<span id="email" class="email"><a href="mailto:[email protected]">[email protected]</a></span><br>
<span id="author2" class="author">Lazarus het Draeke</span><br>
<span id="email2" class="email"><a href="mailto:[email protected]">[email protected]</a></span><br>
<span id="author2" class="author">Jane Doe</span><br>
<span id="email2" class="email"><a href="mailto:[email protected]">[email protected]</a></span><br>
</div>
</div>
<div id="content">
Expand Down Expand Up @@ -111,7 +113,7 @@ a paragraph`
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<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">
<title>Document Title</title>
Expand Down Expand Up @@ -147,7 +149,7 @@ a paragraph`
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<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">
<title>Document Title</title>
Expand Down Expand Up @@ -180,7 +182,7 @@ a paragraph`
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<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">
<title>Document Title</title>
Expand Down Expand Up @@ -215,7 +217,7 @@ a paragraph`
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<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">
<title>Document Title</title>
Expand Down
16 changes: 14 additions & 2 deletions pkg/renderer/html5/html5.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
htmltemplate "html/template"
"io"
"strings"
texttemplate "text/template"

"github.com/bytesparadise/libasciidoc/pkg/configuration"
Expand All @@ -24,10 +25,11 @@ func init() {
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">{{ if .Generator }}
<meta name="generator" content="{{ .Generator }}">{{ end }}{{ if .CSS}}
<link type="text/css" rel="stylesheet" href="{{ .CSS }}">{{ end }}
<link type="text/css" rel="stylesheet" href="{{ .CSS }}">{{ end }}{{ if .Authors }}
<meta name="author" content="{{ .Authors }}">{{ end }}
<title>{{ escape .Title }}</title>
</head>
<body class="{{ .Doctype }}">{{ if .IncludeHeader }}
Expand Down Expand Up @@ -84,6 +86,7 @@ func Render(ctx renderer.Context, doc types.Document, output io.Writer) (types.M
Generator string
Doctype string
Title string
Authors string
Header string
Content htmltemplate.HTML
RevNumber string
Expand All @@ -95,6 +98,7 @@ func Render(ctx renderer.Context, doc types.Document, output io.Writer) (types.M
Generator: "libasciidoc", // TODO: externalize this value and include the lib version ?
Doctype: doc.Attributes.GetAsStringWithDefault(types.AttrDocType, "article"),
Title: string(renderedTitle),
Authors: renderAuthors(doc.Attributes.GetAuthors()),
Header: string(renderedHeader),
Content: htmltemplate.HTML(string(renderedContent)), //nolint: gosec
RevNumber: revNumber,
Expand Down Expand Up @@ -189,6 +193,14 @@ func splitAndRenderForManpage(ctx renderer.Context, doc types.Document) ([]byte,
return []byte{}, result.Bytes(), nil
}

func renderAuthors(authors []types.DocumentAuthor) string {
authorStrs := make([]string, len(authors))
for i, author := range authors {
authorStrs[i] = author.FullName
}
return strings.Join(authorStrs, "; ")
}

func renderDocumentTitle(ctx renderer.Context, doc types.Document) ([]byte, error) {
if header, exists := doc.Header(); exists {
title, err := renderPlainText(ctx, header.Title)
Expand Down
5 changes: 3 additions & 2 deletions pkg/renderer/html5/html5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var _ = Describe("document header", func() {
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<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">
Expand Down Expand Up @@ -168,10 +168,11 @@ Free use of this software is granted under the terms of the MIT License.`
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<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">
<meta name="author" content="Andrew Stanton">
<title>eve(1)</title>
</head>
<body class="manpage">
Expand Down
8 changes: 8 additions & 0 deletions pkg/types/document_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ func (a DocumentAttributes) GetAsStringWithDefault(key, defaultValue string) str
return defaultValue
}

// GetAuthors returns the authors or empty slice if none was set in the document
func (a DocumentAttributes) GetAuthors() []DocumentAuthor {
if authors, ok := a[AttrAuthors].([]DocumentAuthor); ok {
return authors
}
return []DocumentAuthor{}
}

// DocumentAttributesWithOverrides the document attributes with some overrides provided by the CLI (for example)
type DocumentAttributesWithOverrides struct {
Content map[string]interface{}
Expand Down

0 comments on commit 6bdca28

Please sign in to comment.