libasciidoc icon indicating copy to clipboard operation
libasciidoc copied to clipboard

Custom rendering function for header tags

Open Arteneko opened this issue 5 years ago • 4 comments

By the look of it, there's an uniformized rendering function for headers (source).

By the way the renderer works (source), is there any imaginable way to have a custom template?

For example, I'd like to add a # link at beginning of line, which would point to #{{ .ID }}.

I guess that moving every template string to either a global or an instance-specific configuration would allow this feature, and other overwrites, to be implemented without having to do much changes to the actual parser.

Arteneko avatar May 13 '19 16:05 Arteneko

By the look of it, there's an uniformized rendering function for headers (source).

The grammar rules for parsing the headers (a.k.a "sections") is defined in https://github.com/bytesparadise/libasciidoc/blob/master/pkg/parser/asciidoc-grammar.peg#L376-L544. What you've linked in your description was the "RawSectionTitle" which is now used in the "pre-parsing" phase, where the include:: macros are detected and replaced with the actual files to include.

By the way the renderer works (source), is there any imaginable way to have a custom template?

Custom macros are being addressed in #347, but maybe you're talking about something else?

For example, I'd like to add a # link at beginning of line, which would point to #{{ .ID }}.

Could you elaborate on this example, please? My fear is that we would derive from the "standard" Asciidoc format/spec, which I'd rather avoid.

I guess that moving every template string to either a global or an instance-specific configuration would allow this feature, and other overwrites, to be implemented without having to do much changes to the actual parser.

xcoulon avatar May 13 '19 23:05 xcoulon

With the following example snippet, the following HTML code is generated.

package main

import (
	"bytes"
	"fmt"
	"strings"

	"github.com/bytesparadise/libasciidoc"
)

func main() {
	reader := strings.NewReader("== World")
	writer := new(bytes.Buffer)
	_, _ = libasciidoc.ConvertToHTML(nil, reader, writer)
	fmt.Println(writer.String())
}
<div class="sect1">
<h2 id="_world">World</h2>
<div class="sectionbody">
</div>
</div>

Now, let's say that I want to have an anchor tag right on the title on my output, like <h2 id="_world"><a href="#_world">#</a> World</h2>.

This isn't a completely custom tag but a custom final-step rendering of the heading rule. As those use templates, my question was about wether if it was possible to imagine a way to customize the final rendered templates or not.

Arteneko avatar May 14 '19 12:05 Arteneko

ok, I see now. So we would need some kind of custom, configurable post-processor to enrich/modify the output of some elements, as you've shown in your example. It's not possible yet, but that should be doable, actually. Let's keep this issue open so it can be addressed, ok?

xcoulon avatar May 15 '19 02:05 xcoulon

If you're willing to write code, you actually can replace the templates now. (Note however that the specific templates may change over time, as we've not stabilized them yet.)

So for example you could do

import ( "github.com/bytesparadise/libasciidoc/renderer/sgml/html5" "github.com/bytesparadise/libasciidoc/renderer/sgml/html5" )

t := html5.Templates() // modify t.SectionHeader for example... r := sgml.NewRenderer(t)

And then render using that. (You'll have to steal the code to parse from libasciidoc.go)

Probably what we should do is add some high level helpers to make this easier.

gdamore avatar Jun 16 '20 03:06 gdamore