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
// SPDX-FileCopyrightText: 2023-2024 Kunal Mehta <legoktm@debian.org>
// SPDX-License-Identifier: GPL-3.0-or-later
//! `<ref>` and `<references/>` tags are used to generate citation footnotes.
//!
//! There are three nodes here:
//! * [`Reference`]: the contents of the citation, i.e. the content inside `<ref>`
//! * [`ReferenceLink`]: the `[1]` link that goes to the citation, aka the "[cue](https://en.wikipedia.org/wiki/Note_(typography)#Location)"
//! * [`ReferenceList`]: the list of citations, i.e. `<references/>`
//!
//! See the [specification](https://www.mediawiki.org/wiki/Specs/HTML/2.8.0/Extensions/Cite) for more details.

use crate::{
    assert_element, inner_data, node::Wikinode, set_inner_data, Result,
    WikinodeIterator,
};
use kuchikiki::NodeRef;
use serde::{Deserialize, Serialize};

/// Represents the citation contained by a ref tag (`<ref>`)
///
/// See the [spec](https://www.mediawiki.org/wiki/Specs/HTML/2.8.0/Extensions/Cite) for more details.
#[derive(Debug, Clone)]
pub struct Reference(pub(crate) NodeRef);

impl Reference {
    pub(crate) fn new_from_node(element: &NodeRef) -> Self {
        assert_element(element);
        Self(element.clone())
    }

    /// Contents of this reference, i.e. what is in between the `<ref></ref>` tags.
    pub fn contents(&self) -> Wikinode {
        self.select_first(".mw-reference-text")
            .expect("no .mw-reference-text found")
    }

    /// Get the ID of this reference. It correponds to [`ReferenceLink::reference_id()`].
    pub fn id(&self) -> String {
        self.contents()
            .as_element()
            .unwrap()
            .attributes
            .borrow()
            .get("id")
            .unwrap()
            .to_string()
    }

    /// IDs that point back to the links to this reference, corresponds with [`ReferenceLink::id()`].
    pub fn referenced_by_ids(&self) -> Vec<String> {
        // Find the backlink nodes, then parse the anchor out of the href
        self.select(".mw-cite-backlink > a")
            .into_iter()
            .map(|node| {
                node.as_element()
                    .unwrap()
                    .attributes
                    .borrow()
                    .get("href")
                    .unwrap()
                    .split_once('#')
                    .unwrap()
                    .1
                    .to_string()
            })
            .collect()
    }
}

/// Represents the link generated by a ref tag (`<ref>`) that points to the citation
///
/// See the [spec](https://www.mediawiki.org/wiki/Specs/HTML/2.8.0/Extensions/Cite) for more details.
#[derive(Debug, Clone)]
pub struct ReferenceLink(pub(crate) NodeRef);

impl ReferenceLink {
    pub(crate) const TYPEOF: &'static str = "mw:Extension/ref";
    pub(crate) const SELECTOR: &'static str = "[typeof=\"mw:Extension/ref\"]";

    pub(crate) fn new_from_node(element: &NodeRef) -> Self {
        assert_element(element);
        Self(element.clone())
    }

    /// Get the ID of this link, corresponds to [`Reference::referenced_by_ids()`].
    pub fn id(&self) -> String {
        self.as_element()
            .unwrap()
            .attributes
            .borrow()
            .get("id")
            .unwrap()
            .to_string()
    }

    /// Name of the reference, if one is set
    pub fn name(&self) -> Result<Option<String>> {
        Ok(self.inner()?.attrs.name)
    }

    /// Set the name for this reference
    pub fn set_name(&self, name: String) -> Result<()> {
        let mut inner = self.inner()?;
        inner.attrs.name = Some(name);
        self.set_inner(inner)?;
        Ok(())
    }

    /// Remove the name for this reference (if set)
    pub fn remove_name(&self) -> Result<()> {
        let mut inner = self.inner()?;
        inner.attrs.name = None;
        self.set_inner(inner)?;
        Ok(())
    }

    /// Group the reference is in, if one is set
    pub fn group(&self) -> Result<Option<String>> {
        Ok(self.inner()?.attrs.group)
    }

    /// Set the group for this reference
    pub fn set_group(&self, group: String) -> Result<()> {
        let mut inner = self.inner()?;
        inner.attrs.group = Some(group);
        self.set_inner(inner)?;
        Ok(())
    }

    /// Remove the group for this reference
    pub fn remove_group(&self) -> Result<()> {
        let mut inner = self.inner()?;
        inner.attrs.group = None;
        self.set_inner(inner)?;
        Ok(())
    }

    /// Whether this reference link is reusing one that came earlier
    pub fn is_reused(&self) -> Result<bool> {
        Ok(self.inner()?.body.is_none())
    }

    /// ID of the Reference that corresponds to this link, aka [`Reference::id()`].
    pub fn reference_id(&self) -> Result<String> {
        let id = match self.inner()?.body {
            Some(body) => body.id,
            None => {
                // Since we have no body (reused), we have to grab the ID
                // from the href of the link
                let part = self
                    .select_first("a[href]")
                    .unwrap()
                    .as_element()
                    .unwrap()
                    .attributes
                    .borrow()
                    .get("href")
                    .unwrap()
                    .split_once('#')
                    .unwrap()
                    .1
                    .to_string();
                format!("mw-reference-text-{}", part)
            }
        };
        Ok(id)
    }

    fn inner(&self) -> Result<RefDataMw> {
        inner_data(self)
    }

    fn set_inner(&self, data: RefDataMw) -> Result<()> {
        set_inner_data(self, data)
    }
}

#[derive(Deserialize, Serialize)]
struct RefDataMw {
    name: String,
    attrs: RefAttrs,
    #[serde(skip_serializing_if = "Option::is_none")]
    body: Option<RefBody>,
}

#[derive(Deserialize, Serialize)]
struct RefAttrs {
    #[serde(skip_serializing_if = "Option::is_none")]
    group: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<String>,
}

#[derive(Deserialize, Serialize)]
struct RefBody {
    id: String,
}

/// Represents a reference list tag (`<references/>`)
///
/// See the [spec](https://www.mediawiki.org/wiki/Specs/HTML/2.8.0/Extensions/Cite) for more details.
#[derive(Debug, Clone)]
pub struct ReferenceList(pub(crate) NodeRef);

impl ReferenceList {
    pub(crate) const TYPEOF: &'static str = "mw:Extension/references";
    pub(crate) const SELECTOR: &'static str =
        "[typeof=\"mw:Extension/references\"]";

    pub(crate) fn new_from_node(element: &NodeRef) -> Self {
        assert_element(element);
        Self(element.clone())
    }

    /// Get a list of References in this list
    pub fn references(&self) -> Vec<Reference> {
        self.select("ol.mw-references > li")
            .into_iter()
            .map(|node| Reference::new_from_node(&node))
            .collect()
    }

    /// Find a specific `Reference`, given its ID
    pub fn find(&self, id: &str) -> Option<Reference> {
        self.references().into_iter().find(|ref_| ref_.id() == id)
    }

    /// Group this reference list is showing, if one is set
    pub fn group(&self) -> Result<Option<String>> {
        Ok(self.inner()?.attrs.group)
    }

    /// Set the group for this reference list
    pub fn set_group(&self, group: String) -> Result<()> {
        let mut inner = self.inner()?;
        inner.attrs.group = Some(group);
        self.set_inner(inner)?;
        Ok(())
    }

    /// Remove the group for this reference list
    pub fn remove_group(&self) -> Result<()> {
        let mut inner = self.inner()?;
        inner.attrs.group = None;
        self.set_inner(inner)?;
        Ok(())
    }

    /// If this reference list is automatically generated or explicitly in the wikitext
    pub fn is_auto_generated(&self) -> Result<bool> {
        Ok(self.inner()?.auto_generated)
    }

    fn inner(&self) -> Result<ReferencesListDataMw> {
        inner_data(self)
    }

    fn set_inner(&self, data: ReferencesListDataMw) -> Result<()> {
        set_inner_data(self, data)
    }
}

#[derive(Deserialize, Serialize)]
pub(crate) struct ReferencesListDataMw {
    pub(crate) name: String,
    pub(crate) attrs: ReferencesListAttrs,
    #[serde(rename = "autoGenerated")]
    #[serde(default)]
    pub(crate) auto_generated: bool,
}

#[derive(Deserialize, Serialize)]
pub(crate) struct ReferencesListAttrs {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) group: Option<String>,
}