Skip to content

Commit

Permalink
Merge pull request #469 from googlecodelabs/escape-code-jingsa
Browse files Browse the repository at this point in the history
Escape all Code blocks for jingsa templates
  • Loading branch information
cassierecher authored Sep 18, 2020
2 parents ed940ea + 9d7986b commit 6e728f8
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 194 deletions.
2 changes: 2 additions & 0 deletions claat/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func writeCodelabWriter(w io.Writer, clab *types.Codelab, extraVars map[string]s
}{Context: render.Context{
Env: ctx.Env,
Prefix: ctx.Prefix,
Format: ctx.Format,
GlobalGA: ctx.MainGA,
Updated: time.Time(*ctx.Updated).Format(time.RFC3339),
Meta: &clab.Meta,
Expand Down Expand Up @@ -209,6 +210,7 @@ func writeCodelab(dir string, clab *types.Codelab, extraVars map[string]string,
}{Context: render.Context{
Env: ctx.Env,
Prefix: ctx.Prefix,
Format: ctx.Format,
GlobalGA: ctx.MainGA,
Updated: time.Time(*ctx.Updated).Format(time.RFC3339),
Meta: &clab.Meta,
Expand Down
20 changes: 11 additions & 9 deletions claat/parser/gdoc/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestParseTopCodeBlock(t *testing.T) {
code := "start func() {\n}\n\nfunc2() {\n} // comment"
term := "adb shell am start -a VIEW \\\n-d \"http://host\" app"
content := types.NewListNode()
var lang string;
var lang string
content.Append(types.NewCodeNode(code, false, lang))
content.Append(types.NewCodeNode(term, true, lang))

Expand All @@ -127,9 +127,9 @@ func TestParseTopCodeBlock(t *testing.T) {
cur: doc.FirstChild,
}
parseTop(ds)

html1, _ := render.HTML("", ds.step.Content)
html2, _ := render.HTML("", content)
var ctx render.Context
html1, _ := render.HTML(ctx, ds.step.Content)
html2, _ := render.HTML(ctx, content)
s1 := strings.TrimSpace(string(html1))
s2 := strings.TrimSpace(string(html2))
if s1 != s2 {
Expand Down Expand Up @@ -492,7 +492,7 @@ func TestParseDoc(t *testing.T) {
"http://host/file.java", types.NewTextNode("a file")))
content.Append(h)

var lang string;
var lang string
code := "start func() {\n}\n\nfunc2() {\n} // comment"
cn := types.NewCodeNode(code, false, lang)
cn.MutateBlock(1)
Expand Down Expand Up @@ -523,8 +523,9 @@ func TestParseDoc(t *testing.T) {
})
content.Append(sv)

html1, _ := render.HTML("", step.Content)
html2, _ := render.HTML("", content)
var ctx render.Context
html1, _ := render.HTML(ctx, step.Content)
html2, _ := render.HTML(ctx, content)
if html1 != html2 {
t.Errorf("step.Content:\n\n%s\nwant:\n\n%s", html1, html2)
}
Expand Down Expand Up @@ -578,8 +579,9 @@ func TestParseFragment(t *testing.T) {
para.MutateBlock(true)
want = append(want, para)

html1, _ := render.HTML("", nodes...)
html2, _ := render.HTML("", want...)
var ctx render.Context
html1, _ := render.HTML(ctx, nodes...)
html2, _ := render.HTML(ctx, want...)
if html1 != html2 {
t.Errorf("nodes:\n\n%s\nwant:\n\n%s", html1, html2)
}
Expand Down
21 changes: 14 additions & 7 deletions claat/render/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ var (
)

// HTML renders nodes as the markup for the target env.
func HTML(env string, nodes ...types.Node) (htmlTemplate.HTML, error) {
func HTML(ctx Context, nodes ...types.Node) (htmlTemplate.HTML, error) {
var buf bytes.Buffer
if err := WriteHTML(&buf, env, nodes...); err != nil {
if err := WriteHTML(&buf, ctx.Env, ctx.Format, nodes...); err != nil {
return "", err
}
return htmlTemplate.HTML(buf.String()), nil
}

// WriteHTML does the same as HTML but outputs rendered markup to w.
func WriteHTML(w io.Writer, env string, nodes ...types.Node) error {
hw := htmlWriter{w: w, env: env}
func WriteHTML(w io.Writer, env string, fmt string, nodes ...types.Node) error {
hw := htmlWriter{w: w, env: env, format: fmt}
return hw.write(nodes...)
}

Expand All @@ -64,9 +64,10 @@ func ReplaceAmpersand(s string) string {
}

type htmlWriter struct {
w io.Writer // output writer
env string // target environment
err error // error during any writeXxx methods
w io.Writer // output writer
env string // target environment
format string // target template
err error // error during any writeXxx methods
}

func (hw *htmlWriter) matchEnv(v []string) bool {
Expand Down Expand Up @@ -241,7 +242,13 @@ func (hw *htmlWriter) code(n *types.CodeNode) {
}
hw.writeBytes(greaterThan)
}
if hw.format == "devsite" {
hw.writeString("{% verbatim %}")
}
hw.writeEscape(n.Value)
if hw.format == "devsite" {
hw.writeString("{% endverbatim %}")
}
if !n.Term {
hw.writeString("</code>")
}
Expand Down
4 changes: 3 additions & 1 deletion claat/render/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func TestHTMLEnv(t *testing.T) {
{"four", ""},
}
for i, test := range tests {
h, err := HTML(test.env, one, two, three)
var ctx Context
ctx.Env = test.env
h, err := HTML(ctx, one, two, three)
if err != nil {
t.Errorf("%d: %v", i, err)
continue
Expand Down
4 changes: 2 additions & 2 deletions claat/render/lite.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (
)

// Lite renders nodes as a standard HTML markup, without Custom Elements.
func Lite(env string, nodes ...types.Node) (htmlTemplate.HTML, error) {
func Lite(ctx Context, nodes ...types.Node) (htmlTemplate.HTML, error) {
var buf bytes.Buffer
if err := WriteLite(&buf, env, nodes...); err != nil {
if err := WriteLite(&buf, ctx.Env, nodes...); err != nil {
return "", err
}
return htmlTemplate.HTML(buf.String()), nil
Expand Down
4 changes: 2 additions & 2 deletions claat/render/md.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (
)

// MD renders nodes as markdown for the target env.
func MD(env string, nodes ...types.Node) (string, error) {
func MD(ctx Context, nodes ...types.Node) (string, error) {
var buf bytes.Buffer
if err := WriteMD(&buf, env, nodes...); err != nil {
if err := WriteMD(&buf, ctx.Env, nodes...); err != nil {
return "", err
}
return buf.String(), nil
Expand Down
2 changes: 1 addition & 1 deletion claat/render/template-devsite.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
{{if $.Meta.BadgePath}}badge-path="{{$.Meta.BadgePath}}"{{end}}>
</google-codelab-about>
{{end}}
{{.Content | renderHTML $.Env}}
{{.Content | renderHTML $.Context}}
</google-codelab-step>
{{end}}{{end}}
</google-codelab>
Expand Down
2 changes: 1 addition & 1 deletion claat/render/template-offline.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ <h1>{{.Meta.Title}}</h1>
<div class="step__body">
<h1>{{.Meta.Title}}</h1>
<h2>{{.StepNum}}. {{.Current.Title}}</h2>
{{.Current.Content | renderLite .Env}}
{{.Current.Content | renderLite $.Context}}
</div>

</div><!-- codelab__toc -->
Expand Down
1 change: 1 addition & 0 deletions claat/render/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Context struct {
Env string
Prefix string
GlobalGA string
Format string
Meta *types.Meta
Steps []*types.Step
Updated string
Expand Down
2 changes: 1 addition & 1 deletion claat/render/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
feedback-link="{{.Meta.Feedback}}">
{{range $i, $e := .Steps}}{{if matchEnv .Tags $.Env}}
<google-codelab-step label="{{.Title}}" duration="{{.Duration.Minutes}}">
{{.Content | renderHTML $.Env}}
{{.Content | renderHTML $.Context}}
</google-codelab-step>
{{end}}{{end}}
</google-codelab>
Expand Down
2 changes: 1 addition & 1 deletion claat/render/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
{{range .Steps}}{{if matchEnv .Tags $.Env}}
## {{.Title}}
{{if .Duration}}Duration: {{durationStr .Duration}}{{end}}
{{.Content | renderMD $.Env}}
{{.Content | renderMD $.Context}}
{{end}}{{end}}
8 changes: 5 additions & 3 deletions claat/render/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ func TestExecuteBuiltin(t *testing.T) {
Title: "Test step",
Content: types.NewListNode(types.NewTextNode("text")),
}
ctx := &Context{
data := &struct {
Context
}{Context: Context{
Meta: &types.Meta{},
Steps: []*types.Step{step},
}
}}
for _, f := range []string{"html", "md"} {
var buf bytes.Buffer
if err := Execute(&buf, f, ctx); err != nil {
if err := Execute(&buf, f, data); err != nil {
t.Errorf("%s: %v", f, err)
}
}
Expand Down
Loading

0 comments on commit 6e728f8

Please sign in to comment.