From 13c5a2ab438f2496370ef10e7df8096e30962b66 Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Mon, 3 Jul 2017 13:41:00 +0100 Subject: paste.Retrieve(). Small changes to the template --- templ.go | 89 ++++++++++++++++++++++++++++------------------------------------ 1 file changed, 39 insertions(+), 50 deletions(-) (limited to 'templ.go') diff --git a/templ.go b/templ.go index 2e018da..f12fbd6 100644 --- a/templ.go +++ b/templ.go @@ -14,9 +14,9 @@ * . * * (c) Vincenzo "KatolaZ" Nicosia 2017 -- - * - * - * This file is part of "binnit", a minimal no-fuss pastebin-like + * + * + * This file is part of "binnit", a minimal no-fuss pastebin-like * server written in golang * */ @@ -25,83 +25,72 @@ * * minimal Templating support for binit * -*/ + */ package main - import ( - "os" + "errors" "io/ioutil" + "os" "regexp" - "errors" ) +func prepare_paste_page(title, date, content, templ_dir string) (string, error) { -func prepare_paste_page(c *Config, paste_ID string) (string, error) { - - s:= "" + s := "" // insert header - head_file := c.templ_dir + "/header.html" + head_file := templ_dir + "/header.html" f_head, err := os.Open(head_file) defer f_head.Close() - - if err == nil { + + if err == nil { cont, err := ioutil.ReadFile(head_file) - if err == nil{ + if err == nil { s += string(cont) } } - - // insert content - cont_file := c.paste_dir + "/" + paste_ID - f_cont, err := os.Open(cont_file) - defer f_cont.Close() + // insert content - if err == nil { - // Let's read the content of the paste + // ...Let's read the template + templ_file := templ_dir + "/templ.html" + f_templ, err := os.Open(templ_file) + defer f_templ.Close() - cont, err := ioutil.ReadFile(cont_file) - if err == nil { - paste_buf := string(cont) - - // ...Let's read the template - templ_file := c.templ_dir + "/templ.html" - f_templ, err := os.Open(templ_file) - defer f_templ.Close() - - cont, err := ioutil.ReadFile(templ_file) - if err == nil { - tmpl := string(cont) - // ...and replace {{CONTENT}} with the paste itself! - re,_ := regexp.Compile("{{CONTENT}}") - tmpl = string(re.ReplaceAll([]byte(tmpl), []byte(paste_buf))) - - s += tmpl - - } else { - return "", errors.New("Error opening template file") - } - - } else { - return "", errors.New("Error opening paste") - } + + if cont, err := ioutil.ReadFile(templ_file); err == nil { + tmpl := string(cont) + // ...and replace {{CONTENT}} with the paste itself! + re, _ := regexp.Compile("{{TITLE}}") + tmpl = string(re.ReplaceAll([]byte(tmpl), []byte(title))) + + re, _ = regexp.Compile("{{DATE}}") + tmpl = string(re.ReplaceAll([]byte(tmpl), []byte(date))) + + re, _ = regexp.Compile("{{CONTENT}}") + tmpl = string(re.ReplaceAll([]byte(tmpl), []byte(content))) + + s += tmpl + + } else { + return "", errors.New("Error opening template file") } + // insert footer - foot_file := c.templ_dir + "/footer.html" + foot_file := templ_dir + "/footer.html" f_foot, err := os.Open(foot_file) defer f_foot.Close() - if err == nil { + if err == nil { cont, err := ioutil.ReadFile(foot_file) - if err == nil{ + if err == nil { s += string(cont) } } - + return s, nil } -- cgit v1.2.3