aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/template
diff options
context:
space:
mode:
Diffstat (limited to 'internal/feed/template')
-rw-r--r--internal/feed/template/feed.tpl.go76
-rw-r--r--internal/feed/template/template.go68
2 files changed, 144 insertions, 0 deletions
diff --git a/internal/feed/template/feed.tpl.go b/internal/feed/template/feed.tpl.go
new file mode 100644
index 0000000..3f6470f
--- /dev/null
+++ b/internal/feed/template/feed.tpl.go
@@ -0,0 +1,76 @@
+package template
+
+var Feed = fromString("Feed", feedTpl)
+
+//noinspection HtmlDeprecatedAttribute,HtmlUnknownTarget
+const feedTpl = `{{- /*gotype:github.com/Necoro/feed2imap-go/internal/feed.feeditem*/ -}}
+{{define "bottomLine"}}
+ {{if .content}}
+ <tr>
+ <td align="right">
+ <span style="color: #ababab; ">{{.descr}}</span>&nbsp;&nbsp;
+ </td>
+ <td>
+ <span style="color: #ababab; ">{{.content}}</span>
+ </td>
+ </tr>
+ {{end}}
+{{end}}
+<table border="1" width="100%" cellpadding="0" cellspacing="0" style="border-spacing: 0; ">
+ <tr>
+ <td>
+ <table width="100%" bgcolor="#EDEDED" cellpadding="4" cellspacing="2">
+ <tr>
+ <td align="right"><b>Feed</b></td>
+ <td width="100%">
+ {{with .Feed.Link}}<a href="{{.}}">{{end}}
+ <b>{{or .Feed.Title .Feed.Link "Unnammed feed"}}</b>
+ {{if .Feed.Link}}</a>{{end}}
+ </td>
+ </tr>
+ <tr>
+ <td align="right"><b>Item</b></td>
+ <td width="100%">
+ {{with .Item.Link}}<a href="{{.}}">{{end}}
+ <b>{{or .Item.Title .Item.Link}}</b>
+ {{if .Item.Link}}</a>{{end}}
+ </td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+</table>
+{{with .Item.Description}}
+ <br /> <!-- originally: only if content and 'content !~ /\A\s*</m' -->
+ {{html .}}
+{{end}}
+{{with .Item.Content}}
+ <br /> <!-- originally: only if content and 'content !~ /\A\s*</m' -->
+ {{html .}}
+{{end}}
+{{with .Item.Enclosures}}
+ <table border="1" width="100%" cellpadding="0" cellspacing="0" style="border-spacing: 0; ">
+ <tr>
+ <td>
+ <table width="100%" bgcolor="#EDEDED" cellpadding="2" cellspacing="2">
+ <tr><td width="100%"><b>Files:</b></td></tr>
+ {{range .}}
+ <tr>
+ <td>
+ &nbsp;&nbsp;&nbsp;
+ <a href={{.URL}}>{{.URL | lastUrlPart}}</a> ({{.Length | byteCount}}, {{.Type}})
+ </td>
+ </tr>
+ {{end}}
+ </table>
+ </td>
+ </tr>
+ </table>
+{{end}}
+<hr width="100%"/>
+<table width="100%" cellpadding="0" cellspacing="0">
+ {{template "bottomLine" (dict "descr" "Date:" "content" .Item.Published)}}
+ {{with .Item.Author}}{{template "bottomLine" (dict "descr" "Author:" "content" .Name)}}{{end}}
+ {{template "bottomLine" (dict "descr" "Subject:" "content" .Item.Title)}}
+ {{template "bottomLine" (dict "descr" "Filed under:" "content" (join ", " .Item.Categories))}}
+</table>`
diff --git a/internal/feed/template/template.go b/internal/feed/template/template.go
new file mode 100644
index 0000000..dd31f51
--- /dev/null
+++ b/internal/feed/template/template.go
@@ -0,0 +1,68 @@
+package template
+
+import (
+ "fmt"
+ "html/template"
+ "strconv"
+ "strings"
+
+ "github.com/Necoro/feed2imap-go/internal/log"
+)
+
+func dict(v ...string) map[string]string {
+ dict := map[string]string{}
+ lenv := len(v)
+ for i := 0; i < lenv; i += 2 {
+ key := v[i]
+ if i+1 >= lenv {
+ dict[key] = ""
+ continue
+ }
+ dict[key] = v[i+1]
+ }
+ return dict
+}
+
+func join(sep string, parts []string) string {
+ return strings.Join(parts, sep)
+}
+
+func lastUrlPart(url string) string {
+ split := strings.Split(url, "/")
+ return split[len(split)-1]
+}
+
+func byteCount(str string) string {
+ b, err := strconv.ParseUint(str, 10, 64)
+ if err != nil {
+ log.Printf("Cannot convert '%s' to byte count: %s", str, err)
+ }
+
+ const unit = 1024
+ if b < unit {
+ return fmt.Sprintf("%d B", b)
+ }
+ div, exp := uint64(unit), 0
+ for n := b / unit; n >= unit; n /= unit {
+ div *= unit
+ exp++
+ }
+ return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "KMGTPE"[exp])
+}
+
+func html(s string) template.HTML {
+ return template.HTML(s)
+}
+
+var funcMap = template.FuncMap{
+ "dict": dict,
+ "join": join,
+ "lastUrlPart": lastUrlPart,
+ "byteCount": byteCount,
+ "html": html,
+}
+
+func fromString(name, templateStr string) *template.Template {
+ tpl := template.New(name).Funcs(funcMap)
+ return template.Must(tpl.Parse(templateStr))
+}
393/+513 2008-01-18 r643@Devoty: necoro | 2008-01-16 18:55:49 +0100Necoro14-249/+525 2008-01-14 r634@Devoty: necoro | 2008-01-14 23:48:24 +0100Necoro3-1/+4 2008-01-14 r632@Devoty: necoro | 2008-01-14 23:44:52 +0100Necoro3-175/+181 2008-01-14(no commit message)Necoro10-758/+1014 2008-01-14 r621@Devoty: necoro | 2008-01-14 20:21:40 +0100Necoro2-21/+61 2008-01-14 r618@Devoty: necoro | 2008-01-14 20:19:05 +0100Necoro2-35/+57 2008-01-14 r617@Devoty: necoro | 2008-01-14 19:12:59 +0100Necoro2-10/+10 2008-01-14 r609@Devoty: necoro | 2008-01-14 17:04:38 +0100Necoro9-394/+477 2008-01-14 r605@Devoty: necoro | 2008-01-14 11:43:34 +0100Necoro2-26/+129 2008-01-14 r603@Devoty: necoro | 2008-01-14 11:30:26 +0100Necoro4-26/+38 2008-01-11 r598@Devoty: necoro | 2008-01-10 16:36:29 +0100Necoro4-10/+50 2008-01-11 r597@Devoty: necoro | 2008-01-10 14:12:35 +0100Necoro1-3/+3 2008-01-10 r595@Devoty: necoro | 2008-01-10 04:04:15 +0100Necoro1-52/+3 2008-01-09 r586@Devoty: necoro | 2008-01-09 14:54:18 +0100Necoro1-1/+1 2007-12-06 r577@Devoty: necoro | 2007-12-06 20:37:36 +0100Necoro1-1/+1 2007-12-06 r572@Devoty: necoro | 2007-11-28 08:48:15 +0100Necoro1-0/+1 2007-11-27 r570@Devoty: necoro | 2007-11-27 02:08:21 +0100Necoro3-84/+108