Struct Configuration

Source
pub struct Configuration { /* private fields */ }
Expand description

Configuration for the parser.

A configuration to correctly parse a real wiki can be created with Configuration::new. A configuration for testing and quick and dirty prototyping can be created with Default::default.

Implementations§

Source§

impl Configuration

Source

pub fn new(source: &ConfigurationSource<'_>) -> Self

Allocates and returns a new configuration based on the given site specific configuration.

Source

pub fn parse<'a>(&self, wiki_text: &'a str) -> Output<'a>

Parses wiki text into structured data.

Examples found in repository?
examples/test/main.rs (line 26)
10fn main() {
11    let mut args = std::env::args();
12    match args.nth(1) {
13        None => return test::run_test(&Default::default()),
14        Some(command) => match &command as _ {
15            "file" => {
16                if let Some(path) = args.next() {
17                    if args.next().is_none() {
18                        match std::fs::read_to_string(path) {
19                            Err(error) => {
20                                eprintln!("Failed to read file: {}", error);
21                                std::process::exit(1);
22                            }
23                            Ok(file_contents) => {
24                                println!(
25                                    "{:#?}",
26                                    parse_wiki_text::Configuration::default().parse(&file_contents)
27                                );
28                                return;
29                            }
30                        }
31                    }
32                }
33            }
34            "text" => {
35                if let Some(wiki_text) = args.next() {
36                    if args.next().is_none() {
37                        println!(
38                            "{:#?}",
39                            parse_wiki_text::Configuration::default()
40                                .parse(&wiki_text.replace("\\t", "\t").replace("\\n", "\n"))
41                        );
42                        return;
43                    }
44                }
45            }
46            _ => {}
47        },
48    }
49    eprintln!("invalid use");
50    std::process::exit(1);
51}
More examples
Hide additional examples
examples/test/test.rs (line 50)
7pub fn run_test(configuration: &parse_wiki_text::Configuration) {
8    let mut output = concat!(
9        "<title>Parse Wiki Text test cases</title>",
10        "<style>",
11        "a{color:#006064;display:block;padding:8;text-decoration:none}",
12        "a:hover{background:#eee}",
13        "body{background:#f7f7f7;display:flex;font-family:sans-serif;height:100%;margin:0}",
14        "div div{background:#fff;box-shadow: 0 1px 3px rgba(0,0,0,.12),0 1px 2px rgba(0,0,0,.24);margin:16;padding:16}",
15        "h1{font-size:20;margin:24 16 16}",
16        "hr{border:0;border-top:1px solid #ccc}",
17        "pre{margin:0}",
18        "span{color:#aaa}",
19        "</style>",
20        "<div style=\"background:#fff;box-shadow: 0 1px 3px rgba(0,0,0,.12),0 1px 2px rgba(0,0,0,.24);flex:0 1 220px;overflow:auto\">"
21    ).to_owned();
22    if let Some(window) = TEST_CASES
23        .windows(2)
24        .find(|window| window[0].0 >= window[1].0)
25    {
26        panic!("Sort: {:#?}", (window[0].0, window[1].0));
27    }
28    for (title, test_cases) in TEST_CASES {
29        if let Some(window) = test_cases.windows(2).find(|window| window[0] >= window[1]) {
30            panic!("Sort: {:#?}", window);
31        }
32        output += &format!("<a href=#{}>", title.replace(" ", "_"));
33        output += title;
34        output += &format!(" <span>{}</span></a>", test_cases.len());
35    }
36    output += "</div><div style=\"flex:1 1 200px;overflow:auto\">";
37    for (title, test_cases) in TEST_CASES {
38        output += &format!("<h1 id={}>", title.replace(" ", "_"));
39        output += title;
40        output += "</h1>";
41        for wiki_text in *test_cases {
42            output += "<div><pre>";
43            output += &wiki_text
44                .replace("&", "&amp;")
45                .replace("<", "&lt;")
46                .replace("\t", "<span>⭾</span>")
47                .replace("\n", "<span>⏎</span>\n")
48                .replace(" ", "<span>·</span>")
49                .replace("</span><span>", "");
50            match std::panic::catch_unwind(|| configuration.parse(wiki_text)) {
51                Err(_) => {
52                    eprintln!("Panic with wiki text {:?}", wiki_text);
53                    output += "</pre><hr>panic</div>";
54                }
55                Ok(result) => {
56                    output += "</pre><hr><pre>";
57                    output += &format!("{:#?}", result)
58                        .replace("&", "&amp;")
59                        .replace("<", "&lt;");
60                    output += "</pre></div>";
61                }
62            }
63        }
64    }
65    output += "</div>";
66    if let Err(error) = std::fs::write("report.html", output) {
67        eprintln!("Failed to write report: {}", error);
68        std::process::exit(1);
69    }
70}

Trait Implementations§

Source§

impl Default for Configuration

Source§

fn default() -> Self

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.