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
// Copyright 2019 Fredrik Portström <https://portstrom.com>
// This is free software distributed under the terms specified in
// the file LICENSE at the top-level directory of this distribution.

/// Site specific configuration of a wiki.
///
/// This is generated using the program [`fetch_mediawiki_configuration`](https://github.com/portstrom/fetch_mediawiki_configuration).
pub struct ConfigurationSource<'a> {
    /// Aliases of the category namespace.
    pub category_namespaces: &'a [&'a str],

    /// Tag names of extension tags.
    pub extension_tags: &'a [&'a str],

    /// Aliases of the file namespace.
    pub file_namespaces: &'a [&'a str],

    /// Characters that can appear in link trails.
    pub link_trail: &'a str,

    /// Magic words that can appear between `__` and `__`.
    pub magic_words: &'a [&'a str],

    /// Protocols that can be used for external links.
    pub protocols: &'a [&'a str],

    /// Magic words that can be used for redirects.
    pub redirect_magic_words: &'a [&'a str],
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Namespace {
    Category,
    File,
}

impl crate::Configuration {
    /// Allocates and returns a new configuration based on the given site specific configuration.
    #[must_use]
    pub fn new(source: &ConfigurationSource) -> Self {
        let mut configuration = crate::Configuration {
            character_entities: crate::Trie::new(),
            link_trail_character_set: crate::HashSet::new(),
            magic_words: crate::Trie::new(),
            namespaces: crate::Trie::new(),
            protocols: crate::Trie::new(),
            redirect_magic_words: crate::Trie::new(),
            tag_name_map: crate::HashMap::new(),
        };
        for (name, character) in crate::html_entities::HTML_ENTITIES {
            configuration
                .character_entities
                .add_case_sensitive_term(&format!("{};", name), *character);
        }
        for character in source.link_trail.chars() {
            configuration.link_trail_character_set.insert(character);
        }
        for protocol in source.protocols {
            configuration.protocols.add_term(protocol, ());
        }
        for magic_word in source.magic_words {
            configuration.magic_words.add_term(magic_word, ());
        }
        for namespace in source.category_namespaces {
            configuration
                .namespaces
                .add_term(&format!("{}:", namespace), Namespace::Category);
        }
        for namespace in source.file_namespaces {
            configuration
                .namespaces
                .add_term(&format!("{}:", namespace), Namespace::File);
        }
        for redirect_magic_word in source.redirect_magic_words {
            configuration
                .redirect_magic_words
                .add_term(redirect_magic_word, ());
        }
        for tag_name in source.extension_tags {
            configuration
                .tag_name_map
                .insert(tag_name.to_string(), crate::TagClass::ExtensionTag);
        }
        for tag_name in [
            "abbr",
            "b",
            "bdi",
            "bdo",
            "blockquote",
            "br",
            "caption",
            "center",
            "cite",
            "code",
            "data",
            "dd",
            "del",
            "dfn",
            "div",
            "dl",
            "dt",
            "em",
            "font",
            "h1",
            "h2",
            "h3",
            "h4",
            "h5",
            "h6",
            "hr",
            "i",
            "ins",
            "kbd",
            "li",
            "mark",
            "ol",
            "p",
            "pre",
            "q",
            "rb",
            "rp",
            "rt",
            "ruby",
            "s",
            "samp",
            "small",
            "span",
            "strike",
            "strong",
            "sub",
            "sup",
            "table",
            "td",
            "th",
            "time",
            "tr",
            "tt",
            "u",
            "ul",
            "var",
            "wbr",
        ]
        .iter()
        {
            configuration
                .tag_name_map
                .insert(tag_name.to_string(), crate::TagClass::Tag);
        }
        configuration
    }

    /// Parses wiki text into structured data.
    #[must_use]
    pub fn parse<'a>(&self, wiki_text: &'a str) -> crate::Output<'a> {
        crate::parse::parse(self, wiki_text)
    }
}

impl Default for crate::Configuration {
    /// Allocates and returns a configuration suitable for testing and quick and dirty prototyping. For correctly parsing an actual wiki, please get the correct site configuration for that particular wiki.
    fn default() -> Self {
        crate::default::create_configuration()
    }
}