initial changes to generate play buttons, ref #37
This commit is contained in:
parent
b19fd80ef5
commit
0c18c9689a
@ -142,6 +142,7 @@ func router() *mux.Router {
|
|||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
router.HandleFunc("/", static).Methods("GET")
|
router.HandleFunc("/", static).Methods("GET")
|
||||||
router.HandleFunc("/favicon.ico", static).Methods("GET")
|
router.HandleFunc("/favicon.ico", static).Methods("GET")
|
||||||
|
router.HandleFunc("/play.png", static).Methods("GET")
|
||||||
router.HandleFunc("/site.css", static).Methods("GET")
|
router.HandleFunc("/site.css", static).Methods("GET")
|
||||||
entries, err := ioutil.ReadDir("public")
|
entries, err := ioutil.ReadDir("public")
|
||||||
check(err)
|
check(err)
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
<body>
|
<body>
|
||||||
<div class="example" id="{{.Id}}">
|
<div class="example" id="{{.Id}}">
|
||||||
<h2><a href="./">Go by Example</a>: {{.Name}}</h2>
|
<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}}
|
{{range .Segs}}
|
||||||
<table>
|
<table>
|
||||||
{{range .}}
|
{{range .}}
|
||||||
|
BIN
templates/play.png
Normal file
BIN
templates/play.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 30 KiB |
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/russross/blackfriday"
|
"github.com/russross/blackfriday"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -123,13 +124,43 @@ type Seg struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Example struct {
|
type Example struct {
|
||||||
Id, Name string
|
Id, Name string
|
||||||
Segs [][]*Seg
|
GoCode, GoCodeHash, UrlHash string
|
||||||
NextExample *Example
|
Segs [][]*Seg
|
||||||
|
NextExample *Example
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseSegs(sourcePath string) []*Seg {
|
func parseHashFile(sourcePath string) (string, string) {
|
||||||
|
var codehash,urlkey string
|
||||||
lines := readLines(sourcePath)
|
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{}
|
segs := []*Seg{}
|
||||||
lastSeen := ""
|
lastSeen := ""
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
@ -172,21 +203,25 @@ func parseSegs(sourcePath string) []*Seg {
|
|||||||
seg.CodeEmpty = (seg.Code == "")
|
seg.CodeEmpty = (seg.Code == "")
|
||||||
seg.CodeLeading = (i < (len(segs) - 1))
|
seg.CodeLeading = (i < (len(segs) - 1))
|
||||||
}
|
}
|
||||||
return segs
|
return segs,filecontent
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAndRenderSegs(sourcePath string) []*Seg {
|
func parseAndRenderSegs(sourcePath string) ([]*Seg, string) {
|
||||||
segs := parseSegs(sourcePath)
|
segs,filecontent := parseSegs(sourcePath)
|
||||||
lexer := whichLexer(sourcePath)
|
lexer := whichLexer(sourcePath)
|
||||||
for _, seg := range segs {
|
for _, seg := range segs {
|
||||||
if seg.Docs != "" {
|
if seg.Docs != "" {
|
||||||
seg.DocsRendered = markdown(seg.Docs)
|
seg.DocsRendered = markdown(seg.Docs)
|
||||||
}
|
}
|
||||||
if seg.Code != "" {
|
if seg.Code != "" {
|
||||||
seg.CodeRendered = cachedPygmentize(lexer, 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 {
|
func parseExamples() []*Example {
|
||||||
@ -204,9 +239,21 @@ func parseExamples() []*Example {
|
|||||||
example.Segs = make([][]*Seg, 0)
|
example.Segs = make([][]*Seg, 0)
|
||||||
sourcePaths := mustGlob("examples/" + exampleId + "/*")
|
sourcePaths := mustGlob("examples/" + exampleId + "/*")
|
||||||
for _, sourcePath := range sourcePaths {
|
for _, sourcePath := range sourcePaths {
|
||||||
sourceSegs := parseAndRenderSegs(sourcePath)
|
switch strings.HasSuffix(sourcePath,".hash") {
|
||||||
example.Segs = append(example.Segs, sourceSegs)
|
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)
|
examples = append(examples, &example)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -242,6 +289,7 @@ func main() {
|
|||||||
copyFile("templates/site.css", siteDir+"/site.css")
|
copyFile("templates/site.css", siteDir+"/site.css")
|
||||||
copyFile("templates/favicon.ico", siteDir+"/favicon.ico")
|
copyFile("templates/favicon.ico", siteDir+"/favicon.ico")
|
||||||
copyFile("templates/404.html", siteDir+"/404.html")
|
copyFile("templates/404.html", siteDir+"/404.html")
|
||||||
|
copyFile("templates/play.png", siteDir+"/play.png")
|
||||||
examples := parseExamples()
|
examples := parseExamples()
|
||||||
renderIndex(examples)
|
renderIndex(examples)
|
||||||
renderExamples(examples)
|
renderExamples(examples)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user