Skip to content

Commit

Permalink
parse url to png
Browse files Browse the repository at this point in the history
  • Loading branch information
weaming committed Nov 1, 2018
1 parent c8a2f6f commit 9d88a1a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 28 deletions.
2 changes: 1 addition & 1 deletion fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"io/ioutil"
"log"
"os"
"path"
"log"
)

func ExistFile(filename string) bool {
Expand Down
2 changes: 1 addition & 1 deletion listFlag.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"strings"
"fmt"
"strings"
)

type ArrayFlags []string
Expand Down
90 changes: 64 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"

"github.com/golang-commonmark/markdown"
Expand All @@ -27,39 +31,44 @@ func main() {

print := flag.Bool("print", false, "print generated html")
includeCSS := flag.Bool("include-css", false, "include css file as html header when read content from *.html file")
url := flag.String("url", "", "capture screenshot of the url")
flag.Parse()

if *cssName != "" {
cssUrlList = append(cssUrlList, getCssUrl(*cssName))
}

if *outputPath == "" {
*outputPath = ReplaceExt(path.Base(*markdownPath), "png")
}

//prepare static files
//go staticServer(*staticPath)
html := ""
if *url != "" {
html = GetHTMLFromURL(*url)
} else {
if *cssName != "" {
cssUrlList = append(cssUrlList, getCssUrl(*cssName))
}

header := ""
if *htmlFile == "" || *includeCSS {
header = `<meta http-equiv="content-Type" content="text/html; charset=UTF-8" />`
for _, f := range cssFileList {
header += renderCssPath(f)
if *outputPath == "" {
*outputPath = ReplaceExt(path.Base(*markdownPath), "png")
}
for _, u := range cssUrlList {
header += renderCssUrl(u)

//prepare static files
//go staticServer(*staticPath)

header := ""
if *htmlFile == "" || *includeCSS {
header = `<meta http-equiv="content-Type" content="text/html; charset=UTF-8" />`
for _, f := range cssFileList {
header += renderCssPath(f)
}
for _, u := range cssUrlList {
header += renderCssUrl(u)
}
}
}

html := ""
if *htmlFile == "" {
md := ReadFile(*markdownPath)
html = fmt.Sprintf("%v\n\n<div class='main container content article'>\n%v\n</div>", header, markdown2html(md))
} else {
if *includeCSS {
html = header + ReadFile(*htmlFile)
if *htmlFile == "" {
md := ReadFile(*markdownPath)
html = fmt.Sprintf("%v\n\n<div class='main container content article'>\n%v\n</div>", header, markdown2html(md))
} else {
html = ReadFile(*htmlFile)
if *includeCSS {
html = header + ReadFile(*htmlFile)
} else {
html = ReadFile(*htmlFile)
}
}
}

Expand All @@ -82,6 +91,35 @@ func markdown2html(text string) string {
return md.RenderToString([]byte(text))
}

func GetHTMLFromURL(URL string) string {
u, err := url.Parse(URL)
if err != nil {
panic(err)
}
baseUrl := path.Dir(u.Path)
if baseUrl != "." && baseUrl != "" {
u.Path = baseUrl
}
base := u.String()

rv := ""
response, err := http.Get(URL)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
rv += fmt.Sprintf("<head><base href=\"%v\"></head>", base)
rv += string(contents)
}
return rv
}

func (r *ImageRender) generateImage(html, format, output string, width, quality int) []byte {
c := ImageOptions{
BinaryPath: *r.BinaryPath,
Expand Down
2 changes: 2 additions & 0 deletions wkhtmltoimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ func buildParams(options *ImageOptions) ([]string, error) {
// might want to add --javascript-delay too?
a = append(a, "-q")
a = append(a, "--disable-plugins")
a = append(a, "--javascript-delay")
a = append(a, "2000")

// support unicode
a = append(a, "--encoding")
Expand Down

0 comments on commit 9d88a1a

Please sign in to comment.