Skip to content

Commit

Permalink
- [+] add -y/--style option
Browse files Browse the repository at this point in the history
  • Loading branch information
suntong committed Jul 17, 2021
1 parent d77076b commit 71375b6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
5 changes: 5 additions & 0 deletions cascadia_cli.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ Options:
Flag: w,wrap-html
Usage: wrap up the output with html tags

- Name: Style
Type: string
Flag: y,style
Usage: style component within the wrapped html head

- Name: Base
Type: string
Flag: b,base
Expand Down
17 changes: 9 additions & 8 deletions cascadia_cliDef.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
////////////////////////////////////////////////////////////////////////////
// Program: cascadiaC
// Purpose: cascadia wrapper
// Authors: Tong Sun (c) 2020, All rights reserved
// Authors: Tong Sun (c) 2021, All rights reserved
////////////////////////////////////////////////////////////////////////////

package main
Expand Down Expand Up @@ -29,18 +29,19 @@ type rootT struct {
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"`
Deli string `cli:"d,delimiter" usage:"delimiter for pieces csv output" dft:"\t"`
WrapHTML bool `cli:"w,wrap-html" usage:"wrap up the output with html tags"`
Style string `cli:"y,style" usage:"style component within the wrapped html head"`
Base string `cli:"b,base" usage:"base href tag used in the wrapped up html"`
Quiet bool `cli:"q,quiet" usage:"be quiet"`
}

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

NumOption: cli.AtLeast(3),
}
Expand All @@ -57,6 +58,7 @@ var root = &cli.Command{
// Piece MapStringString
// Deli string
// WrapHTML bool
// Style string
// Base string
// Quiet bool
// Verbose int
Expand All @@ -68,7 +70,7 @@ var root = &cli.Command{
// var (
// progname = "cascadiaC"
// version = "0.1.0"
// date = "2020-04-19"
// date = "2021-07-16"

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

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

// func cascadiaC(ctx *cli.Context) error {
// CascadiaC - main dispatcher dumb handler
// func CascadiaC(ctx *cli.Context) error {
// ctx.JSON(ctx.RootArgv())
// ctx.JSON(ctx.Argv())
// fmt.Println()
Expand Down
24 changes: 13 additions & 11 deletions cascadia_main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
////////////////////////////////////////////////////////////////////////////
// Program: cascadia
// Purpose: go cascadia CSS selection from command line
// Authors: Tong Sun (c) 2016-2018, All rights reserved
// Authors: Tong Sun (c) 2016-2021, All rights reserved
////////////////////////////////////////////////////////////////////////////

//go:generate sh -v cascadia_cliGen.sh
Expand Down Expand Up @@ -40,8 +40,8 @@ type MapStringString struct {

var (
progname = "cascadia"
version = "1.2.3"
date = "2020-04-20"
version = "1.2.5"
date = "2021-07-16"

rootArgv *rootT
)
Expand All @@ -62,8 +62,9 @@ func main() {
//==========================================================================
// css selection

func cascadiaC(ctx *cli.Context) error {
func CascadiaC(ctx *cli.Context) error {
// ctx.JSON(ctx.RootArgv())
// fmt.Println()
// ctx.JSON(ctx.Argv())
// fmt.Println()

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

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

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

if wrapHTML {
fmt.Fprintln(bw, WrapHTMLBeg)
}
// Print csv headers
for _, key := range piece.Keys {
fmt.Fprintf(bw, "%s%s", key, deli)
Expand All @@ -139,9 +141,9 @@ func Cascadia(bi io.Reader, bw io.Writer, cssa []string, piece MapStringString,
}
fmt.Fprintf(bw, "\n")
})
if wrapHTML {
fmt.Fprintln(bw, WrapHTMLEnd)
}
}
if wrapHTML {
fmt.Fprintln(bw, WrapHTMLEnd)
}
return nil
}
Expand Down

0 comments on commit 71375b6

Please sign in to comment.