|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
4 | 7 | "code.google.com/p/go.net/html"
|
5 | 8 | "code.google.com/p/go.net/html/atom"
|
6 |
| - "fmt" |
| 9 | + |
7 | 10 | "github.com/fatih/color"
|
8 | 11 | "github.com/mattn/go-colorable"
|
9 |
| - "regexp" |
10 | 12 | )
|
11 | 13 |
|
12 | 14 | var (
|
13 | 15 | // Colors
|
14 |
| - tagColor *color.Color = color.New(color.FgYellow).Add(color.Bold) |
15 |
| - tokenColor = color.New(color.FgCyan).Add(color.Bold) |
16 |
| - attrKeyColor = color.New(color.FgRed) |
| 16 | + tagColor *color.Color = color.New(color.FgCyan) |
| 17 | + tokenColor = color.New(color.FgCyan) |
| 18 | + attrKeyColor = color.New(color.FgMagenta) |
17 | 19 | quoteColor = color.New(color.FgBlue)
|
18 |
| - |
19 |
| - // Regexp helpers |
20 |
| - whitespaceRegexp *regexp.Regexp = regexp.MustCompile(`^\s*$`) |
21 |
| - preWhitespace = regexp.MustCompile(`^\s+`) |
22 |
| - postWhitespace = regexp.MustCompile(`\s+$`) |
| 20 | + commentColor = color.New(color.FgYellow) |
23 | 21 | )
|
24 | 22 |
|
25 | 23 | func init() {
|
@@ -75,10 +73,9 @@ func (t TreeDisplayer) printNode(n *html.Node, level int) {
|
75 | 73 | switch n.Type {
|
76 | 74 | case html.TextNode:
|
77 | 75 | s := html.EscapeString(n.Data)
|
78 |
| - if !whitespaceRegexp.MatchString(s) { |
79 |
| - s = preWhitespace.ReplaceAllString(s, "") |
80 |
| - s = postWhitespace.ReplaceAllString(s, "") |
81 |
| - t.printIndent(level) |
| 76 | + s = strings.TrimSpace(s) |
| 77 | + if s != "" { |
| 78 | + t.printIndent(level + 1) |
82 | 79 | fmt.Println(s)
|
83 | 80 | }
|
84 | 81 | case html.ElementNode:
|
@@ -117,7 +114,15 @@ func (t TreeDisplayer) printNode(n *html.Node, level int) {
|
117 | 114 | fmt.Printf("</%s>\n", n.Data)
|
118 | 115 | }
|
119 | 116 | }
|
120 |
| - case html.CommentNode, html.DoctypeNode, html.DocumentNode: |
| 117 | + case html.CommentNode: |
| 118 | + t.printIndent(level) |
| 119 | + if printColor { |
| 120 | + commentColor.Printf("<!--%s-->\n", n.Data) |
| 121 | + } else { |
| 122 | + fmt.Printf("<!--%s-->\n", n.Data) |
| 123 | + } |
| 124 | + t.printChildren(n, level) |
| 125 | + case html.DoctypeNode, html.DocumentNode: |
121 | 126 | t.printChildren(n, level)
|
122 | 127 | }
|
123 | 128 | }
|
0 commit comments