| 1 | package webread |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/websource" |
| 5 | ) |
| 6 | |
| 7 | // The page reader is websource.ExtractHTML, shared with the instance's |
| 8 | // lookups; webread keeps its own view of what it read. |
| 9 | |
| 10 | type refKind = websource.HTMLRefKind |
| 11 | |
| 12 | const ( |
| 13 | refLink = websource.RefLink |
| 14 | refStylesheet = websource.RefStylesheet |
| 15 | refIcon = websource.RefIcon |
| 16 | refOG = websource.RefOG |
| 17 | refImage = websource.RefImage |
| 18 | ) |
| 19 | |
| 20 | var kindName = map[refKind]string{refLink: "link", refStylesheet: "stylesheet", refIcon: "icon", refOG: "og", refImage: "img"} |
| 21 | |
| 22 | // ref is a URL a page names, resolved against the page (or its base), or |
| 23 | // the refusal resolving it met. logo marks an img whose src, alt, id or |
| 24 | // class says "logo". |
| 25 | type ref struct { |
| 26 | kind refKind |
| 27 | url websource.URL |
| 28 | raw string |
| 29 | err error |
| 30 | logo bool |
| 31 | alt string |
| 32 | } |
| 33 | |
| 34 | type meta struct{ name, content string } |
| 35 | |
| 36 | type page struct { |
| 37 | title string |
| 38 | metas []meta |
| 39 | text string |
| 40 | styles string |
| 41 | refs []ref |
| 42 | } |
| 43 | |
| 44 | // extractHTML reads src, an HTML page fetched from base. |
| 45 | func extractHTML(src []byte, base websource.URL) page { |
| 46 | h := websource.ExtractHTML(src, base, MaxPageText, MaxLinks) |
| 47 | p := page{title: h.Title, text: h.Text, styles: h.Styles} |
| 48 | for _, m := range h.Metas { |
| 49 | p.metas = append(p.metas, meta{m.Name, m.Content}) |
| 50 | } |
| 51 | for _, r := range h.Refs { |
| 52 | p.refs = append(p.refs, ref{kind: r.Kind, url: r.URL, raw: r.Raw, err: r.Err, logo: r.Logo, alt: r.Alt}) |
| 53 | } |
| 54 | return p |
| 55 | } |
| 56 | |
| 57 | // clip cuts s to at most n bytes on a UTF-8 boundary. |
| 58 | func clip(s string, n int) string { return websource.Clip(s, n) } |
| 59 | |
| 60 | // Expansion caps per approved page (spec §3.2). |
| 61 | const ( |
| 62 | maxStylesheets = 5 |
| 63 | maxIcons = 5 |
| 64 | maxOG = 1 |
| 65 | maxLogos = 3 |
| 66 | maxSameHost = 5 |
| 67 | ) |
| 68 | |
| 69 | type candidate struct { |
| 70 | kind refKind |
| 71 | url websource.URL |
| 72 | raw string |
| 73 | err error |
| 74 | } |
| 75 | |
| 76 | // expansions is what an approved HTML page at pageURL adds to the fetch, |
| 77 | // in document order: stylesheets, icons, og:image, logo images and links |
| 78 | // on the same host, each within its cap. A URL that fails the contract is |
| 79 | // kept, as a refusal to log, and counts against its cap; a link to another |
| 80 | // host, or one that fails, is not a candidate at all. |
| 81 | func expansions(p page, pageURL websource.URL) []candidate { |
| 82 | caps := map[refKind]int{refStylesheet: maxStylesheets, refIcon: maxIcons, refOG: maxOG, refImage: maxLogos, refLink: maxSameHost} |
| 83 | seen := map[string]bool{pageURL.String(): true} |
| 84 | var out []candidate |
| 85 | for _, r := range p.refs { |
| 86 | if caps[r.kind] == 0 || (r.kind == refImage && !r.logo) { |
| 87 | continue |
| 88 | } |
| 89 | if r.kind == refLink && (r.err != nil || r.url.Host != pageURL.Host) { |
| 90 | continue |
| 91 | } |
| 92 | if r.err == nil { |
| 93 | if seen[r.url.String()] { |
| 94 | continue |
| 95 | } |
| 96 | seen[r.url.String()] = true |
| 97 | } |
| 98 | caps[r.kind]-- |
| 99 | out = append(out, candidate{kind: r.kind, url: r.url, raw: r.raw, err: r.err}) |
| 100 | } |
| 101 | return out |
| 102 | } |
| 103 | |