comments now displayed

pull/28/head v0.3.2
ericchiang 10 years ago
parent 2fb2dc084d
commit d3a7d17e6c

@ -76,6 +76,13 @@ func jsonify(node *html.Node) map[string]interface{} {
} }
vals["text"] = text vals["text"] = text
} }
case html.CommentNode:
comment := strings.TrimSpace(child.Data)
currComment, ok := vals["comment"]
if ok {
comment = fmt.Sprintf("%s %s", currComment, comment)
}
vals["comment"] = comment
} }
} }
if len(children) > 0 { if len(children) > 0 {

@ -11,7 +11,7 @@ import (
"strings" "strings"
) )
const VERSION string = "0.3.1" const VERSION string = "0.3.2"
var ( var (
// Flags // Flags
@ -35,7 +35,7 @@ func Fatal(format string, args ...interface{}) {
func PrintHelp() { func PrintHelp() {
helpString := `Usage helpString := `Usage
pup [list of css selectors] pup [flags] [selectors] [optional display function]
Version Version

@ -1,25 +1,23 @@
package main package main
import ( import (
"fmt"
"strings"
"code.google.com/p/go.net/html" "code.google.com/p/go.net/html"
"code.google.com/p/go.net/html/atom" "code.google.com/p/go.net/html/atom"
"fmt"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/mattn/go-colorable" "github.com/mattn/go-colorable"
"regexp"
) )
var ( var (
// Colors // Colors
tagColor *color.Color = color.New(color.FgYellow).Add(color.Bold) tagColor *color.Color = color.New(color.FgCyan)
tokenColor = color.New(color.FgCyan).Add(color.Bold) tokenColor = color.New(color.FgCyan)
attrKeyColor = color.New(color.FgRed) attrKeyColor = color.New(color.FgMagenta)
quoteColor = color.New(color.FgBlue) quoteColor = color.New(color.FgBlue)
commentColor = color.New(color.FgYellow)
// Regexp helpers
whitespaceRegexp *regexp.Regexp = regexp.MustCompile(`^\s*$`)
preWhitespace = regexp.MustCompile(`^\s+`)
postWhitespace = regexp.MustCompile(`\s+$`)
) )
func init() { func init() {
@ -75,10 +73,9 @@ func (t TreeDisplayer) printNode(n *html.Node, level int) {
switch n.Type { switch n.Type {
case html.TextNode: case html.TextNode:
s := html.EscapeString(n.Data) s := html.EscapeString(n.Data)
if !whitespaceRegexp.MatchString(s) { s = strings.TrimSpace(s)
s = preWhitespace.ReplaceAllString(s, "") if s != "" {
s = postWhitespace.ReplaceAllString(s, "") t.printIndent(level + 1)
t.printIndent(level)
fmt.Println(s) fmt.Println(s)
} }
case html.ElementNode: case html.ElementNode:
@ -117,7 +114,15 @@ func (t TreeDisplayer) printNode(n *html.Node, level int) {
fmt.Printf("</%s>\n", n.Data) fmt.Printf("</%s>\n", n.Data)
} }
} }
case html.CommentNode, html.DoctypeNode, html.DocumentNode: case html.CommentNode:
t.printIndent(level)
if printColor {
commentColor.Printf("<!--%s-->\n", n.Data)
} else {
fmt.Printf("<!--%s-->\n", n.Data)
}
t.printChildren(n, level)
case html.DoctypeNode, html.DocumentNode:
t.printChildren(n, level) t.printChildren(n, level)
} }
} }

Loading…
Cancel
Save