From f57c0d15c931563a55abdc9cea72d189a24f7456 Mon Sep 17 00:00:00 2001 From: Nicolas Paul Date: Mon, 1 May 2023 13:03:03 +0200 Subject: Add slug to HTML templates --- crocc.go | 3 ++- doc/src/doc/template.md | 2 ++ template.go | 4 +++- transformations.go | 5 +++-- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/crocc.go b/crocc.go index 74b14f5..e88ab33 100644 --- a/crocc.go +++ b/crocc.go @@ -136,7 +136,8 @@ func Crocc(path string, d os.DirEntry, e error) error { // If the file is a Markdown file, transform it into HTML o = strings.TrimSuffix(o, filepath.Ext(o)) + ".html" - if err := TransformMarkdownFile(path, o); err != nil { + s := strings.TrimSuffix(strings.TrimPrefix(o, *out), ".html") + if err := TransformMarkdownFile(path, o, s); err != nil { return err } diff --git a/doc/src/doc/template.md b/doc/src/doc/template.md index bf0e387..ab209b9 100644 --- a/doc/src/doc/template.md +++ b/doc/src/doc/template.md @@ -41,6 +41,7 @@ The following variables are available: - `.Content`: The content of the document, as HTML. - `.Site`: The URL of the site. - `.Generator`: A string containing the name and version of the generator. +- `.Slug`: The slug of the document. > **Note:** The `.Content` variable is already HTML, so it does not need to be > escaped. @@ -58,6 +59,7 @@ Here is a sample template: +
diff --git a/template.go b/template.go index 1fa243b..8545e1e 100644 --- a/template.go +++ b/template.go @@ -21,10 +21,11 @@ type TemplateData struct { Content string Site string Generator string + Slug string } // GenerateHTML generates the HTML file from the Markdown document. -func GenerateHTML(fm FrontMatter, content string) ([]byte, error) { +func GenerateHTML(fm FrontMatter, slug, content string) ([]byte, error) { var buffer bytes.Buffer err := htmlTemplate.Execute(&buffer, TemplateData{ @@ -37,6 +38,7 @@ func GenerateHTML(fm FrontMatter, content string) ([]byte, error) { Content: content, Site: *url, Generator: fmt.Sprintf("crocc %s (https://crocc.nc0.fr)", version), + Slug: slug, }) return buffer.Bytes(), err diff --git a/transformations.go b/transformations.go index 14c57b3..aa2fc30 100644 --- a/transformations.go +++ b/transformations.go @@ -42,7 +42,8 @@ func TransformDirectory(o string) error { // TransformMarkdownFile generates the corresponding HTML document from a // Markdown file. -func TransformMarkdownFile(i, o string) error { +// s corresponds to the slug of the document. +func TransformMarkdownFile(i, o, s string) error { raw, err := os.ReadFile(i) if err != nil { return err @@ -75,7 +76,7 @@ func TransformMarkdownFile(i, o string) error { renderer := html.NewRenderer(html.RendererOptions{Flags: htmlFlags}) html := markdown.Render(ast, renderer) - c, err := GenerateHTML(fm, string(html)) + c, err := GenerateHTML(fm, s, string(html)) if err != nil { return err } -- cgit v1.2.3