defter 0.4.0

Self-hosted Zotero sync and file storage server, with metadata in SQLite and attachments in S3.
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! The public profile's languages. The wording follows the English and German
//! locales of commonmeta.org (dragoman's `ui/src/locales`), so a person's page
//! reads the same on both.

/// A language the public profile is available in.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Lang {
    En,
    De,
}

/// The words on the public profile.
pub struct Text {
    pub person: &'static str,
    pub given_name: &'static str,
    pub family_name: &'static str,
    pub additional_names: &'static str,
    pub websites: &'static str,
    pub country: &'static str,
    pub affiliations: &'static str,
    pub started: &'static str,
    pub ended: &'static str,
    pub filters: &'static str,
    pub facet_year: &'static str,
    pub facet_type: &'static str,
    pub open_all: &'static str,
    pub close_all: &'static str,
    pub clear_filters: &'static str,
    pub search: &'static str,
    pub search_placeholder: &'static str,
    pub copy_citation: &'static str,
    pub content: &'static str,
    pub published: &'static str,
    pub in_container: &'static str,
    pub license: &'static str,
    pub no_results: &'static str,
    pub no_works: &'static str,
    pub pagination: &'static str,
    pub previous_page: &'static str,
    pub next_page: &'static str,
    pub language: &'static str,
    pub apply: &'static str,
    pub featured_only: &'static str,
    pub clear: &'static str,
    pub export_format: &'static str,
    pub citation_style: &'static str,
    pub formatted_citation: &'static str,
    pub export: &'static str,
    pub copy: &'static str,
    pub copied: &'static str,
}

const EN: Text = Text {
    person: "Person",
    given_name: "Given name",
    family_name: "Family name",
    additional_names: "Also known as",
    websites: "Websites & social links",
    country: "Country",
    affiliations: "Affiliations",
    started: "Started",
    ended: "Ended at",
    filters: "Filters",
    facet_year: "Publication year",
    facet_type: "Content type",
    open_all: "Open all",
    close_all: "Close all",
    clear_filters: "Clear filters",
    search: "Search",
    search_placeholder: "Search these works",
    copy_citation: "Copy citation",
    content: "Content",
    published: "published",
    in_container: "in",
    license: "Distributed under the terms of the",
    no_results: "No results found.",
    no_works: "No works on this page.",
    pagination: "Pagination",
    previous_page: "Previous page",
    next_page: "Next page",
    language: "Language",
    apply: "Apply",
    featured_only: "Featured only",
    clear: "Clear",
    export_format: "Export format",
    citation_style: "Citation style",
    formatted_citation: "Formatted Citation",
    export: "Export",
    copy: "Copy",
    copied: "Copied",
};

const DE: Text = Text {
    person: "Person",
    given_name: "Vorname",
    family_name: "Familienname",
    additional_names: "Auch bekannt als",
    websites: "Webseiten & soziale Netzwerke",
    country: "Land",
    affiliations: "Zugehörigkeiten",
    started: "Beginn",
    ended: "Ende",
    filters: "Filter",
    facet_year: "Erscheinungsjahr",
    facet_type: "Inhaltstyp",
    open_all: "Alle öffnen",
    close_all: "Alle schließen",
    clear_filters: "Filter zurücksetzen",
    search: "Suche",
    search_placeholder: "Diese Werke durchsuchen",
    copy_citation: "Zitat kopieren",
    content: "Inhalt",
    published: "veröffentlicht",
    in_container: "in",
    license: "Veröffentlicht unter den Bedingungen der",
    no_results: "Keine Ergebnisse gefunden.",
    no_works: "Keine Werke auf dieser Seite.",
    pagination: "Seitennummerierung",
    previous_page: "Vorherige Seite",
    next_page: "Nächste Seite",
    language: "Sprache",
    apply: "Anwenden",
    featured_only: "Nur hervorgehobene",
    clear: "Löschen",
    export_format: "Exportformat",
    citation_style: "Zitierstil",
    formatted_citation: "Formatiertes Zitat",
    export: "Exportieren",
    copy: "Kopieren",
    copied: "Kopiert",
};

/// The words in `templates/base.html`.
pub struct LayoutText {
    pub lang: &'static str,
    pub toggle_dark: &'static str,
    pub sign_out: &'static str,
    pub tagline: &'static str,
    pub built_with: &'static str,
    pub by: &'static str,
    pub software: &'static str,
    pub mit_license: &'static str,
}

pub const LAYOUT_EN: LayoutText = LayoutText {
    lang: "en",
    toggle_dark: "Toggle dark mode",
    sign_out: "Sign out",
    tagline: "Self-hosted Zotero sync and file storage",
    built_with: "Built with",
    by: "by",
    software: "software provided under an",
    mit_license: "MIT license",
};

const LAYOUT_DE: LayoutText = LayoutText {
    lang: "de",
    toggle_dark: "Dunkelmodus umschalten",
    sign_out: "Abmelden",
    tagline: "Selbst gehostete Zotero-Synchronisierung und Dateiablage",
    built_with: "Erstellt mit",
    by: "von",
    software: "Software bereitgestellt unter einer",
    mit_license: "MIT-Lizenz",
};

const MONTHS_EN: [&str; 12] = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December",
];

const MONTHS_DE: [&str; 12] = [
    "Januar",
    "Februar",
    "März",
    "April",
    "Mai",
    "Juni",
    "Juli",
    "August",
    "September",
    "Oktober",
    "November",
    "Dezember",
];

/// German names of Zotero's item types. English ones are derived from the type.
const ITEM_TYPES_DE: &[(&str, &str)] = &[
    ("artwork", "Kunstwerk"),
    ("audioRecording", "Tonaufnahme"),
    ("bill", "Gesetzentwurf"),
    ("blogPost", "Blogbeitrag"),
    ("book", "Buch"),
    ("bookSection", "Buchkapitel"),
    ("case", "Gerichtsentscheidung"),
    ("computerProgram", "Software"),
    ("conferencePaper", "Konferenzbeitrag"),
    ("dataset", "Datensatz"),
    ("dictionaryEntry", "Wörterbucheintrag"),
    ("document", "Dokument"),
    ("email", "E-Mail"),
    ("encyclopediaArticle", "Lexikonartikel"),
    ("film", "Film"),
    ("forumPost", "Forumsbeitrag"),
    ("hearing", "Anhörung"),
    ("instantMessage", "Sofortnachricht"),
    ("interview", "Interview"),
    ("journalArticle", "Zeitschriftenartikel"),
    ("letter", "Brief"),
    ("magazineArticle", "Magazinartikel"),
    ("manuscript", "Manuskript"),
    ("map", "Karte"),
    ("newspaperArticle", "Zeitungsartikel"),
    ("patent", "Patent"),
    ("podcast", "Podcast"),
    ("preprint", "Preprint"),
    ("presentation", "Präsentation"),
    ("radioBroadcast", "Radiosendung"),
    ("report", "Bericht"),
    ("standard", "Norm"),
    ("statute", "Gesetz"),
    ("thesis", "Abschlussarbeit"),
    ("tvBroadcast", "Fernsehsendung"),
    ("videoRecording", "Videoaufnahme"),
    ("webpage", "Webseite"),
];

/// The languages of works most often seen, by primary subtag.
const LANGUAGES: &[(&str, &str, &str)] = &[
    ("ar", "Arabic", "Arabisch"),
    ("cs", "Czech", "Tschechisch"),
    ("da", "Danish", "Dänisch"),
    ("de", "German", "Deutsch"),
    ("el", "Greek", "Griechisch"),
    ("en", "English", "Englisch"),
    ("es", "Spanish", "Spanisch"),
    ("fi", "Finnish", "Finnisch"),
    ("fr", "French", "Französisch"),
    ("he", "Hebrew", "Hebräisch"),
    ("hu", "Hungarian", "Ungarisch"),
    ("it", "Italian", "Italienisch"),
    ("ja", "Japanese", "Japanisch"),
    ("ko", "Korean", "Koreanisch"),
    ("nb", "Norwegian", "Norwegisch"),
    ("nl", "Dutch", "Niederländisch"),
    ("no", "Norwegian", "Norwegisch"),
    ("pl", "Polish", "Polnisch"),
    ("pt", "Portuguese", "Portugiesisch"),
    ("ru", "Russian", "Russisch"),
    ("sv", "Swedish", "Schwedisch"),
    ("tr", "Turkish", "Türkisch"),
    ("zh", "Chinese", "Chinesisch"),
];

impl Lang {
    /// What the language switcher offers.
    pub const ALL: [Lang; 2] = [Lang::En, Lang::De];

    pub fn parse(code: &str) -> Option<Lang> {
        match code.to_ascii_lowercase().as_str() {
            "en" => Some(Lang::En),
            "de" => Some(Lang::De),
            _ => None,
        }
    }

    /// The first language in an `Accept-Language` header that is available,
    /// else English. Browsers list their languages by preference.
    pub fn from_accept_language(header: &str) -> Lang {
        header
            .split(',')
            .filter_map(|tag| {
                let tag = tag.split(';').next()?.trim();
                Lang::parse(tag.split('-').next()?)
            })
            .next()
            .unwrap_or(Lang::En)
    }

    pub fn code(&self) -> &'static str {
        match self {
            Lang::En => "en",
            Lang::De => "de",
        }
    }

    /// The language's name, in that language.
    pub fn name(&self) -> &'static str {
        match self {
            Lang::En => "English",
            Lang::De => "Deutsch",
        }
    }

    /// The locale formatted citations are written in.
    pub fn citation_locale(self) -> &'static str {
        match self {
            Lang::En => "en-US",
            Lang::De => "de-DE",
        }
    }

    pub fn text(self) -> &'static Text {
        match self {
            Lang::En => &EN,
            Lang::De => &DE,
        }
    }

    pub fn layout(self) -> &'static LayoutText {
        match self {
            Lang::En => &LAYOUT_EN,
            Lang::De => &LAYOUT_DE,
        }
    }

    /// `1043` → "1,043" or "1.043".
    pub fn count(&self, n: usize) -> String {
        let separator = match self {
            Lang::En => ',',
            Lang::De => '.',
        };
        let digits = n.to_string();
        let mut out = String::with_capacity(digits.len() + digits.len() / 3);
        for (i, digit) in digits.chars().enumerate() {
            if i > 0 && (digits.len() - i).is_multiple_of(3) {
                out.push(separator);
            }
            out.push(digit);
        }
        out
    }

    /// "1 work", "1,043 works".
    pub fn works(self, n: usize) -> String {
        let noun = match (self, n) {
            (Lang::En, 1) => "work",
            (Lang::En, _) => "works",
            (_, 1) => "Werk",
            (_, _) => "Werke",
        };
        format!("{} {noun}", self.count(n))
    }

    /// "page 1 of 105".
    pub fn page_of(self, page: usize, pages: usize) -> String {
        let (page, pages) = (self.count(page), self.count(pages));
        match self {
            Lang::En => format!("page {page} of {pages}"),
            Lang::De => format!("Seite {page} von {pages}"),
        }
    }

    /// A partial date for people: `2015` → "2015", `2015-08` → "August 2015",
    /// `2021-08-01` → "August 1, 2021" or "1. August 2021". Anything else, such
    /// as Zotero's free-form dates, stays as it is.
    pub fn date(self, date: &str) -> String {
        let parts: Vec<&str> = date.trim().split('-').collect();
        let months = match self {
            Lang::En => &MONTHS_EN,
            Lang::De => &MONTHS_DE,
        };
        let month = parts
            .get(1)
            .and_then(|m| m.parse::<usize>().ok())
            .and_then(|m| months.get(m.wrapping_sub(1)));
        let day = parts.get(2).and_then(|d| d.parse::<u32>().ok());
        match (self, parts[0], month, day) {
            (Lang::En, year, Some(month), Some(day)) => format!("{month} {day}, {year}"),
            (_, year, Some(month), Some(day)) => format!("{day}. {month} {year}"),
            (_, year, Some(month), None) => format!("{month} {year}"),
            (_, year, _, _) => year.to_string(),
        }
    }

    pub fn date_range(self, start: &str, end: &str) -> String {
        let text = self.text();
        match (start.trim().is_empty(), end.trim().is_empty()) {
            (false, false) => format!("{} – {}", self.date(start), self.date(end)),
            (false, true) => format!("{} {}", text.started, self.date(start)),
            (true, false) => format!("{} {}", text.ended, self.date(end)),
            (true, true) => String::new(),
        }
    }

    /// `journalArticle` → "Journal article" or "Zeitschriftenartikel".
    pub fn item_type(self, item_type: &str) -> String {
        if self != Lang::En
            && let Some((_, label)) = ITEM_TYPES_DE.iter().find(|(t, _)| *t == item_type)
        {
            return label.to_string();
        }
        let mut label = String::new();
        for (i, c) in item_type.chars().enumerate() {
            if i == 0 {
                label.extend(c.to_uppercase());
            } else if c.is_uppercase() {
                label.push(' ');
                label.extend(c.to_lowercase());
            } else {
                label.push(c);
            }
        }
        label
    }

    /// The name of a work's language: `en-US` → "English". Zotero's field is
    /// free text, so anything else stays as it is.
    pub fn language(self, language: &str) -> String {
        let language = language.trim();
        let primary = language
            .split('-')
            .next()
            .unwrap_or("")
            .to_ascii_lowercase();
        match LANGUAGES.iter().find(|(code, _, _)| *code == primary) {
            Some((_, en, _)) if self == Lang::En => en.to_string(),
            Some((_, _, de)) => de.to_string(),
            None => language.to_string(),
        }
    }
}

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

    #[test]
    fn picks_a_language_from_the_browser() {
        let accept = Lang::from_accept_language;
        assert_eq!(accept("de-DE,de;q=0.9,en;q=0.8"), Lang::De);
        assert_eq!(accept("de-AT,de;q=0.9"), Lang::De);
        assert_eq!(accept("fr-FR,fr;q=0.9,de;q=0.8,en;q=0.7"), Lang::De);
        assert_eq!(accept("en-GB,de;q=0.5"), Lang::En);
        assert_eq!(accept("fr"), Lang::En);
        assert_eq!(accept(""), Lang::En);
    }

    #[test]
    fn formats_dates_and_ranges() {
        assert_eq!(Lang::En.date("2015"), "2015");
        assert_eq!(Lang::En.date("2015-08"), "August 2015");
        assert_eq!(Lang::En.date("2021-08-01"), "August 1, 2021");
        assert_eq!(Lang::De.date("2021-08-01"), "1. August 2021");
        assert_eq!(Lang::De.date("2015-03"), "März 2015");
        assert_eq!(Lang::De.date("2021-01-05"), "5. Januar 2021");
        assert_eq!(Lang::En.date("May 2021"), "May 2021");
        assert_eq!(
            Lang::En.date_range("2021-08-01", ""),
            "Started August 1, 2021"
        );
        assert_eq!(
            Lang::De.date_range("2021-08-01", ""),
            "Beginn 1. August 2021"
        );
        assert_eq!(
            Lang::En.date_range("2015-08", "2021-07-31"),
            "August 2015 – July 31, 2021"
        );
        assert_eq!(Lang::En.date_range("", ""), "");
    }

    #[test]
    fn counts_in_the_page_language() {
        assert_eq!(Lang::En.count(1043), "1,043");
        assert_eq!(Lang::De.count(1043), "1.043");
        assert_eq!(Lang::En.count(999), "999");
        assert_eq!(Lang::En.works(1), "1 work");
        assert_eq!(Lang::De.works(1043), "1.043 Werke");
        assert_eq!(Lang::De.page_of(1, 105), "Seite 1 von 105");
    }

    #[test]
    fn names_types_and_languages() {
        assert_eq!(Lang::En.item_type("blogPost"), "Blog post");
        assert_eq!(Lang::De.item_type("blogPost"), "Blogbeitrag");
        assert_eq!(Lang::De.item_type("unknownType"), "Unknown type");
        assert_eq!(Lang::En.language("en-US"), "English");
        assert_eq!(Lang::De.language("en"), "Englisch");
        assert_eq!(Lang::En.language("Klingon"), "Klingon");
    }
}