added clipboard functionality with your comments

This commit is contained in:
julien 2017-01-03 17:50:49 +01:00
parent 1b53607cd5
commit dc33273d8a
5 changed files with 34 additions and 3 deletions

BIN
templates/clipboard.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 488 B

View File

@ -28,7 +28,7 @@
{{.DocsRendered}}
</td>
<td class="code{{if .CodeEmpty}} empty{{end}}{{if .CodeLeading}} leading{{end}}">
{{if .CodeRun}}<a href="http://play.golang.org/p/{{$.UrlHash}}"><img title="Run code" src="play.png" class="run" /></a>{{end}}
{{if .CodeRun}}<a href="http://play.golang.org/p/{{$.UrlHash}}"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />{{end}}
{{.CodeRendered}}
</td>
</tr>
@ -44,5 +44,10 @@
by <a href="https://twitter.com/mmcgrana">@mmcgrana</a> | <a href="mailto:mmcgrana@gmail.com">feedback</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/{{.Id}}">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
</p>
</div>
<script>
var codeLines = [];
{{range .Segs}}{{range .}}codeLines.push('{{js .CodeForJs}}'); {{end}}{{end}}
</script>
<script src="site.js" async></script>
</body>
</html>

View File

@ -136,11 +136,14 @@ pre, code {
font-size: 14px; line-height: 18px;
font-family: 'Menlo', 'Monaco', 'Consolas', 'Lucida Console', monospace;
}
img.run {
img.copy, img.run {
height: 16px;
width: 16px;
float: right
}
img.copy {
cursor: pointer;
}
/* Syntax highlighting */
body .hll { background-color: #ffffcc }

17
templates/site.js Normal file

File diff suppressed because one or more lines are too long

View File

@ -120,7 +120,7 @@ var dashPat = regexp.MustCompile("\\-+")
type Seg struct {
Docs, DocsRendered string
Code, CodeRendered string
Code, CodeRendered, CodeForJs string
CodeEmpty, CodeLeading, CodeRun bool
}
@ -205,6 +205,10 @@ func parseAndRenderSegs(sourcePath string) ([]*Seg, string) {
}
if seg.Code != "" {
seg.CodeRendered = cachedPygmentize(lexer, seg.Code)
// adding the content to the js code for copying to the clipboard
if strings.HasSuffix(sourcePath, ".go") {
seg.CodeForJs = strings.Trim(seg.Code, "\n") + "\n"
}
}
}
// we are only interested in the 'go' code to pass to play.golang.org
@ -276,9 +280,11 @@ func renderExamples(examples []*Example) {
func main() {
copyFile("templates/site.css", siteDir+"/site.css")
copyFile("templates/site.js", siteDir+"/site.js")
copyFile("templates/favicon.ico", siteDir+"/favicon.ico")
copyFile("templates/404.html", siteDir+"/404.html")
copyFile("templates/play.png", siteDir+"/play.png")
copyFile("templates/clipboard.png", siteDir+"/clipboard.png")
examples := parseExamples()
renderIndex(examples)
renderExamples(examples)