Created
April 1, 2019 03:15
-
-
Save estroz/0c7e9b70cca4da592c1acb2d5046c18d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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