summaryrefslogtreecommitdiff
path: root/templ.go
blob: 8c290fa1ec1026ebf27dedc755729ec126cfcb52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 *  This program is free software: you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 *  (c) Vincenzo "KatolaZ" Nicosia 2017 -- <katolaz@freaknet.org>
 *
 *
 *  This file is part of "binnit", a minimal no-fuss pastebin-like
 *  server written in golang
 *
 */

/*
*
* minimal Templating support for binit
*
 */

package main

import (
	"errors"
	"io/ioutil"
	"os"
	"regexp"
	"strings"
	"strconv"
)

func format_rows(content string) (string) {

	var ret string

	lines := strings.Split(content, "\n")

	ret += "<table class='content'>"
	
	for l_num, l := range lines {
		ret += "<tr>\n"
		ret += "<td class='lineno'><pre>"+ strconv.Itoa(l_num+1) + "</pre></td>"
		ret += "<td class='line'><pre>"+ l +"</pre></td>"
		ret += "</tr>"
	}
	ret += "</table>"
	return ret
}


func prepare_paste_page(title, date, content, templ_dir string) (string, error) {

	s := ""

	// insert header

	head_file := 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

	// ...Let's read the template
	templ_file := templ_dir + "/templ.html"
	f_templ, err := os.Open(templ_file)
	defer f_templ.Close()

	
	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(format_rows(content))))

		re, _ = regexp.Compile("{{RAW_CONTENT}}")
		tmpl = string(re.ReplaceAll([]byte(tmpl), []byte(content)))

		s += tmpl
		
	} else {
		return "", errors.New("Error opening template file")
	}
	
	// insert footer
	foot_file := 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
}