Skip to content

Commit 8cbdaf7

Browse files
zikaerohvalyala
authored andcommitted
Add skipLineComments flag
1 parent 5f68e4f commit 8cbdaf7

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

parser/parser.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type parser struct {
1616
s *scanner
1717
w io.Writer
1818
packageName string
19+
lineComments bool
1920
prefix string
2021
forDepth int
2122
switchDepth int
@@ -29,10 +30,20 @@ type parser struct {
2930
// the supplied writer. Uses filename as the source file for line comments, and
3031
// pkg as the Go package name.
3132
func Parse(w io.Writer, r io.Reader, filename, pkg string) error {
33+
return parse(w, r, filename, pkg, true)
34+
}
35+
36+
// ParseNoLineComments is the same as Parse, but does not write line comments.
37+
func ParseNoLineComments(w io.Writer, r io.Reader, filename, pkg string) error {
38+
return parse(w, r, filename, pkg, false)
39+
}
40+
41+
func parse(w io.Writer, r io.Reader, filename, pkg string, lineComments bool) error {
3242
p := &parser{
33-
s: newScanner(r, filename),
34-
w: w,
35-
packageName: pkg,
43+
s: newScanner(r, filename),
44+
w: w,
45+
packageName: pkg,
46+
lineComments: lineComments,
3647
}
3748
return p.parseTemplate()
3849
}
@@ -801,8 +812,10 @@ func (p *parser) Printf(format string, args ...interface{}) {
801812
return
802813
}
803814
w := p.w
804-
// line comments are required to start at the beginning of the line
805-
p.s.WriteLineComment(w)
815+
if p.lineComments {
816+
// line comments are required to start at the beginning of the line
817+
p.s.WriteLineComment(w)
818+
}
806819
fmt.Fprintf(w, "%s", p.prefix)
807820
fmt.Fprintf(w, format, args...)
808821
fmt.Fprintf(w, "\n")

qtc/main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ var (
2525
file = flag.String("file", "", "Path to template file to compile.\n"+
2626
"Flags -dir and -ext are ignored if file is set.\n"+
2727
"The compiled file will be placed near the original file with .go extension added.")
28-
ext = flag.String("ext", "qtpl", "Only files with this extension are compiled")
28+
ext = flag.String("ext", "qtpl", "Only files with this extension are compiled")
29+
skipLineComments = flag.Bool("skipLineComments", false, "Don't write line comments")
2930
)
3031

3132
var logger = log.New(os.Stderr, "qtc: ", log.LstdFlags)
@@ -127,9 +128,16 @@ func compileFile(infile string) {
127128
if err != nil {
128129
logger.Fatalf("cannot determine package name for %q: %s", infile, err)
129130
}
130-
if err = parser.Parse(outf, inf, infile, packageName); err != nil {
131+
132+
parseFunc := parser.Parse
133+
if *skipLineComments {
134+
parseFunc = parser.ParseNoLineComments
135+
}
136+
137+
if err = parseFunc(outf, inf, infile, packageName); err != nil {
131138
logger.Fatalf("error when parsing file %q: %s", infile, err)
132139
}
140+
133141
if err = outf.Close(); err != nil {
134142
logger.Fatalf("error when closing file %q: %s", tmpfile, err)
135143
}

0 commit comments

Comments
 (0)