diff --git a/app/menu.go b/app/menu.go index 5c6316e..13ff1d6 100644 --- a/app/menu.go +++ b/app/menu.go @@ -9,6 +9,7 @@ import ( "git.sr.ht/~rockorager/vaxis" "git.sr.ht/~rockorager/vaxis/widgets/term" + "git.tilde.town/nbsp/neofeels/ttbp" "git.tilde.town/nbsp/neofeels/ui" ) @@ -131,7 +132,8 @@ func newFeels(state *ui.State) { vt.TERM = os.Getenv("TERM") vt.Attach(state.PostEvent()) vt.Focus() - err := vt.Start(exec.Command(os.ExpandEnv(os.Getenv("EDITOR")), path.Join(os.Getenv("HOME"), ".ttbp/entries", time.Now().Format("20060102")+".txt"))) + now := time.Now() + err := vt.Start(exec.Command(os.ExpandEnv(os.Getenv("EDITOR")), path.Join(os.Getenv("HOME"), ".ttbp/entries", now.Format("20060102")+".txt"))) if err != nil { panic(err) } @@ -142,6 +144,8 @@ func newFeels(state *ui.State) { case term.EventClosed: state.HideCursor() state.Window().Clear() + ttbp.NewNopub(now) + ttbp.Publish(now) ui.ViewChange <- NewPosted() return case vaxis.Redraw: diff --git a/go.mod b/go.mod index 8a54edf..5d03c9b 100644 --- a/go.mod +++ b/go.mod @@ -2,12 +2,15 @@ module git.tilde.town/nbsp/neofeels go 1.23.4 -require git.sr.ht/~rockorager/vaxis v0.11.0 +require ( + git.sr.ht/~rockorager/vaxis v0.11.0 + github.com/dustin/go-humanize v1.0.1 + github.com/yuin/goldmark v1.4.13 +) require ( github.com/containerd/console v1.0.3 // indirect github.com/creack/pty v1.1.18 // indirect - github.com/dustin/go-humanize v1.0.1 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect github.com/mattn/go-sixel v0.0.5 // indirect github.com/rivo/uniseg v0.4.4 // indirect diff --git a/go.sum b/go.sum index f5e0224..7d8ef1a 100644 --- a/go.sum +++ b/go.sum @@ -21,6 +21,7 @@ github.com/soniakeys/quant v1.0.0 h1:N1um9ktjbkZVcywBVAAYpZYSHxEfJGzshHCxx/DaI0Y github.com/soniakeys/quant v1.0.0/go.mod h1:HI1k023QuVbD4H8i9YdfZP2munIHU4QpjsImz6Y6zds= github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= diff --git a/ttbp/ttbp.go b/ttbp/ttbp.go index 2003898..1ac8c95 100644 --- a/ttbp/ttbp.go +++ b/ttbp/ttbp.go @@ -2,13 +2,18 @@ package ttbp import ( "bufio" + "bytes" "encoding/json" + "fmt" "os" + "os/user" "path" "sort" + "strings" "time" "git.tilde.town/nbsp/neofeels/config" + "github.com/yuin/goldmark" ) var ( @@ -86,6 +91,7 @@ type Post struct { LastEdited time.Time Words int Author string + Nopub bool } func GetPostsForUser(user string) (posts []Post) { @@ -93,6 +99,16 @@ func GetPostsForUser(user string) (posts []Post) { if err != nil { return } + nopubFile, err := os.OpenFile(path.Join(os.Getenv("HOME"), ".ttbp/config/nopub"), os.O_RDONLY|os.O_CREATE, 0644) + if err != nil { + return + } + defer nopubFile.Close() + var nopubs []string + nopubScanner := bufio.NewScanner(nopubFile) + for nopubScanner.Scan() { + nopubs = append(nopubs, nopubScanner.Text()) + } for _, post := range postFiles { // retrieve date of file // assume file ends in .txt @@ -118,11 +134,21 @@ func GetPostsForUser(user string) (posts []Post) { continue } + // see if file is in nopub + nopub := false + for _, name := range nopubs { + if name == post.Name() { + nopub = true + break + } + } + posts = append([]Post{Post{ Author: user, Date: fileDate, LastEdited: stat.ModTime(), Words: count, + Nopub: nopub, }}, posts...) } } @@ -143,7 +169,7 @@ type Subscriptions struct { } func GetSubscriptions() *Subscriptions { - file, err := os.OpenFile(path.Join(os.Getenv("HOME"), ".ttbp/config/subs"), os.O_RDONLY|os.O_CREATE, 0644) + file, err := os.OpenFile(path.Join(os.Getenv("HOME"), ".ttbp/config/subs"), os.O_RDONLY|os.O_CREATE, 0600) if err != nil { return &Subscriptions{} } @@ -199,3 +225,96 @@ func (subscriptions *Subscriptions) Write() { writer.Flush() } + +func NewNopub(t time.Time) { + cfg, err := config.Read() + if err != nil || (!cfg.Nopub && cfg.Publishing) { + return + } + dateString := t.Format("20060102.txt") + file, err := os.OpenFile(path.Join(os.Getenv("HOME"), ".ttbp/config/nopub"), os.O_RDWR|os.O_CREATE, 0600) + if err != nil { + return + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + if scanner.Text() == dateString { + return + } + } + + writer := bufio.NewWriter(file) + writer.WriteString(dateString) + writer.Flush() +} + +func Publish(t time.Time) { + cfg, err := config.Read() + if err != nil { + return // TODO: expose this error to the user + } + if cfg.Publishing { + file, err := os.Create(path.Join(os.Getenv("HOME"), ".ttbp/www/index.html")) + defer file.Close() + + // load header and footer + header, err := os.ReadFile(path.Join(os.Getenv("HOME"), ".ttbp/config/header.txt")) + footer, err := os.ReadFile(path.Join(os.Getenv("HOME"), ".ttbp/config/footer.txt")) + + if err != nil { + return + } + + writer := bufio.NewWriter(file) + writer.WriteString(fmt.Sprintf("\n", time.Now().Format(time.DateTime))) + writer.WriteString(string(header) + "\n") + user, _ := user.Current() + for _, post := range GetPostsForUser(user.Username) { + if !post.Nopub { + writePage(post, header, footer) + writer.WriteString(writeEntry(post) + "\n") + } + } + writer.WriteString(string(footer)) + writer.Flush() + } + // TODO: gopher +} + +func writePage(post Post, header, footer []byte) { + dateString := post.Date.Format("20060102") + file, err := os.Create(path.Join(os.Getenv("HOME"), ".ttbp/www/"+dateString+".html")) + if err != nil { + return + } + writer := bufio.NewWriter(file) + writer.WriteString(fmt.Sprintf("<-- generated by neofeels on %s — https://tilde.town/~nbsp/neofeels -->\n", time.Now().Format(time.DateTime))) + writer.WriteString(string(header) + "\n") + writer.WriteString(writeEntry(post) + "\n") + writer.WriteString(string(footer)) + writer.Flush() +} + +func writeEntry(post Post) string { + dateString := post.Date.Format("20060102") + file, err := os.ReadFile(path.Join(os.Getenv("HOME"), ".ttbp/entries/"+dateString+".txt")) + if err != nil { + return "" + } + var buf bytes.Buffer + goldmark.Convert(file, &buf) + return fmt.Sprintf( + `
+ + `, + dateString, dateString, post.Date.Day(), + strings.ToLower(post.Date.Month().String()), post.Date.Year(), + buf.String(), dateString, + ) +}