go.tools/present: add -edit and -numbers flags to .code/.play
Also update style.css to hide outline of editable text areas and apply correct styles to line numbers in non-playground snippets. R=golang-dev, r CC=golang-dev https://golang.org/cl/13946043
This commit is contained in:
parent
577fe73c91
commit
88be67fd25
|
@ -2073,9 +2073,10 @@ div#playground .output {
|
|||
padding: 0;
|
||||
background: none;
|
||||
border: none;
|
||||
outline: 0px solid transparent;
|
||||
overflow: auto;
|
||||
}
|
||||
#content .playground .number {
|
||||
#content .playground .number, #content .code .number {
|
||||
color: #999;
|
||||
}
|
||||
#content .code, #content .playground, #content .output {
|
||||
|
|
|
@ -548,9 +548,10 @@ div#playground .output {
|
|||
padding: 0;
|
||||
background: none;
|
||||
border: none;
|
||||
outline: 0px solid transparent;
|
||||
overflow: auto;
|
||||
}
|
||||
#content .playground .number {
|
||||
#content .playground .number, #content .code .number {
|
||||
color: #999;
|
||||
}
|
||||
#content .code, #content .playground, #content .output {
|
||||
|
|
|
@ -41,9 +41,12 @@ func (c Code) TemplateName() string { return "code" }
|
|||
var (
|
||||
highlightRE = regexp.MustCompile(`\s+HL([a-zA-Z0-9_]+)?$`)
|
||||
hlCommentRE = regexp.MustCompile(`(.+) // HL(.*)$`)
|
||||
codeRE = regexp.MustCompile(`\.(code|play)\s+([^\s]+)(\s+)?(.*)?$`)
|
||||
codeRE = regexp.MustCompile(`\.(code|play)\s+((?:(?:-edit|-numbers)\s+)*)([^\s]+)(?:\s+(.*))?$`)
|
||||
)
|
||||
|
||||
// parseCode parses a code present directive. Its syntax:
|
||||
// .code [-numbers] [-edit] <filename> [address] [highlight]
|
||||
// The directive may also be ".play" if the snippet is executable.
|
||||
func parseCode(ctx *Context, sourceFile string, sourceLine int, cmd string) (Elem, error) {
|
||||
cmd = strings.TrimSpace(cmd)
|
||||
|
||||
|
@ -58,14 +61,14 @@ func parseCode(ctx *Context, sourceFile string, sourceLine int, cmd string) (Ele
|
|||
// Arguments:
|
||||
// args[0]: whole match
|
||||
// args[1]: .code/.play
|
||||
// args[2]: file name
|
||||
// args[3]: space, if any, before optional address
|
||||
// args[2]: flags ("-edit -numbers")
|
||||
// args[3]: file name
|
||||
// args[4]: optional address
|
||||
args := codeRE.FindStringSubmatch(cmd)
|
||||
if len(args) != 5 {
|
||||
return nil, fmt.Errorf("%s:%d: syntax error for .code/.play invocation", sourceFile, sourceLine)
|
||||
}
|
||||
command, file, addr := args[1], args[2], strings.TrimSpace(args[4])
|
||||
command, flags, file, addr := args[1], args[2], args[3], strings.TrimSpace(args[4])
|
||||
play := command == "play" && PlayEnabled
|
||||
|
||||
// Read in code file and (optionally) match address.
|
||||
|
@ -106,7 +109,11 @@ func parseCode(ctx *Context, sourceFile string, sourceLine int, cmd string) (Ele
|
|||
lines[i] = line
|
||||
}
|
||||
|
||||
data := &codeTemplateData{Lines: lines}
|
||||
data := &codeTemplateData{
|
||||
Lines: lines,
|
||||
Edit: strings.Contains(flags, "-edit"),
|
||||
Numbers: strings.Contains(flags, "-numbers"),
|
||||
}
|
||||
|
||||
// Include before and after in a hidden span for playground code.
|
||||
if play {
|
||||
|
@ -124,6 +131,7 @@ func parseCode(ctx *Context, sourceFile string, sourceLine int, cmd string) (Ele
|
|||
type codeTemplateData struct {
|
||||
Lines []codeLine
|
||||
Prefix, Suffix []byte
|
||||
Edit, Numbers bool
|
||||
}
|
||||
|
||||
var leadingSpaceRE = regexp.MustCompile(`^[ \t]*`)
|
||||
|
@ -136,7 +144,8 @@ var codeTemplate = template.Must(template.New("code").Funcs(template.FuncMap{
|
|||
const codeTemplateHTML = `
|
||||
{{with .Prefix}}<pre style="display: none"><span>{{printf "%s" .}}</span></pre>{{end}}
|
||||
|
||||
<pre>{{range .Lines}}<span num="{{.N}}">{{/*
|
||||
<pre{{if .Edit}} contenteditable="true" spellcheck="false"{{end}}{{if .Numbers}} class="numbers"{{end}}>{{/*
|
||||
*/}}{{range .Lines}}<span num="{{.N}}">{{/*
|
||||
*/}}{{if .HL}}{{leadingSpace .L}}<b>{{trimSpace .L}}</b>{{/*
|
||||
*/}}{{else}}{{.L}}{{end}}{{/*
|
||||
*/}}</span>
|
||||
|
|
|
@ -140,6 +140,12 @@ Such highlights are enabled only if the code invocation ends with
|
|||
"HL" followed by the word:
|
||||
.code test.go /^type Foo/,/^}/ HLxxx
|
||||
|
||||
The .code function may take one or more flags immediately preceding
|
||||
the filename. This command shows test.go in an editable text area:
|
||||
.code -edit test.go
|
||||
This command shows test.go with line numbers:
|
||||
.code -numbers test.go
|
||||
|
||||
play:
|
||||
|
||||
The function "play" is the same as "code" but puts a button
|
||||
|
|
Loading…
Reference in New Issue