Simple XSL stylesheet for sitemaps

This commit is contained in:
Jan-Lukas Else 2021-08-04 15:45:58 +02:00
parent 7d3cfe784a
commit ba4c32b40b
5 changed files with 61 additions and 23 deletions

2
go.mod
View File

@ -45,7 +45,7 @@ require (
github.com/spf13/cast v1.4.0
github.com/spf13/viper v1.8.1
github.com/stretchr/testify v1.7.0
github.com/tdewolff/minify/v2 v2.9.20
github.com/tdewolff/minify/v2 v2.9.21
github.com/thoas/go-funk v0.9.0
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80
github.com/vcraescu/go-paginator v1.0.1-0.20201114172518-2cfc59fe05c2

4
go.sum
View File

@ -346,8 +346,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tdewolff/minify/v2 v2.9.20 h1:Fut7w3T7nWfDOb/bOgyEvshQRRMt+xzi1T7spEEKXDw=
github.com/tdewolff/minify/v2 v2.9.20/go.mod h1:PoDBts2L7sCwUT28vTAlozGeD6qxjrrihtin4bR/RMM=
github.com/tdewolff/minify/v2 v2.9.21 h1:nO4s1PEMy7aRjlIlbr3Jgr+bJby8QYuifa2Vs2f9lh4=
github.com/tdewolff/minify/v2 v2.9.21/go.mod h1:PoDBts2L7sCwUT28vTAlozGeD6qxjrrihtin4bR/RMM=
github.com/tdewolff/parse/v2 v2.5.19 h1:Kjaj3KQOx/4elIxlBSglus4E2oMfdROphvbq2b+OBZ0=
github.com/tdewolff/parse/v2 v2.5.19/go.mod h1:WzaJpRSbwq++EIQHYIRTpbYKNA3gn9it1Ik++q4zyho=
github.com/tdewolff/test v1.0.6 h1:76mzYJQ83Op284kMT+63iCNCI7NEERsIN8dLM+RiKr4=

View File

@ -3,6 +3,7 @@ package main
import (
"bytes"
"database/sql"
"encoding/xml"
"net/http"
"github.com/araddon/dateparse"
@ -28,7 +29,7 @@ func (a *goBlog) serveSitemap(w http.ResponseWriter, r *http.Request) {
})
}
// Write sitemap
a.writeSitemapIndex(w, sm)
a.writeSitemapXML(w, sm)
}
func (a *goBlog) serveSitemapBlog(w http.ResponseWriter, r *http.Request) {
@ -46,7 +47,7 @@ func (a *goBlog) serveSitemapBlog(w http.ResponseWriter, r *http.Request) {
Loc: a.getFullAddress(a.getRelativePath(b, sitemapBlogPostsPath)),
})
// Write sitemap
a.writeSitemapIndex(w, sm)
a.writeSitemapXML(w, sm)
}
func (a *goBlog) serveSitemapBlogFeatures(w http.ResponseWriter, r *http.Request) {
@ -101,7 +102,7 @@ func (a *goBlog) serveSitemapBlogFeatures(w http.ResponseWriter, r *http.Request
})
}
// Write sitemap
a.writeSitemap(w, sm)
a.writeSitemapXML(w, sm)
}
func (a *goBlog) serveSitemapBlogArchives(w http.ResponseWriter, r *http.Request) {
@ -144,7 +145,7 @@ func (a *goBlog) serveSitemapBlogArchives(w http.ResponseWriter, r *http.Request
})
}
// Write sitemap
a.writeSitemap(w, sm)
a.writeSitemapXML(w, sm)
}
// Serve sitemap with all the blog's posts
@ -167,23 +168,17 @@ func (a *goBlog) serveSitemapBlogPosts(w http.ResponseWriter, r *http.Request) {
sm.Add(item)
}
// Write sitemap
a.writeSitemap(w, sm)
a.writeSitemapXML(w, sm)
}
func (a *goBlog) writeSitemap(w http.ResponseWriter, sm *sitemap.Sitemap) {
var buf bytes.Buffer
sm.WriteTo(&buf)
a.writeSitemapXML(w, &buf)
}
func (a *goBlog) writeSitemapIndex(w http.ResponseWriter, sm *sitemap.SitemapIndex) {
var buf bytes.Buffer
sm.WriteTo(&buf)
a.writeSitemapXML(w, &buf)
}
func (a *goBlog) writeSitemapXML(w http.ResponseWriter, buf *bytes.Buffer) {
func (a *goBlog) writeSitemapXML(w http.ResponseWriter, sm interface{}) {
w.Header().Set(contentType, contenttype.XMLUTF8)
var buf bytes.Buffer
buf.WriteString(xml.Header)
buf.WriteString(`<?xml-stylesheet type="text/xsl" href="`)
buf.WriteString(a.assetFileName("sitemap.xsl"))
buf.WriteString(`" ?>`)
xml.NewEncoder(&buf).Encode(sm)
a.min.Write(w, contenttype.XML, buf.Bytes())
}

View File

@ -51,12 +51,17 @@ func (a *goBlog) compileAsset(name string) (string, error) {
m := a.min.Get()
switch ext {
case ".js":
content, err = m.Bytes("application/javascript", content)
content, err = m.Bytes(contenttype.JS, content)
if err != nil {
return "", err
}
case ".css":
content, err = m.Bytes("text/css", content)
content, err = m.Bytes(contenttype.CSS, content)
if err != nil {
return "", err
}
case ".xml", ".xsl":
content, err = m.Bytes(contenttype.XML, content)
if err != nil {
return "", err
}

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9" exclude-result-prefixes="sitemap">
<xsl:output method="html" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<html>
<body>
<table>
<tbody>
<xsl:for-each select="sitemap:sitemapindex/sitemap:sitemap">
<tr>
<td>
<a href="{sitemap:loc}">
<xsl:value-of select="sitemap:loc" />
</a>
</td>
<td>
<xsl:value-of select="sitemap:lastmod" />
</td>
</tr>
</xsl:for-each>
<xsl:for-each select="sitemap:urlset/sitemap:url">
<tr>
<td>
<a href="{sitemap:loc}">
<xsl:value-of select="sitemap:loc" />
</a>
</td>
<td>
<xsl:value-of select="sitemap:lastmod" />
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>