paste.Retrieve(). Small changes to the template

master
KatolaZ 8 years ago
parent 8f5b41695d
commit 13c5a2ab43
  1. 11
      binnit.go
  2. 7
      html/templ.html
  3. 25
      paste/paste.go
  4. 43
      templ.go

@ -61,7 +61,6 @@ func min (a, b int) int {
func handle_get_paste(w http.ResponseWriter, r *http.Request) { func handle_get_paste(w http.ResponseWriter, r *http.Request) {
var paste_name, orig_name string var paste_name, orig_name string
var err error
orig_name = filepath.Clean(r.URL.Path) orig_name = filepath.Clean(r.URL.Path)
paste_name = p_conf.paste_dir + "/" + orig_name paste_name = p_conf.paste_dir + "/" + orig_name
@ -75,9 +74,11 @@ func handle_get_paste(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, p_conf.templ_dir + "/index.html") http.ServeFile(w, r, p_conf.templ_dir + "/index.html")
} else { } else {
// otherwise, if the requested paste exists, we serve it... // otherwise, if the requested paste exists, we serve it...
if _, err = os.Stat(paste_name); err == nil && orig_name != "./" {
//http.ServeFile(w, r, paste_name) title, date, content, err := paste.Retrieve(paste_name)
s, err := prepare_paste_page(&p_conf, orig_name)
if err == nil {
s, err := prepare_paste_page(title, date, content, p_conf.templ_dir)
if err == nil { if err == nil {
fmt.Fprintf(w, "%s", s) fmt.Fprintf(w, "%s", s)
return return
@ -87,7 +88,7 @@ func handle_get_paste(w http.ResponseWriter, r *http.Request) {
} }
} else { } else {
// otherwise, we give say we didn't find it // otherwise, we give say we didn't find it
fmt.Fprintf(w, "Paste '%s' not found\n", orig_name) fmt.Fprintf(w, "%s\n", err)
return return
} }
} }

@ -1,5 +1,12 @@
<body> <body>
<div>
Title: {{TITLE}}
</div>
<div>
Date: {{DATE}}
</div>
<pre> <pre>
{{CONTENT}} {{CONTENT}}
</pre> </pre>

@ -7,6 +7,8 @@ import(
"os" "os"
"io/ioutil" "io/ioutil"
"errors" "errors"
"bufio"
"strings"
) )
@ -44,9 +46,28 @@ func Store(title, date, content, dest_dir string) (string, error) {
} }
//func Retrieve(URI string) (title, date, content string) { func Retrieve(URI string) (title, date, content string, err error) {
f_cont, err := os.Open(URI)
defer f_cont.Close()
//} if err == nil {
stuff := bufio.NewScanner(f_cont)
// The first line contains the title
stuff.Scan()
title = strings.Trim(strings.Split(stuff.Text(), ":")[1], " ")
stuff.Scan()
date = strings.Trim(strings.Join(strings.Split(stuff.Text(), ":")[1:], ":"), " ")
for stuff.Scan() {
content += stuff.Text()
}
} else {
return "", "", "", errors.New("Cannot retrieve paste!!!")
}
return title, date, content, nil
}

@ -29,22 +29,20 @@
package main package main
import ( import (
"os" "errors"
"io/ioutil" "io/ioutil"
"os"
"regexp" "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 // insert header
head_file := c.templ_dir + "/header.html" head_file := templ_dir + "/header.html"
f_head, err := os.Open(head_file) f_head, err := os.Open(head_file)
defer f_head.Close() defer f_head.Close()
@ -58,28 +56,23 @@ func prepare_paste_page(c *Config, paste_ID string) (string, error) {
// insert content // insert content
cont_file := c.paste_dir + "/" + paste_ID
f_cont, err := os.Open(cont_file)
defer f_cont.Close()
if err == nil {
// Let's read the content of the paste
cont, err := ioutil.ReadFile(cont_file)
if err == nil {
paste_buf := string(cont)
// ...Let's read the template // ...Let's read the template
templ_file := c.templ_dir + "/templ.html" templ_file := templ_dir + "/templ.html"
f_templ, err := os.Open(templ_file) f_templ, err := os.Open(templ_file)
defer f_templ.Close() defer f_templ.Close()
cont, err := ioutil.ReadFile(templ_file)
if err == nil { if cont, err := ioutil.ReadFile(templ_file); err == nil {
tmpl := string(cont) tmpl := string(cont)
// ...and replace {{CONTENT}} with the paste itself! // ...and replace {{CONTENT}} with the paste itself!
re,_ := regexp.Compile("{{CONTENT}}") re, _ := regexp.Compile("{{TITLE}}")
tmpl = string(re.ReplaceAll([]byte(tmpl), []byte(paste_buf))) 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 s += tmpl
@ -87,12 +80,8 @@ func prepare_paste_page(c *Config, paste_ID string) (string, error) {
return "", errors.New("Error opening template file") return "", errors.New("Error opening template file")
} }
} else {
return "", errors.New("Error opening paste")
}
}
// insert footer // insert footer
foot_file := c.templ_dir + "/footer.html" foot_file := templ_dir + "/footer.html"
f_foot, err := os.Open(foot_file) f_foot, err := os.Open(foot_file)
defer f_foot.Close() defer f_foot.Close()

Loading…
Cancel
Save