Skip to content

Commit

Permalink
Merge pull request #15 from Lucas-C/master
Browse files Browse the repository at this point in the history
Adding --parent arg
  • Loading branch information
plainas authored Jun 12, 2018
2 parents f136b07 + d932665 commit a4a6a86
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ For a modern, user friendly http client, check httpie. Or you can just use curl,

* `-t, --text`
Outputs only the inner text of the selected elements.

* `-p, --parent`
Select the parent elements instead.

* `-q, --squash`
Squash lines.
Expand Down
3 changes: 3 additions & 0 deletions doc/tq.1
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ Perform a css query with SELECTOR on an html document passed to the standard inp
\fB\-t, \-\-text\fR Outputs only the inner text of the selected elements\.
.
.IP "\(bu" 4
\fB\-p, \-\-parrent\fR Select the parent elements instead\.
.
.IP "\(bu" 4
\fB\-q, \-\-squash\fR Squash lines\.
.
.IP "\(bu" 4
Expand Down
3 changes: 3 additions & 0 deletions doc/tq.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Perform a css query with SELECTOR on an html document passed to the standard inp

* `-t, --text`
Outputs only the inner text of the selected elements.

* `-p, --parent`
Select the elements instead.

* `-q, --squash`
Squash lines.
Expand Down
5 changes: 3 additions & 2 deletions tq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
parser.add_argument("-J", "--json", action="store_true", help="Output as json array of strings.")
parser.add_argument("-v", "--version", action="store_true", help="Ouputs tq version")
parser.add_argument("-a", "--attr", help="Ouputs only te contents of given HTML attribute of selected elements")
parser.add_argument("-p", "--parent", action="store_true", help="Select the parents of the elements matching the selector")

args = parser.parse_args()

Expand All @@ -54,6 +55,8 @@ def get_els(css_selector):

selected_els = get_els(args.selector)

if args.parent:
selected_els = [el.parent for el in selected_els]

if args.attr:
selected_els = [el.get(args.attr) for el in selected_els if args.attr in el.attrs]
Expand All @@ -68,11 +71,9 @@ def get_els(css_selector):
selected_els = [el.replace('\t', ' ') for el in selected_els]
selected_els = [' '.join( el.split(' ')) for el in selected_els]


if args.json or args.json_lines:
selected_els = [json.dumps(str(el_text)) for el_text in selected_els]


if args.json:
sys.stdout.write(json.dumps(selected_els, indent=1))
sys.stdout.write("\n")
Expand Down

0 comments on commit a4a6a86

Please sign in to comment.