Skip to content

Commit

Permalink
refactor(renderer): remove context.Context in renderer context type (b…
Browse files Browse the repository at this point in the history
…ytesparadise#480)

also, stop using pointer to context during rendering

Fixes bytesparadise#479

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Jan 19, 2020
1 parent c609364 commit cdccf6f
Show file tree
Hide file tree
Showing 38 changed files with 92 additions and 127 deletions.
3 changes: 1 addition & 2 deletions cmd/libasciidoc/root_cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"context"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -49,7 +48,7 @@ func NewRootCmd() *cobra.Command {
defer close()
path, _ := filepath.Abs(source)
log.Debugf("Starting to process file %v", path)
_, err := libasciidoc.ConvertFileToHTML(context.Background(), source, out, renderer.IncludeHeaderFooter(!noHeaderFooter))
_, err := libasciidoc.ConvertFileToHTML(source, out, renderer.IncludeHeaderFooter(!noHeaderFooter))
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions libasciidoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package libasciidoc

import (
"context"
"io"
"os"
"time"
Expand All @@ -28,7 +27,7 @@ var (
// ConvertFileToHTML converts the content of the given filename into an HTML document.
// The conversion result is written in the given writer `output`, whereas the document metadata (title, etc.) (or an error if a problem occurred) is returned
// as the result of the function call.
func ConvertFileToHTML(ctx context.Context, filename string, output io.Writer, options ...renderer.Option) (map[string]interface{}, error) {
func ConvertFileToHTML(filename string, output io.Writer, options ...renderer.Option) (map[string]interface{}, error) {
file, err := os.Open(filename)
if err != nil {
return nil, errors.Wrapf(err, "error opening %s", filename)
Expand All @@ -40,12 +39,12 @@ func ConvertFileToHTML(ctx context.Context, filename string, output io.Writer, o
return nil, errors.Wrapf(err, "error opening %s", filename)
}
options = append(options, renderer.LastUpdated(stat.ModTime()))
return ConvertToHTML(ctx, filename, file, output, options...)
return ConvertToHTML(filename, file, output, options...)
}

// ConvertToHTML converts the content of the given reader `r` into a full HTML document, written in the given writer `output`.
// Returns an error if a problem occurred
func ConvertToHTML(ctx context.Context, filename string, r io.Reader, output io.Writer, options ...renderer.Option) (map[string]interface{}, error) {
func ConvertToHTML(filename string, r io.Reader, output io.Writer, options ...renderer.Option) (map[string]interface{}, error) {
start := time.Now()
defer func() {
duration := time.Since(start)
Expand All @@ -56,7 +55,7 @@ func ConvertToHTML(ctx context.Context, filename string, r io.Reader, output io.
if err != nil {
return nil, errors.Wrapf(err, "error while parsing the document")
}
rendererCtx := renderer.Wrap(ctx, doc, options...)
rendererCtx := renderer.NewContext(doc, options...)
// insert tables of contents, preamble and process file inclusions
err = renderer.Prerender(rendererCtx)
if err != nil {
Expand Down
36 changes: 4 additions & 32 deletions pkg/renderer/context.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package renderer

import (
"context"
"errors"
"io"
"time"

"github.com/bytesparadise/libasciidoc/pkg/types"
log "github.com/sirupsen/logrus"
Expand All @@ -18,22 +16,20 @@ type MacroTemplate interface {
// Context is a custom implementation of the standard golang context.Context interface,
// which carries the types.Document which is being processed
type Context struct {
context context.Context
Document types.Document
options map[string]interface{}
macros map[string]MacroTemplate
}

// Wrap wraps the given `ctx` context into a new context which will contain the given `document` document.
func Wrap(ctx context.Context, document types.Document, options ...Option) *Context {
result := &Context{
context: ctx,
// NewContext returns a new rendering context for the given document.
func NewContext(document types.Document, options ...Option) Context {
result := Context{
Document: document,
options: make(map[string]interface{}),
macros: make(map[string]MacroTemplate),
}
for _, option := range options {
option(result)
option(&result)
}
return result
}
Expand Down Expand Up @@ -162,27 +158,3 @@ func (ctx *Context) MacroTemplate(name string) (MacroTemplate, error) {
}
return nil, errors.New("unknown user macro: " + name)
}

// -----------------------
// context.Context methods
// -----------------------

// Deadline wrapper implementation of context.Context.Deadline()
func (ctx *Context) Deadline() (deadline time.Time, ok bool) {
return ctx.context.Deadline()
}

// Done wrapper implementation of context.Context.Done()
func (ctx *Context) Done() <-chan struct{} {
return ctx.context.Done()
}

// Err wrapper implementation of context.Context.Err()
func (ctx *Context) Err() error {
return ctx.context.Err()
}

// Value wrapper implementation of context.Context.Value(interface{})
func (ctx *Context) Value(key interface{}) interface{} {
return ctx.context.Value(key)
}
3 changes: 2 additions & 1 deletion pkg/renderer/document_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// ProcessDocumentHeader includes the authors and revision in the document attributes
func ProcessDocumentHeader(ctx *Context) {
func ProcessDocumentHeader(ctx Context) Context {
if authors, ok := ctx.Document.Authors(); ok {
for i, author := range authors {
var part1, part2, part3, email string
Expand Down Expand Up @@ -70,6 +70,7 @@ func ProcessDocumentHeader(ctx *Context) {
ctx.Document.Attributes.AddNonEmpty("revdate", revision.Revdate)
ctx.Document.Attributes.AddNonEmpty("revremark", revision.Revremark)
}
return ctx
}

func key(k string, i int) string {
Expand Down
5 changes: 2 additions & 3 deletions pkg/renderer/html5/article_adoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package html5_test
import (
"bufio"
"bytes"
"context"
"os"

"github.com/bytesparadise/libasciidoc/pkg/parser"
Expand All @@ -25,8 +24,8 @@ var _ = Describe("article.adoc", func() {
Expect(err).ToNot(HaveOccurred())
GinkgoT().Logf("actual document: `%s`", spew.Sdump(doc))
buff := bytes.NewBuffer(nil)
rendererCtx := renderer.Wrap(context.Background(), doc)
_, err = html5.Render(rendererCtx, buff)
ctx := renderer.NewContext(doc)
_, err = html5.Render(ctx, buff)
Expect(err).ToNot(HaveOccurred())
})
})
2 changes: 1 addition & 1 deletion pkg/renderer/html5/blank_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
log "github.com/sirupsen/logrus"
)

func renderBlankLine(ctx *renderer.Context, l types.BlankLine) ([]byte, error) { //nolint:unparam
func renderBlankLine(ctx renderer.Context, l types.BlankLine) ([]byte, error) { //nolint:unparam
if ctx.IncludeBlankLine() {
log.Debug("rendering blankline")
return []byte("\n\n"), nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/renderer/html5/contextual_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
// ContextualPipeline as structure that carries the renderer context along with
// the pipeline data to process in a template or in a nested template
type ContextualPipeline struct {
Context *renderer.Context
Context renderer.Context
// The actual pipeline
Data interface{}
}
4 changes: 2 additions & 2 deletions pkg/renderer/html5/cross_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func init() {
externalCrossReferenceTmpl = newTextTemplate("external cross reference", `<a href="{{ .Href }}">{{ .Label }}</a>`)
}

func renderInternalCrossReference(ctx *renderer.Context, xref types.InternalCrossReference) ([]byte, error) {
func renderInternalCrossReference(ctx renderer.Context, xref types.InternalCrossReference) ([]byte, error) {
log.Debugf("rendering cross reference with ID: %s", xref.ID)
result := bytes.NewBuffer(nil)
var label string
Expand Down Expand Up @@ -52,7 +52,7 @@ func renderInternalCrossReference(ctx *renderer.Context, xref types.InternalCros
return result.Bytes(), nil
}

func renderExternalCrossReference(ctx *renderer.Context, xref types.ExternalCrossReference) ([]byte, error) {
func renderExternalCrossReference(ctx renderer.Context, xref types.ExternalCrossReference) ([]byte, error) {
log.Debugf("rendering cross reference with ID: %s", xref.Location)
result := bytes.NewBuffer(nil)
label, err := renderInlineElements(ctx, xref.Label)
Expand Down
20 changes: 10 additions & 10 deletions pkg/renderer/html5/delimited_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func init() {
})
}

func renderDelimitedBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) {
func renderDelimitedBlock(ctx renderer.Context, b types.DelimitedBlock) ([]byte, error) {
log.Debugf("rendering delimited block of kind '%v'", b.Attributes[types.AttrKind])
var err error
kind := b.Kind
Expand All @@ -156,7 +156,7 @@ func renderDelimitedBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte
}
}

func renderFencedBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) {
func renderFencedBlock(ctx renderer.Context, b types.DelimitedBlock) ([]byte, error) {
previouslyWithin := ctx.SetWithinDelimitedBlock(true)
previouslyInclude := ctx.SetIncludeBlankLine(true)
defer func() {
Expand All @@ -179,7 +179,7 @@ func renderFencedBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, e
return result.Bytes(), err
}

func renderListingBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) {
func renderListingBlock(ctx renderer.Context, b types.DelimitedBlock) ([]byte, error) {
previouslyWithin := ctx.SetWithinDelimitedBlock(true)
previouslyInclude := ctx.SetIncludeBlankLine(true)
defer func() {
Expand All @@ -202,7 +202,7 @@ func renderListingBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte,
return result.Bytes(), err
}

func renderSourceBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) {
func renderSourceBlock(ctx renderer.Context, b types.DelimitedBlock) ([]byte, error) {
previouslyWithin := ctx.SetWithinDelimitedBlock(true)
previouslyInclude := ctx.SetIncludeBlankLine(true)
defer func() {
Expand All @@ -228,7 +228,7 @@ func renderSourceBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, e
return result.Bytes(), err
}

func renderExampleBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) {
func renderExampleBlock(ctx renderer.Context, b types.DelimitedBlock) ([]byte, error) {
result := bytes.NewBuffer(nil)
if k, ok := b.Attributes[types.AttrAdmonitionKind].(types.AdmonitionKind); ok {
err := admonitionBlockTmpl.Execute(result, ContextualPipeline{
Expand Down Expand Up @@ -271,7 +271,7 @@ func renderExampleBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte,
return result.Bytes(), err
}

func renderQuoteBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) {
func renderQuoteBlock(ctx renderer.Context, b types.DelimitedBlock) ([]byte, error) {
result := bytes.NewBuffer(nil)
err := quoteBlockTmpl.Execute(result, ContextualPipeline{
Context: ctx,
Expand All @@ -290,7 +290,7 @@ func renderQuoteBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, er
return result.Bytes(), err
}

func renderVerseBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) {
func renderVerseBlock(ctx renderer.Context, b types.DelimitedBlock) ([]byte, error) {
result := bytes.NewBuffer(nil)
err := verseBlockTmpl.Execute(result, ContextualPipeline{
Context: ctx,
Expand All @@ -309,7 +309,7 @@ func renderVerseBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, er
return result.Bytes(), err
}

func renderVerseBlockElement(ctx *renderer.Context, element interface{}) ([]byte, error) {
func renderVerseBlockElement(ctx renderer.Context, element interface{}) ([]byte, error) {
before := ctx.SetIncludeBlankLine(true)
defer ctx.SetIncludeBlankLine(before)
switch e := element.(type) {
Expand All @@ -322,7 +322,7 @@ func renderVerseBlockElement(ctx *renderer.Context, element interface{}) ([]byte
}
}

func renderVerseBlockParagraph(ctx *renderer.Context, p types.Paragraph) ([]byte, error) {
func renderVerseBlockParagraph(ctx renderer.Context, p types.Paragraph) ([]byte, error) {
log.Debugf("rendering paragraph with %d line(s) within a delimited block or a list", len(p.Lines))
result := bytes.NewBuffer(nil)
err := verseBlockParagraphTmpl.Execute(result, ContextualPipeline{
Expand All @@ -336,7 +336,7 @@ func renderVerseBlockParagraph(ctx *renderer.Context, p types.Paragraph) ([]byte
return result.Bytes(), err
}

func renderSidebarBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) {
func renderSidebarBlock(ctx renderer.Context, b types.DelimitedBlock) ([]byte, error) {
result := bytes.NewBuffer(nil)
err := sidebarBlockTmpl.Execute(result, ContextualPipeline{
Context: ctx,
Expand Down
8 changes: 4 additions & 4 deletions pkg/renderer/html5/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Last updated {{ .LastUpdated }}
}

// renderDocument renders the whole document, including the HEAD and BODY containers if needed
func renderDocument(ctx *renderer.Context, output io.Writer) (map[string]interface{}, error) {
func renderDocument(ctx renderer.Context, output io.Writer) (map[string]interface{}, error) {
renderedTitle, err := renderDocumentTitle(ctx)
if err != nil {
return nil, errors.Wrapf(err, "unable to render full document")
Expand Down Expand Up @@ -113,7 +113,7 @@ func renderDocument(ctx *renderer.Context, output io.Writer) (map[string]interfa

// renderDocumentElements renders all document elements, including the footnotes,
// but not the HEAD and BODY containers
func renderDocumentElements(ctx *renderer.Context) ([]byte, error) {
func renderDocumentElements(ctx renderer.Context) ([]byte, error) {
elements := []interface{}{}
for i, e := range ctx.Document.Elements {
switch e := e.(type) {
Expand Down Expand Up @@ -160,7 +160,7 @@ func renderDocumentElements(ctx *renderer.Context) ([]byte, error) {
return buff.Bytes(), nil
}

func renderDocumentTitle(ctx *renderer.Context) ([]byte, error) {
func renderDocumentTitle(ctx renderer.Context) ([]byte, error) {
if documentTitle, hasTitle := ctx.Document.Title(); hasTitle {
title, err := renderPlainText(ctx, documentTitle)
if err != nil {
Expand All @@ -171,7 +171,7 @@ func renderDocumentTitle(ctx *renderer.Context) ([]byte, error) {
return nil, nil
}

func renderDocumentHeader(ctx *renderer.Context) ([]byte, error) {
func renderDocumentHeader(ctx renderer.Context) ([]byte, error) {
if documentTitle, hasTitle := ctx.Document.Title(); hasTitle {
title, err := renderInlineElements(ctx, documentTitle)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/renderer/html5/document_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func init() {
<span id="email{{ .Index }}" class="email"><a href="mailto:{{ .Email }}">{{ .Email }}</a></span><br>{{ end }}`)
}

func renderDocumentDetails(ctx *renderer.Context) (*htmltemplate.HTML, error) {
func renderDocumentDetails(ctx renderer.Context) (*htmltemplate.HTML, error) {
if ctx.Document.Attributes.HasAuthors() {
authors, err := renderDocumentAuthorsDetails(ctx)
if err != nil {
Expand Down Expand Up @@ -56,7 +56,7 @@ func renderDocumentDetails(ctx *renderer.Context) (*htmltemplate.HTML, error) {
return nil, nil
}

func renderDocumentAuthorsDetails(ctx *renderer.Context) (*htmltemplate.HTML, error) {
func renderDocumentAuthorsDetails(ctx renderer.Context) (*htmltemplate.HTML, error) {
authorsDetailsBuff := bytes.NewBuffer(nil)
i := 1
for {
Expand Down
6 changes: 3 additions & 3 deletions pkg/renderer/html5/footnote.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func init() {
</div>{{ end }}{{ end }}
</div>`,
texttemplate.FuncMap{
"renderFootnoteContent": func(ctx *renderer.Context, elements []interface{}) (string, error) {
"renderFootnoteContent": func(ctx renderer.Context, elements []interface{}) (string, error) {
result, err := renderInlineElements(ctx, elements)
if err != nil {
return "", errors.Wrapf(err, "unable to render foot note content")
Expand All @@ -51,7 +51,7 @@ func renderFootnoteIndex(idx int) string {
return strconv.Itoa(idx + 1)
}

func renderFootnote(ctx *renderer.Context, note types.Footnote) ([]byte, error) {
func renderFootnote(ctx renderer.Context, note types.Footnote) ([]byte, error) {
result := bytes.NewBuffer(nil)
ref := ""
noteRef, hasRef := ctx.Document.FootnoteReferences[note.Ref]
Expand Down Expand Up @@ -102,7 +102,7 @@ func renderFootnote(ctx *renderer.Context, note types.Footnote) ([]byte, error)
return result.Bytes(), nil
}

func renderFootnotes(ctx *renderer.Context, notes types.Footnotes) ([]byte, error) {
func renderFootnotes(ctx renderer.Context, notes types.Footnotes) ([]byte, error) {
// skip if there's no foot note in the doc
if len(notes) == 0 {
return []byte{}, nil
Expand Down
Loading

0 comments on commit cdccf6f

Please sign in to comment.