From 246da560726363fa64b37f815d7377dfc87490a4 Mon Sep 17 00:00:00 2001 From: Francesc Campoy Date: Tue, 25 Feb 2014 17:53:00 -0800 Subject: [PATCH] go.tools/present: Remove formatting comments (HL, OMIT) from raw code. LGTM=adg R=adg CC=golang-codereviews https://golang.org/cl/68870043 --- present/code.go | 49 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/present/code.go b/present/code.go index cc1e45e50d..2816a8797d 100644 --- a/present/code.go +++ b/present/code.go @@ -98,22 +98,8 @@ func parseCode(ctx *Context, sourceFile string, sourceLine int, cmd string) (Ele lines := codeLines(textBytes, lo, hi) - for i, line := range lines { - // Replace tabs by spaces, which work better in HTML. - line.L = strings.Replace(line.L, "\t", " ", -1) - - // Highlight lines that end with "// HL[highlight]" - // and strip the magic comment. - if m := hlCommentRE.FindStringSubmatch(line.L); m != nil { - line.L = m[1] - line.HL = m[2] == highlight - } - - lines[i] = line - } - data := &codeTemplateData{ - Lines: lines, + Lines: formatLines(lines, highlight), Edit: strings.Contains(flags, "-edit"), Numbers: strings.Contains(flags, "-numbers"), } @@ -133,10 +119,41 @@ func parseCode(ctx *Context, sourceFile string, sourceLine int, cmd string) (Ele Play: play, FileName: filepath.Base(filename), Ext: filepath.Ext(filename), - Raw: textBytes, + Raw: rawCode(lines), }, nil } +// formatLines returns a new slice of codeLine with the given lines +// replacing tabs with spaces and adding highlighting where needed. +func formatLines(lines []codeLine, highlight string) []codeLine { + formatted := make([]codeLine, len(lines)) + for i, line := range lines { + // Replace tabs with spaces, which work better in HTML. + line.L = strings.Replace(line.L, "\t", " ", -1) + + // Highlight lines that end with "// HL[highlight]" + // and strip the magic comment. + if m := hlCommentRE.FindStringSubmatch(line.L); m != nil { + line.L = m[1] + line.HL = m[2] == highlight + } + + formatted[i] = line + } + return formatted +} + +// rawCode returns the code represented by the given codeLines without any kind +// of formatting. +func rawCode(lines []codeLine) []byte { + b := new(bytes.Buffer) + for _, line := range lines { + b.WriteString(line.L) + b.WriteByte('\n') + } + return b.Bytes() +} + type codeTemplateData struct { Lines []codeLine Prefix, Suffix []byte