mirror of
https://github.com/nushell/nushell
synced 2025-01-15 22:54:16 +00:00
Remove dependencies (#4087)
* fix regression * Removed the nipper dependency * fix linting * fix clippy
This commit is contained in:
parent
ab2d2db987
commit
72c241348b
5 changed files with 46 additions and 62 deletions
14
Cargo.lock
generated
14
Cargo.lock
generated
|
@ -2421,19 +2421,6 @@ dependencies = [
|
||||||
"smallvec",
|
"smallvec",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "nipper"
|
|
||||||
version = "0.1.9"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "761382864693f4bb171abf9e8de181a320b00464a83a9a5071059057b1fe0116"
|
|
||||||
dependencies = [
|
|
||||||
"cssparser",
|
|
||||||
"html5ever",
|
|
||||||
"markup5ever",
|
|
||||||
"selectors",
|
|
||||||
"tendril",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nix"
|
name = "nix"
|
||||||
version = "0.20.1"
|
version = "0.20.1"
|
||||||
|
@ -3037,7 +3024,6 @@ name = "nu_plugin_selector"
|
||||||
version = "0.38.0"
|
version = "0.38.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"indexmap",
|
"indexmap",
|
||||||
"nipper",
|
|
||||||
"nu-errors",
|
"nu-errors",
|
||||||
"nu-plugin",
|
"nu-plugin",
|
||||||
"nu-protocol",
|
"nu-protocol",
|
||||||
|
|
|
@ -10,7 +10,6 @@ version = "0.38.0"
|
||||||
doctest = false
|
doctest = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
nipper = "0.1.9"
|
|
||||||
scraper = "0.12.0"
|
scraper = "0.12.0"
|
||||||
nu-errors = { version = "0.38.0", path="../nu-errors" }
|
nu-errors = { version = "0.38.0", path="../nu-errors" }
|
||||||
nu-plugin = { version = "0.38.0", path="../nu-plugin" }
|
nu-plugin = { version = "0.38.0", path="../nu-plugin" }
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
|
use crate::{selector::begin_selector_query, Selector};
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_plugin::Plugin;
|
use nu_plugin::Plugin;
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
CallInfo, Primitive, ReturnSuccess, ReturnValue, Signature, SyntaxShape, UntaggedValue, Value,
|
CallInfo, Primitive, ReturnSuccess, ReturnValue, Signature, SyntaxShape, UntaggedValue, Value,
|
||||||
};
|
};
|
||||||
|
use scraper::Selector as ScraperSelector;
|
||||||
use crate::{selector::begin_selector_query, Selector};
|
|
||||||
|
|
||||||
impl Plugin for Selector {
|
impl Plugin for Selector {
|
||||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||||
|
@ -63,6 +63,13 @@ impl Plugin for Selector {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
||||||
|
if !self.query.is_empty() && ScraperSelector::parse(&self.query).is_err() {
|
||||||
|
return Err(ShellError::labeled_error(
|
||||||
|
"Can not parse this query as a valid css selector",
|
||||||
|
"Parse error",
|
||||||
|
&self.tag,
|
||||||
|
));
|
||||||
|
}
|
||||||
match input {
|
match input {
|
||||||
Value {
|
Value {
|
||||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::Table;
|
use crate::Table;
|
||||||
use nipper::Document;
|
|
||||||
use nu_protocol::{value::StringExt, Primitive, TaggedDictBuilder, UntaggedValue, Value};
|
use nu_protocol::{value::StringExt, Primitive, TaggedDictBuilder, UntaggedValue, Value};
|
||||||
use nu_source::Tag;
|
use nu_source::Tag;
|
||||||
|
use scraper::{Html, Selector as ScraperSelector};
|
||||||
|
|
||||||
pub struct Selector {
|
pub struct Selector {
|
||||||
pub query: String,
|
pub query: String,
|
||||||
|
@ -166,13 +166,14 @@ fn execute_selector_query_with_attribute(
|
||||||
query_string: &str,
|
query_string: &str,
|
||||||
attribute: &str,
|
attribute: &str,
|
||||||
) -> Vec<Value> {
|
) -> Vec<Value> {
|
||||||
let doc = Document::from(input_string);
|
let doc = Html::parse_fragment(input_string);
|
||||||
|
|
||||||
doc.select(query_string)
|
doc.select(&css(query_string))
|
||||||
.iter()
|
|
||||||
.map(|selection| {
|
.map(|selection| {
|
||||||
selection
|
selection
|
||||||
.attr_or(attribute, "")
|
.value()
|
||||||
|
.attr(attribute)
|
||||||
|
.unwrap_or("")
|
||||||
.to_string()
|
.to_string()
|
||||||
.to_string_value_create_tag()
|
.to_string_value_create_tag()
|
||||||
})
|
})
|
||||||
|
@ -180,57 +181,51 @@ fn execute_selector_query_with_attribute(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute_selector_query(input_string: &str, query_string: &str, as_html: bool) -> Vec<Value> {
|
fn execute_selector_query(input_string: &str, query_string: &str, as_html: bool) -> Vec<Value> {
|
||||||
let doc = Document::from(input_string);
|
let doc = Html::parse_fragment(input_string);
|
||||||
|
|
||||||
match as_html {
|
match as_html {
|
||||||
true => doc
|
true => doc
|
||||||
.select(query_string)
|
.select(&css(query_string))
|
||||||
.iter()
|
.map(|selection| selection.html().to_string_value_create_tag())
|
||||||
.map(|selection| selection.html().to_string().to_string_value_create_tag())
|
|
||||||
.collect(),
|
.collect(),
|
||||||
false => doc
|
false => doc
|
||||||
.select(query_string)
|
.select(&css(query_string))
|
||||||
.iter()
|
.map(|selection| {
|
||||||
.map(|selection| selection.text().to_string().to_string_value_create_tag())
|
selection
|
||||||
|
.text()
|
||||||
|
.fold("".to_string(), |acc, x| format!("{}{}", acc, x))
|
||||||
|
.to_string_value_create_tag()
|
||||||
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn css(selector: &str) -> ScraperSelector {
|
||||||
|
ScraperSelector::parse(selector).expect("this should never trigger")
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use nipper::Document;
|
use super::*;
|
||||||
|
|
||||||
|
const SIMPLE_LIST: &'static str = r#"
|
||||||
|
<ul>
|
||||||
|
<li>Coffee</li>
|
||||||
|
<li>Tea</li>
|
||||||
|
<li>Milk</li>
|
||||||
|
</ul>
|
||||||
|
"#;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn create_document_from_string() {
|
fn test_first_child_is_not_empty() {
|
||||||
let html = r#"<div name="foo" value="bar"></div>"#;
|
assert!(!execute_selector_query(SIMPLE_LIST, "li:first-child", false).is_empty())
|
||||||
let document = Document::from(html);
|
|
||||||
let shouldbe =
|
|
||||||
r#"<html><head></head><body><div name="foo" value="bar"></div></body></html>"#;
|
|
||||||
|
|
||||||
assert_eq!(shouldbe.to_string(), document.html().to_string());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn modify_html_document() {
|
fn test_first_child() {
|
||||||
let html = r#"<div name="foo" value="bar"></div>"#;
|
assert_eq!(
|
||||||
let document = Document::from(html);
|
vec!["Coffee".to_string().to_string_value_create_tag()],
|
||||||
let mut input = document.select(r#"div[name="foo"]"#);
|
execute_selector_query(SIMPLE_LIST, "li:first-child", false)
|
||||||
input.set_attr("id", "input");
|
)
|
||||||
input.remove_attr("name");
|
|
||||||
|
|
||||||
let shouldbe = "bar".to_string();
|
|
||||||
let actual = input.attr("value").unwrap().to_string();
|
|
||||||
|
|
||||||
assert_eq!(shouldbe, actual);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[test]
|
|
||||||
// fn test_hacker_news() -> Result<(), ShellError> {
|
|
||||||
// let html = reqwest::blocking::get("https://news.ycombinator.com")?.text()?;
|
|
||||||
// let document = Document::from(&html);
|
|
||||||
// let result = query(html, ".hnname a".to_string(), Tag::unknown());
|
|
||||||
// let shouldbe = Ok(vec!["Hacker News".to_str_value_create_tag()]);
|
|
||||||
// assert_eq!(shouldbe, result);
|
|
||||||
// Ok(())
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::selector::css;
|
||||||
use scraper::{element_ref::ElementRef, Html, Selector as ScraperSelector};
|
use scraper::{element_ref::ElementRef, Html, Selector as ScraperSelector};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
@ -263,10 +264,6 @@ impl<'a> IntoIterator for Row<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn css(selector: &'static str) -> ScraperSelector {
|
|
||||||
ScraperSelector::parse(selector).expect("Unable to parse selector with scraper")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn select_cells(
|
fn select_cells(
|
||||||
element: ElementRef,
|
element: ElementRef,
|
||||||
selector: &ScraperSelector,
|
selector: &ScraperSelector,
|
||||||
|
|
Loading…
Reference in a new issue