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 # hugoext
Utility to parse the hugo config file and create the same file structure for content through an Utility to parse the hugo config file and recreate the same file structure for content files through
arbitrary output pipe extension. an arbitrary output pipe extension for processing.
Hugo parses primarily markdown files and go templates. The initial motivation for this utility was 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 to enable the same tools to publish a Gemlog version of a hugo blog to make it accessible as Gemtext
gemtext via the Gemini protocol alongside the web html version. 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 Features
- reads hugo `.toml` file for section output formats - reads hugo `.toml` file for section output formats
- supports an arbitrary document processor, any program that supports UNIX pipes - supports an arbitrary document processor, any program that supports UNIX pipes
- ugly urls - ugly urls
- section listings - section listings
- with drafts - supports drafts
- composable with other tools
To illustrate what this program does, run the following in the hugo directory. 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 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. 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 Parent string
Name string Name string
Extension string Extension string
Draft bool
Draft bool Metadata hugo.PageMetadata
Body []byte
Body []byte NewBody []byte
NewBody []byte
} }
func parse(fullpath string) ([]byte, *hugo.Content, error) { func parsePage(fullpath string) (hugo.Page, error) {
file, err := os.Open(fullpath) file, err := os.Open(fullpath)
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
defer file.Close() defer file.Close()
page, err := hugo.ReadFrom(file) page, err := hugo.ReadFrom(file)
if err != nil { 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() meta, err := page.Metadata()
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
c := NewContentFromMeta(meta) 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 { func destinationPath(file *File, pattern string) error {
body, c, err := parse(file.Source) p, err := parsePage(file.Source)
if err != nil { if err != nil {
return err return err
} }
// create content
c, err := parseMetadata(p)
c.Filepath = file.Name c.Filepath = file.Name
file.Body = body
file.Draft = c.Draft
if file.Parent != "." { if file.Parent != "." {
link, err := hugo.PathPattern(pattern).Expand(c) 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.Destination = strings.TrimLeft(file.Name, "_")
} }
file.Draft = c.Draft
file.Metadata = *c
file.Body = p.Body()
return nil return nil
} }

View file

@ -6,8 +6,8 @@ import (
"github.com/n0x1m/hugoext/hugo" "github.com/n0x1m/hugoext/hugo"
) )
func NewContentFromMeta(meta map[string]interface{}) *hugo.Content { func NewContentFromMeta(meta map[string]interface{}) *hugo.PageMetadata {
return &hugo.Content{ return &hugo.PageMetadata{
Title: stringFromInterface(meta["title"]), Title: stringFromInterface(meta["title"]),
Slug: stringFromInterface(meta["slug"]), Slug: stringFromInterface(meta["slug"]),
Summary: stringFromInterface(meta["summary"]), Summary: stringFromInterface(meta["summary"]),