Commit 16fb59e7 authored by Ronny Bangsund's avatar Ronny Bangsund

Actual functionality.

-Posted scraps are shown when their hashes are added to the index URL
-Prism.js adds syntax highlighting
-Small bugfixes
parent f8352b1d
......@@ -29,7 +29,9 @@ type Scrap struct {
func (s Scrap) hasExpired() bool {
if s.Expiry < time.Now().Unix() {
info("%s has expired.", s.Hash)
if s.Hash != "" {
info("%s has expired.", s.Hash)
}
s.delete()
return true
}
......@@ -57,7 +59,6 @@ func (s Scrap) save() bool {
return false
}
defer file.Close()
info("%s, %d", s.Language, s.Expiry)
lang := s.Language
file.WriteString(fmt.Sprintf("%s\n%d\n", lang, s.Expiry))
name = "./data/" + s.Language + "/" + s.Hash
......@@ -71,13 +72,14 @@ func (s Scrap) save() bool {
return true
}
func (s Scrap) load() bool {
file, err := os.Open("./data/" + s.Hash)
func loadScrap(hash string) Scrap {
file, err := os.Open("./data/" + hash)
if err != nil {
info("Error: %s.", err.Error())
return false
return Scrap{}
}
defer file.Close()
s := Scrap{Hash: hash}
scanner := bufio.NewScanner(file)
scanner.Scan()
s.Language = scanner.Text()
......@@ -85,9 +87,12 @@ func (s Scrap) load() bool {
expiry, _ := strconv.ParseInt(scanner.Text(), 10, 64)
s.Expiry = expiry
if s.hasExpired() {
return false
return Scrap{}
}
return true
var content []byte
content, _ = ioutil.ReadFile("./data/" + s.Language + "/" + s.Hash)
s.Content = fmt.Sprintf("%s", content)
return s
}
func (s Scrap) delete() {
......@@ -106,10 +111,12 @@ func loadScraps() {
}
for _, fi := range files {
if !fi.IsDir() {
s := Scrap{Hash: fi.Name()}
if s.load() {
scraps[s.Hash] = s
info("Scrap %s loaded.", fi.Name())
s := loadScrap(fi.Name())
if s.Content != "" {
if !s.hasExpired() {
scraps[s.Hash] = s
info("Scrap %s loaded.", fi.Name())
}
}
}
}
......
......@@ -3,21 +3,23 @@
<title>{{.Title}}</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="author" content="Neural Short-Circuit">
<link rel="stylesheet" href="/prism.css" />
</head>
<body style="background-color:#A7DBFF;">
<div style="position:absolute; top:1%; left:2%; width:80%;">
<div style="position:absolute;top:1%;left:2%;width:80%;">
<h1>{{.Sitename}}</h1>
{{if .View}}
<p>Moo!</p>
<div style="position:relative;color:white;background-color:black;width:100%;height:400px;overflow:auto;">
<pre><code class="language-{{.Language}}" style="height:400px;white-space: pre-wrap;">{{.Content}}</code></pre>
</div>
{{end}}
<form name="scrap" style="position:relative; left:-9px; top:0;" method="post" action="/submit">
<form name="scrap" style="position:relative;left:-9px;top:0;" method="post" action="/submit">
<select name="expiry" style="position:relative; left:10px; top:0;">
<option value="burn" >Burn after reading</option>
<option value="hour" >An hour</option>
<option value="day" >A day</option>
<option value="week" >A week</option>
</select>
<select name="language" style="position:relative; left:10px; top:0;">
<select name="language" style="position:relative;left:10px;top:0;">
{{with .}}
{{range $k,$v := .Languages}}
<option value="{{$k}}">{{$v}}</option>
......@@ -35,5 +37,6 @@
<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>
</div>
<script src="/prism.js"></script>
</body>
</html>
......@@ -12,6 +12,7 @@ type IndexPage struct {
Sitename string
Languages map[string]string
View bool
Language string
Content string
}
......@@ -36,22 +37,26 @@ func get_index(ctx *web.Context, p string) {
title := cfg.Main.Sitename
name := cfg.Main.Sitename
view := false
var s Scrap
var lang string
if p != "" {
ctx.WriteString("Posted:")
view = true
s = loadScrap(sane(p))
if !s.hasExpired() {
view = true
lang = s.Language
}
}
var content string
indexTemplate.Execute(ctx.ResponseWriter, &IndexPage{
Title: title,
Sitename: name,
Languages: languages,
View: view,
Content: content,
Language: lang,
Content: s.Content,
})
}
func post_scrap(ctx *web.Context) {
ctx.Redirect(303, "/")
p := ctx.Params
lang := sane(p["language"])
if !validLanguage(lang) {
......@@ -85,4 +90,5 @@ func post_scrap(ctx *web.Context) {
} else {
info("Error saving scrap %s.", s.Hash)
}
ctx.Redirect(303, "/"+s.Hash)
}
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