convert .org to html output

This commit is contained in:
facundoolano 2024-02-09 18:02:59 -03:00
parent bbf7ee41bc
commit 465bc6536d
3 changed files with 34 additions and 11 deletions

6
go.mod
View file

@ -2,4 +2,8 @@ module github.com/facundoolano/blorg
go 1.22.0
require gopkg.in/yaml.v3 v3.0.1 // indirect
require (
github.com/niklasfasching/go-org v1.7.0 // indirect
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

8
go.sum
View file

@ -1,3 +1,11 @@
github.com/niklasfasching/go-org v1.7.0 h1:vyMdcMWWTe/XmANk19F4k8XGBYg0GQ/gJGMimOjGMek=
github.com/niklasfasching/go-org v1.7.0/go.mod h1:WuVm4d45oePiE0eX25GqTDQIt/qPW1T9DGkRscqLW5o=
golang.org/x/net v0.0.0-20201224014010-6772e930b67b h1:iFwSg7t5GZmB/Q5TjiEAsdoLDrdJRC1RiF2WhuV29Qw=
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

31
main.go
View file

@ -11,6 +11,7 @@ import (
"path/filepath"
"strings"
"github.com/niklasfasching/go-org/org"
"gopkg.in/yaml.v3"
)
@ -83,6 +84,7 @@ func build() {
os.MkdirAll(targetSubpath, FILE_MODE)
} else {
// FIXME what if non text file?
data, err := render(path)
if err != nil {
@ -90,7 +92,7 @@ func build() {
}
// write the file contents over to target at the same location
err = os.WriteFile(targetSubpath, data, FILE_MODE)
err = os.WriteFile(targetSubpath, []byte(data), FILE_MODE)
if err != nil {
panic(fmt.Sprintf("failed to load %s", targetSubpath))
}
@ -103,28 +105,37 @@ func build() {
}
// TODO move elsewhere?
func render(path string) ([]byte, error) {
func render(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
fileContent, frontMatter, err := extractFrontMatter(file)
fileContent, _, err := extractFrontMatter(file)
if err != nil {
exit(fmt.Sprintf("error in %s: %s", path, err))
}
if len(frontMatter) > 0 {
fmt.Println("Detected front matter:", frontMatter)
return "", errors.New(fmt.Sprintf("error in %s: %s", path, err))
}
var html string
// FIXME this should be renamed to .html
// (in general, the render process should be able to instruct a differnt target path)
if filepath.Ext(path) == ".org" {
// TODO produce html from org
doc := org.New().Parse(bytes.NewReader(fileContent), path)
html, err = doc.Write(org.NewHTMLWriter())
if err != nil {
return "", err
}
} else {
// TODO render liquid template
}
// TODO if yaml contains layout, pass to parent
// TODO minify
return fileContent, err
return html, nil
}
func extractFrontMatter(file *os.File) ([]byte, map[string]interface{}, error) {