7
7
package main // import "golang.org/x/tools/cmd/html2article"
8
8
9
9
import (
10
- "bufio"
11
10
"bytes"
12
11
"errors"
13
12
"flag"
@@ -39,7 +38,9 @@ func convert(w io.Writer, r io.Reader) error {
39
38
}
40
39
41
40
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
+ }
43
44
44
45
body := find (root , isTag (atom .Body ))
45
46
if body == nil {
@@ -60,59 +61,44 @@ const (
60
61
61
62
var cssRules = make (map [string ]Style )
62
63
63
- func parseStyles (style * html.Node ) {
64
+ func parseStyles (style * html.Node ) error {
64
65
if style == nil || style .FirstChild == nil {
65
- log .Println ("couldn't find styles" )
66
- return
66
+ return errors .New ("couldn't find styles" )
67
67
}
68
- s := bufio .NewScanner (strings .NewReader (style .FirstChild .Data ))
69
68
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
74
74
}
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
93
78
}
94
79
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 == "" {
100
83
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 )
101
91
}
102
- b := strings .ToLower (s .Text ())
103
92
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
110
99
}
111
- s .Split (findRule )
112
- }
113
- if err := s .Err (); err != nil {
114
- log .Println (err )
115
100
}
101
+ return nil
116
102
}
117
103
118
104
var newlineRun = regexp .MustCompile (`\n\n+` )
0 commit comments