Skip to content

Commit 0b2f4dc

Browse files
committed
tools: fix 'Split called after Scan'
Change-Id: I2dae23440d33fa830107575987805e858e4bf4a7 Reviewed-on: https://go-review.googlesource.com/22749 Reviewed-by: Andrew Gerrand <[email protected]>
1 parent 8dab6f1 commit 0b2f4dc

File tree

1 file changed

+30
-44
lines changed

1 file changed

+30
-44
lines changed

cmd/html2article/conv.go

+30-44
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package main // import "golang.org/x/tools/cmd/html2article"
88

99
import (
10-
"bufio"
1110
"bytes"
1211
"errors"
1312
"flag"
@@ -39,7 +38,9 @@ func convert(w io.Writer, r io.Reader) error {
3938
}
4039

4140
style := find(root, isTag(atom.Style))
42-
parseStyles(style)
41+
if err := parseStyles(style); err != nil {
42+
log.Printf("couldn't parse all styles: %v", err)
43+
}
4344

4445
body := find(root, isTag(atom.Body))
4546
if body == nil {
@@ -60,59 +61,44 @@ const (
6061

6162
var cssRules = make(map[string]Style)
6263

63-
func parseStyles(style *html.Node) {
64+
func parseStyles(style *html.Node) error {
6465
if style == nil || style.FirstChild == nil {
65-
log.Println("couldn't find styles")
66-
return
66+
return errors.New("couldn't find styles")
6767
}
68-
s := bufio.NewScanner(strings.NewReader(style.FirstChild.Data))
6968

70-
findRule := func(b []byte, atEOF bool) (advance int, token []byte, err error) {
71-
if i := bytes.Index(b, []byte("{")); i >= 0 {
72-
token = bytes.TrimSpace(b[:i])
73-
advance = i
69+
styles := style.FirstChild.Data
70+
readUntil := func(end rune) (string, bool) {
71+
i := strings.IndexRune(styles, end)
72+
if i < 0 {
73+
return "", false
7474
}
75-
return
76-
}
77-
findBody := func(b []byte, atEOF bool) (advance int, token []byte, err error) {
78-
if len(b) == 0 {
79-
return
80-
}
81-
if b[0] != '{' {
82-
err = fmt.Errorf("expected {, got %c", b[0])
83-
return
84-
}
85-
if i := bytes.Index(b, []byte("}")); i < 0 {
86-
err = fmt.Errorf("can't find closing }")
87-
return
88-
} else {
89-
token = b[1:i]
90-
advance = i + 1
91-
}
92-
return
75+
s := styles[:i]
76+
styles = styles[i:]
77+
return s, true
9378
}
9479

95-
s.Split(findRule)
96-
for s.Scan() {
97-
rule := s.Text()
98-
s.Split(findBody)
99-
if !s.Scan() {
80+
for {
81+
sel, ok := readUntil('{')
82+
if !ok && sel == "" {
10083
break
84+
} else if !ok {
85+
return fmt.Errorf("could not parse selector %q", styles)
86+
}
87+
88+
value, ok := readUntil('}')
89+
if !ok {
90+
return fmt.Errorf("couldn't parse style body for %s", sel)
10191
}
102-
b := strings.ToLower(s.Text())
10392
switch {
104-
case strings.Contains(b, "italic"):
105-
cssRules[rule] = Italic
106-
case strings.Contains(b, "bold"):
107-
cssRules[rule] = Bold
108-
case strings.Contains(b, "Consolas") || strings.Contains(b, "Courier New"):
109-
cssRules[rule] = Code
93+
case strings.Contains(value, "italic"):
94+
cssRules[sel] = Italic
95+
case strings.Contains(value, "bold"):
96+
cssRules[sel] = Bold
97+
case strings.Contains(value, "Consolas") || strings.Contains(value, "Courier New"):
98+
cssRules[sel] = Code
11099
}
111-
s.Split(findRule)
112-
}
113-
if err := s.Err(); err != nil {
114-
log.Println(err)
115100
}
101+
return nil
116102
}
117103

118104
var newlineRun = regexp.MustCompile(`\n\n+`)

0 commit comments

Comments
 (0)