package websearch import ( "io" "strings" "golang.org/x/net/html" "golang.org/x/net/html/atom" ) // noise is markup that is never content: code the browser was meant to run, // and controls pretending to be text. Dropping it is safe on any page. var noise = map[atom.Atom]bool{ atom.Script: true, atom.Style: true, atom.Noscript: true, atom.Template: true, atom.Svg: true, atom.Iframe: true, atom.Form: true, atom.Button: true, atom.Select: true, atom.Textarea: true, } // furniture is the site around the page: menus, banners, the small print. It // is usually most of the bytes and never the reason anyone fetched the URL — // but "usually" is why it can be put back, see HTMLToText. var furniture = map[atom.Atom]bool{ atom.Nav: true, atom.Header: true, atom.Footer: true, atom.Aside: true, } // breaking are elements that end a line. The list is short on purpose: the // point is to keep a document's shape, not to reimplement a renderer. var breaking = map[atom.Atom]bool{ atom.P: true, atom.Br: true, atom.Div: true, atom.Section: true, atom.Article: true, atom.Li: true, atom.Tr: true, atom.Blockquote: true, atom.Pre: true, atom.H1: true, atom.H2: true, atom.H3: true, atom.H4: true, atom.H5: true, atom.H6: true, atom.Table: true, atom.Ul: true, atom.Ol: true, atom.Dd: true, atom.Dt: true, atom.Hr: true, atom.Figcaption: true, } // minUseful is how little text is so little that the reduction is more likely // to have gone wrong than the page to have been empty. const minUseful = 200 // HTMLToText reduces a page to the text a reader would see. // // It parses rather than scans, and the difference is not academic. A scanner // has to track which elements are open by counting tags, and real pages do not // balance: go.dev's release notes open five