mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
A main menu item, News, weather, quotes (actions news, headlines, weather, quotes for sysops' menus): - internal/feeds: RSS 0.9x-2.0, RSS 1.0, Atom and JSON Feed read into read-only message areas (so also NNTP, QWK, the web forum, gopher), each item its headline, summary and link; conditional GETs, Retry-After, one post per item by a Message-ID made from its id. A catalogue of 26 tested feeds: world news, business (SEC), technology, science (NASA, USGS), government (Federal Register, Congress.gov, White House, NWS). [feeds], [feed NAME]; bbs feeds catalog|list|read. - internal/weather: each caller's place (US ZIP codes built in from the Census gazetteer, towns worldwide by Open-Meteo's gazetteer, or lat,lon); NWS forecasts and alerts in the US, Open-Meteo forecasts elsewhere, USGS earthquakes near; serious warnings told at the login. [weather]. - internal/quotes: an IEX-TP/TOPS decoder, pcap and pcapng readers, IEX's free HIST file of the last session read as it downloads (16 GB, under five minutes, resumed when cut off) into a small snapshot, or a live TOPS feed over UDP multicast. Each caller's list of symbols. [quotes]; bbs quotes fetch|load|show. Docs: docs/wiki/News-Weather-and-Quotes.md, the sample, the reference. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRTDuJZo7JNHQEC6M1Y
109 lines
2.8 KiB
Go
109 lines
2.8 KiB
Go
package feeds
|
|
|
|
import (
|
|
"html"
|
|
"strings"
|
|
|
|
xhtml "golang.org/x/net/html"
|
|
)
|
|
|
|
// blocks are the HTML elements that end a paragraph.
|
|
var blocks = map[string]bool{"p": true, "div": true, "br": true, "li": true, "tr": true, "h1": true,
|
|
"h2": true, "h3": true, "h4": true, "h5": true, "h6": true, "blockquote": true, "pre": true,
|
|
"ul": true, "ol": true, "table": true, "hr": true, "figure": true, "section": true, "article": true,
|
|
"dl": true, "dt": true, "dd": true}
|
|
|
|
// Text is a feed's text (often HTML, sometimes HTML escaped twice) as
|
|
// plain text: paragraphs kept, a blank line between them, spaces runs made
|
|
// one, pictures, scripts and styles left out.
|
|
func Text(s string) string { return text(s, 2) }
|
|
|
|
func text(s string, depth int) string {
|
|
if !strings.ContainsAny(s, "<&") {
|
|
return tidy(s)
|
|
}
|
|
z := xhtml.NewTokenizer(strings.NewReader(s))
|
|
var b strings.Builder
|
|
skip := 0
|
|
for {
|
|
tt := z.Next()
|
|
switch tt {
|
|
case xhtml.ErrorToken:
|
|
out := tidy(b.String())
|
|
// text that was HTML escaped twice: once more
|
|
if depth > 0 && strings.Contains(out, "<") && strings.Contains(out, ">") && out != s {
|
|
return text(out, depth-1)
|
|
}
|
|
return out
|
|
case xhtml.TextToken:
|
|
if skip == 0 {
|
|
// the tokenizer has unescaped it: escaped again for tidy
|
|
b.WriteString(html.EscapeString(string(z.Text())))
|
|
}
|
|
case xhtml.StartTagToken, xhtml.EndTagToken, xhtml.SelfClosingTagToken:
|
|
name, _ := z.TagName()
|
|
tag := string(name)
|
|
switch tag {
|
|
case "script", "style", "noscript":
|
|
if tt == xhtml.StartTagToken {
|
|
skip++
|
|
} else if tt == xhtml.EndTagToken && skip > 0 {
|
|
skip--
|
|
}
|
|
}
|
|
if blocks[tag] {
|
|
b.WriteString("\n\n")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// tidy makes runs of spaces one and keeps at most one blank line between
|
|
// paragraphs.
|
|
func tidy(s string) string {
|
|
s = html.UnescapeString(s)
|
|
var paras []string
|
|
for _, p := range strings.Split(strings.ReplaceAll(s, "\r", ""), "\n\n") {
|
|
if p = strings.Join(strings.Fields(p), " "); p != "" {
|
|
paras = append(paras, p)
|
|
}
|
|
}
|
|
return strings.Join(paras, "\n\n")
|
|
}
|
|
|
|
// Wrap breaks text into lines of at most width characters, at spaces
|
|
// where it can; paragraphs keep a blank line between them.
|
|
func Wrap(s string, width int) []string {
|
|
var out []string
|
|
for i, p := range strings.Split(s, "\n\n") {
|
|
if i > 0 {
|
|
out = append(out, "")
|
|
}
|
|
line := ""
|
|
for _, w := range strings.Fields(p) {
|
|
for len([]rune(w)) > width {
|
|
if line != "" {
|
|
out = append(out, line)
|
|
line = ""
|
|
}
|
|
r := []rune(w)
|
|
out = append(out, string(r[:width]))
|
|
w = string(r[width:])
|
|
}
|
|
switch {
|
|
case line == "":
|
|
line = w
|
|
case len([]rune(line))+1+len([]rune(w)) <= width:
|
|
line += " " + w
|
|
default:
|
|
out = append(out, line)
|
|
line = w
|
|
}
|
|
}
|
|
if line != "" {
|
|
out = append(out, line)
|
|
}
|
|
}
|
|
return out
|
|
}
|