diff options
author | Andrew Gerrand <adg@golang.org> | 2012-10-04 16:53:05 +1000 |
---|---|---|
committer | Andrew Gerrand <adg@golang.org> | 2012-10-04 16:53:05 +1000 |
commit | 3fd5e0be9dd321e990e0322ca173149505197e82 (patch) | |
tree | e1964c907c1caff2c175d4f51850b89596e3c6c4 /lib | |
parent | 65dbe6786d3c97deb534d024f21301c445fad7ac (diff) | |
download | go-git-3fd5e0be9dd321e990e0322ca173149505197e82.tar.gz |
godoc: make examples editable and runnable in playground
R=dsymonds
CC=golang-dev
https://golang.org/cl/6523045
Diffstat (limited to 'lib')
-rw-r--r-- | lib/godoc/example.html | 23 | ||||
-rw-r--r-- | lib/godoc/package.html | 42 |
2 files changed, 60 insertions, 5 deletions
diff --git a/lib/godoc/example.html b/lib/godoc/example.html index ede31d61f9..a6df54be6f 100644 --- a/lib/godoc/example.html +++ b/lib/godoc/example.html @@ -5,11 +5,24 @@ <div class="expanded"> <p class="exampleHeading toggleButton">▾ <span class="text">Example{{example_suffix .Name}}</span></p> {{with .Doc}}<p>{{html .}}</p>{{end}} - <p>Code:</p> - <pre class="code">{{.Code}}</pre> - {{with .Output}} - <p>Output:</p> - <pre class="output">{{html .}}</pre> + {{$output := .Output}} + {{with .Play}} + <div class="play"> + <div class="input"><textarea class="code">{{.}}</textarea></div> + <div class="output"><pre>{{html $output}}</pre></div> + <div class="buttons"> + <a class="run" title="Run this code [shift-enter]">Run</a> + <a class="fmt" title="Format this code">Format</a> + <a class="share" title="Share this code">Share</a> + </div> + </div> + {{else}} + <p>Code:</p> + <pre class="code">{{.Code}}</pre> + {{with .Output}} + <p>Output:</p> + <pre class="output">{{html .}}</pre> + {{end}} {{end}} </div> </div> diff --git a/lib/godoc/package.html b/lib/godoc/package.html index 3c0dfa41bf..c5152741ec 100644 --- a/lib/godoc/package.html +++ b/lib/godoc/package.html @@ -222,3 +222,45 @@ <p>Need more packages? Take a look at the <a href="http://godashboard.appspot.com/">Go Project Dashboard</a>.</p> {{end}} {{end}} + +{{if $.Examples}} +<script type="text/javascript" src="/doc/play/playground.js"></script> +<script> +$(document).ready(function() { + 'use strict'; + // Set up playground when each element is toggled. + $('div.play').each(function (i, el) { + var built = false; + $(el).closest('.toggle').click(function() { + // Only set up playground once. + if (built) { + return; + } + built = true; + + // Set up playground. + var code = $('.code', el); + playground({ + 'codeEl': code, + 'outputEl': $('.output', el), + 'runEl': $('.run', el), + 'fmtEl': $('.fmt', el), + 'shareEl': $('.share', el), + 'shareRedirect': 'http://play.golang.org/p/' + }); + + // Make the code textarea resize to fit content. + var resize = function() { + code.height(0); + var h = code[0].scrollHeight; + code.height(h+20); // minimize bouncing + code.closest('.input').height(h); + }; + code.on('keydown', resize); + code.on('keyup', resize); + code.keyup(); // resize now. + }); + }); +}); +</script> +{{end}} |