Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding --parent arg #15

Merged
merged 5 commits into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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