From e2c8c278c532f2906ebd948f559d813525959289 Mon Sep 17 00:00:00 2001 From: dre Date: Sun, 11 Jul 2021 12:00:30 +0800 Subject: [PATCH] improve docs and adapt hugo changes --- README.md | 21 +++++++++++++++------ file_tree.go | 36 +++++++++++++++++++++--------------- page_metadata.go | 4 ++-- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index fe97363..b3ccacc 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,21 @@ # hugoext -Utility to parse the hugo config file and create the same file structure for content through an -arbitrary output pipe extension. +Utility to parse the hugo config file and recreate the same file structure for content files through +an arbitrary output pipe extension for processing. Hugo parses primarily markdown files and go templates. The initial motivation for this utility was -to enable the same tools to publish a gemlog version of the same blog to make it accessible as -gemtext via the Gemini protocol alongside the web html version. +to enable the same tools to publish a Gemlog version of a hugo blog to make it accessible as Gemtext +via the Gemini protocol. -**NOTE**: this is minimal and only has one use case for now, many edge cases may not be covered. +**NOTE**: this is rather minimal and only has one use case for now, many edge cases may not be covered. Features - reads hugo `.toml` file for section output formats - supports an arbitrary document processor, any program that supports UNIX pipes - ugly urls - section listings -- with drafts +- supports drafts +- composable with other tools To illustrate what this program does, run the following in the hugo directory. @@ -89,3 +90,11 @@ publish: build The output directory for both hugo and hugoext is `./public`. It's ok to mix the two into the same file tree as each directory will contain an `index.html` and an `index.gmi` file. + +## Acknowledgements & Attribution + +For config parsing and compatibility, this repo uses the latest hugo source tree as the functions are all +exported. For markdown and metadata parsing of hugo content files, I've extracted some code from +[hugo@v0.49.2](https://github.com/gohugoio/hugo/tree/v0.49.2/) tree and made them importable in the +local [hugo](./hugo) package. The version had required frontmatter, metadata and section permalink +parsers still available in well isolated functions, so I didn't need to recreate them. diff --git a/file_tree.go b/file_tree.go index 4cf57cf..6dd8aea 100644 --- a/file_tree.go +++ b/file_tree.go @@ -20,44 +20,46 @@ type File struct { Parent string Name string Extension string + Draft bool - Draft bool - - Body []byte - NewBody []byte + Metadata hugo.PageMetadata + Body []byte + NewBody []byte } -func parse(fullpath string) ([]byte, *hugo.Content, error) { +func parsePage(fullpath string) (hugo.Page, error) { file, err := os.Open(fullpath) if err != nil { - return nil, nil, err + return nil, err } defer file.Close() page, err := hugo.ReadFrom(file) if err != nil { - return nil, nil, err + return nil, err } + return page, nil +} + +func parseMetadata(page hugo.Page) (*hugo.PageMetadata, error) { meta, err := page.Metadata() if err != nil { - return nil, nil, err + return nil, err } c := NewContentFromMeta(meta) - body := page.FrontMatter() - body = append(body, '\n') - body = append(body, page.Content()...) - return body, c, nil + return c, nil } func destinationPath(file *File, pattern string) error { - body, c, err := parse(file.Source) + p, err := parsePage(file.Source) if err != nil { return err } + + // create content + c, err := parseMetadata(p) c.Filepath = file.Name - file.Body = body - file.Draft = c.Draft if file.Parent != "." { link, err := hugo.PathPattern(pattern).Expand(c) @@ -69,6 +71,10 @@ func destinationPath(file *File, pattern string) error { file.Destination = strings.TrimLeft(file.Name, "_") } + file.Draft = c.Draft + file.Metadata = *c + file.Body = p.Body() + return nil } diff --git a/page_metadata.go b/page_metadata.go index e96d9a9..38e3b8f 100644 --- a/page_metadata.go +++ b/page_metadata.go @@ -6,8 +6,8 @@ import ( "github.com/n0x1m/hugoext/hugo" ) -func NewContentFromMeta(meta map[string]interface{}) *hugo.Content { - return &hugo.Content{ +func NewContentFromMeta(meta map[string]interface{}) *hugo.PageMetadata { + return &hugo.PageMetadata{ Title: stringFromInterface(meta["title"]), Slug: stringFromInterface(meta["slug"]), Summary: stringFromInterface(meta["summary"]),