Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(renderer): support "Q and A" labeled lists #280

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/parser/delimited_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1323,8 +1323,9 @@ end
verify(GinkgoT(), expectedResult, actualContent, parser.Entrypoint("DocumentBlock"))
})

It("with source and languages attributes", func() {
It("with title, source and languages attributes", func() {
actualContent := `[source,ruby]
.Source block title
----
require 'sinatra'

Expand All @@ -1336,6 +1337,7 @@ end
Attributes: types.ElementAttributes{
types.AttrKind: types.Source,
types.AttrLanguage: "ruby",
types.AttrTitle: "Source block title",
},
Kind: types.Source,
Elements: []interface{}{
Expand Down
55 changes: 55 additions & 0 deletions pkg/parser/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,61 @@ second term:: definition of the second term`
})
})

Context("q and a", func() {

It("q and a with title", func() {
actualContent := `.Q&A
[qanda]
What is libsciidoc?::
An implementation of the AsciiDoc processor in Golang.
What is the answer to the Ultimate Question?:: 42`

expectedResult := types.LabeledList{
Attributes: types.ElementAttributes{
types.AttrTitle: "Q&A",
types.AttrQandA: nil,
},
Items: []types.LabeledListItem{
{
Attributes: types.ElementAttributes{},
Level: 1,
Term: "What is libsciidoc?",
Elements: []interface{}{
types.Paragraph{
Attributes: types.ElementAttributes{},
Lines: []types.InlineElements{
{
types.StringElement{
Content: "An implementation of the AsciiDoc processor in Golang.",
},
},
},
},
},
},
{
Attributes: types.ElementAttributes{},
Level: 1,
Term: "What is the answer to the Ultimate Question?",
Elements: []interface{}{
types.Paragraph{
Attributes: types.ElementAttributes{},
Lines: []types.InlineElements{
{
types.StringElement{
Content: "42",
},
},
},
},
},
},
},
}
verify(GinkgoT(), expectedResult, actualContent, parser.Entrypoint("DocumentBlock"))
})
})

Context("unordered list", func() {
Context("ordered list item alone", func() {

Expand Down
50 changes: 28 additions & 22 deletions pkg/renderer/html5/delimited_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package html5
import (
"bytes"
"fmt"
"html"
"strings"
texttemplate "text/template"

Expand All @@ -24,48 +25,52 @@ var sidebarBlockTmpl texttemplate.Template
// initializes the templates
func init() {
fencedBlockTmpl = newTextTemplate("listing block", `{{ $ctx := .Context }}{{ with .Data }}<div {{ if .ID }}id="{{ .ID }}" {{ end }}class="listingblock">{{ if .Title }}
<div class="title">{{ .Title }}</div>{{ end }}
<div class="title">{{ escape .Title }}</div>{{ end }}
<div class="content">
<pre class="highlight"><code>{{ range $index, $element := .Elements }}{{ renderPlainString $ctx $element | printf "%s" }}{{ end }}</code></pre>
</div>
</div>{{ end }}`,
texttemplate.FuncMap{
"renderPlainString": renderPlainString,
"escape": html.EscapeString,
})

listingBlockTmpl = newTextTemplate("listing block", `{{ $ctx := .Context }}{{ with .Data }}<div {{ if .ID }}id="{{ .ID }}" {{ end }}class="listingblock">{{ if .Title }}
<div class="title">{{ .Title }}</div>{{ end }}
<div class="title">{{ escape .Title }}</div>{{ end }}
<div class="content">
<pre>{{ range $index, $element := .Elements }}{{ renderPlainString $ctx $element | printf "%s" }}{{ end }}</pre>
</div>
</div>{{ end }}`,
texttemplate.FuncMap{
"renderPlainString": renderPlainString,
"escape": html.EscapeString,
})

sourceBlockTmpl = newTextTemplate("source block",
`{{ $ctx := .Context }}{{ with .Data }}<div {{ if .ID }}id="{{ .ID }}" {{ end }}class="listingblock">{{ if .Title }}
<div class="title">{{ .Title }}</div>{{ end }}
<div class="title">{{ escape .Title }}</div>{{ end }}
<div class="content">
<pre class="highlight"><code{{ if .Language}} class="language-{{ .Language}}" data-lang="{{ .Language}}"{{ end }}>{{ range $index, $element := .Elements }}{{ renderPlainString $ctx $element | printf "%s" }}{{ end }}</code></pre>
</div>
</div>{{ end }}`,
texttemplate.FuncMap{
"renderPlainString": renderPlainString,
"escape": html.EscapeString,
})

exampleBlockTmpl = newTextTemplate("example block", `{{ $ctx := .Context }}{{ with .Data }}<div {{ if .ID }}id="{{ .ID }}" {{ end }}class="exampleblock">{{ if .Title }}
<div class="title">{{ .Title }}</div>{{ end }}
<div class="title">{{ escape .Title }}</div>{{ end }}
<div class="content">
{{ $elements := .Elements }}{{ renderElements $ctx $elements | printf "%s" }}
</div>
</div>{{ end }}`,
texttemplate.FuncMap{
"renderElements": renderElements,
"escape": html.EscapeString,
})

quoteBlockTmpl = newTextTemplate("quote block", `{{ $ctx := .Context }}{{ with .Data }}<div {{ if .ID }}id="{{ .ID }}" {{ end }}class="quoteblock">{{ if .Title }}
<div class="title">{{ .Title }}</div>{{ end }}
<div class="title">{{ escape .Title }}</div>{{ end }}
<blockquote>
{{ renderElements $ctx .Elements | printf "%s" }}
</blockquote>{{ if .Attribution.First }}
Expand All @@ -76,10 +81,11 @@ func init() {
</div>{{ end }}`,
texttemplate.FuncMap{
"renderElements": renderElements,
"escape": html.EscapeString,
})

verseBlockTmpl = newTextTemplate("verse block", `{{ $ctx := .Context }}{{ with .Data }}<div {{ if .ID }}id="{{ .ID }}" {{ end }}class="verseblock">{{ if .Title }}
<div class="title">{{ .Title }}</div>{{ end }}
<div class="title">{{ escape .Title }}</div>{{ end }}
<pre class="content">{{ renderElements $ctx .Elements | printf "%s" }}</pre>{{ if .Attribution.First }}
<div class="attribution">
&#8212; {{ .Attribution.First }}{{ if .Attribution.Second }}<br>
Expand All @@ -88,6 +94,7 @@ func init() {
</div>{{ end }}`,
texttemplate.FuncMap{
"renderElements": renderElements,
"escape": html.EscapeString,
})

admonitionBlockTmpl = newTextTemplate("admonition block", `{{ $ctx := .Context }}{{ with .Data }}<div {{ if .ID }}id="{{ .ID}}" {{ end }}class="admonitionblock {{ .Class }}">
Expand All @@ -97,39 +104,37 @@ func init() {
{{ if .IconClass }}<i class="fa icon-{{ .IconClass }}" title="{{ .IconTitle }}"></i>{{ else }}<div class="title">{{ .IconTitle }}</div>{{ end }}
</td>
<td class="content">
{{ if .Title }}<div class="title">{{ .Title }}</div>
{{ if .Title }}<div class="title">{{ escape .Title }}</div>
{{ end }}{{ renderElements $ctx .Elements | printf "%s" }}
</td>
</tr>
</table>
</div>{{ end }}`,
texttemplate.FuncMap{
"renderElements": renderElements,
"escape": html.EscapeString,
})

sidebarBlockTmpl = newTextTemplate("sidebar block", `{{ $ctx := .Context }}{{ with .Data }}<div {{ if .ID }}id="{{ .ID }}" {{ end }}class="sidebarblock">
<div class="content">{{ if .Title }}
<div class="title">{{ .Title }}</div>{{ end }}
<div class="title">{{ escape .Title }}</div>{{ end }}
{{ renderElements $ctx .Elements | printf "%s" }}
</div>
</div>{{ end }}`,
texttemplate.FuncMap{
"renderElements": renderElements,
"escape": html.EscapeString,
})
}

func renderDelimitedBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) {
log.Debugf("rendering delimited block of kind '%v'", b.Attributes[types.AttrKind])
result := bytes.NewBuffer(nil)
elements := discardTrailingBlankLines(b.Elements)
var id, title string

var id string
if i, ok := b.Attributes[types.AttrID].(string); ok { // TODO: replace with b.Attributes.GetAsString?
id = strings.TrimSpace(i)
}
if t, ok := b.Attributes[types.AttrTitle].(string); ok {
title = strings.TrimSpace(t)
}
var err error
kind := b.Kind
switch kind {
Expand All @@ -148,7 +153,7 @@ func renderDelimitedBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte
Elements []interface{}
}{
ID: id,
Title: title,
Title: getTitle(b.Attributes),
Elements: elements,
},
})
Expand All @@ -167,7 +172,7 @@ func renderDelimitedBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte
Elements []interface{}
}{
ID: id,
Title: title,
Title: getTitle(b.Attributes),
Elements: elements,
},
})
Expand All @@ -188,7 +193,7 @@ func renderDelimitedBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte
Elements []interface{}
}{
ID: id,
Title: title,
Title: getTitle(b.Attributes),
Language: language,
Elements: elements,
},
Expand All @@ -209,14 +214,15 @@ func renderDelimitedBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte
Class: getClass(k),
IconClass: getIconClass(ctx, k),
IconTitle: getIconTitle(k),
Title: title,
Title: getTitle(b.Attributes),
Elements: elements,
},
})
} else {
// default, example block
if title != "" {
title = fmt.Sprintf("Example %d. %s", ctx.GetAndIncrementExampleBlockCounter(), title)
var title string
if b.Attributes.Has(types.AttrTitle) {
title = fmt.Sprintf("Example %d. %s", ctx.GetAndIncrementExampleBlockCounter(), getTitle(b.Attributes))
}
err = exampleBlockTmpl.Execute(result, ContextualPipeline{
Context: ctx,
Expand Down Expand Up @@ -256,7 +262,7 @@ func renderDelimitedBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte
Elements []interface{}
}{
ID: id,
Title: title,
Title: getTitle(b.Attributes),
Attribution: attribution,
Elements: b.Elements,
},
Expand Down Expand Up @@ -294,7 +300,7 @@ func renderDelimitedBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte
Elements []interface{}
}{
ID: id,
Title: title,
Title: getTitle(b.Attributes),
Attribution: attribution,
Elements: elements,
},
Expand All @@ -310,7 +316,7 @@ func renderDelimitedBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte
Elements []interface{}
}{
ID: id,
Title: title,
Title: getTitle(b.Attributes),
Elements: elements,
},
})
Expand Down
4 changes: 3 additions & 1 deletion pkg/renderer/html5/delimited_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ end</code></pre>
verify(GinkgoT(), expectedResult, actualContent)
})

It("with source and languages attributes", func() {
It("with title, source and languages attributes", func() {
actualContent := `[source,ruby]
.Source block title
----
require 'sinatra'

Expand All @@ -102,6 +103,7 @@ get '/hi' do
end
----`
expectedResult := `<div class="listingblock">
<div class="title">Source block title</div>
<div class="content">
<pre class="highlight"><code class="language-ruby" data-lang="ruby">require 'sinatra'

Expand Down
8 changes: 6 additions & 2 deletions pkg/renderer/html5/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package html5

import (
"bytes"
"html"
htmltemplate "html/template"
"io"
texttemplate "text/template"
Expand All @@ -23,7 +24,7 @@ func init() {
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">{{ if .Generator }}
<meta name="generator" content="{{ .Generator }}">{{ end }}
<title>{{ .Title }}</title>
<title>{{ escape .Title }}</title>
</head>
<body class="article">
<div id="header">
Expand All @@ -40,7 +41,10 @@ Last updated {{ .LastUpdated }}
</div>
</div>
</body>
</html>`)
</html>`,
texttemplate.FuncMap{
"escape": html.EscapeString,
})

}

Expand Down
12 changes: 6 additions & 6 deletions pkg/renderer/html5/document_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ var documentAuthorDetailsTmpl texttemplate.Template

func init() {
documentDetailsTmpl = newTextTemplate("document details", `<div class="details">{{ if .Authors }}
{{.Authors}}{{ end }}{{ if .RevNumber }}
<span id="revnumber">version {{.RevNumber}},</span>{{ end }}{{ if .RevDate }}
<span id="revdate">{{.RevDate}}</span>{{ end }}{{ if .RevRemark }}
<br><span id="revremark">{{.RevRemark}}</span>{{ end }}
{{ .Authors }}{{ end }}{{ if .RevNumber }}
<span id="revnumber">version {{ .RevNumber }},</span>{{ end }}{{ if .RevDate }}
<span id="revdate">{{ .RevDate }}</span>{{ end }}{{ if .RevRemark }}
<br><span id="revremark">{{ .RevRemark }}</span>{{ end }}
</div>`)

documentAuthorDetailsTmpl = newTextTemplate("author details", `{{ if .Name }}<span id="author{{.Index}}" class="author">{{.Name}}</span><br>{{ end }}{{ if .Email }}
<span id="email{{.Index}}" class="email"><a href="mailto:{{.Email}}">{{.Email}}</a></span><br>{{ end }}`)
documentAuthorDetailsTmpl = newTextTemplate("author details", `{{ if .Name }}<span id="author{{ .Index }}" class="author">{{ .Name }}</span><br>{{ end }}{{ if .Email }}
<span id="email{{ .Index }}" class="email"><a href="mailto:{{ .Email }}">{{ .Email }}</a></span><br>{{ end }}`)
}

func renderDocumentDetails(ctx *renderer.Context) (*htmltemplate.HTML, error) {
Expand Down
17 changes: 12 additions & 5 deletions pkg/renderer/html5/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package html5
import (
"bytes"
"fmt"
"html"
"net/url"
"path/filepath"
texttemplate "text/template"
Expand All @@ -22,17 +23,23 @@ func init() {
<div class="content">
{{ if ne .Href "" }}<a class="image" href="{{ .Href }}">{{ end }}<img src="{{ .Path }}" alt="{{ .Alt }}"{{ if .Width }} width="{{ .Width }}"{{ end }}{{ if .Height }} height="{{ .Height }}"{{ end }}>{{ if ne .Href "" }}</a>{{ end }}
</div>{{ if .Title }}
<div class="title">{{ .Title }}</div>
<div class="title">{{ escape .Title }}</div>
{{ else }}
{{ end }}</div>`)
inlineImageTmpl = newTextTemplate("inline image", `<span class="image{{ if .Role }} {{ .Role }}{{ end }}"><img src="{{ .Path }}" alt="{{ .Alt }}"{{ if .Width }} width="{{ .Width }}"{{ end }}{{ if .Height }} height="{{ .Height }}"{{ end }}{{ if .Title }} title="{{ .Title }}"{{ end }}></span>`)
{{ end }}</div>`,
texttemplate.FuncMap{
"escape": html.EscapeString,
})
inlineImageTmpl = newTextTemplate("inline image", `<span class="image{{ if .Role }} {{ .Role }}{{ end }}"><img src="{{ .Path }}" alt="{{ .Alt }}"{{ if .Width }} width="{{ .Width }}"{{ end }}{{ if .Height }} height="{{ .Height }}"{{ end }}{{ if .Title }} title="{{ escape .Title }}"{{ end }}></span>`,
texttemplate.FuncMap{
"escape": html.EscapeString,
})
}

func renderImageBlock(ctx *renderer.Context, img types.ImageBlock) ([]byte, error) {
result := bytes.NewBuffer(nil)
title := ""
if t := img.Attributes.GetAsString(types.AttrTitle); t != "" {
title = fmt.Sprintf("Figure %d. %s", ctx.GetAndIncrementImageCounter(), t)
title = fmt.Sprintf("Figure %d. %s", ctx.GetAndIncrementImageCounter(), html.EscapeString(t))
}
err := blockImageTmpl.Execute(result, struct {
ID string
Expand Down Expand Up @@ -72,7 +79,7 @@ func renderInlineImage(ctx *renderer.Context, img types.InlineImage) ([]byte, er
Height string
Path string
}{
Title: img.Attributes.GetAsString(types.AttrTitle),
Title: getTitle(img.Attributes),
Role: img.Attributes.GetAsString(types.AttrRole),
Alt: img.Attributes.GetAsString(types.AttrImageAlt),
Width: img.Attributes.GetAsString(types.AttrImageWidth),
Expand Down
Loading