Skip to content

Instantly share code, notes, and snippets.

@fumin
Created July 5, 2014 04:09
Show Gist options
  • Save fumin/4e0aba4d9d6bdaf3b28e to your computer and use it in GitHub Desktop.
Save fumin/4e0aba4d9d6bdaf3b28e to your computer and use it in GitHub Desktop.
Example of using the present package
package main
import (
"bytes"
"html/template"
"net/http"
"code.google.com/p/go.tools/present"
)
var blogTmpl *template.Template
func init() {
blogTmpl = template.Must(present.Template().Parse(blogTmplStr))
}
// blogTmplStr defines how elements are turned into HTML. It is heavily based on
// https://code.google.com/p/go/source/browse/template/doc.tmpl?repo=blog
const blogTmplStr = `
{{/* This doc template is given to the present tool to format articles. */}}
{{define "root"}}
<body>
{{with .Subtitle}}<h2>{{.}}</h2>{{end}}
{{range .Sections}}
{{elem $.Template .}}
{{end}}
</body>
{{end}}
{{define "TOC"}}
<ul>
{{range .}}
<li><a href="#TOC_{{.FormattedNumber}}">{{.Title}}</a></li>
{{with .Sections}}{{template "TOC" .}}{{end}}
{{end}}
</ul>
{{end}}
{{define "newline"}}
{{/* No automatic line break. Paragraphs are free-form. */}}
{{end}}
{{define "section"}}
{{if eq .Level 2}}
<h3 id="TOC_{{.FormattedNumber}}">{{.Title}}</h4>
{{range .Elem}}{{elem $.Template .}}{{end}}
{{else}}
<h4 id="TOC_{{.FormattedNumber}}">{{.Title}}</h4>
{{range .Elem}}{{elem $.Template .}}{{end}}
{{end}}
{{end}}
{{define "list"}}
<ul>
{{range .Bullet}}
<li>{{style .}}</li>
{{end}}
</ul>
{{end}}
{{define "text"}}
{{if .Pre}}
<div class="code"><pre>{{range .Lines}}{{.}}{{end}}</pre></div>
{{else}}
<p>
{{range $i, $l := .Lines}}{{if $i}}{{template "newline"}}
{{end}}{{style $l}}{{end}}
</p>
{{end}}
{{end}}
{{define "code"}}
{{if .Play}}
<div class="playground">{{.Text}}</div>
{{else}}
<div class="code">{{.Text}}</div>
{{end}}
{{end}}
{{define "image"}}
<div class="image">
<img src="{{.URL}}"{{with .Height}} height="{{.}}"{{end}}{{with .Width}} width="{{.}}"{{end}}>
</div>
{{end}}
{{define "iframe"}}
<div class="iframe">
<iframe src="{{.URL}}"{{with .Height}} height="{{.}}"{{end}}{{with .Width}} width="{{.}}"{{end}} frameborder="0" allowfullscreen mozallowfullscreen webkitallowfullscreen></iframe>
</div>
{{end}}
{{define "link"}}<p class="link"><a href="{{.URL}}" target="_blank">{{style .Label}}</a></p>{{end}}
{{define "html"}}{{.HTML}}{{end}}
`
func root(w http.ResponseWriter, r *http.Request) {
test := `
Title of document
@awawfumin
* My first document
Drinks are good!
May god bless you~
** Subsection
<script>alert("you are screwed!");</script>
- a one
- a two
- a three
More text, perhaps a story.
* dfsdfadf
soidfjsodifj
- 111
- 222
- 3333
.image http://stellarglobal.com/webdata/images/2013/09/dinningtable.png
Again, some more text for you to read...
`
b := bytes.NewBufferString(test)
doc, err := present.Parse(b, "test", 0)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ob := bytes.NewBuffer([]byte{})
err = doc.Render(ob, blogTmpl)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Write(ob.Bytes())
}
func main() {
http.HandleFunc("/", root)
http.ListenAndServe(":3000", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment