// pruneContent checks the URL to see if it is a site that is calling for
// content pruning. If so, it parses the HTML, removes the specified tags, and
// re-renders the HTML. It returns true if the content was changed. The content
// may be pre-parsed and passed in as tree; the final parse tree will be stored
// in tree.
pruneContent checks the URL to see if it is a site that is calling for
content pruning. If so, it parses the HTML, removes the specified tags, and
re-renders the HTML. It returns true if the content was changed. The content
may be pre-parsed and passed in as tree; the final parse tree will be stored
in tree.
// pruneContent checks the URL to see if it is a site that is calling for // content pruning . If so it parses the HTML removes the specified tags and // re - renders the HTML . It returns true if the content was changed . The content // may be pre - parsed and passed in as tree ; the final parse tree will be stored // in tree .
func (c *config) pruneContent(URL *url.URL, content *[]byte, cs string, tree **html.Node) bool {
	URLMatches := c.PruneMatcher.MatchingRules(URL)
	if len(URLMatches) == 0 {
		return false
	}
	if *tree == nil {
		doc, err := parseHTML(*content, cs)
		if err != nil {
			log.Printf("Error parsing html from %s: %s", URL, err)
			return false
		}
		*tree = doc
	}
	toDelete := map[*html.Node]bool{}
	var selectors []selector
	for urlRule := range URLMatches {
		if sel, ok := c.PruneActions[urlRule]; ok {
			prune(*tree, sel, toDelete)
			selectors = append(selectors, sel)
		}
	}
	if len(toDelete) == 0 {
		return false
	}
	prune(*tree, metaCharsetSelector, toDelete)
	for n := range toDelete {
		n.Parent.RemoveChild(n)
	}
	css := new(bytes.Buffer)
	for _, sel := range selectors {
		fmt.Fprintf(css, "%s { display: none !important }\n", sel)
	}
	style := &html.Node{
		Type: html.ElementNode,
		Data: "style",
	}
	style.AppendChild(&html.Node{
		Type: html.TextNode,
		Data: string(css.Bytes()),
	})
	head := headSelector.MatchFirst(*tree)
	if head != nil {
		head.AppendChild(style)
	}
	b := new(bytes.Buffer)
	err := html.Render(b, *tree)
	if err != nil {
		log.Printf("Error rendering modified content from %s: %s", URL, err)
		return false
	}
	*content = b.Bytes()
	return true
}
func ( c * config ) pruneContent ( URL * url . URL , content * [ ] byte , cs string , tree * * html . Node ) bool { URLMatches := c . PruneMatcher . MatchingRules ( URL ) if len ( URLMatches ) == 0 { return false } if * tree == nil { doc , err := parseHTML ( * content , cs ) if err != nil { log . Printf ( " " , URL , err ) return false } * tree = doc } toDelete := map [ * html . Node ] bool { } var selectors [ ] selector for urlRule := range URLMatches { if sel , ok := c . PruneActions [ urlRule ] ; ok { prune ( * tree , sel , toDelete ) selectors = append ( selectors , sel ) } } if len ( toDelete ) == 0 { return false } prune ( * tree , metaCharsetSelector , toDelete ) for n := range toDelete { n . Parent . RemoveChild ( n ) } css := new ( bytes . Buffer ) for _ , sel := range selectors { fmt . Fprintf ( css , " \n " , sel ) } style := & html . Node { Type : html . ElementNode , Data : " " , } style . AppendChild ( & html . Node { Type : html . TextNode , Data : string ( css . Bytes ( ) ) , } ) head := headSelector . MatchFirst ( * tree ) if head != nil { head . AppendChild ( style ) } b := new ( bytes . Buffer ) err := html . Render ( b , * tree ) if err != nil { log . Printf ( " " , URL , err ) return false } * content = b . Bytes ( ) return true }
pruneContent checks the URL to see if it is a site that is calling for
content pruning.
pruneContent checks the URL to see if it is a site that is calling for content pruning .
// Remove any meta tag that indicated the charset, since it will be // re-rendered as UTF-8. // Actually delete the nodes that are to be removed. // Add a style element to hide nodes that match the pruned selectors, in case they // get added by Ajax.