Skip to content

Commit

Permalink
Merge pull request #233 from googlecodelabs/linebreaks
Browse files Browse the repository at this point in the history
Remove potential line breaks from getting into codelab IDs and titles.
  • Loading branch information
nicolasgarnier authored Apr 29, 2019
2 parents b05c952 + 6fdfe49 commit 11fecc6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion claat/parser/gdoc/css.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func parseStyle(doc *html.Node) (cssStyle, error) {
if node == nil {
return style, nil
}
css := stringifyNode(node, true)
css := stringifyNode(node, true, true)

var skip bool
var sel []string
Expand Down
19 changes: 13 additions & 6 deletions claat/parser/gdoc/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ func nodeAttr(n *html.Node, name string) string {
}

// stringifyNode extracts and concatenates all text nodes starting with root.
// Line breaks are inserted at <br> and any non-<span> elements.
func stringifyNode(root *html.Node, trim bool) string {
// Line breaks are inserted at <br> and any non-<span> elements if requested.
func stringifyNode(root *html.Node, trim bool, lineBreak bool) string {
if root.Type == html.TextNode {
s := textCleaner.Replace(root.Data)
if !trim {
Expand All @@ -236,7 +236,10 @@ func stringifyNode(root *html.Node, trim bool) string {
return strings.TrimSpace(s)
}
if root.DataAtom == atom.Br && !trim {
return "\n"
if lineBreak {
return "\n"
}
return ""
}
var buf bytes.Buffer
for c := root.FirstChild; c != nil; c = c.NextSibling {
Expand All @@ -248,17 +251,21 @@ func stringifyNode(root *html.Node, trim bool) string {
}
}
if c.DataAtom == atom.Br {
buf.WriteRune('\n')
if lineBreak {
buf.WriteRune('\n')
}
continue
}
if c.Type == html.TextNode {
buf.WriteString(c.Data)
continue
}
if c.DataAtom != atom.Span && c.DataAtom != atom.A {
buf.WriteRune('\n')
if lineBreak {
buf.WriteRune('\n')
}
}
buf.WriteString(stringifyNode(c, false))
buf.WriteString(stringifyNode(c, false, lineBreak))
}
s := textCleaner.Replace(buf.String())
if !trim {
Expand Down
26 changes: 13 additions & 13 deletions claat/parser/gdoc/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func parseDoc(doc *html.Node) (*types.Codelab, error) {
}
switch {
case hasClass(ds.cur, "title") && ds.step == nil:
if v := stringifyNode(ds.cur, true); v != "" {
if v := stringifyNode(ds.cur, true, false); v != "" {
ds.clab.Title = v
}
if ds.clab.ID == "" {
Expand Down Expand Up @@ -361,7 +361,7 @@ func parseNode(ds *docState) (types.Node, bool) {
// newStep creates a new codelab step from ds.cur
// and finalizes nodes of the previous step.
func newStep(ds *docState) {
t := stringifyNode(ds.cur, true)
t := stringifyNode(ds.cur, true, false)
if t == "" {
return
}
Expand All @@ -376,16 +376,16 @@ func metaTable(ds *docState) {
if tr.FirstChild == nil || tr.FirstChild.NextSibling == nil {
continue
}
s := stringifyNode(tr.FirstChild.NextSibling, true)
switch strings.ToLower(stringifyNode(tr.FirstChild, true)) {
s := stringifyNode(tr.FirstChild.NextSibling, true, false)
switch strings.ToLower(stringifyNode(tr.FirstChild, true, false)) {
case "id", "url":
ds.clab.ID = s
case "author", "authors":
ds.clab.Authors = s
case "badge", "badge id":
ds.clab.BadgeID = s
case "summary":
ds.clab.Summary = s
ds.clab.Summary = stringifyNode(tr.FirstChild.NextSibling, true, true)
case "category", "categories":
ds.clab.Categories = util.Unique(stringSlice(s))
case "environment", "environments", "tags":
Expand All @@ -411,7 +411,7 @@ func metaTable(ds *docState) {
func metaStep(ds *docState) {
var text string
for {
text += stringifyNode(ds.cur, false)
text += stringifyNode(ds.cur, false, false)
if ds.cur.NextSibling == nil || !isMeta(ds.css, ds.cur.NextSibling) {
break
}
Expand Down Expand Up @@ -463,7 +463,7 @@ func header(ds *docState) types.Node {
return nil
}
n := types.NewHeaderNode(headerLevel[ds.cur.DataAtom], nodes...)
switch strings.ToLower(stringifyNode(ds.cur, true)) {
switch strings.ToLower(stringifyNode(ds.cur, true, false)) {
case headerLearn, headerCover:
n.MutateType(types.NodeHeaderCheck)
case headerFAQ:
Expand Down Expand Up @@ -556,7 +556,7 @@ func survey(ds *docState) types.Node {
opt, next := surveyOpt(c.NextSibling)
if len(opt) > 0 {
gg = append(gg, &types.SurveyGroup{
Name: stringifyNode(c, true),
Name: stringifyNode(c, true, false),
Options: opt,
})
}
Expand All @@ -583,7 +583,7 @@ func surveyOpt(hn *html.Node) ([]string, *html.Node) {
if li.DataAtom != atom.Li {
continue
}
opt = append(opt, stringifyNode(li, true))
opt = append(opt, stringifyNode(li, true, true))
}
}
return opt, nil
Expand All @@ -598,7 +598,7 @@ func code(ds *docState, term bool) types.Node {
return text(ds)
}
// block code or terminal
v := stringifyNode(ds.cur, false)
v := stringifyNode(ds.cur, false, true)
if v == "" {
if countDirect(ds.cur.Parent) > 1 {
return nil
Expand Down Expand Up @@ -700,7 +700,7 @@ func button(ds *docState) types.Node {
return nil
}

s := strings.ToLower(stringifyNode(a, true))
s := strings.ToLower(stringifyNode(a, true, false))
dl := strings.HasPrefix(s, "download ")
btn := types.NewButtonNode(true, true, dl, nodes...)

Expand All @@ -719,7 +719,7 @@ func link(ds *docState) types.Node {
return nil
}

text := stringifyNode(ds.cur, false)
text := stringifyNode(ds.cur, false, true)
if strings.TrimSpace(text) == "" {
return nil
}
Expand Down Expand Up @@ -776,7 +776,7 @@ func text(ds *docState) types.Node {
}
}

v := stringifyNode(ds.cur, false)
v := stringifyNode(ds.cur, false, true)
n := types.NewTextNode(v)
n.Bold = bold
n.Italic = italic
Expand Down

0 comments on commit 11fecc6

Please sign in to comment.