From fcb702f219eca2b8b41007fbe2d2fab4c5282c45 Mon Sep 17 00:00:00 2001 From: Artemis Date: Sat, 19 Nov 2022 15:47:41 +0100 Subject: [PATCH] feat: added sanitize and strings.trimspace/join template functions --- go.mod | 3 +++ go.sum | 6 ++++++ templates.go | 18 +++++++++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 7018dc8..15070dd 100644 --- a/go.mod +++ b/go.mod @@ -23,9 +23,12 @@ require ( github.com/akrennmair/slice v0.0.0-20220105203817-49445747ab81 // indirect github.com/alexflint/go-scalar v1.1.0 // indirect github.com/andybalholm/cascadia v1.3.1 // indirect + github.com/aymerick/douceur v0.2.0 // indirect github.com/go-playground/locales v0.14.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect + github.com/gorilla/css v1.0.0 // indirect github.com/leodido/go-urn v1.2.1 // indirect + github.com/microcosm-cc/bluemonday v1.0.21 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/smartystreets/goconvey v1.7.2 // indirect golang.org/x/crypto v0.0.0-20220919173607-35f4265a4bc0 // indirect diff --git a/go.sum b/go.sum index 1f854af..5ae8bb6 100644 --- a/go.sum +++ b/go.sum @@ -15,6 +15,8 @@ github.com/alexflint/go-scalar v1.1.0 h1:aaAouLLzI9TChcPXotr6gUhq+Scr8rl0P9P4Pnl github.com/alexflint/go-scalar v1.1.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o= github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c= github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= +github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk= +github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= github.com/blevesearch/segment v0.9.0 h1:5lG7yBCx98or7gK2cHMKPukPZ/31Kag7nONpoBt22Ac= github.com/blevesearch/segment v0.9.0/go.mod h1:9PfHYUdQCgHktBgvtUOF4x+pc4/l8rdH0u5spnW85UQ= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -32,12 +34,16 @@ github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaU github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY= +github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c= github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= +github.com/microcosm-cc/bluemonday v1.0.21 h1:dNH3e4PSyE4vNX+KlRGHT5KrSvjeUkoNPwEORjffHJg= +github.com/microcosm-cc/bluemonday v1.0.21/go.mod h1:ytNkv4RrDrLJ2pqlsSI46O6IVXmZOBBD4SaJyDwwTkM= github.com/pelletier/go-toml/v2 v2.0.2 h1:+jQXlF3scKIcSEKkdHzXhCTDLPFi5r1wnK6yPS+49Gw= github.com/pelletier/go-toml/v2 v2.0.2/go.mod h1:MovirKjgVRESsAvNZlAjtFwV867yGuwRkXbG66OzopI= github.com/pelletier/go-toml/v2 v2.0.5 h1:ipoSadvV8oGUjnUbMub59IDPPwfxF694nG/jwbMiyQg= diff --git a/templates.go b/templates.go index 247ab89..1edd26c 100644 --- a/templates.go +++ b/templates.go @@ -1,16 +1,22 @@ package main import ( - "path" "path/filepath" "sort" + "strings" "text/template" "git.sr.ht/~artemis/cap/domain" + "github.com/microcosm-cc/bluemonday" ) // templateFuncs contains every anonymous function made available in template rendering. var templateFuncs = template.FuncMap{ + // Standard functions + "join": strings.Join, + "trim": strings.TrimSpace, + // Custom functions + "sanitize": Sanitize(), "get_root_section": GetRootSection, "get_subsection": GetSubsection, "all_pages": GetAllPages, @@ -32,6 +38,16 @@ func (f *FsBuilder) LoadTemplates() error { return nil } +// Sanitize sanitizes the HTML given as input. +// Uses the https://pkg.go.dev/github.com/microcosm-cc/bluemonday library +func Sanitize() func(string) string { + p := bluemonday.StrictPolicy() + + return func(s string) string { + return p.Sanitize(s) + } +} + // GetRootSection takes a section, and tries to explore it to find the first section pointing to the capsule root. func GetRootSection(section *domain.Section) *domain.Section { if section.Parent == nil { -- 2.45.2