Skip to content

Commit 70ba3d2

Browse files
fix: Trim whitespace from TEXT piece style output
This commit applies `strings.TrimSpace` to the output of the `PieceStyleTEXT` case. This ensures that the output is clean and free of any leading or trailing whitespace.
1 parent 5ff6f73 commit 70ba3d2

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

cascadia_cliDef.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type rootT struct {
2828
CSS []string `cli:"*c,css" usage:"CSS selectors (can provide more if not using --piece)"`
2929
TextOut bool `cli:"t,text" usage:"Text output for none-block selection mode"`
3030
TextRaw bool `cli:"R,Raw" usage:"Raw text output, no trimming of leading and trailing white space"`
31-
Piece PieceStyleMap `cli:"p,piece" usage:"sub CSS selectors within -css to split that block up into pieces\n\t\t\tformat: PieceName=[PieceStyle:]selector_string\n\t\t\t PieceStyle:\n\t\t\t RAW : will return the selected as-is\n\t\t\t ATTR : will return the value of attribute selector_string\n\t\t\t Else the text will be returned"`
31+
Piece PieceStyleMap `cli:"p,piece" usage:"sub CSS selectors within -css to split that block up into pieces\n\t\t\tformat: PieceName=[PieceStyle:]selector_string\n\t\t\t PieceStyle:\n\t\t\t RAW : will return the selected as-is\n\t\t\t ATTR : will return the value of attribute selector_string\n\t\t\t GOQR : will extract attribute value from the selector_string of the format selector.attr(attributeName)\n\t\t\t Else the text will be returned"`
3232
Deli string `cli:"d,delimiter" usage:"delimiter for pieces csv output" dft:"\t"`
3333
WrapHTML bool `cli:"w,wrap-html" usage:"wrap up the output with html tags"`
3434
Style string `cli:"y,style" usage:"style component within the wrapped html head"`

cascadia_main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,13 @@ func Cascadia(bi io.Reader, bw io.Writer, Opts OptsT) error {
173173
case PieceStyleATTR:
174174
fmt.Fprintf(bw, "%s%s",
175175
item.AttrOr(piece.Values[key], ""), deli)
176+
case PieceStyleGOQR:
177+
val := strings.TrimSpace(item.Find(piece.GoqrSelectors[key]).AttrOr(piece.GoqrAttrs[key], ""))
178+
fmt.Fprintf(bw, "%s%s", val, deli)
176179
case PieceStyleTEXT:
177-
fmt.Fprintf(bw, "%s%s",
178-
item.Find(piece.Values[key]).Contents().Text(), deli)
180+
txt := strings.TrimSpace(item.Find(piece.Values[key]).Contents().Text())
181+
//fmt.Fprintf(os.Stderr, "] %s: '%+v'\n", key, txt)
182+
fmt.Fprintf(bw, "%s%s", txt, deli)
179183
}
180184
}
181185
fmt.Fprintf(bw, "\n")

prop_piece.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package main
99
import (
1010
"errors"
1111
"regexp"
12+
"strings"
1213
)
1314

1415
type PieceStyle int
@@ -17,17 +18,21 @@ const (
1718
PieceStyleTEXT PieceStyle = iota
1819
PieceStyleRAW
1920
PieceStyleATTR
21+
PieceStyleGOQR
2022
)
2123

2224
type PieceStyleMap struct {
23-
Keys []string
24-
Values map[string]string
25-
PieceStyles map[string]PieceStyle
25+
Keys []string
26+
Values map[string]string
27+
PieceStyles map[string]PieceStyle
28+
GoqrSelectors map[string]string
29+
GoqrAttrs map[string]string
2630
}
2731

2832
var pieceStyles = map[string]PieceStyle{
2933
"RAW": PieceStyleRAW,
3034
"ATTR": PieceStyleATTR,
35+
"GOQR": PieceStyleGOQR,
3136
}
3237

3338
//==========================================================================
@@ -39,11 +44,13 @@ func (PieceStyleMap) DecodeSlice() {}
3944

4045
// Decode implements cli.Decoder interface
4146
func (m *PieceStyleMap) Decode(s string) error {
42-
if (m.Values) == nil {
47+
if m.Values == nil {
4348
m.Values = make(map[string]string)
4449
m.PieceStyles = make(map[string]PieceStyle)
50+
m.GoqrSelectors = make(map[string]string)
51+
m.GoqrAttrs = make(map[string]string)
4552
}
46-
matches := regexp.MustCompile("(.*)=((.*?):)?(.*)").FindStringSubmatch(s)
53+
matches := regexp.MustCompile("(.*?)=((.*?):)?(.*)").FindStringSubmatch(s)
4754
if len(matches) < 4 {
4855
return errors.New("format error. To get help, run: " + progname)
4956
}
@@ -56,6 +63,19 @@ func (m *PieceStyleMap) Decode(s string) error {
5663
if len(ptp) != 0 && !ok {
5764
return errors.New("Piece style specification error. To get help, run: " + progname)
5865
}
66+
67+
if style == PieceStyleGOQR {
68+
// selector.attr(attributeName)
69+
parts := strings.Split(val, ".attr(")
70+
if len(parts) != 2 || !strings.HasSuffix(parts[1], ")") {
71+
return errors.New("GOQR format error. To get help, run: " + progname)
72+
}
73+
selector := parts[0]
74+
attr := strings.TrimSuffix(parts[1], ")")
75+
m.GoqrSelectors[key] = selector
76+
m.GoqrAttrs[key] = attr
77+
}
78+
5979
m.Keys = append(m.Keys, key)
6080
m.PieceStyles[key] = style
6181
m.Values[key] = val

prop_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,9 @@ func testCases(t *testing.T, name string, testData []testCase) {
7171
}
7272

7373
}
74+
75+
func TestPieceGoqr(t *testing.T) {
76+
testCases(t, "Piece Goqr", []testCase{
77+
{"piece_goqr", []string{"-i", "opt_piece_attr.html", "-o", "-c", "li", "-p", "id=GOQR:a.attr(id)", "-p", "url=GOQR:a.attr(href)"}},
78+
})
79+
}

test/piece_goqr.ref

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
id url
2+
a1 http://www.google.com/finance
3+
a2 http://finance.yahoo.com/
4+
a3 https://www.google.com/news
5+
a4 http://news.yahoo.com

0 commit comments

Comments
 (0)