diff options
| -rw-r--r-- | html/footer.html | 2 | ||||
| -rw-r--r-- | html/header.html | 11 | ||||
| -rw-r--r-- | html/templ.html | 7 | ||||
| -rw-r--r-- | main.go | 12 | ||||
| -rw-r--r-- | templ.go | 84 | 
5 files changed, 115 insertions, 1 deletions
| diff --git a/html/footer.html b/html/footer.html new file mode 100644 index 0000000..9743df7 --- /dev/null +++ b/html/footer.html @@ -0,0 +1,2 @@ + +</html> diff --git a/html/header.html b/html/header.html new file mode 100644 index 0000000..0cdb721 --- /dev/null +++ b/html/header.html @@ -0,0 +1,11 @@ +<html> +  <head> +    <title>binit</title> +    <style type="text/css"> +      pre {margin-left: 50px; border-left: solid 3px #0000ff; padding: +      5px;} +    </style> +  </head> +   + +   diff --git a/html/templ.html b/html/templ.html new file mode 100644 index 0000000..2e86f38 --- /dev/null +++ b/html/templ.html @@ -0,0 +1,7 @@ +<body> + +<pre> +{{CONTENT}} +</pre> + +</body> @@ -52,7 +52,15 @@ func handle_get_paste(w http.ResponseWriter, r *http.Request) {  	} else {  		// 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) +			//http.ServeFile(w, r, paste_name) +			s, err := prepare_paste_page(&p_conf, orig_name) +			if err == nil { +				fmt.Fprintf(w, "%s", s) +				return +			} else { +				fmt.Fprintf(w, "Error recovering paste '%s'\n", orig_name) +				return +			}  		} else {  			// otherwise, we give say we didn't find it  			fmt.Fprintf(w, "Paste '%s' not found\n", orig_name) @@ -142,6 +150,8 @@ func main() {  		fmt.Fprintf(os.Stderr, "Error opening logfile: %s. Exiting\n", p_conf.log_fname)  		os.Exit(1)  	} +	defer f.Close() +  	log.SetOutput(io.Writer(f))  	log.SetPrefix("[binit]: ") diff --git a/templ.go b/templ.go new file mode 100644 index 0000000..72656bd --- /dev/null +++ b/templ.go @@ -0,0 +1,84 @@ +/* +* +* Templating support for binit +* +*/ + +package main + + +import ( +	"os" +	"io/ioutil" +	"regexp" +	"errors" +) + + +func prepare_paste_page(c *Config, paste_ID string) (string, error) { + +	s:= "" + +	// insert header + +	head_file := c.templ_dir + "/header.html" + +	f_head, err := os.Open(head_file) +	defer f_head.Close() +	 +	if  err == nil { +		cont, err := ioutil.ReadFile(head_file) +		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() + +	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 +			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") +		} +	} +	// insert footer +	foot_file := c.templ_dir + "/footer.html" +	f_foot, err := os.Open(foot_file) +	defer f_foot.Close() + +	if  err == nil { +		cont, err := ioutil.ReadFile(foot_file) +		if err == nil{ +			s += string(cont) +		} +	} +	 +	return s, nil +} | 
