Skip to content

Commit

Permalink
Downgrade text from a language feature to a filter
Browse files Browse the repository at this point in the history
- no longer cached, could change this
  • Loading branch information
suaviloquence committed Aug 7, 2024
1 parent 1775b60 commit b87541b
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 36 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ For example, take the input element `<p id="meowing">Meow mrrp meow meow!</p>`.

```
meow_element: #meowing {
meows: $text;
meows: $element | text();
};
```

Expand Down Expand Up @@ -177,7 +177,7 @@ this scrp
```
parent: #parent {
child: p {
child_text: $text;
child_text: $element | text();
}
};
```
Expand Down
4 changes: 2 additions & 2 deletions doc/src/quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ A `scrapelect` program stored in a `name.scrp` file. Let's create and edit the

```scrp
title: .mw-page-title-main {
content: $text;
content: $element | text();
};
headings: .mw-heading > * {
content: $text;
content: $element | text();
}*;
```

Expand Down
2 changes: 1 addition & 1 deletion doc/theme/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -3358,7 +3358,7 @@ if (typeof exports === "object" && typeof module !== "undefined") {
function scrp(hljs) {
return {
case_insensitive: false,
keywords: ["text", "element"],
keywords: ["element"],
contains: [hljs.C_LINE_COMMENT_MODE, hljs.QUOTE_STRING_MODE],
};
}
Expand Down
2 changes: 1 addition & 1 deletion examples/outputs/abc.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ expression: result
"link": {
"parent": "Hello, world!"
},
"txt": "Hello, world!"
"text": "Hello, world!"
}
}
16 changes: 8 additions & 8 deletions examples/outputs/qualifiers.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,36 @@ expression: result
"items": [
{
"special": null,
"txt": "Hello! 1"
"text": "Hello! 1"
},
{
"special": "special!",
"txt": "Hello! 2"
"text": "Hello! 2"
},
{
"special": null,
"txt": "Hello! 3"
"text": "Hello! 3"
},
{
"special": null,
"txt": "Hello! 4"
"text": "Hello! 4"
},
{
"special": null,
"txt": "Hello! 5"
"text": "Hello! 5"
},
{
"special": null,
"txt": "Hello! 6"
"text": "Hello! 6"
},
{
"special": null,
"txt": "Hello! 7"
"text": "Hello! 7"
}
],
"sixth": {
"special": null,
"txt": "Hello! 6"
"text": "Hello! 6"
},
"specials": [
null,
Expand Down
4 changes: 2 additions & 2 deletions examples/scrps/abc.scrp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
header: h2 {
txt: $text;
text: $element | text();

link: a {
child: $element;
parent: $txt;
parent: $text;
};
};
4 changes: 2 additions & 2 deletions examples/scrps/qualifiers.scrp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
items: li {
special: .special { txt: $text; }? | take(key: "txt")?;
txt: $text;
special: .special { text: $element | text(); }? | take(key: "text")?;
text: $element | text();
}*;

specials: $items | take(key: "special")*;
Expand Down
2 changes: 1 addition & 1 deletion examples/scrps/recurser.scrp
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ link: a {
} | take(key: "href");

new_page: <$link> h1 {
content: $text;
content: $element | text();
};
2 changes: 1 addition & 1 deletion examples/scrps/relative.scrp
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ href: a {
} | take(key: "href");

relative: <$href> #success {
x: $text;
x: $element | text();
} | take(key: "x");
22 changes: 22 additions & 0 deletions src/interpreter/filter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,27 @@ pub fn truthy<'doc>(value: PValue<'doc>) -> anyhow::Result<PValue<'doc>> {
Ok(Value::Bool(truthy))
}

/// Signature: `value: Element | text(): String`
///
/// Returns the text contained inside this element. Note that this is just
/// direct text, not the text of any descendent elements.
///
/// # Examples
///
/// - Let `element` = `<div>Hello...<span>inner</span>...world!</div>`
/// - Then `$element | text()` is `"Hello......world!"`
/// - `<img /> | text()` is `""`
#[filter_fn]
pub fn text<'doc>(value: scraper::ElementRef<'doc>) -> anyhow::Result<PValue<'doc>> {
Ok(Value::String(
value
.children()
.filter_map(|x| x.value().as_text().map(|text| &*text.text))
.collect::<String>()
.into(),
))
}

macro_rules! build_map {
($(
$id: ident,
Expand Down Expand Up @@ -407,6 +428,7 @@ pub static FILTERS: LazyLock<BTreeMap<&'static str, Box<dyn FilterDyn + Send + S
split,
eq,
is_in,
text,
}
.into_iter()
.collect()
Expand Down
22 changes: 6 additions & 16 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, cell::OnceCell, collections::BTreeMap, sync::Arc};
use std::{borrow::Cow, collections::BTreeMap, sync::Arc};

use anyhow::Context;
use execution_mode::ExecutionMode;
Expand Down Expand Up @@ -86,7 +86,6 @@ impl<'ast> From<DataVariables<'ast>> for Value {
pub struct ElementContext<'ast, 'ctx> {
variables: Variables<'ast, 'ctx>,
element: scraper::ElementRef<'ctx>,
text: OnceCell<Arc<str>>,
parent: Option<&'ctx ElementContext<'ast, 'ctx>>,
url: Url,
}
Expand Down Expand Up @@ -162,7 +161,6 @@ impl<'ast> Interpreter<'ast> {
element,
parent,
variables: Variables::default(),
text: OnceCell::new(),
url,
};

Expand Down Expand Up @@ -264,7 +262,6 @@ impl<'ast> Interpreter<'ast> {
let mut inner_scope = ElementContext {
element: ctx.element,
variables: Variables::default(),
text: OnceCell::new(),
parent: Some(ctx),
url: ctx.url.clone(),
};
Expand Down Expand Up @@ -325,13 +322,6 @@ impl<'ast, 'ctx> ElementContext<'ast, 'ctx> {
pub fn get_var(&self, id: &str) -> anyhow::Result<EValue<'ctx>> {
match id {
"element" => Ok(self.element.into()),
"text" => Ok(Value::String(Arc::clone(self.text.get_or_init(|| {
self.element
.children()
.filter_map(|x| x.value().as_text().map(|x| &**x))
.collect::<String>()
.into()
})))),
var => match self.variables.0.get(var) {
Some(var) => Ok(var.clone()),
None => self
Expand All @@ -344,7 +334,7 @@ impl<'ast, 'ctx> ElementContext<'ast, 'ctx> {

pub fn set_var(&mut self, name: Cow<'ast, str>, value: EValue<'ctx>) -> anyhow::Result<()> {
match &*name {
immutable @ ("element" | "text") => {
immutable @ "element" => {
anyhow::bail!("Can't assign to immutable variable `{immutable}`")
}
_ => self.variables.0.insert(name, value),
Expand Down Expand Up @@ -441,11 +431,11 @@ mod tests {
let output = super::interpret_string_harness(
r#"
h3: h3 {
txt: $text;
text: $element | text();
a: a {
child: $text;
parent: $txt;
child: $element | text();
parent: $text;
}*;
div: div {}?;
};
Expand Down Expand Up @@ -488,7 +478,7 @@ mod tests {
);

assert!(
match d.get("txt") {
match d.get("text") {
Some(String(x)) => &**x == "Hello,parent!",
_ => false,
},
Expand Down

0 comments on commit b87541b

Please sign in to comment.