inital work on feeds
This commit is contained in:
parent
7129e200f7
commit
41dca2aaf5
91
main.go
91
main.go
|
@ -9,6 +9,7 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
"tildegit.org/nihilazo/go-gemtext"
|
||||
"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) {
|
||||
|
@ -132,7 +151,7 @@ func processLinkString(s string, mode int) (string, error) {
|
|||
}
|
||||
|
||||
// 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
|
||||
|
@ -284,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)
|
||||
|
@ -370,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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Reference in New Issue