Skip to content

Commit d3a7d17

Browse files
committed
comments now displayed
1 parent 2fb2dc0 commit d3a7d17

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

display.go

+7
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ func jsonify(node *html.Node) map[string]interface{} {
7676
}
7777
vals["text"] = text
7878
}
79+
case html.CommentNode:
80+
comment := strings.TrimSpace(child.Data)
81+
currComment, ok := vals["comment"]
82+
if ok {
83+
comment = fmt.Sprintf("%s %s", currComment, comment)
84+
}
85+
vals["comment"] = comment
7986
}
8087
}
8188
if len(children) > 0 {

main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"strings"
1212
)
1313

14-
const VERSION string = "0.3.1"
14+
const VERSION string = "0.3.2"
1515

1616
var (
1717
// Flags
@@ -35,7 +35,7 @@ func Fatal(format string, args ...interface{}) {
3535
func PrintHelp() {
3636
helpString := `Usage
3737
38-
pup [list of css selectors]
38+
pup [flags] [selectors] [optional display function]
3939
4040
Version
4141

printing.go

+20-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
package main
22

33
import (
4+
"fmt"
5+
"strings"
6+
47
"code.google.com/p/go.net/html"
58
"code.google.com/p/go.net/html/atom"
6-
"fmt"
9+
710
"github.com/fatih/color"
811
"github.com/mattn/go-colorable"
9-
"regexp"
1012
)
1113

1214
var (
1315
// 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)
1719
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)
2321
)
2422

2523
func init() {
@@ -75,10 +73,9 @@ func (t TreeDisplayer) printNode(n *html.Node, level int) {
7573
switch n.Type {
7674
case html.TextNode:
7775
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)
8279
fmt.Println(s)
8380
}
8481
case html.ElementNode:
@@ -117,7 +114,15 @@ func (t TreeDisplayer) printNode(n *html.Node, level int) {
117114
fmt.Printf("</%s>\n", n.Data)
118115
}
119116
}
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:
121126
t.printChildren(n, level)
122127
}
123128
}

0 commit comments

Comments
 (0)