Skip to content

Commit 71375b6

Browse files
committed
- [+] add -y/--style option
1 parent d77076b commit 71375b6

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

cascadia_cli.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ Options:
4444
Flag: w,wrap-html
4545
Usage: wrap up the output with html tags
4646

47+
- Name: Style
48+
Type: string
49+
Flag: y,style
50+
Usage: style component within the wrapped html head
51+
4752
- Name: Base
4853
Type: string
4954
Flag: b,base

cascadia_cliDef.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
////////////////////////////////////////////////////////////////////////////
22
// Program: cascadiaC
33
// Purpose: cascadia wrapper
4-
// Authors: Tong Sun (c) 2020, All rights reserved
4+
// Authors: Tong Sun (c) 2021, All rights reserved
55
////////////////////////////////////////////////////////////////////////////
66

77
package main
@@ -29,18 +29,19 @@ type rootT struct {
2929
Piece MapStringString `cli:"p,piece" usage:"sub CSS selectors within -css to split that block up into pieces\n\t\t\tformat: PieceName=[RAW:]selector_string\n\t\t\tRAW: will return the selected as-is; else the text will be returned"`
3030
Deli string `cli:"d,delimiter" usage:"delimiter for pieces csv output" dft:"\t"`
3131
WrapHTML bool `cli:"w,wrap-html" usage:"wrap up the output with html tags"`
32+
Style string `cli:"y,style" usage:"style component within the wrapped html head"`
3233
Base string `cli:"b,base" usage:"base href tag used in the wrapped up html"`
3334
Quiet bool `cli:"q,quiet" usage:"be quiet"`
3435
}
3536

3637
var root = &cli.Command{
3738
Name: "cascadiaC",
3839
Desc: "cascadia wrapper\nVersion " + version + " built on " + date +
39-
"\nCopyright (C) 2020, Tong Sun",
40+
"\nCopyright (C) 2021, Tong Sun",
4041
Text: "Command line interface to go cascadia CSS selectors package" +
4142
"\n\nUsage:\n cascadia -i in -c css -o [Options...]",
4243
Argv: func() interface{} { return new(rootT) },
43-
Fn: cascadiaC,
44+
Fn: CascadiaC,
4445

4546
NumOption: cli.AtLeast(3),
4647
}
@@ -57,6 +58,7 @@ var root = &cli.Command{
5758
// Piece MapStringString
5859
// Deli string
5960
// WrapHTML bool
61+
// Style string
6062
// Base string
6163
// Quiet bool
6264
// Verbose int
@@ -68,7 +70,7 @@ var root = &cli.Command{
6870
// var (
6971
// progname = "cascadiaC"
7072
// version = "0.1.0"
71-
// date = "2020-04-19"
73+
// date = "2021-07-16"
7274

7375
// rootArgv *rootT
7476
// // Opts store all the configurable options
@@ -80,9 +82,7 @@ var root = &cli.Command{
8082

8183
// Function main
8284
// func main() {
83-
// cli.SetUsageStyle(cli.DenseNormalStyle) // left-right, for up-down, use ManualStyle
84-
// //NOTE: You can set any writer implements io.Writer
85-
// // default writer is os.Stdout
85+
// cli.SetUsageStyle(cli.DenseNormalStyle)
8686
// if err := cli.Root(root,).Run(os.Args[1:]); err != nil {
8787
// fmt.Fprintln(os.Stderr, err)
8888
// os.Exit(1)
@@ -94,7 +94,8 @@ var root = &cli.Command{
9494
//==========================================================================
9595
// Dumb root handler
9696

97-
// func cascadiaC(ctx *cli.Context) error {
97+
// CascadiaC - main dispatcher dumb handler
98+
// func CascadiaC(ctx *cli.Context) error {
9899
// ctx.JSON(ctx.RootArgv())
99100
// ctx.JSON(ctx.Argv())
100101
// fmt.Println()

cascadia_main.go

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

77
//go:generate sh -v cascadia_cliGen.sh
@@ -40,8 +40,8 @@ type MapStringString struct {
4040

4141
var (
4242
progname = "cascadia"
43-
version = "1.2.3"
44-
date = "2020-04-20"
43+
version = "1.2.5"
44+
date = "2021-07-16"
4545

4646
rootArgv *rootT
4747
)
@@ -62,8 +62,9 @@ func main() {
6262
//==========================================================================
6363
// css selection
6464

65-
func cascadiaC(ctx *cli.Context) error {
65+
func CascadiaC(ctx *cli.Context) error {
6666
// ctx.JSON(ctx.RootArgv())
67+
// fmt.Println()
6768
// ctx.JSON(ctx.Argv())
6869
// fmt.Println()
6970

@@ -73,8 +74,9 @@ func cascadiaC(ctx *cli.Context) error {
7374
<head>
7475
<meta charset="utf-8">
7576
<base href="%s">
77+
%s
7678
</head>
77-
<body>`, argv.Base)
79+
<body>`, argv.Base, argv.Style)
7880

7981
Cascadia(argv.Filei, argv.Fileo, argv.CSS, argv.Piece, argv.Deli,
8082
argv.WrapHTML, argv.Quiet)
@@ -87,6 +89,9 @@ func cascadiaC(ctx *cli.Context) error {
8789

8890
// Cascadia filters the input buffer/stream `bi` with CSS selectors array `cssa` and write to the output buffer/stream `bw`.
8991
func Cascadia(bi io.Reader, bw io.Writer, cssa []string, piece MapStringString, deli string, wrapHTML bool, beQuiet bool) error {
92+
if wrapHTML {
93+
fmt.Fprintln(bw, WrapHTMLBeg)
94+
}
9095
if len(piece.Values) == 0 {
9196
// no sub CSS selectors
9297
doc, err := html.Parse(bi)
@@ -115,9 +120,6 @@ func Cascadia(bi io.Reader, bw io.Writer, cssa []string, piece MapStringString,
115120
doc, err := goquery.NewDocumentFromReader(bi)
116121
abortOn("Input", err)
117122

118-
if wrapHTML {
119-
fmt.Fprintln(bw, WrapHTMLBeg)
120-
}
121123
// Print csv headers
122124
for _, key := range piece.Keys {
123125
fmt.Fprintf(bw, "%s%s", key, deli)
@@ -139,9 +141,9 @@ func Cascadia(bi io.Reader, bw io.Writer, cssa []string, piece MapStringString,
139141
}
140142
fmt.Fprintf(bw, "\n")
141143
})
142-
if wrapHTML {
143-
fmt.Fprintln(bw, WrapHTMLEnd)
144-
}
144+
}
145+
if wrapHTML {
146+
fmt.Fprintln(bw, WrapHTMLEnd)
145147
}
146148
return nil
147149
}

0 commit comments

Comments
 (0)