Compare commits

...

3 Commits

Author SHA1 Message Date
Nico 41dca2aaf5 inital work on feeds 2020-11-20 23:42:55 +00:00
Nico 7129e200f7 added gitignore 2020-11-20 18:44:51 +00:00
Nico 9fc816c300 added dep on feeds, some cleanup 2020-11-20 18:44:28 +00:00
4 changed files with 91 additions and 16 deletions

1
.gitignore vendored
View File

@ -1,2 +1 @@
go.sum
sitegenerator

5
go.mod
View File

@ -2,4 +2,7 @@ module tildegit.org/nihilazo/sitegenerator
go 1.15
require tildegit.org/nihilazo/go-gemtext v0.0.0-20201114202124-890be6eb3742
require (
github.com/gorilla/feeds v1.1.1 // indirect
tildegit.org/nihilazo/go-gemtext v0.0.0-20201114202124-890be6eb3742
)

2
go.sum
View File

@ -1,2 +1,4 @@
github.com/gorilla/feeds v1.1.1 h1:HwKXxqzcRNg9to+BbvJog4+f3s/xzvtZXICcQGutYfY=
github.com/gorilla/feeds v1.1.1/go.mod h1:Nk0jZrvPFZX1OBe5NPiddPw7CfwF6Q9eqzaBbaightA=
tildegit.org/nihilazo/go-gemtext v0.0.0-20201114202124-890be6eb3742 h1:SE9EroZwQEH3mHxWah4UoHQGgBTa008VIjmh2PqSZXw=
tildegit.org/nihilazo/go-gemtext v0.0.0-20201114202124-890be6eb3742/go.mod h1:KOdDbr9CNSpgmfNfCEjpDhaGPCd86Mp5+XGrZDBKZvo=

99
main.go
View File

@ -9,8 +9,9 @@ import (
"os"
"path/filepath"
"strings"
"time"
"tildegit.org/nihilazo/go-gemtext"
//ht "html/template"
"github.com/gorilla/feeds"
t "text/template"
)
@ -23,10 +24,11 @@ var templateDir string = filepath.Join(root, "templates")
var htmlOutputDir string = filepath.Join(root, "public_html")
var inputDir string = filepath.Join(root, "content")
var tagFile string = filepath.Join(root, "tags.json")
var feedFile string = filepath.Join(root, "feeds.json")
type filePathInfo struct {
Path string
New bool // True if the file is new
New bool
}
type pageInfo struct {
@ -46,9 +48,20 @@ const (
// RemoveIndex is a reslicing function.
func RemoveIndex(s []pageInfo, index int) []pageInfo {
if index == len(s) - 1 {
return s[:index]
} else {
return append(s[:index], s[index+1:]...)
}
}
// RemoveIndexFeed is a reslicing function.
func RemoveIndexFeed(s []*feeds.Item, index int) []*feeds.Item {
if index > len(s) {
return s[:len(s)-1]
} else {
return append(s[:index], s[index+1:]...)
}
}
// wraps the string in a basic standalone HTML document.
func htmlWrap(d *string) string {
header := `<!DOCTYPE html><head><link rel=stylesheet href="https://itwont.work/style.css"><title>Nico's site</title></head><body><article>`
@ -58,6 +71,12 @@ func htmlWrap(d *string) string {
var tagData map[string][]pageInfo = make(map[string][]pageInfo)
var feed *feeds.Feed = &feeds.Feed{
Title: "Nico's site",
Link: &feeds.Link{Href: geminiPrefix},
Author: &feeds.Author{Name: "Nico", Email: "nico@itwont.work"},
}
var files []filePathInfo // Files to process (those that are new or have been edited)
func copy(src, dst string) (int64, error) {
@ -124,15 +143,15 @@ func processLinkString(s string, mode int) (string, error) {
return "", err
}
if mode == GEMINI {
o = filepath.Join(geminiPrefix, o)
o = geminiPrefix + "/" + o
} else if mode == HTML {
o = filepath.Join(htmlPrefix, strings.Replace(o, ".gmi", ".html", -1))
o = htmlPrefix + "/" + strings.Replace(o, ".gmi", ".html", -1)
}
return o, nil
}
// processPage finds a page, gets the tags, and writes it out to HTML and gemini output directories.
// TODO BUG: somehow tagData ends up empty.
// TODO BUG: Fix removing old feed entries for the same item (make it not t)
func processPage(f filePathInfo) error {
// Delete any existing tag references to this file.
@ -146,6 +165,18 @@ func processPage(f filePathInfo) error {
}
}
}
// TODO maybe feeds for html?
ls, err := processLinkString(f.Path, GEMINI)
if err != nil {
return err
}
l := &feeds.Link{Href: ls}
for i, d := range feed.Items {
if *l == *d.Link {
fmt.Println("got dupe!")
feed.Items = RemoveIndexFeed(feed.Items,i)
}
}
// Open the file and parse the gemtext
data, err := ioutil.ReadFile(f.Path)
if err != nil {
@ -155,14 +186,24 @@ func processPage(f filePathInfo) error {
if err != nil {
return err
}
var info pageInfo
if isTagged(&parse) {
title := parse[0].Text
data := pageInfo{Title: title, Path: f.Path}
info = pageInfo{Title: title, Path: f.Path}
tags := strings.Split(parse[1].Text[5:], ",") // Remove the Tags: prefix and split
for _, tag := range tags {
tagData[tag] = append(tagData[tag], data) // For each tag, append the data to it
tagData[tag] = append(tagData[tag], info) // For each tag, append the data to it
}
s, err := os.Stat(f.Path)
if err != nil {
return err
}
feed.Add(&feeds.Item{
Title: info.Title,
Link: &feeds.Link{Href: ls},
Updated: s.ModTime(),
Description: "New/Updated Page: " + info.Title,
})
}
var gemlinks gemtext.GemtextPage // Page with prefixed links for gemini
var htmllinks gemtext.GemtextPage // Page with prefixed links for html
@ -198,8 +239,6 @@ func processPage(f filePathInfo) error {
if err != nil {
return err
}
} else if filepath.Ext(f.Path) == ".tpl" {
// TODO handle template files
} else { // Non-gemtext files
rel, err := filepath.Rel(inputDir, f.Path)
if err != nil {
@ -286,6 +325,23 @@ func main() {
panic(err)
}
}
loadingJson = true
if _, err := os.Stat(feedFile); err != nil {
if os.IsNotExist(err) {
loadingJson = false
fmt.Println("Not loading feed file, it doesn't exist yet")
} else {
panic(err)
}
}
feedFileData, err := ioutil.ReadFile(feedFile)
if loadingJson {
err = json.Unmarshal([]byte(feedFileData), feed)
if err != nil {
panic(err)
}
}
feed.Updated = time.Now()
htmlTagPageTemplateFile, err := ioutil.ReadFile(filepath.Join(templateDir, "tag.html"))
if err != nil {
panic(err)
@ -372,10 +428,25 @@ func main() {
panic(err)
}
}
json, err := json.Marshal(tagData)
err = ioutil.WriteFile(tagFile, json, 0644)
// Write Feeds
fe, err := feed.ToAtom()
if err != nil {
panic(err)
}
err = ioutil.WriteFile(filepath.Join(geminiOutputDir,"atom.xml"), []byte(fe), 0644)
if err != nil {
panic(err)
}
j, err := json.Marshal(tagData)
err = ioutil.WriteFile(tagFile, j, 0644)
if err != nil {
panic(err)
}
j, err = json.Marshal(feed)
err = ioutil.WriteFile(feedFile, j, 0644)
if err != nil {
panic(err)
}
}