Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed external resources #90

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use data urls for every resource
  • Loading branch information
ArmanMazdaee committed Mar 28, 2019
commit 6116b5258e47e33609841f578a6d737002e9e37d
8 changes: 2 additions & 6 deletions example.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<html><head>
<style>body {
background: blue;
}</style>
<link rel="stylesheet" type="text/css" href="data:text/css; charset=utf-8;base64,Ym9keSB7CiAgICBiYWNrZ3JvdW5kOiBibHVlOwp9"/>
</head>
<body>

Expand All @@ -11,8 +9,6 @@
<p> Powerd By <a href="https://github.com/resumic/schema">resumic</a></p>
<img src="data:image/jpeg;base64,/9j/4QBWRXhpZgAATU0AKgAAAAgABAESAAMAAAABAAEAAAEaAAUAAAABAAAAPgEbAAUAAAABAAAARgEoAAMAAAABAAIAAAAAAAAAAAEsAAAAAQAAASwAAAAB/9sAQwABAQEBAQEBAQEBAQEBAQECAQEBAQECAQEBAgICAgICAgICAwMEAwMDAwMCAgMEAwMEBAQEBAIDBQUEBAUEBAQE/9sAQwEBAQEBAQECAQECBAMCAwQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE/8AAEQgAQABAAwERAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1R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QsLg6hJaPZn7YsywecY2tf3wYKY/n20agfqD/wZxf8AJGP24v8Asp/g3/01a3VU+oH9nNaAFAHwz/wU6/5Rzft0/wDZpvj7/wBRnUqUtmB/lM/s36H+wXqvhrX5f2s/iH+1L4O8Xx66I/DNl8Bvhv4Y8a+H7vTfs8RMt7PqeqWksdx55mXZGjJsVDuySBgB9F/8If8A8EXv+i5/8FGf/DCfD3/5o6AD/hD/APgi9/0XP/goz/4YT4e//NHQBheKPCf/AASCh8NeIZfB3xp/4KA33i6LRLqTwtZeIfgf4DsdAu9RWCQ2UV9PF4geWO3aYRrI8aO6oWKqxABAP6pf+DOL/kjH7cP/AGU/wb/6atbrSn1A/s5rQAoA+H/+CmNtcXn/AATu/bjtLSCa6urn9lLx7Bb21tE09xO7eGtRCoiKCWYkgAAZJNKWzA/yxv2bf2jvjX+zL4Z1/wAL+H/2VfgH8UrfxDro1+fVfj/+ynYfGLxDpri3it/s9je30Bkgt8RB/IT5d7u3VjWCYH0b/wAPIv2iv+jBv2Cf/Fevhz/5Gp3YB/w8i/aK/wCjBv2Cf/Fevhz/AORqLsDC8Uf8FA/2hPFXhrxD4Yuf2Fv2ItIt/Eeh3ehT6r4c/YI0DRPEGmJdwSW7XFjeR22+C4jEheKZPmjdVYcgUXA/qG/4M99A13Qfg3+25Hrmi6vo0lx8TfB726atps2nPOF0rWgxQSKu4AkZI6ZFXT6gf2R1oAUAFABQAUAFABQAUAFAH//Z"/>
</div>
<script>document.addEventListener("DOMContentLoaded", function(event) {
console.log("Hi!")
});</script>
<script src="data:application/javascript;base64,ZG9jdW1lbnQuYWRkRXZlbnRMaXN0ZW5lcigiRE9NQ29udGVudExvYWRlZCIsIGZ1bmN0aW9uKGV2ZW50KSB7IAogICAgY29uc29sZS5sb2coIkhpISIpCiAgfSk7"></script>

</body></html>
164 changes: 28 additions & 136 deletions render/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,163 +7,55 @@ import (
"net/url"
"path"

"golang.org/x/net/html/atom"

"golang.org/x/net/html"
)

func findAttributeIndex(attrs []html.Attribute, key string) int {
index := -1
for i := 0; i < len(attrs); i++ {
if attrs[i].Key == key {
index = i
break
}
}
return index
}

func embedStyleNode(node *html.Node, baseURL *url.URL, site hugoSite) error {
hrefAttrIndex := findAttributeIndex(node.Attr, "href")
if hrefAttrIndex == -1 {
return nil
}
hrefAttr := node.Attr[hrefAttrIndex]
contentURL, err := baseURL.Parse(hrefAttr.Val)
if err != nil {
return err
}
if contentURL.Host != baseURL.Host {
return nil
}

content, err := site.readPublic(contentURL)
if err != nil {
return err
}
contentNode := &html.Node{}
contentNode.Type = html.TextNode
contentNode.Data = string(content)

styleNode := &html.Node{}
styleNode.Type = html.ElementNode
styleNode.DataAtom = atom.Style
styleNode.Data = "style"
if index := findAttributeIndex(node.Attr, "media"); index != -1 {
attr := styleNode.Attr[index]
styleNode.Attr = append(styleNode.Attr, attr)
}
if index := findAttributeIndex(node.Attr, "title"); index != -1 {
attr := styleNode.Attr[index]
styleNode.Attr = append(styleNode.Attr, attr)
}

styleNode.AppendChild(contentNode)
node.Parent.InsertBefore(styleNode, node)
node.Parent.RemoveChild(node)
return nil
}

func embedScriptNode(node *html.Node, baseURL *url.URL, site hugoSite) error {
srcAttrIndex := findAttributeIndex(node.Attr, "src")
if srcAttrIndex == -1 {
return nil
}
srcAttr := node.Attr[srcAttrIndex]
contentURL, err := baseURL.Parse(srcAttr.Val)
if err != nil {
return err
}
if contentURL.Host != baseURL.Host {
return nil
}

content, err := site.readPublic(contentURL)
if err != nil {
return err
}
contentNode := &html.Node{}
contentNode.Type = html.TextNode
contentNode.Data = string(content)

node.Attr = append(node.Attr[:srcAttrIndex], node.Attr[srcAttrIndex+1:]...)
node.AppendChild(contentNode)
return nil
}

func embedImgNode(node *html.Node, baseURL *url.URL, site hugoSite) error {
srcAttrIndex := findAttributeIndex(node.Attr, "src")
if srcAttrIndex == -1 {
return nil
}
srcAttr := node.Attr[srcAttrIndex]
imageURL, err := baseURL.Parse(srcAttr.Val)
func makeDataURL(resourceURL *url.URL, site hugoSite) (string, error) {
resourceContent, err := site.readPublic(resourceURL)
if err != nil {
return err
}
if imageURL.Host != baseURL.Host {
return nil
return "", err
}

imageExt := path.Ext(imageURL.Path)
imageMime := mime.TypeByExtension(imageExt)

imageData, err := site.readPublic(imageURL)
if err != nil {
return err
}
imageBase64 := base64.StdEncoding.EncodeToString(imageData)

node.Attr[srcAttrIndex].Val = "data:" + imageMime + ";base64," + imageBase64
return nil
resourceBase64 := base64.StdEncoding.EncodeToString(resourceContent)
resourceExt := path.Ext(resourceURL.Path)
resourceMime := mime.TypeByExtension(resourceExt)
return "data:" + resourceMime + ";base64," + resourceBase64, nil
}

func embedNode(node *html.Node, baseURL *url.URL, site hugoSite) error {
var err error
switch node.DataAtom {
case atom.Link:
relIndex := findAttributeIndex(node.Attr, "rel")
if relIndex == -1 {
break
func embedHTMLNode(node *html.Node, baseURL *url.URL, site hugoSite) error {
if node.Type == html.ElementNode {
for index, attr := range node.Attr {
if attr.Key == "href" || attr.Key == "src" {
resourceURL, err := baseURL.Parse(attr.Val)
if err != nil {
return err
}
if baseURL.Host == resourceURL.Host {
dataURL, err := makeDataURL(resourceURL, site)
if err != nil {
return err
}
node.Attr[index].Val = dataURL
}
}
}
relValue := node.Attr[relIndex].Val

switch relValue {
case "stylesheet":
err = embedStyleNode(node, baseURL, site)
}

case atom.Script:
err = embedScriptNode(node, baseURL, site)

case atom.Img:
err = embedImgNode(node, baseURL, site)
}

if err != nil {
return err
}

for child := node.FirstChild; child != nil; child = child.NextSibling {
if err := embedNode(child, baseURL, site); err != nil {
if err := embedHTMLNode(child, baseURL, site); err != nil {
return err
}
}

return nil
}

func embedHTML(pageURL *url.URL, site hugoSite) ([]byte, error) {
rawHTML, err := site.readPublic(pageURL)
if err != nil {
return nil, err
}

node, err := html.Parse(bytes.NewReader(rawHTML))
func embedHTML(pageHTML []byte, pageURL *url.URL, site hugoSite) ([]byte, error) {
node, err := html.Parse(bytes.NewReader(pageHTML))
if err != nil {
return nil, err
}

if err := embedNode(node, pageURL, site); err != nil {
if err := embedHTMLNode(node, pageURL, site); err != nil {
return nil, err
}

Expand Down
8 changes: 7 additions & 1 deletion render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,11 @@ func RenderHTML(resumeJSON []byte, themeDir string) ([]byte, error) {
if err != nil {
return nil, err
}
return embedHTML(resumeURL, site)

resumeHTML, err := site.readPublic(resumeURL)
if err != nil {
return nil, err
}

return embedHTML(resumeHTML, resumeURL, site)
}