scrapelect 0.3.2

Interpreter for scrapelect, a CSS-inspired web scraping DSL
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#![cfg_attr(
    feature = "filter_doc",
    doc = "Documentation for `scrapelect`'s built-in filters.

Conventions used:

- Signature of a filter: `value: T | name(arg_1: U_1, ...): V` means that `name` is a filter
    that takes a *pipeline value* of type `T`, has arguments `arg_i` of type `U_i`, and returns
    a value of type `V`
  - Specifying an arg type with a question mark (e.g., `value: Value | dbg(msg: String?): Value`)
    means that that argument (e.g., `msg`) is *optional* and can be omitted.
- List shorthand: a `List` is represented as `[a_0, a_1, a_2, ..., a_n]` to mean that
its elements are `a_0, ..., a_n` in that order.  Indexing starts at 0.  This syntax is currently
not valid `scrapelect`, but it is useful to express in documentation.
- Structure shorthand: similarly, a structure is represented as { key_1: value_1, key_2: value_2, ... } to
indicate that it has keys that correspond to the given values. This is not valid `scrapelect`,
but it is useful in documentation.
- Element shorthand: an inline HTML element (e.g., `<a href=\"github.com/suaviloquence/scrapelect\">Link text</a>`)
   is also not valid `scrapelect`, but is useful to demonstrate how `Element`s are used
   in filters.
"
)]

use std::{
    collections::BTreeMap,
    sync::{Arc, LazyLock},
};

use anyhow::Context as _;

use crate::interpreter::{
    filter::{filter_fn, FilterDyn},
    value::{EValue, ListIter, PValue, Pipeline, Value},
    ElementContext,
};

type Structure<'doc> = BTreeMap<Arc<str>, PValue<'doc>>;

/// Signature: `value | id(): Value`
///
/// Returns the `value` passed into it.
///
/// # Examples
///
/// - `"hi" | id()` returns `"hi"`
#[filter_fn]
pub fn id<'doc>(value: PValue<'doc>) -> anyhow::Result<PValue<'doc>> {
    Ok(value)
}

/// Signature: `value | dbg(msg: String?): Value`
///
/// Returns the `value` passed into it, printing a copy to the console,
/// prepended with `msg` if specified.
///
/// # Examples
///
/// - `"hi" | dbg()` returns `"hi"` and prints `dbg message: "hi"`
/// - `"hi" | dbg(msg: "I say")` returns `"hi"` and prints `I say: "hi"`
#[filter_fn]
pub fn dbg<'doc>(value: PValue<'doc>, msg: Option<Arc<str>>) -> anyhow::Result<PValue<'doc>> {
    let value: EValue = value.into();
    eprintln!("{}: {}", value, msg.as_deref().unwrap_or("dbg message"));

    Ok(value.into())
}

/// Signature: `value | tee(into: String): Value`
///
/// Returns the `value` passed into it, saving a copy into the
/// variable with name `into` in the current scope.
///
/// # Examples
///
/// - `"abcde" | tee(into: "tmp")` returns `"abcde"`, and `$tmp` is also `"abcde"`
/// - Chaining filters (see [`strip`]):
///   `stripped: "   hi   " | tee(into: "spacey") | strip()` results in:
///   - `$spacey = "   hi   "`
///   - `$stripped = "hi"`
#[filter_fn]
pub fn tee<'doc>(
    value: PValue<'doc>,
    into: Arc<str>,
    ctx: &mut ElementContext<'_, 'doc>,
) -> anyhow::Result<PValue<'doc>> {
    let value: EValue = value.into();
    ctx.set_var(into.to_string().into(), value.clone())?;
    Ok(value.into())
}

/// Signature: `value: String | strip(): String`
///
/// Removes leading and trailing whitespace from a string.
///
/// # Examples
///
/// - `"    helloooo   ooo   " | strip()` returns `"helloooo   ooo"`
/// - `"    " | strip()` returns `""`
#[filter_fn]
pub fn strip<'doc>(value: Arc<str>) -> anyhow::Result<PValue<'doc>> {
    Ok(Value::String(value.trim().into()))
}

/// Signature: `value: Element | attrs(): Structure`
///
/// Returns the attributes of an element as key-value records in the structure.
///
/// Note that all values will be strings, use filters like [`int`] to convert them if needed.
///
/// # Examples
///
/// - `<img src="./cat.png" alt="a Kitty Cat" /> | attrs()` returns `{ src: "./cat.png", alt: "a Kitty Cat" }`
/// - `<p>Hello!</p> | attrs()` returns `{}`
#[filter_fn]
pub fn attrs<'doc>(value: scraper::ElementRef<'doc>) -> anyhow::Result<PValue<'doc>> {
    Ok(Value::Structure(
        value
            .value()
            .attrs()
            .map(|(k, v)| (Arc::from(k), Value::String(Arc::from(v))))
            .collect(),
    ))
}

/// Signature: `value: Structure | take(key: String): Value`
///
/// Returns the value at key `key` in the structure.  If there is no value there,
/// returns `Null`.
///
/// # Examples
///
/// - `{ kitty: "cat" } | take(key: "kitty")` returns `"cat"`
/// - `{ kitty: "cat" } | take(key: "cat")` returns `null`
#[filter_fn]
pub fn take<'doc>(mut value: Structure<'doc>, key: Arc<str>) -> anyhow::Result<PValue<'doc>> {
    Ok(value.remove(&key).unwrap_or(Value::Null))
}

/// Signature: `value: (String or Int or Float) | int(): Int`
///
/// Turns the value into an `Int`.  If it is a String, it must be a valid
/// representation of an interger; if not it will raise an error.  If a `Float`
/// is passed to this filter, it will truncate (round down) to the nearest integer.
///
/// # Examples
///
/// - `1 | int()` returns `1`
/// - `1.5 | int()` returns `1`
/// - `"1" | int()` returns `1`
/// - `">_<" | int()` raises an error.
#[filter_fn]
pub fn int<'doc>(value: PValue<'doc>) -> anyhow::Result<PValue<'doc>> {
    let n = match value {
        Value::Int(n) => n,
        Value::Float(x) => x as i64,
        Value::String(s) => s
            .parse()
            .with_context(|| format!("`{s}` is not an integer."))?,
        _ => anyhow::bail!("expected an int, float, or string"),
    };

    Ok(Value::Int(n))
}

/// Signature: `value: (String or Int or Float) | float(): Float`
///
/// Turns the value into a `Float`.  If it is a String, it must be a valid
/// representation of a float; if not it will raise an error.
///
/// # Examples
///
/// - `1 | float()` returns `1.0`
/// - `1.5 | float()` returns `1.5`
/// - `"1.5" | float()` returns `1.5`
/// - `">_<" | float()` raises an error.
#[filter_fn]
pub fn float<'doc>(value: PValue<'doc>) -> anyhow::Result<PValue<'doc>> {
    let x = match value {
        Value::Int(n) => n as f64,
        Value::Float(x) => x,
        Value::String(s) => s
            .parse()
            .with_context(|| format!("`{s}` is not a float."))?,
        _ => anyhow::bail!("expected an int, float, or string"),
    };

    Ok(Value::Float(x))
}

/// Signature: `value: List | nth(i: Int): Value`
///
/// Obtains the `i`th element in the list `value`, starting from zero.
///
/// Negative indices or indices >= the `length` of the list are invalid arguments
/// and will raise an error.
///
/// # Examples
///
/// - `[1, 2, 3, 4] | nth(i: 0)` returns `1`
/// - `[1, 2, 3, 4] | nth(i: 4)` raises an out-of-bounds error.
#[filter_fn]
pub fn nth<'doc>(mut value: ListIter<'doc>, i: i64) -> anyhow::Result<PValue<'doc>> {
    match value.nth(i.try_into().context("negative indices are not supported")?) {
        Some(x) => Ok(x),
        None => anyhow::bail!("No element at index {i}"),
    }
}

/// Signature: `value: Structure | keys(): List<String>`
///
/// Turns the structure `value` into a list of the *keys* of the structure.
///
/// Output is in alphabetical order.
///
/// # Examples
///
/// - `{a: 1, b: "hello"} | values()` returns `["a", "b"]`
/// - `{} | values()` returns `[]`
#[filter_fn]
pub fn keys<'doc>(value: Structure<'doc>) -> anyhow::Result<PValue<'doc>> {
    Ok(Value::Extra(Pipeline::ListIter(Box::new(
        value.into_keys().map(Value::String),
    ))))
}

/// Signature: `value: Structure | values(): List`
///
/// Turns the structure `value` into a list of the *values* of the structure.
///
/// Output is in alphabetical order by key, duplicates are included.
///
/// # Examples
///
/// - `{a: 1, b: "hello"} | values()` returns `[1, "hello"]`
/// - `{} | values()` returns `[]`
#[filter_fn]
pub fn values<'doc>(value: Structure<'doc>) -> anyhow::Result<PValue<'doc>> {
    Ok(Value::Extra(Pipeline::ListIter(Box::new(
        value.into_values(),
    ))))
}

/// Signature: `value: Bool | and(with: Bool): Bool`
///
/// Returns the boolean AND of `value` and `with`: `true` if both
/// (or both) of `value` and `and` are true.
///
/// # Examples
///
/// - `true | or(with: true)` returns `true`
/// - `false | or(with: true)` returns `false`
#[filter_fn]
pub fn and<'doc>(value: bool, with: bool) -> anyhow::Result<PValue<'doc>> {
    Ok(Value::Bool(value && with))
}

/// Signature: `value: Bool | or(with: Bool): Bool`
///
/// Returns the boolean OR of `value` and `with`: `true` if either
/// (or both) of `value` and `and` is true.
///
/// # Examples
///
/// - `true | or(with: false)` returns `true`
/// - `false | or(with: false)` returns `false`
#[filter_fn]
pub fn or<'doc>(value: bool, with: bool) -> anyhow::Result<PValue<'doc>> {
    Ok(Value::Bool(value || with))
}

/// Signature: `value: Bool | not(): Bool`
///
/// Returns the boolean NOT (opposite)  of `value`.
///
/// # Examples
///
/// - `true | not()` returns `false`
/// - `false | not()` returns `true`
#[filter_fn]
pub fn not<'doc>(value: bool) -> anyhow::Result<PValue<'doc>> {
    Ok(Value::Bool(!value))
}

/// Signature: `value: String | is_in(on: String?): List<String>`
///
/// Splits a `String` into a `List` of substrings, on the specified delimiter `on`
/// or over whitespace if `on` is not set.
///
/// # Examples
///
/// - `"my very excellent mother" | split()` returns `["my", "very", "excellent", "mother"]`
/// - `"my very excellent mother" | split(on: "excellent") returns `["my very ", " mother"]
/// - `"my very excellent mother" | split(on: "HAMPSTERS") returns ["my very excellent mother"]
#[filter_fn]
pub fn split<'doc>(value: Arc<str>, on: Option<Arc<str>>) -> anyhow::Result<PValue<'doc>> {
    if let Some(delim) = on {
        Ok(Value::List(
            value
                .split(&*delim)
                .map(|x| Value::String(Arc::from(x)))
                .collect(),
        ))
    } else {
        Ok(Value::List(
            value
                .split_whitespace()
                .map(|x| Value::String(Arc::from(x)))
                .collect(),
        ))
    }
}

/// Signature: `value | eq(to: Value): Bool`
///
/// Takes a two values `value` and `to`, and returns whether `value` equal to `to`.
///
/// # Examples
///
/// - `2 | eq(to: 2)` is `true`
/// - `6 | eq(to: 2)` is `false`.
#[filter_fn]
pub fn eq<'doc>(value: PValue<'doc>, to: EValue<'doc>) -> anyhow::Result<PValue<'doc>> {
    Ok(Value::Bool(EValue::from(value) == to))
}

/// Signature: `value | is_in(list: List): Bool`
///
/// Takes a value and a List `list`, and returns whether `value` is in the `List`
///
/// # Examples
///
/// Let `list: [1, 2, 3, 4, 5];`:
///
/// Then `2 | is_in(list: $list)` is `true`, but `6 | is_in(list: $list)` is `false.
#[filter_fn]
pub fn is_in<'doc>(value: PValue<'doc>, list: Vec<EValue<'doc>>) -> anyhow::Result<PValue<'doc>> {
    Ok(Value::Bool(list.contains(&value.into())))
}

/// Signature: `value | truthy(): Bool`
///
/// Takes a `value` and converts it into a `Bool`, based on whether it is "truthy".
///
/// Rules:
///
/// - `Null` is `false`
/// - `Int`s and `Float`s are `true` if they are nonzero.
/// - `String`s, `List`s, and `Structure`s are `true` if they are nonempty
/// - `Element`s are `true`
/// - `Bool`s are the boolean value
///
/// # Examples
///
/// - `"" | truthy()` is `false`
/// - `"hi!" | truthy()` is `true`
/// - `0 | truthy()` is `false`
/// - `0.6931 | truthy()` is `true`
/// - `null | truthy()` is `false`
/// - `[null] | truthy()` is `true` (a List containing `null` is nonempty)
/// - `true | truthy()` is `true`
#[filter_fn]
pub fn truthy<'doc>(value: PValue<'doc>) -> anyhow::Result<PValue<'doc>> {
    let truthy = match value {
        Value::Null => false,
        Value::Float(f) => f != 0.,
        Value::Int(i) => i != 0,
        Value::Bool(b) => b,
        Value::String(s) => !s.is_empty(),
        Value::List(l) => !l.is_empty(),
        Value::Structure(s) => !s.is_empty(),
        Value::Extra(Pipeline::Element(_)) => true,
        Value::Extra(Pipeline::ListIter(mut i)) => i.next().is_some(),
        Value::Extra(Pipeline::StructIter(mut i)) => i.next().is_some(),
    };

    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,
    )*) => {
        [$(
            (stringify!($id), Box::new($id()) as Box<dyn FilterDyn + Send + Sync>),

        )*]
    };
}

#[cfg(not(feature = "filter_doc"))]
pub static FILTERS: LazyLock<BTreeMap<&'static str, Box<dyn FilterDyn + Send + Sync>>> =
    LazyLock::new(|| {
        build_map! {
            dbg,
            tee,
            strip,
            take,
            attrs,
            int,
            float,
            nth,
            keys,
            values,
            and,
            or,
            not,
            split,
            eq,
            is_in,
            text,
        }
        .into_iter()
        .collect()
    });