| 1 | package websource |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "encoding/xml" |
| 7 | "errors" |
| 8 | "io" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | // MaxReadable bounds a page's readable text (spec 2026-11-26-assistant- |
| 13 | // abilities §4.3: each page at most 16 KiB before the answer step's |
| 14 | // budget cuts it further). |
| 15 | const MaxReadable = 16 << 10 |
| 16 | |
| 17 | // Readable is a fetched body's title and readable text, by its sniffed |
| 18 | // type (guard.Response.Type): HTML through ExtractHTML (scripts and |
| 19 | // styles dropped), a feed's channel title and each item's title and |
| 20 | // summary (their markup reduced to text), JSON pretty-printed, XML's |
| 21 | // text, plain text as it is. Anything else gives nothing. The text |
| 22 | // is at most MaxReadable bytes, cut on a character boundary. |
| 23 | func Readable(body []byte, typ string, base URL) (title, text string) { |
| 24 | switch typ { |
| 25 | case "html": |
| 26 | p := ExtractHTML(body, base, MaxReadable, 0) |
| 27 | return p.Title, p.Text |
| 28 | case "feed": |
| 29 | return readFeed(body, base) |
| 30 | case "json": |
| 31 | var v any |
| 32 | if json.Unmarshal(body, &v) != nil { |
| 33 | return "", "" |
| 34 | } |
| 35 | out, err := json.MarshalIndent(v, "", " ") |
| 36 | if err != nil { |
| 37 | return "", "" |
| 38 | } |
| 39 | return "", clip(string(out), MaxReadable) |
| 40 | case "xml": |
| 41 | return "", clip(strings.Join(xmlText(body), "\n"), MaxReadable) |
| 42 | case "text": |
| 43 | return "", clip(strings.ToValidUTF8(string(body), ""), MaxReadable) |
| 44 | } |
| 45 | return "", "" |
| 46 | } |
| 47 | |
| 48 | // readFeed reads an RSS or Atom feed: its title, then each item's title |
| 49 | // and summary (RSS description, Atom summary or content), a blank line |
| 50 | // between items. |
| 51 | func readFeed(body []byte, base URL) (string, string) { |
| 52 | d := xml.NewDecoder(bytes.NewReader(body)) |
| 53 | d.Strict = false |
| 54 | d.CharsetReader = func(_ string, r io.Reader) (io.Reader, error) { return r, nil } |
| 55 | var title string |
| 56 | var items []string |
| 57 | var cur []string |
| 58 | var buf strings.Builder |
| 59 | inItem := false |
| 60 | for { |
| 61 | tok, err := d.Token() |
| 62 | if err != nil { |
| 63 | if !errors.Is(err, io.EOF) && len(items) == 0 && title == "" { |
| 64 | return "", "" |
| 65 | } |
| 66 | break |
| 67 | } |
| 68 | switch t := tok.(type) { |
| 69 | case xml.StartElement: |
| 70 | name := strings.ToLower(t.Name.Local) |
| 71 | if name == "item" || name == "entry" { |
| 72 | inItem, cur = true, nil |
| 73 | } |
| 74 | buf.Reset() |
| 75 | case xml.CharData: |
| 76 | buf.Write(t) |
| 77 | case xml.EndElement: |
| 78 | name := strings.ToLower(t.Name.Local) |
| 79 | value := strings.TrimSpace(buf.String()) |
| 80 | buf.Reset() |
| 81 | switch { |
| 82 | case name == "item" || name == "entry": |
| 83 | if len(cur) > 0 { |
| 84 | items = append(items, strings.Join(cur, "\n")) |
| 85 | } |
| 86 | inItem = false |
| 87 | case inItem && (name == "title" || name == "description" || name == "summary" || name == "content"): |
| 88 | // A summary may carry escaped markup: its text only. |
| 89 | if v := ExtractHTML([]byte(value), base, MaxReadable, 0).Text; v != "" { |
| 90 | cur = append(cur, oneLine(v)) |
| 91 | } |
| 92 | case !inItem && name == "title" && title == "": |
| 93 | title = clip(oneLine(value), maxMetaValue) |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | return title, clip(strings.Join(items, "\n\n"), MaxReadable) |
| 98 | } |
| 99 | |
| 100 | // xmlText is a document's text runs, each trimmed, in order. |
| 101 | func xmlText(body []byte) []string { |
| 102 | d := xml.NewDecoder(bytes.NewReader(body)) |
| 103 | d.Strict = false |
| 104 | d.CharsetReader = func(_ string, r io.Reader) (io.Reader, error) { return r, nil } |
| 105 | var out []string |
| 106 | size := 0 |
| 107 | for size < MaxReadable { |
| 108 | tok, err := d.Token() |
| 109 | if err != nil { |
| 110 | break |
| 111 | } |
| 112 | if c, ok := tok.(xml.CharData); ok { |
| 113 | if s := oneLine(string(c)); s != "" { |
| 114 | out = append(out, s) |
| 115 | size += len(s) + 1 |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | return out |
| 120 | } |
| 121 | |