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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
Copyright (C) 2020 Kunal Mehta <legoktm@member.fsf.org>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
//! # parsoid-rs
//!
//! Wrapper around [Parsoid HTML](https://www.mediawiki.org/wiki/Specs/HTML/2.1.0)
//! that provides convenient accessors for processing and extraction.
//!
//! Inspired by [mwparserfromhell](https://github.com/earwig/mwparserfromhell/),
//! [parsoid-jsapi](https://github.com/wikimedia/parsoid-jsapi) and built on top
//! of Servo's [html5ever](https://github.com/servo/html5ever).
//!
//! Mutation/modification is still a work in progress.
//!
//! # Example
//! Go through all templates on a page from the English Wikipedia
//! ```rust
//! use parsoid::{Client, Result};
//! # async fn example() -> Result<()> {
//! let client = Client::new("https://en.wikipedia.org/api/rest_v1", "parsoid-rs demo")?;
//! let code = client.get("Taylor_Swift").await?;
//! for template in code.filter_templates()? {
//!     dbg!(&template);
//! }
//! # Ok(())
//! # }
//! ```

#[cfg(feature = "http")]
mod api;
pub mod error;
mod iter;
pub mod map {
    pub use indexmap::IndexMap;
}
pub mod node;
pub mod prelude {
    #[cfg(feature = "http")]
    pub use crate::api::Client;
    pub use crate::iter::WikinodeIterator;
    pub use crate::map;
    pub use crate::node::{
        BehaviorSwitch, Category, Comment, ExtLink, Heading, HtmlEntity,
        InterwikiLink, LanguageLink, Nowiki, Redirect, Section, WikiLink,
        Wikinode,
    };
    pub use crate::template::Template;
    pub use crate::{Result, Wikicode};
}
pub mod template;

#[cfg(feature = "http")]
pub use crate::api::Client;
pub use crate::iter::WikinodeIterator;
use crate::node::{Comment, ExtLink, Redirect, Section, WikiLink, Wikinode};
use crate::template::Template;
use kuchiki::traits::*;
use kuchiki::NodeRef;
use markup5ever::QualName;
use std::ops::Deref;

pub type Result<T> = std::result::Result<T, crate::error::Error>;

#[macro_use]
extern crate markup5ever;

/// Helper wrapper to create a new `QualName`
fn build_qual_name(tag: &str) -> QualName {
    QualName::new(None, ns!(html), tag.into())
}

/// Container instance for Parsoid HTML
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Wikicode {
    document: NodeRef,
    etag: Option<String>,
}

impl Deref for Wikicode {
    type Target = NodeRef;

    fn deref(&self) -> &Self::Target {
        &self.document
    }
}

impl Wikicode {
    /// Create a new `Wikicode` instance from raw Parsoid HTML.
    pub fn new(body: &str) -> Self {
        Wikicode {
            document: kuchiki::parse_html().one(body),
            etag: None,
        }
    }

    pub fn new_fragment(frag: &str) -> Self {
        // TODO: Do I just pick a random tag name?
        let ctx_name = build_qual_name("span");
        Wikicode {
            document: kuchiki::parse_fragment(ctx_name, vec![])
                .one(frag)
                .first_child()
                .unwrap()
                .first_child()
                .unwrap(),
            etag: None,
        }
    }

    /// Create a new `Wikicode` instance from a node we already have
    fn new_from_node(node: &NodeRef) -> Self {
        Wikicode {
            document: node.clone(),
            etag: None,
        }
    }

    /// Set the etag that came with this request. This allows Parsoid to
    /// preserve formatting and avoid dirty diffs when converting modified
    /// HTML back to wikitext.
    pub fn set_etag(&mut self, etag: &str) {
        self.etag = Some(etag.to_string());
    }

    /// Get the etag that was set on this Wikicode instance.
    pub fn get_etag(&self) -> Option<&String> {
        self.etag.as_ref()
    }

    /// Get the revision id associated with the Parsoid HTML, if it has one.
    pub fn revision_id(&self) -> Option<u32> {
        match self.html_element() {
            Some(element) => {
                match element
                    .as_element()
                    .unwrap()
                    .attributes
                    .borrow()
                    .get("about")
                {
                    Some(url) => Some(
                        url.to_string()
                            .split('/')
                            .last()
                            .unwrap()
                            .to_string()
                            .parse()
                            .unwrap(),
                    ),
                    None => None,
                }
            }
            None => None,
        }
    }

    /// Get the title associated with the Parsoid HTML, if it has one.
    pub fn title(&self) -> Option<String> {
        match self.document.select_first("title") {
            Ok(element) => Some(element.as_node().text_contents()),
            Err(_) => None,
        }
    }

    pub fn get_redirect(&self) -> Option<Redirect> {
        match self.document.select_first(Redirect::SELECTOR) {
            Ok(element) => Some(Redirect::new_from_node(element.as_node())),
            Err(_) => None,
        }
    }

    /// Get the root <html> element if it exists, primarily intended to help
    /// getting metadata.
    fn html_element(&self) -> Option<NodeRef> {
        match self.document.select_first("html") {
            Ok(element) => Some(element.as_node().clone()),
            Err(_) => None,
        }
    }

    /// Get the <body> element or everything if there is no body. This is
    /// intented to help ensure nodes (e.g. comments) just come from the body.
    fn body_element(&self) -> Wikinode {
        match self.document.select_first("body") {
            // Just the <body>
            Ok(element) => Wikinode::new_from_node(&element.as_node()),
            // Everything as there is no <body>
            Err(_) => Wikinode::Generic(self.clone()),
        }
    }

    /// Get a plain text representation of the Parsoid HTML with all markup
    /// stripped.
    pub fn text_contents(&self) -> String {
        self.body_element().text_contents()
    }

    /// Get a list of all wikilinks (`[[Foo|bar]]`)
    pub fn filter_links(&self) -> Result<Vec<WikiLink>> {
        match self.document.select(WikiLink::SELECTOR) {
            Ok(select) => Ok(select
                .map(|ref_| WikiLink::new_from_node(ref_.as_node()))
                .collect()),
            Err(_) => Ok(vec![]),
        }
    }

    /// Get a list of all external links (`[https://example.org/ Example]`)
    pub fn filter_external_links(&self) -> Result<Vec<ExtLink>> {
        match self.document.select(ExtLink::SELECTOR) {
            Ok(select) => Ok(select
                .map(|ref_| ExtLink::new_from_node(ref_.as_node()))
                .collect()),
            Err(_) => Ok(vec![]),
        }
    }

    /// Get a list of [templates](https://www.mediawiki.org/wiki/Specs/HTML/2.1.0#Template_markup).
    pub fn filter_templates(&self) -> Result<Vec<Template>> {
        let templates = match self.document.select(Template::SELECTOR) {
            Ok(select) => {
                let mut templates = vec![];
                for ref_ in select {
                    let element = ref_.as_node();
                    let data: template::Transclusion = serde_json::from_str(
                        element
                            .as_element()
                            .unwrap()
                            .attributes
                            .borrow()
                            .get("data-mw")
                            .unwrap(),
                    )?;
                    for (part_num, part) in data.parts.iter().enumerate() {
                        if let template::TransclusionPart::Template {
                            template: _,
                        } = part
                        {
                            templates.push(Template::new_from_node(
                                &element, part_num,
                            ));
                        }
                        // Note: we ignore interspersed wikitext, and treat it as read-only,
                        // which is the behavior documented in the spec.
                    }
                }
                templates
            }
            Err(_) => vec![],
        };

        Ok(templates)
    }

    /// Get a list of all comments (`<!-- example -->`)
    pub fn filter_comments(&self) -> Result<Vec<Comment>> {
        Ok(self
            .body_element()
            .inclusive_descendants()
            .filter_map(|node| node.as_comment())
            .collect())
    }

    pub fn iter_sections(&self) -> Vec<Section> {
        match self.document.select(Section::SELECTOR) {
            Ok(select) => select
                .map(|node| Section::new_from_node(node.as_node()))
                .collect(),
            Err(_) => vec![],
        }
    }
}

impl From<Wikinode> for Wikicode {
    fn from(node: Wikinode) -> Self {
        Wikicode::new_from_node(node.as_node())
    }
}

impl WikinodeIterator for Wikicode {
    fn as_node(&self) -> &NodeRef {
        &self.document
    }
}

#[cfg(test)]
mod tests {
    use crate::prelude::*;
    use crate::{map::IndexMap, Result};

    fn build_client() -> Client {
        Client::new(
            "https://www.mediawiki.org/api/rest_v1",
            "parsoid-rs testing",
        )
        .unwrap()
    }

    #[test]
    fn test_fragment() {
        assert_eq!(
            Wikicode::new_fragment("foo").to_string(),
            "foo".to_string()
        );
        assert_eq!(
            Wikicode::new_fragment("<b>bar</b>").to_string(),
            "<b>bar</b>".to_string()
        );
    }

    #[tokio::test]
    #[should_panic] // FIXME: doesn't work yet
    async fn test_serialize() {
        let client = build_client();
        let html = client.get_raw("User:Legoktm").await.unwrap();
        let code = Wikicode::new(&html);
        assert_eq!(code.to_string(), html);
    }

    #[tokio::test]
    async fn test_templates() -> Result<()> {
        let client = build_client();
        let code = client.get("MediaWiki").await?;
        let mut found = false;
        for template in code.filter_templates()? {
            if template.name() == "Main page" {
                found = true;
            }
        }
        assert!(found);
        Ok(())
    }

    #[tokio::test]
    async fn test_more_cases() -> Result<()> {
        let client = build_client();
        let code = client
            .transform_to_html(
                "{{1x|param<!--comment-->name=value|normal=value2}}{{#if:{{{1}}}|foo|bar}}",
            )
            .await?;
        let templates = code.filter_templates()?;
        let temp = &templates[0];
        assert!(temp.is_template());
        assert!(!temp.is_parser_function());
        assert_eq!(temp.normalized_name(), "./Template:1x");
        let mut params = IndexMap::new();
        params.insert("normal".to_string(), "value2".to_string());
        params.insert("paramname".to_string(), "value".to_string());
        assert_eq!(temp.get_params(), params);
        assert_eq!(temp.get_param("paramname"), Some("value".to_string()));
        assert_eq!(temp.get_param("notset"), None);
        assert_eq!(
            temp.get_param_in_wikitext("paramname"),
            Some("param<!--comment-->name".to_string())
        );
        assert_eq!(
            temp.get_param_in_wikitext("normal"),
            Some("normal".to_string())
        );
        assert_eq!(temp.get_param_in_wikitext("notset"), None);
        let pf = &templates[1];
        assert!(pf.is_parser_function());
        assert!(!pf.is_template());
        assert_eq!(pf.normalized_name(), "if");
        Ok(())
    }

    #[tokio::test]
    async fn test_template_mutation() -> Result<()> {
        let client = build_client();
        let original = "{{1x|foo=bar}}";
        let code = client.transform_to_html(original).await?;
        let mut templates = code.filter_templates()?;
        let temp = &mut templates[0];
        temp.set_param("new", "wikitext")?;
        let html = client.transform_to_wikitext(&code).await?;
        assert_eq!(html, "{{1x|foo=bar|new=wikitext}}".to_string());
        temp.remove_param("new")?;
        let new_html = client.transform_to_wikitext(&code).await?;
        assert_eq!(new_html, original.to_string());
        Ok(())
    }

    #[tokio::test]
    async fn test_text_contents() -> Result<()> {
        let client = build_client();
        let code = client.get("User:Legoktm/parsoid-rs/strip_code").await?;
        assert_eq!(
            code.text_contents(),
            "This is some formatted code. Also a link.".to_string()
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_wikilinks() -> Result<()> {
        let client = build_client();
        let code = client.transform_to_html("[[Main Page|link text]]").await?;
        let links = code.filter_links()?;
        let link = &links[0];
        assert_eq!(link.target(), "./Main_Page".to_string());
        assert_eq!(link.text_contents(), "link text".to_string());
        assert_eq!(
            &link.to_string(),
            "<a class=\"mw-redirect\" href=\"./Main_Page\" id=\"mwAw\" rel=\"mw:WikiLink\" title=\"Main Page\">link text</a>"
        );
        // Mutability
        link.set_target("./MediaWiki");
        assert_eq!(link.target(), "./MediaWiki".to_string());
        assert!(code.to_string().contains("href=\"./MediaWiki\""));
        let wikitext =
            client.transform_to_wikitext_raw(&code.to_string()).await?;
        assert_eq!(wikitext, "[[MediaWiki|link text]]".to_string());
        Ok(())
    }

    #[tokio::test]
    async fn test_new_link() -> Result<()> {
        let client = build_client();
        let link = WikiLink::new("./Foo", &Wikicode::new_fragment("bar"));
        assert_eq!(
            &link.to_string(),
            "<a href=\"./Foo\" rel=\"mw:WikiLink\">bar</a>"
        );
        let code = Wikicode::new("");
        //        let new_code = Wikicode::from(&new_link);
        code.append(&link);
        let new_wikitext = client.transform_to_wikitext(&code).await?;
        assert_eq!(new_wikitext, "[[Foo|bar]]".to_string());
        Ok(())
    }

    #[tokio::test]
    async fn test_external_links() -> Result<()> {
        let client = build_client();
        let code = client
            .transform_to_html("[https://example.com Link content] ")
            .await?;
        let links = code.filter_external_links()?;
        let link = &links[0];
        assert_eq!(link.target(), "https://example.com".to_string());
        assert_eq!(link.text_contents(), "Link content".to_string());
        assert_eq!(
            &link.to_string(),
            "<a class=\"external text\" href=\"https://example.com\" id=\"mwAw\" rel=\"mw:ExtLink\">Link content</a>"
        );
        // Mutability
        link.set_target("https://wiki.example.org/foo?query=1");
        assert_eq!(
            link.target(),
            "https://wiki.example.org/foo?query=1".to_string()
        );
        let wikitext =
            client.transform_to_wikitext_raw(&code.to_string()).await?;
        assert_eq!(
            wikitext,
            "[https://wiki.example.org/foo?query=1 Link content] ".to_string()
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_comments() -> Result<()> {
        let client = build_client();
        let code = client.transform_to_html("<!--comment-->").await?;
        let comments = code.filter_comments()?;
        let comment = &comments[0];
        assert_eq!(comment.text(), "comment".to_string());
        // Surround with spaces for extra whitespace
        comment.set_text(" new ");
        assert_eq!(comment.text(), " new ".to_string());
        // Change is reflected in Wikicode serialization
        assert!(code.to_string().contains("<!-- new -->"));
        Ok(())
    }

    #[tokio::test]
    async fn test_properties() -> Result<()> {
        let client = build_client();
        // FIXME: Use a real stable page
        let code = client.get("User:Legoktm/archive.txt").await?;
        assert_eq!(code.revision_id(), Some(2016428));
        assert_eq!(code.title(), Some("User:Legoktm/archive.txt".to_string()));
        Ok(())
    }

    #[tokio::test]
    async fn test_iterators() -> Result<()> {
        let client = build_client();
        let code = client.transform_to_html("This is a [[sentence]].").await?;
        let link = code
            .descendants()
            .filter_map(|node| {
                dbg!(&node);
                node.as_wikilink()
            })
            .next()
            .unwrap();
        assert_eq!(link.target(), "./Sentence".to_string());
        assert_eq!(link.text_contents(), "sentence".to_string());
        Ok(())
    }

    #[tokio::test]
    async fn test_title() -> Result<()> {
        let client = build_client();
        let code = client.get("Project:Requests").await?;
        assert_eq!(code.title().unwrap(), "Project:Requests".to_string());
        Ok(())
    }

    #[tokio::test]
    async fn test_sections() -> Result<()> {
        let client = build_client();
        let wikitext = r#"
...lead section contents...
== foo=bar ==
...section contents...
=== nested ===
...section contents...
"#;
        let code = client.transform_to_html(wikitext).await?;
        let sections = code.iter_sections();
        {
            let section = &sections[0];
            assert!(section.is_pseudo_section());
            assert_eq!(section.section_id(), 0);
            assert!(section.heading().is_none());
        }
        {
            let section = &sections[1];
            assert!(!section.is_pseudo_section());
            assert_eq!(section.section_id(), 1);
            let heading = section.heading().unwrap();
            assert_eq!(heading.text_contents(), "foo=bar")
        }
        {
            let section = &sections[2];
            assert!(!section.is_pseudo_section());
            assert_eq!(section.section_id(), 2);
            let heading = section.heading().unwrap();
            assert_eq!(heading.text_contents(), "nested")
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_heading() -> Result<()> {
        let client = build_client();
        let heading = Heading::new(2, "Some text")?;
        let code = Wikicode::new("");
        code.append(&heading);
        let wikitext = client.transform_to_wikitext(&code).await?;
        assert_eq!(&wikitext, "== Some text ==\n");

        Ok(())
    }

    #[tokio::test]
    async fn test_category() -> Result<()> {
        let client = build_client();
        let category = Category::new("Category:Foo", Some("Bar baz#quux"));
        let code = Wikicode::new("");
        code.append(&category);
        let wikitext = client.transform_to_wikitext(&code).await?;
        assert_eq!(&wikitext, "[[Category:Foo|Bar baz#quux]]");

        Ok(())
    }

    #[tokio::test]
    async fn test_language_link() -> Result<()> {
        let client = build_client();
        let link = LanguageLink::new("https://en.wikipedia.org/wiki/Foo");
        let code = Wikicode::new("");
        code.append(&link);
        let wikitext = client.transform_to_wikitext(&code).await?;
        assert_eq!(&wikitext, "[[en:Foo]]");

        Ok(())
    }

    #[tokio::test]
    async fn test_behavior_switch() -> Result<()> {
        let client = build_client();
        let code = Wikicode::new("");
        code.append(&BehaviorSwitch::new("toc", None));
        code.append(&BehaviorSwitch::new("displaytitle", Some("foo")));
        let wikitext = client.transform_to_wikitext(&code).await?;
        assert_eq!(&wikitext, "__TOC__\n{{DISPLAYTITLE:foo}}\n");

        Ok(())
    }

    #[tokio::test]
    async fn test_redirect() -> Result<()> {
        let client = build_client();
        let code = Wikicode::new("");
        code.append(&Redirect::new("./Foo"));
        assert_eq!(code.get_redirect().unwrap().target(), "./Foo".to_string());
        let wikitext = client.transform_to_wikitext(&code).await?;
        assert_eq!(&wikitext, "#REDIRECT [[Foo]]");

        Ok(())
    }
}