Browse Source

templating added

master
Christian Müller 8 years ago
parent
commit
8f12a1a691
  1. 7
      assets/footer.html
  2. 36
      assets/markdown/Demo.md
  3. 2
      assets/public/index.html
  4. 7
      assets/public/style.css
  5. 16
      assets/template.html
  6. 21
      assets/templates/note.html
  7. 19
      assets/templates/page.html
  8. 29
      server.go

7
assets/footer.html

@ -1,7 +0,0 @@
<footer>
<a href="/">&#8962; notehub</a> &middot;
<a href="%LINK%/stats">statistics</a> &middot;
<a href="%LINK%/edit">edit</a> &middot;
<a href="%LINK%/export">export</a> &middot;
<a href="/TOS.md">terms of service</a>
</footer>

36
assets/markdown/Demo.md

@ -1,36 +0,0 @@
# Demo Note
## Text Formatting
This is a _short_ note demonstrating the **capabilities** of Markdown. [Markdown](http://en.wikipedia.org/wiki/Markdown) is a _markup language_ with plain text
formatting syntax. But you also can use <u>standard HTML</u> tags.
## Backquotes, Lists & Code
This is a backquote:
> _"Our greatest glory is not in never falling but in rising every time we fall."_
> — Confucius
To create simple lists, just enumerate all items using a dash in the prefix:
- Alpha
- Beta
- Gamma
Also you can either mark some special `words` or write entire `code` blocks:
(defn fact [n]
(if (< n 2) 1
(* n (fact (- n 1)))))
## Tables
Also simple tables is a piece of cake:
Column 1 | Column 2 | Column 3
--- | --- | ---
Text 1 | Text 3 | <s>Text 5</s>
Text 2 | Text 4 | <mark>Text 6</mark>
Take a look at the [source code](/Demo.md/export) of this page, to see how it works.

2
assets/public/index.html

@ -18,7 +18,7 @@
<article class="bottom-space"> <article class="bottom-space">
<h2>Changelog</h2> <h2>Changelog</h2>
<ul> <ul>
<li><strong>2017-08</strong>: NoteHub 3.0 released: rewritten in Go; all features but essential dropped.</li> <li><strong>2017-08</strong>: NoteHub 3.0.</li>
<li><strong>2016-03</strong>: Note deletion feature added.</li> <li><strong>2016-03</strong>: Note deletion feature added.</li>
<li><strong>2015-10</strong>: NoteHub rewritten in Node.js.</li> <li><strong>2015-10</strong>: NoteHub rewritten in Node.js.</li>
<li><strong>2015-10</strong>: NoteHub API and note styling discontinued due to low adoption.</li> <li><strong>2015-10</strong>: NoteHub API and note styling discontinued due to low adoption.</li>

7
assets/public/style.css

@ -10,6 +10,7 @@ html, body {
color: #b0b0b0; color: #b0b0b0;
background: #353a3a; background: #353a3a;
font-family: monospace; font-family: monospace;
font-size: 1.1em;
} }
#hero { #hero {
@ -36,7 +37,6 @@ html, body {
} }
a { a {
text-decoration: none;
color: #097; color: #097;
} }
@ -45,7 +45,7 @@ a:hover {
} }
a:visited { a:visited {
color: #075; color: #086;
} }
.button { .button {
@ -103,7 +103,8 @@ footer a {
} }
h1, h2, h3, h4, h5, h6 { h1, h2, h3, h4, h5, h6 {
font-weight: 400; font-weight: bold;
font-family: "Arial";
} }
h1 { h1 {

16
assets/template.html

@ -1,16 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>NoteHub &mdash; %TITLE%</title>
<meta charset="UTF-8" />
<meta content="width=device-width, initial-scale=1.0" name="viewport" />
<link href="/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
%HEADER%
<article>
%CONTENT%
</article>
%FOOTER%
</body>
</html>

21
assets/templates/note.html

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<title>NoteHub &mdash; {{.Title}}</title>
<meta charset="UTF-8" />
<meta content="width=device-width, initial-scale=1.0" name="viewport" />
<link href="/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<article>
{{.Content}}
</article>
<footer>
<a href="/">&#8962; notehub</a> &middot;
<a href="{{.ID}}/stats">statistics</a> &middot;
<a href="{{.ID}}/edit">edit</a> &middot;
<a href="{{.ID}}/export">export</a> &middot;
<a href="/TOS.md">terms of service</a>
</footer>
</body>
</html>

19
assets/templates/page.html

@ -0,0 +1,19 @@
{{define "Page"}}
<!DOCTYPE html>
<html>
<head>
<title>NoteHub &mdash; {{.Title}}</title>
<meta charset="UTF-8" />
<meta content="width=device-width, initial-scale=1.0" name="viewport" />
<link href="/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<article>
{{.Content}}
</article>
<footer>
<a href="/">&#8962; notehub</a>
</footer>
</body>
</html>
{{end}}

29
server.go

@ -1,6 +1,8 @@
package main package main
import ( import (
"html/template"
"io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -8,26 +10,31 @@ import (
"github.com/russross/blackfriday" "github.com/russross/blackfriday"
) )
type Template struct{ templates *template.Template }
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func main() { func main() {
e := echo.New() e := echo.New()
e.Renderer = &Template{templates: template.Must(template.ParseGlob("assets/templates/*.html"))}
e.Static("/", "assets/public") e.Static("/", "assets/public")
e.GET("/TOS.md", func(c echo.Context) error { return c.Render(http.StatusOK, "Page", md2html(c, "TOS")) })
e.GET("/Demo.md", func(c echo.Context) error { return c.String(http.StatusOK, mdPage(c, "Demo")) })
e.GET("/TOS.md", func(c echo.Context) error { return c.String(http.StatusOK, mdPage(c, "TOS")) })
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Logger.Fatal(e.Start(":3000")) e.Logger.Fatal(e.Start(":3000"))
} }
func mdPage(c echo.Context, name string) string { type Note struct {
ID, Title string
Content template.HTML
}
func md2html(c echo.Context, name string) *Note {
path := "assets/markdown/" + name + ".md" path := "assets/markdown/" + name + ".md"
mdContent, err := ioutil.ReadFile(path) mdContent, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
c.Logger().Errorf("couldn't open markdown page %q: %v", path, err) c.Logger().Errorf("couldn't open markdown page %q: %v", path, err)
return "" return nil
} }
return string(blackfriday.Run(mdContent)) return &Note{Content: template.HTML(string(blackfriday.Run(mdContent)))}
} }

Loading…
Cancel
Save