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
use regex::Regex;

use crate::core::{ParseConfig, ParsedItem};

enum ParsedToken {
    Highlight,
    Underline,
}

/// Parses the provided GoodReader annotations emitting a [`ParsedItem`] for each identified
/// annotation and page number indicators if so desired.
/// Use the [`ParseConfig`] to configure if page numbers should be included, etc.
pub fn parse_goodreader_annotations(annotations: &str, config: &ParseConfig) -> Vec<ParsedItem> {
    let file_rx = Regex::new(r"^File: (.+)").unwrap();
    let page_roman_rx = Regex::new(r"^--- Page ([mdclxvi]+) ---").unwrap();
    let page_number_rx = Regex::new(r"^--- Page (\d+) ---").unwrap();
    let highlight_rx = Regex::new(r"^Highlight( \([^)]+\))?:").unwrap();
    let underline_rx = Regex::new(r"(?i)^(Squiggly )?Underline( \([^)]+\))?:").unwrap();

    let lines = annotations.lines().filter(|&x| x.len() > 0);

    let mut token: Option<ParsedToken> = None;
    lines
        .filter_map(|line| {
            // Handling tokens we encountered on previous line
            let item = match token {
                Some(ParsedToken::Highlight) => {
                    token = None;
                    Some(ParsedItem::Highlight(line.to_string()))
                }
                Some(ParsedToken::Underline) => {
                    token = None;
                    Some(ParsedItem::Underline(line.to_string()))
                }
                None => None,
            };
            if item.is_some() {
                return item;
            }

            // File: File_Name.pdf
            if let Some(_) = file_rx.find(line) {
                let c = file_rx.captures(line).unwrap();
                let s: String = c.get(1).map(|x| x.as_str().to_string()).unwrap();
                return Some(ParsedItem::File(s));
            }

            // --- Page NNN ---
            if config.page_numbers {
                if let Some(_) = page_number_rx.find(line) {
                    let c = page_number_rx.captures(line).unwrap();
                    let n: u32 = c
                        .get(1)
                        .map(|x| {
                            let s = x.as_str().to_string();
                            s.parse::<u32>().unwrap()
                        })
                        .unwrap();
                    return match config.page_offset {
                        // physical page is annotated, thus page offset is negative if set
                        offset if offset < 0 => { 
                            assert!(n as i32 > offset, format!("negative page offset {} seems to small, encountered physical page {}", offset, n));
                            Some(ParsedItem::PageNumber((n as i32 + offset) as u32, n))
                        }
                        // page number is annotated, page offset is positive if set
                        offset if offset > 0 => Some(ParsedItem::PageNumber(n, n + offset as u32)),
                        // page offset not set, i.e. is 0
                        _ =>Some(ParsedItem::PageNumber(n, n)), 
                    };
                }
                if let Some(_) = page_roman_rx.find(line) {
                    let c = page_roman_rx.captures(line).unwrap();
                    let s: String = c
                        .get(1)
                        .map(|x| {
                            x.as_str().to_string()
                        })
                        .unwrap();
                    return Some(ParsedItem::PageRoman(s))
                }
            }

            // Highlight:
            // Highlighted Text
            if let Some(_) = highlight_rx.find(line) {
                token = Some(ParsedToken::Highlight);
                return None;
            }
            // Underline:
            // Underlined Text
            if let Some(_) = underline_rx.find(line) {
                token = Some(ParsedToken::Underline);
                return None;
            }
            None
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    static WITH_PAGE_NUMBERS: ParseConfig = ParseConfig {
        page_offset: 0,
        page_numbers: true,
    };
    static WITHOUT_PAGE_NUMBERS: ParseConfig = ParseConfig {
        page_offset: 0,
        page_numbers: false,
    };

    #[test]
    fn parsing_file_indicator() {
        let annotations = r#"
File: Hello_World.pdf
"#;
        let items = parse_goodreader_annotations(annotations, &WITHOUT_PAGE_NUMBERS);
        assert_eq!(ParsedItem::File("Hello_World.pdf".to_string()), items[0]);
    }

    #[test]
    fn parsing_page_indicator() {
        let annotations = r#"
--- Page 45 ---
--- Page 22 ---
-- Page 45 ---
"#;
        let items = parse_goodreader_annotations(annotations, &WITH_PAGE_NUMBERS);
        assert_eq!(
            items,
            vec![
                ParsedItem::PageNumber(45, 45),
                ParsedItem::PageNumber(22, 22)
            ]
        )
    }

    #[test]
    fn parsing_page_indicator_with_positive_page_offset() {
        let annotations = r#"
--- Page 45 ---
--- Page 22 ---
"#;
        let items = parse_goodreader_annotations(
            annotations,
            &ParseConfig {
                page_offset: 3,
                page_numbers: true,
            },
        );
        assert_eq!(
            items,
            vec![
                ParsedItem::PageNumber(45, 48),
                ParsedItem::PageNumber(22, 25)
            ]
        )
    }

    #[test]
    fn parsing_page_roman_indicator() {
        let annotations = r#"
--- Page xxxviii ---
--- Page xxxix ---
"#;
        let items = parse_goodreader_annotations(annotations, &WITH_PAGE_NUMBERS);
        assert_eq!(
            items,
            vec![
                ParsedItem::PageRoman("xxxviii".to_string()),
                ParsedItem::PageRoman("xxxix".to_string())
            ]
        )
    }

    #[test]
    fn parsing_page_indicator_with_negative_page_offset() {
        let annotations = r#"
--- Page 45 ---
--- Page 22 ---
"#;
        let items = parse_goodreader_annotations(
            annotations,
            &ParseConfig {
                page_offset: -3,
                page_numbers: true,
            },
        );
        assert_eq!(
            items,
            vec![
                ParsedItem::PageNumber(42, 45),
                ParsedItem::PageNumber(19, 22)
            ]
        )
    }

    #[test]
    fn parsing_highlight_indicator() {
        let annotations = r#"
Highlight:
Practical: A Simple Database
"#;
        let items = parse_goodreader_annotations(annotations, &WITHOUT_PAGE_NUMBERS);
        assert_eq!(
            items,
            vec![ParsedItem::Highlight(
                "Practical: A Simple Database".to_string()
            )]
        )
    }

    #[test]
    fn parsing_highlight_color_indicator() {
        let annotations = r#"
Highlight (blue):
Practical: A Simple Database
"#;
        let items = parse_goodreader_annotations(annotations, &WITHOUT_PAGE_NUMBERS);
        assert_eq!(
            items,
            vec![ParsedItem::Highlight(
                "Practical: A Simple Database".to_string()
            )],
        )
    }

    #[test]
    fn parsing_underline_indicator() {
        let annotations = r#"
Underline:
`(equal (getf cd ,field) ,value)
"#;
        let items = parse_goodreader_annotations(annotations, &WITHOUT_PAGE_NUMBERS);
        assert_eq!(
            items,
            vec![ParsedItem::Underline(
                "`(equal (getf cd ,field) ,value)".to_string()
            )],
        )
    }

    #[test]
    fn parsing_underline_color() {
        let annotations = r#"
Underline: (color #6F77FF):
`(equal (getf cd ,field) ,value)
"#;
        let items = parse_goodreader_annotations(annotations, &WITHOUT_PAGE_NUMBERS);
        assert_eq!(
            items,
            vec![ParsedItem::Underline(
                "`(equal (getf cd ,field) ,value)".to_string()
            )],
        );
    }

    static SECTION: &str = "
File: Practical_Common_Lisp.pdf

Annotation summary:

--- Page 45 ---

Highlight:
Practical: A Simple Database


--- Page 46 ---

Underline:
property list, or plist

Underline:
(list :a 1 :b 2 :c 3)

Underline:
GETF, which takes a plist and a symbol and returns the value in the plist

Underline:
(getf (list :a 1 :b 2 :c 3) :a)


--- Page 47 ---

Underline:
global variable, *db*, which you can define with the DEFVAR macro
";
    #[test]
    fn parsing_section_with_zero_based_page_numbers() {
        let items = parse_goodreader_annotations(SECTION, &WITH_PAGE_NUMBERS);
        assert_eq!(
            items,
            vec![
                ParsedItem::File("Practical_Common_Lisp.pdf".to_string()),
                ParsedItem::PageNumber(45, 45),
                ParsedItem::Highlight("Practical: A Simple Database".to_string()),
                ParsedItem::PageNumber(46, 46),
                ParsedItem::Underline("property list, or plist".to_string()),
                ParsedItem::Underline("(list :a 1 :b 2 :c 3)".to_string()),
                ParsedItem::Underline(
                    "GETF, which takes a plist and a symbol and returns the value in the plist"
                        .to_string()
                ),
                ParsedItem::Underline("(getf (list :a 1 :b 2 :c 3) :a)".to_string()),
                ParsedItem::PageNumber(47, 47),
                ParsedItem::Underline(
                    "global variable, *db*, which you can define with the DEFVAR macro".to_string()
                )
            ]
        );
    }

    #[test]
    fn parsing_section_with_10_based_page_numbers() {
        let items = parse_goodreader_annotations(
            SECTION,
            &ParseConfig {
                page_offset: 10,
                page_numbers: true,
            },
        );
        assert_eq!(
            items,
            vec![
                ParsedItem::File("Practical_Common_Lisp.pdf".to_string()),
                ParsedItem::PageNumber(45, 55),
                ParsedItem::Highlight("Practical: A Simple Database".to_string()),
                ParsedItem::PageNumber(46, 56),
                ParsedItem::Underline("property list, or plist".to_string()),
                ParsedItem::Underline("(list :a 1 :b 2 :c 3)".to_string()),
                ParsedItem::Underline(
                    "GETF, which takes a plist and a symbol and returns the value in the plist"
                        .to_string()
                ),
                ParsedItem::Underline("(getf (list :a 1 :b 2 :c 3) :a)".to_string()),
                ParsedItem::PageNumber(47, 57),
                ParsedItem::Underline(
                    "global variable, *db*, which you can define with the DEFVAR macro".to_string()
                )
            ]
        );
    }

    #[test]
    fn parsing_section_without_page_numbers() {
        let items = parse_goodreader_annotations(SECTION, &WITHOUT_PAGE_NUMBERS);
        assert_eq!(
            items,
            vec![
                ParsedItem::File("Practical_Common_Lisp.pdf".to_string()),
                ParsedItem::Highlight("Practical: A Simple Database".to_string()),
                ParsedItem::Underline("property list, or plist".to_string()),
                ParsedItem::Underline("(list :a 1 :b 2 :c 3)".to_string()),
                ParsedItem::Underline(
                    "GETF, which takes a plist and a symbol and returns the value in the plist"
                        .to_string()
                ),
                ParsedItem::Underline("(getf (list :a 1 :b 2 :c 3) :a)".to_string()),
                ParsedItem::Underline(
                    "global variable, *db*, which you can define with the DEFVAR macro".to_string()
                )
            ]
        );
    }
}