summaryrefslogtreecommitdiffhomepage
path: root/transform
diff options
context:
space:
mode:
authorRyan Martinsen <[email protected]>2014-12-18 14:59:39 -0500
committerbep <[email protected]>2014-12-26 16:24:28 +0100
commit7dacc999f8c0bb2554ddae7a256928b4c499403b (patch)
tree24607e8a66ba172ecd2206b188fe172a45afb2c7 /transform
parentf5a3fb149fff3a44c2b3be8a0c46d02594d755c8 (diff)
downloadhugo-7dacc999f8c0bb2554ddae7a256928b4c499403b.tar.gz
hugo-7dacc999f8c0bb2554ddae7a256928b4c499403b.zip
Add ability to canonify URLs in rendered XML output.
Diffstat (limited to 'transform')
-rw-r--r--transform/absurl.go25
-rw-r--r--transform/chain_test.go14
2 files changed, 39 insertions, 0 deletions
diff --git a/transform/absurl.go b/transform/absurl.go
index 6fb1cb4a1..0a0cd7239 100644
--- a/transform/absurl.go
+++ b/transform/absurl.go
@@ -31,6 +31,31 @@ func AbsURL(absURL string) (trs []link, err error) {
return
}
+func AbsURLInXML(absURL string) (trs []link, err error) {
+ var baseURL *url.URL
+
+ if baseURL, err = url.Parse(absURL); err != nil {
+ return
+ }
+
+ base := strings.TrimRight(baseURL.String(), "/")
+
+ var (
+ srcedq = []byte(" src=&#34;" + base + "/")
+ hrefedq = []byte(" href=&#34;" + base + "/")
+ srcesq = []byte(" src=&#39;" + base + "/")
+ hrefesq = []byte(" href=&#39;" + base + "/")
+ )
+ trs = append(trs, func(content []byte) []byte {
+ content = guardReplace(content, []byte(" src=&#34;//"), []byte(" src=&#34;/"), srcedq)
+ content = guardReplace(content, []byte(" src=&#39;//"), []byte(" src=&#39;/"), srcesq)
+ content = guardReplace(content, []byte(" href=&#34;//"), []byte(" href=&#34;/"), hrefedq)
+ content = guardReplace(content, []byte(" href=&#39;//"), []byte(" href=&#39;/"), hrefesq)
+ return content
+ })
+ return
+}
+
func guardReplace(content, guard, match, replace []byte) []byte {
if !bytes.Contains(content, guard) {
content = bytes.Replace(content, match, replace, -1)
diff --git a/transform/chain_test.go b/transform/chain_test.go
index 94135f6d7..936a6a1ad 100644
--- a/transform/chain_test.go
+++ b/transform/chain_test.go
@@ -9,10 +9,18 @@ const H5_JS_CONTENT_ABS_URL_WITH_NAV = "<!DOCTYPE html><html><head><script src=\
const CORRECT_OUTPUT_SRC_HREF_WITH_NAV = "<!DOCTYPE html><html><head><script src=\"http://two/foobar.js\"></script></head><body><nav><ul><li hugo-nav=\"section_0\"></li><li hugo-nav=\"section_1\"></li></ul></nav><article>content <a href=\"http://two/foobar\">foobar</a>. Follow up</article></body></html>"
+const H5_XML_CONTENT_ABS_URL = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?><feed xmlns=\"http://www.w3.org/2005/Atom\"><entry><content type=\"html\">&lt;p&gt;&lt;a href=&#34;/foobar&#34;&gt;foobar&lt;/a&gt;&lt;/p&gt; &lt;p&gt;A video: &lt;iframe src=&#39;/foo&#39;&gt;&lt;/iframe&gt;&lt;/p&gt;</content></entry></feed>"
+
+const CORRECT_OUTPUT_SRC_HREF_IN_XML = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?><feed xmlns=\"http://www.w3.org/2005/Atom\"><entry><content type=\"html\">&lt;p&gt;&lt;a href=&#34;http://xml/foobar&#34;&gt;foobar&lt;/a&gt;&lt;/p&gt; &lt;p&gt;A video: &lt;iframe src=&#39;http://xml/foo&#39;&gt;&lt;/iframe&gt;&lt;/p&gt;</content></entry></feed>"
+
var two_chain_tests = []test{
{H5_JS_CONTENT_ABS_URL_WITH_NAV, CORRECT_OUTPUT_SRC_HREF_WITH_NAV},
}
+var xml_abs_url_tests = []test{
+ {H5_XML_CONTENT_ABS_URL, CORRECT_OUTPUT_SRC_HREF_IN_XML},
+}
+
func TestChainZeroTransformers(t *testing.T) {
tr := NewChain()
in := new(bytes.Buffer)
@@ -31,3 +39,9 @@ func BenchmarkChain(b *testing.B) {
apply(b.Errorf, tr, two_chain_tests)
}
}
+
+func TestXMLAbsUrl(t *testing.T) {
+ absURLInXML, _ := AbsURLInXML("http://xml")
+ tr := NewChain(absURLInXML...)
+ apply(t.Errorf, tr, xml_abs_url_tests)
+}