initial changes to generate play buttons, ref #37

This commit is contained in:
Mark McGranaghan 2013-09-11 08:40:30 -07:00
parent b19fd80ef5
commit 0c18c9689a
4 changed files with 66 additions and 16 deletions

View File

@ -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)

View File

@ -20,6 +20,7 @@
<body>
<div class="example" id="{{.Id}}">
<h2><a href="./">Go by Example</a>: {{.Name}}</h2>
<a href="http://play.golang.org/p/{{$.UrlHash}}"><img height="16" width="16" title="Run code" src="play.png" style="float:right" /></a>
{{range .Segs}}
<table>
{{range .}}

BIN
templates/play.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

View File

@ -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)