1
0
mirror of https://github.com/ericchiang/pup synced 2024-11-24 08:58:08 +00:00

comments now displayed

This commit is contained in:
ericchiang 2014-10-13 11:10:51 -04:00
parent 2fb2dc084d
commit d3a7d17e6c
3 changed files with 29 additions and 17 deletions

View File

@ -76,6 +76,13 @@ func jsonify(node *html.Node) map[string]interface{} {
}
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 {

View File

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

View File

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