readstor 0.6.0

A CLI for Apple Books annotations
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
//! Defines functions for string creation/manipulation.

use std::collections::BTreeSet;
use std::ffi::OsStr;

use chrono::DateTime;
use chrono::Utc;
use deunicode::deunicode;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::Serialize;

use super::result::Result;
use crate::render::engine::RenderEngine;

/// Captures a `#tag`. Tags *must* start with a hash symbol `#` followed by a letter in `[a-zA-Z]`
/// and then a series of any characters. A tag ends when a space or another `#` is encountered.
static RE_TAG: Lazy<Regex> = Lazy::new(|| Regex::new(r"#[a-zA-Z][^\s#]+\s?").unwrap());

/// Captures three or more consecutive linebreaks.
static RE_BLOCKS: Lazy<Regex> = Lazy::new(|| Regex::new(r"\n{3,}").unwrap());

/// Strips a string of a set of characters.
///
/// # Arguments
///
/// * `string` - The input string.
/// * `chars` - Characters to strip out.
#[must_use]
pub fn strip(string: &str, chars: &str) -> String {
    let mut stripped = string.to_string();

    stripped.retain(|char| !chars.contains(char));

    stripped
}

/// Removes/replaces problematic characters from a string.
///
/// # Arguments
///
/// * `string` - The string to sanitize.
#[must_use]
pub fn sanitize(string: &str) -> String {
    // These characters can potentially cause problems in filenames.
    let remove = &['\n', '\r', '\0'];
    let replace = &['/', ':'];

    let sanitized: String = string
        .chars()
        .filter(|c| !remove.contains(c))
        .map(|c| if replace.contains(&c) { '_' } else { c })
        .collect();

    let sanitized = OsStr::new(&sanitized);
    let sanitized = sanitized.to_string_lossy().to_string();

    if sanitized != string {
        log::warn!("the string '{}' contained invalid characters", string);
    };

    sanitized
}

/// Slugifies a string.
///
/// Re-implementation of: <https://github.com/Stebalien/slug-rs/> but with an additional argument to
/// toggle whether or not to drop the case of the slugified string.
///
/// # Arguments
///
/// * `string` - The input string.
/// * `lowercase` - Toggle dropping the case of the string.
#[must_use]
pub fn to_slug(string: &str, lowercase: bool) -> String {
    let mut slug = String::with_capacity(string.len());

    // Start `true` to avoid any leading dashes.
    let mut prev_is_dash = true;

    {
        let mut push_char = |mut char: u8| match char {
            b'a'..=b'z' | b'0'..=b'9' => {
                prev_is_dash = false;
                slug.push(char.into());
            }
            b'A'..=b'Z' => {
                prev_is_dash = false;

                char = if lowercase { char - b'A' + b'a' } else { char };

                slug.push(char.into());
            }
            _ => {
                if !prev_is_dash {
                    slug.push('-');
                    prev_is_dash = true;
                }
            }
        };

        for char in string.chars() {
            if char.is_ascii() {
                (push_char)(char as u8);
            } else {
                for &byte in deunicode::deunicode_char(char).unwrap_or("-").as_bytes() {
                    (push_char)(byte);
                }
            }
        }
    }

    if slug.ends_with('-') {
        slug.pop();
    }

    slug.shrink_to_fit();

    slug
}

/// Slugifies a date.
///
/// # Arguments
///
/// * `date` - The date to slugify.
#[must_use]
pub fn to_slug_date(date: &DateTime<Utc>) -> String {
    date.format(crate::defaults::DATE_FORMAT_SLUG).to_string()
}

/// Renders a one-off template string with a context and sanitizes the output string.
///
/// # Errors
///
/// Will return `Err` if the render engine encounters any errors.
pub fn render_and_sanitize<C>(template: &str, context: C) -> Result<String>
where
    C: Serialize,
{
    let string = RenderEngine::default().render_str(template, context)?;

    Ok(sanitize(&string))
}

/// Builds a filename from a file stem and extension and sanitizes the output string.
///
/// This is a helper method to replace `PathBuf::set_extension()` as some file stems might include
/// a period `.`. If we used `PathBuf::set_extension()`, the text after the last period would be
/// replaced with the extension.
///
/// # Arguments
///
/// * `file_stem` - The file stem.
/// * `extension` - The file extension.
#[must_use]
pub fn build_filename_and_sanitize(file_stem: &str, extension: &str) -> String {
    let filename = format!("{file_stem}.{extension}");

    sanitize(&filename)
}

/// Trims whitespace and replaces all linebreaks with: `\n\n`.
///
/// # Arguments
///
/// * `string` - The string to normalize.
#[must_use]
pub fn normalize_whitespace(string: &str) -> String {
    string
        .lines()
        .filter(|&s| !s.is_empty())
        .map(str::trim)
        .map(ToOwned::to_owned)
        .collect::<Vec<_>>()
        .join("\n\n")
}

/// Extracts all `#tags` from a string.
///
/// # Arguments
///
/// * `string` - The string to extract from.
#[must_use]
pub fn extract_tags(string: &str) -> BTreeSet<String> {
    let mut tags = RE_TAG
        .find_iter(string)
        .map(|t| t.as_str())
        .map(str::trim)
        .map(ToOwned::to_owned)
        .collect::<Vec<String>>();

    tags.sort();

    BTreeSet::from_iter(tags)
}

/// Removes all `#tags` from a string.
///
/// # Arguments
///
/// * `string` - The string to remove from.
#[must_use]
pub fn remove_tags(string: &str) -> String {
    RE_TAG.replace_all(string, "").trim().to_owned()
}

/// Converts all Unicode characters to their ASCII equivalent.
///
/// # Arguments
///
/// * `string` - The string to convert.
#[must_use]
pub fn convert_all_to_ascii(string: &str) -> String {
    deunicode(string)
}

/// Converts a subset of "smart" Unicode symbols to their ASCII equivalents.
///
/// See [`UNICODE_TO_ASCII_SYMBOLS`][symbols] for list of symbols and their ASCII equivalents.
///
/// # Arguments
///
/// * `string` - The string to convert.
///
/// [symbols]: crate::defaults::UNICODE_TO_ASCII_SYMBOLS
#[must_use]
pub fn convert_symbols_to_ascii(string: &str) -> String {
    let mut string = string.to_owned();

    for (from, to) in &*crate::defaults::UNICODE_TO_ASCII_SYMBOLS {
        string = string.replace(*from, to);
    }

    string
}

/// Normalizes linebreaks by replacing three or more consecutive linebreaks with two consecutive
/// linebreaks while leaving a single trailing linebreak.
///
/// NOTE: This is a temporary solution that naively mimicks what [`tera`][tera] would do if/when it
/// adds [`trim_blocks`][github-tera]. It is by no means smart and will just normalize whitespace
/// regardless of what the template requested.
///
/// # Arguments
///
/// * `string` - The string to normalize.
///
/// [github-tera]: https://github.com/Keats/tera/issues/637
/// [tera]: https://docs.rs/tera/latest/tera/
#[must_use]
pub fn trim_blocks(string: &str) -> String {
    let string = RE_BLOCKS.replace_all(string, "\n\n");
    let mut string = string.trim_end().to_string();

    string.push('\n');

    string
}

// TODO: Add tests for other functions.
#[cfg(test)]
mod test {

    use super::*;

    #[test]
    fn strip() {
        assert_eq!(
            super::strip("Lorem ipsum. Aedipisicing culpa!?", " .!?"),
            "LoremipsumAedipisicingculpa"
        );
        assert_eq!(
            super::strip("Lorem ipsum.\n   Aedipisicing culpa!?", " .!?\n"),
            "LoremipsumAedipisicingculpa"
        );
        assert_eq!(
            super::strip("--Lorem--ipsum. Aedipisicing   -culpa-", " .-"),
            "LoremipsumAedipisicingculpa"
        );
        assert_eq!(
            super::strip("Lorem & Ipsúm. Ædipisicing culpa!?", " &.!?"),
            "LoremIpsúmÆdipisicingculpa"
        );
    }

    #[test]
    fn slugify_original() {
        assert_eq!(
            super::to_slug("Lorem ipsum. Aedipisicing culpa!?", true),
            "lorem-ipsum-aedipisicing-culpa"
        );
        assert_eq!(
            super::to_slug("Lorem ipsum.\n   Aedipisicing culpa!?", true),
            "lorem-ipsum-aedipisicing-culpa"
        );
        assert_eq!(
            super::to_slug("--Lorem--ipsum. Aedipisicing   -culpa-", true),
            "lorem-ipsum-aedipisicing-culpa"
        );
        assert_eq!(
            super::to_slug("Lorem & Ipsúm. Ædipisicing culpa!?", true),
            "lorem-ipsum-aedipisicing-culpa"
        );
    }

    #[test]
    fn slugify_with_lowercase() {
        assert_eq!(
            super::to_slug("Lorem ipsum. Aedipisicing culpa!?", false),
            "Lorem-ipsum-Aedipisicing-culpa"
        );
        assert_eq!(
            super::to_slug("Lorem ipsum.\n   Aedipisicing culpa!?", false),
            "Lorem-ipsum-Aedipisicing-culpa"
        );
        assert_eq!(
            super::to_slug("--Lorem--ipsum. Aedipisicing   -culpa-", false),
            "Lorem-ipsum-Aedipisicing-culpa"
        );
        assert_eq!(
            super::to_slug("Lorem & Ipsúm. Ædipisicing culpa!?", false),
            "Lorem-Ipsum-AEdipisicing-culpa"
        );
    }

    // https://stackoverflow.com/a/34666891/16968574
    macro_rules! remove_and_extract_tags {
        ($($name:ident: ($input:tt, $tags_removed_expected:tt, $tags_expected:tt),)*) => {
            $(
                #[test]
                fn $name() {
                    let tags_extracted = super::extract_tags($input);
                    let tags_expected: BTreeSet<String> = $tags_expected
                        .into_iter()
                        .map(|t: &str| t.to_string())
                        .collect();

                    let tags_removed = super::remove_tags($input);

                    assert_eq!(tags_extracted, tags_expected);
                    assert_eq!(tags_removed, $tags_removed_expected.to_string());
                }
            )*
        }
    }

    // Tests that extracting and removing tags from a string produces the expected results. Only
    // tags, e.g. contigious strings starting with a hashtag, should be extracted and removed
    // from the original string.
    //
    // "Lorem ipsum. #tag",  // Input string
    // "Lorem ipsum.",       // Expected: tags removed
    // ["#tag"]              // Expected: tags extracted
    remove_and_extract_tags! {
        // Tests no tags in string.
        process_tags_00: (
            "Lorem ipsum.",
            "Lorem ipsum.",
            []
        ),
        // Tests tags at end of a string.
        process_tags_01: (
            "Lorem ipsum. #tag01 #tag02",
            "Lorem ipsum.",
            ["#tag01", "#tag02"]
        ),
        // Tests tags in the middle of a string.
        process_tags_02: (
            "Lorem ipsum. #tag01 #tag02 Adipisicing culpa.",
            "Lorem ipsum. Adipisicing culpa.",
            ["#tag01", "#tag02"]
        ),
        // Tests tags at beginning of a string.
        process_tags_03: (
            "#tag01 #tag02 Lorem ipsum. Adipisicing culpa.",
            "Lorem ipsum. Adipisicing culpa.",
            ["#tag01", "#tag02"]
        ),
        // Tests tags with extra whitespace.
        process_tags_04: (
            "Lorem ipsum.  #tag01  #tag02  ",
            "Lorem ipsum.",
            ["#tag01", "#tag02"]
        ),
        // Tests tags without spacing.
        process_tags_05: (
            "Lorem ipsum.#tag01#tag02",
            "Lorem ipsum.",
            ["#tag01", "#tag02"]
         ),
        // Tests that tags must start with letter.
        process_tags_06: (
            "#tag01 #TAG01 #Tag01 #1 #999",
            "#1 #999",
            ["#tag01", "#TAG01", "#Tag01"]
        ),
        // Tests that a string with only tags ends up empty.
        process_tags_07: (
            "#tag01 #tag02",
            "",
            ["#tag01", "#tag02"]
        ),
        // Tests that tags are deduped.
        process_tags_08: (
            "#tag01 #tag01 #tag01",
            "",
            ["#tag01"]
        ),
        // Tests that extra hashtags are ignored.
        process_tags_09: (
            "###tag01##tag02",
            "###",
            ["#tag01", "#tag02"]
        ),
    }
}