2016-10-10 20:21:46 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2017-01-19 10:31:17 -05:00
|
|
|
"github.com/microcosm-cc/bluemonday"
|
2016-10-10 20:21:46 -04:00
|
|
|
"github.com/russross/blackfriday"
|
|
|
|
)
|
|
|
|
|
2016-10-10 21:30:20 -04:00
|
|
|
// memory usage optimizations
|
|
|
|
const (
|
|
|
|
emtyStr = ""
|
|
|
|
git = "git"
|
|
|
|
checkout = "checkout"
|
|
|
|
force = "-f"
|
|
|
|
pull = "pull"
|
|
|
|
|
|
|
|
// options
|
|
|
|
readmePath = "./README.md"
|
|
|
|
tplPath = "tmpl/tmpl.html"
|
|
|
|
idxPath = "tmpl/index.html"
|
|
|
|
|
|
|
|
bfHTMLRendererOpts = 0 |
|
|
|
|
blackfriday.HTML_USE_XHTML |
|
|
|
|
blackfriday.HTML_USE_SMARTYPANTS |
|
|
|
|
blackfriday.HTML_SMARTYPANTS_FRACTIONS |
|
|
|
|
blackfriday.HTML_SMARTYPANTS_DASHES |
|
|
|
|
blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
|
|
|
|
|
|
|
|
bfMDOpts = 0 |
|
|
|
|
blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
|
|
|
|
blackfriday.EXTENSION_TABLES |
|
|
|
|
blackfriday.EXTENSION_FENCED_CODE |
|
|
|
|
blackfriday.EXTENSION_AUTOLINK |
|
|
|
|
blackfriday.EXTENSION_STRIKETHROUGH |
|
|
|
|
blackfriday.EXTENSION_SPACE_HEADERS |
|
|
|
|
blackfriday.EXTENSION_HEADER_IDS |
|
|
|
|
blackfriday.EXTENSION_BACKSLASH_LINE_BREAK |
|
|
|
|
blackfriday.EXTENSION_DEFINITION_LISTS |
|
|
|
|
blackfriday.EXTENSION_AUTO_HEADER_IDS
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
doneResp = []byte("Done!\n")
|
|
|
|
)
|
|
|
|
|
2016-10-10 20:21:46 -04:00
|
|
|
type content struct {
|
|
|
|
Body string
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateHTML() {
|
|
|
|
// Update repo
|
2016-10-10 21:30:20 -04:00
|
|
|
exec.Command(git, checkout, force).Output()
|
|
|
|
exec.Command(git, pull).Output()
|
2016-10-10 20:21:46 -04:00
|
|
|
|
2016-10-10 21:30:20 -04:00
|
|
|
input, _ := ioutil.ReadFile(readmePath)
|
|
|
|
body := string(
|
2017-01-19 10:31:17 -05:00
|
|
|
bluemonday.UGCPolicy().SanitizeBytes(
|
|
|
|
blackfriday.Markdown(
|
|
|
|
input,
|
|
|
|
blackfriday.HtmlRenderer(
|
|
|
|
bfHTMLRendererOpts,
|
|
|
|
emtyStr,
|
|
|
|
emtyStr,
|
|
|
|
),
|
|
|
|
bfMDOpts,
|
2016-10-10 21:30:20 -04:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2016-10-10 20:21:46 -04:00
|
|
|
c := &content{Body: body}
|
|
|
|
|
2016-10-10 21:30:20 -04:00
|
|
|
t := template.Must(template.ParseFiles(tplPath))
|
|
|
|
f, _ := os.Create(idxPath)
|
2016-10-10 20:21:46 -04:00
|
|
|
t.Execute(f, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func hookHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
go generateHTML()
|
2016-10-10 21:30:20 -04:00
|
|
|
w.Write(doneResp)
|
2016-10-10 20:21:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
r := mux.NewRouter()
|
|
|
|
r.HandleFunc("/hook", hookHandler)
|
|
|
|
http.ListenAndServe(":9000", r)
|
|
|
|
}
|