improve docs and adapt hugo changes

This commit is contained in:
dre 2021-07-11 12:00:30 +08:00
parent 0e754e1028
commit e2c8c278c5
3 changed files with 38 additions and 23 deletions

View file

@ -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.

View file

@ -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
}

View file

@ -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"]),