Skip to content

Commit

Permalink
fix(renderer): section numbering disabled and re-enabled
Browse files Browse the repository at this point in the history
also, moved logix to traverse the document in 'pkg/types'

Fixes bytesparadise#1039

Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
  • Loading branch information
xcoulon committed Jun 11, 2022
1 parent 9823021 commit 627e0eb
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 104 deletions.
2 changes: 1 addition & 1 deletion pkg/renderer/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Context struct {
Attributes types.Attributes
ElementReferences types.ElementReferences
HasHeader bool
SectionNumbering SectionNumbers
SectionNumbering types.SectionNumbers
}

// NewContext returns a new rendering context for the given document.
Expand Down
42 changes: 0 additions & 42 deletions pkg/renderer/section_numbering.go

This file was deleted.

47 changes: 0 additions & 47 deletions pkg/renderer/section_numbering_test.go

This file was deleted.

3 changes: 1 addition & 2 deletions pkg/renderer/sgml/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/bytesparadise/libasciidoc/pkg/types"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

func (r *sgmlRenderer) renderElements(ctx *renderer.Context, elements []interface{}) (string, error) {
Expand Down Expand Up @@ -119,7 +118,7 @@ func (r *sgmlRenderer) renderElement(ctx *renderer.Context, element interface{})
}

func (r *sgmlRenderer) renderPlainText(ctx *renderer.Context, element interface{}) (string, error) {
log.Debugf("rendering plain string for element of type %T", element)
// log.Debugf("rendering plain string for element of type %T", element)
switch e := element.(type) {
case []interface{}:
return r.renderInlineElements(ctx, e, withRenderer(r.renderPlainText))
Expand Down
60 changes: 59 additions & 1 deletion pkg/renderer/sgml/html5/section_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ var _ = Describe("sections", func() {
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

It("with numbering", func() {
It("with numbering always enabled", func() {
source := `= A title
:sectnums:
Expand Down Expand Up @@ -198,6 +198,64 @@ var _ = Describe("sections", func() {
<div class="sectionbody">
</div>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

It("with numbering disabled and enabled again", func() {
source := `= A title
:sectnums!:
== Disclaimer
:sectnums:
== Section A
=== Section A.a
=== Section A.b
==== Section that shall not be in ToC
== Section B
=== Section B.a
== Section C`

expected := `<div class="sect1">
<h2 id="_disclaimer">Disclaimer</h2>
<div class="sectionbody">
</div>
</div>
<div class="sect1">
<h2 id="_section_a">1. Section A</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_section_a_a">1.1. Section A.a</h3>
</div>
<div class="sect2">
<h3 id="_section_a_b">1.2. Section A.b</h3>
<div class="sect3">
<h4 id="_section_that_shall_not_be_in_toc">1.2.1. Section that shall not be in ToC</h4>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_section_b">2. Section B</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_section_b_a">2.1. Section B.a</h3>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_section_c">3. Section C</h2>
<div class="sectionbody">
</div>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})
Expand Down
7 changes: 3 additions & 4 deletions pkg/renderer/sgml/inline_elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/bytesparadise/libasciidoc/pkg/renderer"
"github.com/bytesparadise/libasciidoc/pkg/types"
log "github.com/sirupsen/logrus"
)

func (r *sgmlRenderer) renderInlineElements(ctx *renderer.Context, elements []interface{}, options ...lineRendererOption) (string, error) {
Expand All @@ -32,9 +31,9 @@ func (r *sgmlRenderer) renderInlineElements(ctx *renderer.Context, elements []in
buf.WriteString(renderedElement)
}
}
if log.IsLevelEnabled(log.DebugLevel) {
log.Debugf("rendered inline elements: '%s'", buf.String())
}
// if log.IsLevelEnabled(log.DebugLevel) {
// log.Debugf("rendered inline elements: '%s'", buf.String())
// }
return buf.String(), nil
}

Expand Down
9 changes: 2 additions & 7 deletions pkg/renderer/sgml/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,8 @@ func (r *sgmlRenderer) Render(ctx *renderer.Context, doc *types.Document, output
}
}
}
if ctx.Attributes.Has(types.AttrSectionNumbering) || ctx.Attributes.Has(types.AttrNumbered) {
var err error
if ctx.SectionNumbering, err = renderer.NewSectionNumbers(doc); err != nil {
return metadata, errors.Wrapf(err, "unable to render full document")
}
} else {
log.Debug("section numbering is not enabled")
if ctx.SectionNumbering, err = doc.SectionNumbers(); err != nil {
return metadata, errors.Wrapf(err, "unable to render full document")
}

// needs to be set before rendering the content elements
Expand Down
Loading

0 comments on commit 627e0eb

Please sign in to comment.