-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"io" | ||
"log" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/sym01/htmlsanitizer" | ||
) | ||
|
||
var ( | ||
srcFilePath = flag.String("src", "", "could be either source file path, or the source URL") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if len(*srcFilePath) == 0 { | ||
flag.CommandLine.Usage() | ||
return | ||
} | ||
|
||
var src io.ReadCloser | ||
switch { | ||
case strings.HasPrefix(*srcFilePath, "http://"), strings.HasPrefix(*srcFilePath, "https://"): | ||
resp, err := http.Get(*srcFilePath) | ||
if err != nil { | ||
log.Fatalf("unable to fetch remote content: %s", err) | ||
} | ||
src = resp.Body | ||
default: | ||
file, err := os.OpenFile(*srcFilePath, os.O_RDONLY, 0755) | ||
if err != nil { | ||
log.Fatalf("unable to open src file: %s", err) | ||
} | ||
src = file | ||
} | ||
|
||
defer src.Close() | ||
|
||
san := htmlsanitizer.NewHTMLSanitizer() | ||
writer := san.NewWriter(os.Stdout) | ||
if _, err := io.Copy(writer, src); err != nil { | ||
log.Printf("unable to sanitize HTML content: %s", err) | ||
} | ||
} |
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