Skip to content

Commit a801f4d

Browse files
committed
- [+] refact to & use prop_piece.go
1 parent f50eb59 commit a801f4d

File tree

5 files changed

+79
-68
lines changed

5 files changed

+79
-68
lines changed

cascadia_cli.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ Options:
3939
Usage: Raw text output, no trimming of leading and trailing white space
4040

4141
- Name: Piece
42-
Type: OutputStyleMap
42+
Type: PieceStyleMap
4343
Flag: 'p,piece'
44-
Usage: 'sub CSS selectors within -css to split that block up into pieces\n\t\t\t\tformat: PieceName=[OutputStyle:]selector_string\n\t\t\t\t OutputStyle:\n\t\t\t\t RAW : will return the selected as-is\n\t\t\t\t attr[xx] : will return the value of xx attribute\n\t\t\t\telse the text will be returned'
44+
Usage: 'sub CSS selectors within -css to split that block up into pieces\n\t\t\t\tformat: PieceName=[PieceStyle:]selector_string\n\t\t\t\t PieceStyle:\n\t\t\t\t RAW : will return the selected as-is\n\t\t\t\t attr[xx] : will return the value of xx attribute\n\t\t\t\telse the text will be returned'
4545

4646
- Name: Deli
4747
Type: string

cascadia_cliDef.go

Lines changed: 2 additions & 2 deletions
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 OutputStyleMap `cli:"p,piece" usage:"sub CSS selectors within -css to split that block up into pieces\n\t\t\t\tformat: PieceName=[OutputStyle:]selector_string\n\t\t\t\t OutputStyle:\n\t\t\t\t RAW : will return the selected as-is\n\t\t\t\t attr[xx] : will return the value of xx attribute\n\t\t\t\telse 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\t\tformat: PieceName=[PieceStyle:]selector_string\n\t\t\t\t PieceStyle:\n\t\t\t\t RAW : will return the selected as-is\n\t\t\t\t attr[xx] : will return the value of xx attribute\n\t\t\t\telse 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"`
@@ -59,7 +59,7 @@ var root = &cli.Command{
5959
// CSS []string
6060
// TextOut bool
6161
// TextRaw bool
62-
// Piece OutputStyleMap
62+
// Piece PieceStyleMap
6363
// Deli string
6464
// WrapHTML bool
6565
// Style string

cascadia_main.go

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
////////////////////////////////////////////////////////////////////////////
22
// Program: cascadia
33
// Purpose: go cascadia CSS selection from command line
4-
// Authors: Tong Sun (c) 2016-2021, All rights reserved
4+
// Authors: Tong Sun (c) 2016-2023, All rights reserved
55
////////////////////////////////////////////////////////////////////////////
66

77
//go:generate sh -v cascadia_cliGen.sh
88

99
package main
1010

1111
import (
12-
"errors"
1312
"fmt"
1413
"io"
1514
"os"
16-
"regexp"
1715
"strings"
1816

1917
"github.com/PuerkitoBio/goquery"
@@ -30,27 +28,12 @@ const (
3028
WrapHTMLEnd = `</body>`
3129
)
3230

33-
type OutputStyle int
34-
35-
const (
36-
OutputStyleRAW OutputStyle = iota
37-
OutputStyleATTR
38-
OutputStyleTEXT
39-
)
40-
41-
type OutputStyleMap struct {
42-
Keys []string
43-
Values map[string]string
44-
OutputStyles map[string]OutputStyle
45-
AttrName map[string]string
46-
}
47-
4831
// The OptsT type defines all the configurable options from cli.
4932
type OptsT struct {
5033
CSS []string
5134
TextOut bool
5235
TextRaw bool
53-
Piece OutputStyleMap
36+
Piece PieceStyleMap
5437
Deli string
5538
WrapHTML bool
5639
Style string
@@ -183,14 +166,14 @@ func Cascadia(bi io.Reader, bw io.Writer, Opts OptsT) error {
183166
//fmt.Printf("] #%d: %s\n", index, item.Text())
184167
for _, key := range piece.Keys {
185168
//fmt.Printf("] %s: %s\n", key, piece.Values[key])
186-
switch piece.OutputStyles[key] {
187-
case OutputStyleRAW:
169+
switch piece.PieceStyles[key] {
170+
case PieceStyleRAW:
188171
html.Render(bw, item.Find(piece.Values[key]).Get(0))
189172
fmt.Fprintf(bw, deli)
190-
case OutputStyleATTR:
173+
case PieceStyleATTR:
191174
fmt.Fprintf(bw, "%s%s",
192175
item.Find(piece.Values[key]).AttrOr(piece.AttrName[key], ""), deli)
193-
case OutputStyleTEXT:
176+
case PieceStyleTEXT:
194177
fmt.Fprintf(bw, "%s%s",
195178
item.Find(piece.Values[key]).Contents().Text(), deli)
196179
}
@@ -204,46 +187,6 @@ func Cascadia(bi io.Reader, bw io.Writer, Opts OptsT) error {
204187
return nil
205188
}
206189

207-
//==========================================================================
208-
// cli parameter handling
209-
210-
// DecodeSlice implements cli.SliceDecoder
211-
// NOTE: if SliceDecoder not implemented, the Decode method would be only invoked once
212-
func (OutputStyleMap) DecodeSlice() {}
213-
214-
// Decode implements cli.Decoder interface
215-
func (m *OutputStyleMap) Decode(s string) error {
216-
if (m.Values) == nil {
217-
m.Values = make(map[string]string)
218-
m.OutputStyles = make(map[string]OutputStyle)
219-
m.AttrName = make(map[string]string)
220-
}
221-
matches := regexp.MustCompile("(.*)=(.*)").FindStringSubmatch(s)
222-
if len(matches) < 2 {
223-
return errors.New("format error. To get help, run: " + progname)
224-
}
225-
key := matches[1]
226-
val := matches[2]
227-
index := strings.Index(val, ":")
228-
if index > 0 {
229-
style := val[:index]
230-
val = val[index+1:]
231-
if style == IsRaw {
232-
m.OutputStyles[key] = OutputStyleRAW
233-
} else if strings.HasPrefix(style, "attr[") && strings.HasSuffix(style, "]") {
234-
m.OutputStyles[key] = OutputStyleATTR
235-
m.AttrName[key] = style[5 : len(style)-1]
236-
} else {
237-
m.OutputStyles[key] = OutputStyleTEXT
238-
}
239-
} else {
240-
m.OutputStyles[key] = OutputStyleTEXT
241-
}
242-
m.Keys = append(m.Keys, key)
243-
m.Values[key] = val
244-
return nil
245-
}
246-
247190
//==========================================================================
248191
// support functions
249192

cascadia_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestSelectors(t *testing.T) {
1111
buf := bytes.NewBufferString("")
1212
Opts.CSS, Opts.Piece, Opts.Deli,
1313
Opts.WrapHTML, Opts.TextOut, Opts.TextRaw, Opts.Quiet =
14-
[]string{test.selector}, OutputStyleMap{}, ",",
14+
[]string{test.selector}, PieceStyleMap{}, ",",
1515
false, false, false, false
1616
Cascadia(strings.NewReader(test.HTML), buf, Opts)
1717
got := buf.String()

prop_piece.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
// Program: cascadia
3+
// Purpose: go cascadia CSS selection from command line
4+
// Authors: Tong Sun (c) 2016-2023, All rights reserved
5+
////////////////////////////////////////////////////////////////////////////
6+
7+
package main
8+
9+
import (
10+
"errors"
11+
"regexp"
12+
"strings"
13+
)
14+
15+
type PieceStyle int
16+
17+
const (
18+
PieceStyleRAW PieceStyle = iota
19+
PieceStyleATTR
20+
PieceStyleTEXT
21+
)
22+
23+
type PieceStyleMap struct {
24+
Keys []string
25+
Values map[string]string
26+
PieceStyles map[string]PieceStyle
27+
AttrName map[string]string
28+
}
29+
30+
//==========================================================================
31+
// cli parameter handling
32+
33+
// DecodeSlice implements cli.SliceDecoder
34+
// NOTE: if SliceDecoder not implemented, the Decode method would be only invoked once
35+
func (PieceStyleMap) DecodeSlice() {}
36+
37+
// Decode implements cli.Decoder interface
38+
func (m *PieceStyleMap) Decode(s string) error {
39+
if (m.Values) == nil {
40+
m.Values = make(map[string]string)
41+
m.PieceStyles = make(map[string]PieceStyle)
42+
m.AttrName = make(map[string]string)
43+
}
44+
matches := regexp.MustCompile("(.*)=(.*)").FindStringSubmatch(s)
45+
if len(matches) < 2 {
46+
return errors.New("format error. To get help, run: " + progname)
47+
}
48+
key := matches[1]
49+
val := matches[2]
50+
index := strings.Index(val, ":")
51+
if index > 0 {
52+
style := val[:index]
53+
val = val[index+1:]
54+
if style == IsRaw {
55+
m.PieceStyles[key] = PieceStyleRAW
56+
} else if strings.HasPrefix(style, "attr[") && strings.HasSuffix(style, "]") {
57+
m.PieceStyles[key] = PieceStyleATTR
58+
m.AttrName[key] = style[5 : len(style)-1]
59+
} else {
60+
m.PieceStyles[key] = PieceStyleTEXT
61+
}
62+
} else {
63+
m.PieceStyles[key] = PieceStyleTEXT
64+
}
65+
m.Keys = append(m.Keys, key)
66+
m.Values[key] = val
67+
return nil
68+
}

0 commit comments

Comments
 (0)