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
impl Configuration
Sourcepub fn new(source: &ConfigurationSource<'_>) -> Self
pub fn new(source: &ConfigurationSource<'_>) -> Self
Allocates and returns a new configuration based on the given site specific configuration.
Sourcepub fn parse<'a>(&self, wiki_text: &'a str) -> Output<'a>
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
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("&", "&")
45 .replace("<", "<")
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("&", "&")
59 .replace("<", "<");
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§
Auto Trait Implementations§
impl Freeze for Configuration
impl RefUnwindSafe for Configuration
impl Send for Configuration
impl Sync for Configuration
impl Unpin for Configuration
impl UnwindSafe for Configuration
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more