Ramblings
This commit is contained in:
@@ -1,7 +1,11 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
news=true
|
# Section toggels
|
||||||
quote=true
|
news=true # enables the news section
|
||||||
ramblings=true
|
quote=true # enables the quotes section
|
||||||
|
ramblings=false #enables the ramblings section
|
||||||
|
|
||||||
|
# set the path where wire looks for ramblings, this is mostly for server use.
|
||||||
ramblings_path=${XDG_DOCUMENTS_DIR}/wire/
|
ramblings_path=${XDG_DOCUMENTS_DIR}/wire/
|
||||||
|
|
||||||
|
article_path="https://rss.nytimes.com/services/xml/rss/nyt/US.xml"
|
||||||
|
|||||||
@@ -18,14 +18,15 @@ var wrapper string
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
News bool `json:"news"`
|
News bool `json:"news"`
|
||||||
Quotes bool `json:"quote"`
|
Quotes bool `json:"quote"`
|
||||||
Ramblings bool `json:"ramblings"`
|
Ramble bool `json:"ramblings"`
|
||||||
Ramblings_Path string `json:"ramblings_path"`
|
Ramblings_Path string `json:"ramblings_path"`
|
||||||
|
Articles_Path string `json:"article_path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig() (c Config) {
|
func NewConfig() (c Config) {
|
||||||
c.News = true
|
c.News = true
|
||||||
c.Quotes = true
|
c.Quotes = true
|
||||||
c.Ramblings = true
|
c.Ramble = true
|
||||||
ramblingsDir := os.Getenv("XDG_DOCUMENTS_DIR")
|
ramblingsDir := os.Getenv("XDG_DOCUMENTS_DIR")
|
||||||
if ramblingsDir == "" {
|
if ramblingsDir == "" {
|
||||||
ramblingsDir = filepath.Join(os.Getenv("HOME"), "Documents")
|
ramblingsDir = filepath.Join(os.Getenv("HOME"), "Documents")
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
. $1
|
. $1
|
||||||
printf '{"news":%s,"quote":%s,"ramblings":%s,"ramblings_path":"%s"}\n' "$news" "$quote" "$ramblings" "$ramblings_path"
|
printf '{"news":%s,"quote":%s,"ramblings":%s,"ramblings_path":"%s","article_path":"%s"}\n' "$news" "$quote" "$ramblings" "$ramblings_path" "$article_path"
|
||||||
|
|||||||
74
internal/ramblings/ramblings.go
Normal file
74
internal/ramblings/ramblings.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package ramblings
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/text/cases"
|
||||||
|
"golang.org/x/text/language"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Ramblings []Rambles
|
||||||
|
|
||||||
|
type Rambles struct {
|
||||||
|
Title string
|
||||||
|
Link string
|
||||||
|
Content template.HTML
|
||||||
|
}
|
||||||
|
|
||||||
|
type RamblingsParser struct {
|
||||||
|
path string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewParser(path string) *RamblingsParser {
|
||||||
|
rp := RamblingsParser{
|
||||||
|
path: path,
|
||||||
|
}
|
||||||
|
return &rp
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rp *RamblingsParser) GetRamblings() (rs Ramblings) {
|
||||||
|
entries, err := os.ReadDir(rp.path)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ent := range entries {
|
||||||
|
if ent.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
title := ent.Name()
|
||||||
|
title = strings.ReplaceAll(title, "_", " ")
|
||||||
|
title = strings.ReplaceAll(title, "-", " ")
|
||||||
|
ext := filepath.Ext(ent.Name())
|
||||||
|
title = strings.TrimSuffix(title ,ext)
|
||||||
|
|
||||||
|
caser := cases.Title(language.AmericanEnglish)
|
||||||
|
title = caser.String(title)
|
||||||
|
|
||||||
|
fp := filepath.Join(rp.path, ent.Name())
|
||||||
|
contents, err := os.ReadFile(fp)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rs = append(rs, Rambles{
|
||||||
|
Title: title,
|
||||||
|
Link: template.HTMLEscapeString(ent.Name()),
|
||||||
|
Content: template.HTML(string(contents)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHandler(ramble Rambles) func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Write([]byte(ramble.Content))
|
||||||
|
}
|
||||||
|
}
|
||||||
16
main.go
16
main.go
@@ -10,6 +10,7 @@ import (
|
|||||||
"wire/internal/api"
|
"wire/internal/api"
|
||||||
"wire/internal/articles"
|
"wire/internal/articles"
|
||||||
"wire/internal/config"
|
"wire/internal/config"
|
||||||
|
"wire/internal/ramblings"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed version
|
//go:embed version
|
||||||
@@ -20,7 +21,7 @@ var verboseFlag = flag.Bool("verbose", false, "Enable verbose logging")
|
|||||||
type Data struct {
|
type Data struct {
|
||||||
Version string
|
Version string
|
||||||
Quote template.HTML
|
Quote template.HTML
|
||||||
Ramblings string
|
Ramblings ramblings.Ramblings
|
||||||
Articles articles.Articles
|
Articles articles.Articles
|
||||||
Config config.Config
|
Config config.Config
|
||||||
}
|
}
|
||||||
@@ -42,22 +43,29 @@ func main() {
|
|||||||
cfg.ReadConfig()
|
cfg.ReadConfig()
|
||||||
data.Config = cfg
|
data.Config = cfg
|
||||||
|
|
||||||
ap := articles.NewParser("https://rss.nytimes.com/services/xml/rss/nyt/US.xml")
|
ap := articles.NewParser(cfg.Articles_Path)
|
||||||
data.Articles = ap.GetArticles()
|
data.Articles = ap.GetArticles()
|
||||||
|
|
||||||
|
rp := ramblings.NewParser(cfg.Ramblings_Path)
|
||||||
|
data.Ramblings = rp.GetRamblings()
|
||||||
|
|
||||||
|
for _, ramble := range rp.GetRamblings() {
|
||||||
|
http.HandleFunc("/" + ramble.Link, ramblings.NewHandler(ramble))
|
||||||
|
}
|
||||||
|
|
||||||
quote, err := api.FetchJSON[[]Quote]("https://zenquotes.io/api/today")
|
quote, err := api.FetchJSON[[]Quote]("https://zenquotes.io/api/today")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
data.Quote = template.HTML(quote[0].H)
|
data.Quote = template.HTML(quote[0].H)
|
||||||
|
|
||||||
http.HandleFunc("/", newsHandler)
|
http.HandleFunc("/", mainHandler)
|
||||||
|
|
||||||
fmt.Println("wire: starting server at http://127.0.0.1:1337")
|
fmt.Println("wire: starting server at http://127.0.0.1:1337")
|
||||||
log.Fatal(http.ListenAndServe("127.0.0.1:1337", nil))
|
log.Fatal(http.ListenAndServe("127.0.0.1:1337", nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func newsHandler(w http.ResponseWriter, r *http.Request) {
|
func mainHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
tmpl := template.Must(template.ParseFiles("templates/main.html"))
|
tmpl := template.Must(template.ParseFiles("templates/main.html"))
|
||||||
|
|
||||||
tmpl.ExecuteTemplate(w, "main.html", data)
|
tmpl.ExecuteTemplate(w, "main.html", data)
|
||||||
|
|||||||
@@ -23,7 +23,11 @@
|
|||||||
|
|
||||||
{{if .Config.Ramble}}
|
{{if .Config.Ramble}}
|
||||||
<h2>Schizo Ramblings</h2>
|
<h2>Schizo Ramblings</h2>
|
||||||
{{.Ramblings}}
|
<ul>
|
||||||
|
{{range .Ramblings}}
|
||||||
|
<li><a href="{{.Link}}">{{.Title}}</a></li>
|
||||||
|
{{end}}
|
||||||
|
</ul>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
Reference in New Issue
Block a user