nushell/crates
kik4444 0d518bf813
query web --query should return list<list<string>> like the scraper crate's ElementRef::text() (#11705)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
## Problem
I tried converting one of my Rust web scrapers to Nushell just to see
how it would be done, but quickly ran into an issue that proved annoying
to fix without diving into the source.

For instance, let's say we have the following HTML
```html
<p>Hello there, <span style="color: red;">World</span></p>
```
and we want to extract only the text within the `p` element, but not the
`span`. With the current version of nu_plugin_query, if we run this code
```nushell
echo `<p>Hello there, <span style="color: red;">World</span></p>` | query web -q "p" | get 0
# returns "Hello there, World"

# but we want only "Hello there, "
```
we will get back a `list<string>` that contains 1 string `Hello there,
World`.
To avoid scraping the span, we would have to do something like this
```nushell
const html = `<p>Hello there, <span style="color: red;">World</span></p>`
$html
| query web -q "p"
| get 0
| str replace ($html | query web -q "p > span" | get 0) ""
# returns "Hello there, "
```
In other words, we would have to make a sub scrape of the text we
*don't* want in order to subtract it from the text we *do* want.

## Solution
I didn't like this behavior, so I decided to change it. I modified the
`execute_selector_query` function to collect all text nodes in the HTML
element matching the query. Now `query web --query` will return a
`list<list<string>>`
```nushell
echo `<p>Hello there, <span style="color: red;">World</span></p>` | query web -q "p" | get 0 | to json --raw
# returns ["Hello there, ","World"]
```
This also brings `query web --query`'s behavior more in line with
[scraper's
ElementRef::text()](https://docs.rs/scraper/latest/scraper/element_ref/struct.ElementRef.html#method.text)
which "Returns an iterator over descendent text nodes", allowing you to
choose how much of an element's text you want to scrape without
resorting to string substitutions.

## Consequences
As this is a user-facing change, the usage examples will produce
different results than before. For example
```nushell
http get https://phoronix.com | query web --query 'header'
```
will return a list of lists of 1 string each, whereas before it was just
a list of strings.

I only modified the 3rd example
```nushell
# old
http get https://www.nushell.sh | query web --query 'h2, h2 + p' | group 2 | each {rotate --ccw tagline description} | flatten
# new
http get https://www.nushell.sh | query web --query 'h2, h2 + p' | each {str join} | group 2 | each {rotate --ccw tagline description} | flatten
```
to make it behave like before because I thought this one ought to show
the same results as before.
However, the second reason I changed the 3rd example is because it
otherwise panics! If we run the original 3rd example with my
modifications, we get a panic
```
thread 'main' panicked at crates/nu-protocol/src/value/record.rs:34:9:
assertion `left == right` failed
  left: 2
 right: 17
```
This happens because `rotate` receives a list of lists where the inner
lists have a different number of elements.

However this panic is unrelated to the changes I've made, because it can
be triggered easily without using the plugin. For instance
```nushell
# this is fine
[[[one] [two]] [[three] [four]]] | each {rotate --ccw tagline description}

# this panics!
[[[one] [two]] [[three] [four five]]] | each {rotate --ccw tagline description}
```
Though beyond the scope of this PR, I thought I'd mention this bug since
I found it while testing the usage examples. However, I intend to make a
proper issue about it tomorrow.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
`query web --query "css selector"` now returns a `list<list<string>>`
instead of a `list<string>` to make it more in line with [scraper's
ElementRef::text()](https://docs.rs/scraper/latest/scraper/element_ref/struct.ElementRef.html#method.text).

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
I ran `cargo fmt --all -- --check`, `cargo clippy --workspace -- -D
warnings -D clippy::unwrap_used` and the tests in the plugin.

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
PR that updates the documentation to match the new 3rd example:
https://github.com/nushell/nushell.github.io/pull/1235
2024-02-02 19:40:47 -06:00
..
nu_plugin_custom_values Allow plugins to receive configuration from the nushell configuration (#10955) 2024-01-15 16:59:47 +08:00
nu_plugin_example Plugin explicit flags (#11581) 2024-01-22 15:00:43 -06:00
nu_plugin_formats Bump ical from 0.9.0 to 0.10.0 (#11661) 2024-01-29 09:34:06 +08:00
nu_plugin_gstat Allow plugins to receive configuration from the nushell configuration (#10955) 2024-01-15 16:59:47 +08:00
nu_plugin_inc Plugin explicit flags (#11581) 2024-01-22 15:00:43 -06:00
nu_plugin_python remove vectorize_over_list from python plugin (#9905) 2023-08-03 16:46:48 +02:00
nu_plugin_query query web --query should return list<list<string>> like the scraper crate's ElementRef::text() (#11705) 2024-02-02 19:40:47 -06:00
nu-cli Tidy up the REPL main loop (#11655) 2024-01-31 09:32:19 -08:00
nu-cmd-base Unify glob behavior on open, rm, cp-old, mv, umv, cp and du commands (#11621) 2024-01-26 21:57:35 +08:00
nu-cmd-dataframe rollback polars 0.37.0 (#11695) 2024-01-31 08:39:38 -06:00
nu-cmd-extra change update cells column param from Table to List (#11691) 2024-01-30 19:36:03 -06:00
nu-cmd-lang update nu-ansi-term to 0.50, lscolors to 0.17, and add the Style attribute to Suggestion (#11635) 2024-01-24 20:57:15 -08:00
nu-color-config update nu-ansi-term to 0.50, lscolors to 0.17, and add the Style attribute to Suggestion (#11635) 2024-01-24 20:57:15 -08:00
nu-command cp: expand target path before checking (#11692) 2024-02-01 09:06:03 +08:00
nu-engine make the ansi command const (#11682) 2024-01-30 16:09:43 -06:00
nu-explore update nu-ansi-term to 0.50, lscolors to 0.17, and add the Style attribute to Suggestion (#11635) 2024-01-24 20:57:15 -08:00
nu-glob Bump to dev version 0.89.1 (#11513) 2024-01-11 00:19:21 +13:00
nu-json Refactor lines command (#11685) 2024-01-30 15:56:19 -06:00
nu-lsp Fix "Char index out of bounds" Error (#11526) 2024-01-11 15:24:49 -06:00
nu-parser Respect SyntaxShape when parsing spread operator (#11674) 2024-01-30 13:49:42 +08:00
nu-path Bump to dev version 0.89.1 (#11513) 2024-01-11 00:19:21 +13:00
nu-plugin Plugin explicit flags (#11581) 2024-01-22 15:00:43 -06:00
nu-pretty-hex update nu-ansi-term to 0.50, lscolors to 0.17, and add the Style attribute to Suggestion (#11635) 2024-01-24 20:57:15 -08:00
nu-protocol fix exit_code handling when running a scripts with ctrlc (#11466) 2024-01-30 22:41:14 +08:00
nu-std cleanup hide testing PR (#11638) 2024-01-25 06:49:04 -06:00
nu-system Fix tarpaulin skip attribute to latest (#11552) 2024-01-17 07:26:34 -06:00
nu-table update nu-ansi-term to 0.50, lscolors to 0.17, and add the Style attribute to Suggestion (#11635) 2024-01-24 20:57:15 -08:00
nu-term-grid Bump to dev version 0.89.1 (#11513) 2024-01-11 00:19:21 +13:00
nu-test-support Remove duplicate which 4.4.2 (#11613) 2024-01-22 09:28:47 -06:00
nu-utils add match-text style + config setting for ide menu (#11670) 2024-01-29 09:59:01 -06:00
README.md Remove old nushell/merge engine-q 2022-02-07 14:54:06 -05:00

Nushell core libraries and plugins

These sub-crates form both the foundation for Nu and a set of plugins which extend Nu with additional functionality.

Foundational libraries are split into two kinds of crates:

  • Core crates - those crates that work together to build the Nushell language engine
  • Support crates - a set of crates that support the engine with additional features like JSON support, ANSI support, and more.

Plugins are likewise also split into two types:

  • Core plugins - plugins that provide part of the default experience of Nu, including access to the system properties, processes, and web-connectivity features.
  • Extra plugins - these plugins run a wide range of different capabilities like working with different file types, charting, viewing binary data, and more.