summaryrefslogtreecommitdiff
path: root/template.go
blob: 1fa243b67d6ee8184960b955b3340f264dd348d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright (c) 2023 Nicolas Paul All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"bytes"
	"fmt"
	"time"
)

// TemplateData is the data passed to the HTML template.
type TemplateData struct {
	Title           string
	Description     string
	PublicationTime string
	LastUpdateTime  string
	Keywords        string
	Author          string
	Content         string
	Site            string
	Generator       string
}

// GenerateHTML generates the HTML file from the Markdown document.
func GenerateHTML(fm FrontMatter, content string) ([]byte, error) {
	var buffer bytes.Buffer

	err := htmlTemplate.Execute(&buffer, TemplateData{
		Title:           fm.Title,
		Description:     fm.Description,
		PublicationTime: fm.PublicationTime.Format(time.RFC3339),
		LastUpdateTime:  fm.LastUpdateTime.Format(time.RFC3339),
		Keywords:        fmt.Sprintf("%s", fm.Keywords),
		Author:          fm.Author,
		Content:         content,
		Site:            *url,
		Generator:       fmt.Sprintf("crocc %s (https://crocc.nc0.fr)", version),
	})

	return buffer.Bytes(), err
}