Commit a32140cf authored by Ronny Bangsund's avatar Ronny Bangsund

Fuller functionality.

-Some styling changes
-Recent posts show up, max 10
-Newly created scraps were not added to the global list
parent 380ec18f
......@@ -60,7 +60,7 @@ func (s Scrap) save() bool {
}
defer file.Close()
lang := s.Language
file.WriteString(fmt.Sprintf("%s\n%d\n", lang, s.Expiry))
file.WriteString(fmt.Sprintf("%s\n%d\n%s\n", lang, s.Expiry, s.User))
name = "./data/" + s.Language + "/" + s.Hash
file, err = os.OpenFile(name, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
......@@ -85,6 +85,8 @@ func loadScrap(hash string) Scrap {
s.Language = scanner.Text()
scanner.Scan()
expiry, _ := strconv.ParseInt(scanner.Text(), 10, 64)
scanner.Scan()
s.User = scanner.Text()
s.Expiry = expiry
if s.hasExpired() {
return Scrap{}
......
......@@ -36,6 +36,15 @@
<div style="position:absolute;right:0;top:0;width:15%;height:100%;border-style:solid;border-left:2px solid black;border-top:0;border-right:0;border-bottom:0;padding-left:4px;background-color:white;">
<h2>Scraps</h2>
<ul style="display:inline;list-style:none;font-size:18px;">
{{range .Links}}
{{if .Hash}}
<li style="color:green;background-color:#ffa509;width:100%;margin-left:0;padding-left:0;left:0;">
<a href="/{{.Hash}}">{{.User}}</a>
</li>
{{end}}
{{end}}
</ul>
</div>
<script src="/prism.js"></script>
</body>
......
......@@ -14,14 +14,16 @@ type IndexPage struct {
View bool
Language string
Content string
Links []ScrapLink
}
type ScrapLink struct {
Hash string
Creator string
Hash string
User string
}
var indexTemplate, _ = template.ParseFiles("tpl/index.tpl")
var links = []ScrapLink{}
func initWeb() {
info("Launching web service.")
......@@ -37,6 +39,7 @@ func endWeb() {
func get_index(ctx *web.Context, p string) {
ctx.SetHeader("Content-type", "text/html", true)
buildLinks()
title := cfg.Main.Sitename
name := cfg.Main.Sitename
view := false
......@@ -44,6 +47,7 @@ func get_index(ctx *web.Context, p string) {
var lang string
if p != "" {
s = loadScrap(sane(p))
info("Links: %d (%d)", len(links), len(scraps))
if !s.hasExpired() {
view = true
lang = s.Language
......@@ -56,6 +60,7 @@ func get_index(ctx *web.Context, p string) {
View: view,
Language: lang,
Content: s.Content,
Links: links,
})
}
......@@ -89,9 +94,22 @@ func post_scrap(ctx *web.Context) {
}
s.Hash = s.getHash()
if s.save() {
scraps[s.Hash] = s
info("Saved scrap %s.", s.Hash)
} else {
info("Error saving scrap %s.", s.Hash)
}
ctx.Redirect(303, "/"+s.Hash)
}
func buildLinks() {
i := 0
links = []ScrapLink{}
for _, v := range scraps {
links = append(links, ScrapLink{v.Hash, v.User})
i++
if i > 9 {
break
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment