Skip to content

Commit

Permalink
comments now displayed
Browse files Browse the repository at this point in the history
  • Loading branch information
ericchiang committed Oct 13, 2014
1 parent 2fb2dc0 commit d3a7d17
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
7 changes: 7 additions & 0 deletions display.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
)

const VERSION string = "0.3.1"
const VERSION string = "0.3.2"

var (
// Flags
Expand All @@ -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
Expand Down
35 changes: 20 additions & 15 deletions printing.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
}
}

0 comments on commit d3a7d17

Please sign in to comment.