Skip to content

Instantly share code, notes, and snippets.

@estroz
Created April 1, 2019 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save estroz/0c7e9b70cca4da592c1acb2d5046c18d to your computer and use it in GitHub Desktop.
Save estroz/0c7e9b70cca4da592c1acb2d5046c18d to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"io/ioutil"
"log"
"os"
"strings"
"golang.org/x/net/html"
)
func main() {
files := os.Args[2:]
output := os.Args[1]
all := &bytes.Buffer{}
for _, f := range files {
b, _ := ioutil.ReadFile(f)
doc, _ := html.Parse(bytes.NewBuffer(b))
removeUselessTags(doc)
temp := &bytes.Buffer{}
html.Render(temp, doc)
if _, err := all.Write(temp.Bytes()); err != nil {
log.Fatal(err)
}
if _, err := all.WriteString("\n\n---\n\n"); err != nil {
log.Fatal(err)
}
}
ioutil.WriteFile(output, all.Bytes(), 0644)
}
func removeUselessTags(n *html.Node) {
for c := n.FirstChild; c != nil; c = c.NextSibling {
if n.Type == html.ElementNode {
switch n.Data {
case "script", "aside", "style", "div":
if n.Data == "div" {
for _, a := range n.Attr {
if a.Key == "class" && strings.Contains(a.Val, "postActions js-postActionsFooter") {
n.RemoveChild(c)
}
}
} else {
n.RemoveChild(c)
}
}
}
removeUselessTags(c)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment