diff --git a/server.go b/server.go
index 4b7164c..fc420c8 100644
--- a/server.go
+++ b/server.go
@@ -142,6 +142,7 @@ func router() *mux.Router {
router := mux.NewRouter()
router.HandleFunc("/", static).Methods("GET")
router.HandleFunc("/favicon.ico", static).Methods("GET")
+ router.HandleFunc("/play.png", static).Methods("GET")
router.HandleFunc("/site.css", static).Methods("GET")
entries, err := ioutil.ReadDir("public")
check(err)
diff --git a/templates/example.tmpl b/templates/example.tmpl
index 3ad84f3..66dc55d 100644
--- a/templates/example.tmpl
+++ b/templates/example.tmpl
@@ -20,6 +20,7 @@
+

{{range .Segs}}
{{range .}}
diff --git a/templates/play.png b/templates/play.png
new file mode 100644
index 0000000..b450204
Binary files /dev/null and b/templates/play.png differ
diff --git a/tools/generate.go b/tools/generate.go
index 3aceaa3..1286bb5 100644
--- a/tools/generate.go
+++ b/tools/generate.go
@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/russross/blackfriday"
"io/ioutil"
+ "net/http"
"os"
"os/exec"
"path/filepath"
@@ -123,13 +124,43 @@ type Seg struct {
}
type Example struct {
- Id, Name string
- Segs [][]*Seg
- NextExample *Example
+ Id, Name string
+ GoCode, GoCodeHash, UrlHash string
+ Segs [][]*Seg
+ NextExample *Example
}
-func parseSegs(sourcePath string) []*Seg {
+func parseHashFile(sourcePath string) (string, string) {
+ var codehash,urlkey string
lines := readLines(sourcePath)
+ for idx,line := range lines {
+ switch idx {
+ case 0:
+ codehash = line
+ case 1:
+ urlkey = line
+ }
+ }
+ return codehash,urlkey
+}
+
+func resetUrlHashFile(codehash, code, sourcePath string) string {
+ payload := strings.NewReader(code)
+ resp, err := http.Post("http://play.golang.org/share", "text/plain", payload)
+ if err != nil {
+ // handle error
+ }
+ defer resp.Body.Close()
+ body, err := ioutil.ReadAll(resp.Body)
+ urlkey := string(body)
+ data := fmt.Sprintf("%s\n%s",codehash,urlkey)
+ ioutil.WriteFile(sourcePath,[]byte(data),0644)
+ return urlkey
+}
+
+func parseSegs(sourcePath string) ([]*Seg, string) {
+ lines := readLines(sourcePath)
+ filecontent := strings.Join(lines, "\n")
segs := []*Seg{}
lastSeen := ""
for _, line := range lines {
@@ -172,21 +203,25 @@ func parseSegs(sourcePath string) []*Seg {
seg.CodeEmpty = (seg.Code == "")
seg.CodeLeading = (i < (len(segs) - 1))
}
- return segs
+ return segs,filecontent
}
-func parseAndRenderSegs(sourcePath string) []*Seg {
- segs := parseSegs(sourcePath)
+func parseAndRenderSegs(sourcePath string) ([]*Seg, string) {
+ segs,filecontent := parseSegs(sourcePath)
lexer := whichLexer(sourcePath)
for _, seg := range segs {
- if seg.Docs != "" {
- seg.DocsRendered = markdown(seg.Docs)
- }
- if seg.Code != "" {
- seg.CodeRendered = cachedPygmentize(lexer, seg.Code)
- }
+ if seg.Docs != "" {
+ seg.DocsRendered = markdown(seg.Docs)
+ }
+ if seg.Code != "" {
+ seg.CodeRendered = cachedPygmentize(lexer, seg.Code)
+ }
}
- return segs
+ // we are only interested in the 'go' code to pass to play.golang.org
+ if lexer != "go" {
+ filecontent = ""
+ }
+ return segs,filecontent
}
func parseExamples() []*Example {
@@ -204,9 +239,21 @@ func parseExamples() []*Example {
example.Segs = make([][]*Seg, 0)
sourcePaths := mustGlob("examples/" + exampleId + "/*")
for _, sourcePath := range sourcePaths {
- sourceSegs := parseAndRenderSegs(sourcePath)
- example.Segs = append(example.Segs, sourceSegs)
+ switch strings.HasSuffix(sourcePath,".hash") {
+ case true:
+ example.GoCodeHash,example.UrlHash = parseHashFile(sourcePath)
+ default:
+ sourceSegs,filecontents := parseAndRenderSegs(sourcePath)
+ if (filecontents != "") {
+ example.GoCode = filecontents
+ }
+ example.Segs = append(example.Segs, sourceSegs)
+ }
}
+ newCodeHash := sha1Sum(example.GoCode)
+ if (example.GoCodeHash != newCodeHash) {
+ example.UrlHash = resetUrlHashFile(newCodeHash,example.GoCode,"examples/"+example.Id+"/"+ example.Id + ".hash")
+ }
examples = append(examples, &example)
}
}
@@ -242,6 +289,7 @@ func main() {
copyFile("templates/site.css", siteDir+"/site.css")
copyFile("templates/favicon.ico", siteDir+"/favicon.ico")
copyFile("templates/404.html", siteDir+"/404.html")
+ copyFile("templates/play.png", siteDir+"/play.png")
examples := parseExamples()
renderIndex(examples)
renderExamples(examples)