From 48ed5014eb4488a284de6689bf675dcf22df40d6 Mon Sep 17 00:00:00 2001 From: dre Date: Sun, 11 Jul 2021 17:03:59 +0800 Subject: [PATCH] clear section list --- README.md | 8 ++++---- main.go | 8 ++------ section_list.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 section_list.go diff --git a/README.md b/README.md index 8e913f6..fea9bed 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,6 @@ gemtext. Executed from the hugo directory: hugoext -ext gmi -pipe md2gmi ``` - It abides the hugo section config in `[permalinks]` but only uses the content subdirectory to determine the section. An example section config in hugo looks like this: @@ -69,11 +68,12 @@ files. Here an example for a Gemlog using [gmifs](https://github.com/n0x1m/gmifs ```makefile serve: - hugoext -ext gmi -pipe md2gmi -serve="gmifs -autoindex" + hugoext -ext gmi -pipe md2gmi + gmifs -autoindex ``` -hugoext pipes the input through the `md2gmi` extension and spawns `gmifs` to serve the local gemini -directory with auto indexing enabled. +hugoext pipes the input through the `md2gmi` extension and writes the output file tree. We then +spawn `gmifs` to serve it on `gemini://localhost` with auto indexing enabled. ### Production diff --git a/main.go b/main.go index 3c01b6c..52b66ea 100644 --- a/main.go +++ b/main.go @@ -180,15 +180,11 @@ func main() { section, ok := sections[seconOnRoot] if ok { sectionFile := filepath.Join(destination, "index."+ext) + // TODO: come up with sth better as one might have content there. + os.Remove(sectionFile) section.Write(sectionFile) fmt.Printf("written section listing for root to %s\n", section.File) } - - // TODO - // in watch mode, compare timestamps of files before replacement, keep index? - // check/replace links - // write rss? - // write listings from template? } func pipe(cmd string, input io.Reader) []byte { diff --git a/section_list.go b/section_list.go new file mode 100644 index 0000000..12e0fb1 --- /dev/null +++ b/section_list.go @@ -0,0 +1,46 @@ +package main + +import ( + "bytes" + "fmt" + "os" + "sort" + "time" +) + +type Section struct { + List []SectionEntry + File string +} + +type SectionEntry struct { + Link string + Title string + Date time.Time + Summary string +} + +func (section *Section) Write(file string) error { + // sort section list + sort.Slice(section.List, func(i, j int) bool { + return section.List[i].Date.Before(section.List[j].Date) + }) + + var buf bytes.Buffer + for _, file := range section.List { + // TODO: this could be a template + entry := "\n" + entry += fmt.Sprintf("=> %s %s\n", file.Link, file.Title) + entry += fmt.Sprintf("%v - %s\n", file.Date.Format("2006-01-02"), file.Summary) + + buf.Write([]byte(entry)) + } + + f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return err + } + _, err = f.Write(buf.Bytes()) + f.Close() + return err +}