Basic implementation of the parser for the title of the link, does not pass the test with nested brackets

This commit is contained in:
Mathieu David 2016-07-07 14:39:16 +02:00
parent fb648ef61e
commit 82a9dad4df
2 changed files with 48 additions and 4 deletions

View File

@ -73,7 +73,11 @@ extern crate rustc_serialize;
extern crate handlebars;
extern crate pulldown_cmark;
#[macro_use] pub mod macros;
#[macro_use]
extern crate nom;
#[macro_use]
pub mod macros;
pub mod book;
mod parse;
pub mod renderer;

View File

@ -1,6 +1,7 @@
extern crate nom;
use nom::{self, IResult, alphanumeric};
use self::nom::IResult;
use std::str;
use std::str::FromStr;
#[derive(Debug, Clone)]
struct Link {
@ -23,9 +24,48 @@ fn link(i: &[u8]) -> IResult<&[u8], Link> {
/// Parser for parsing the title part of the link: [title](destination)
/// ^^^^^^^
fn link_text(i: &[u8]) -> IResult<&[u8], String> {
unimplemented!();
map_res!(i, map_res!(delimited!(char!('['), is_not!("[]"), char!(']')), str::from_utf8), FromStr::from_str)
}
/// Parser for parsing the destination part of the link: [title](destination)
/// ^^^^^^^^^^^^^
fn link_destination(i: &[u8]) -> IResult<&[u8], String> {
unimplemented!();
}
#[cfg(test)]
mod tests {
use nom::{self, IResult};
// Tests for link_text
#[test]
fn link_text_one_ch() {
assert_eq!(super::link_text(b"[a]"), nom::IResult::Done(&b""[..], String::from("a")));
}
#[test]
fn link_text_multi_ch() {
assert_eq!(super::link_text(b"[Intro]"), nom::IResult::Done(&b""[..], String::from("Intro")));
}
#[test]
fn link_text_sp_ch() {
assert_eq!(super::link_text(b"[Intro!]"), nom::IResult::Done(&b""[..], String::from("Intro!")));
}
#[test]
fn link_text_unicode() {
assert_eq!(super::link_text("[Heizölrückstoßabdämpfung]".as_bytes()),
nom::IResult::Done(&b""[..], String::from("Heizölrückstoßabdämpfung")));
assert_eq!(super::link_text("[Здравствуйте]".as_bytes()),
nom::IResult::Done(&b""[..], String::from("Здравствуйте")));
}
#[test]
fn link_text_brackets() {
assert_eq!(super::link_text(b"[Intro[]]"), nom::IResult::Done(&b""[..], String::from("Intro[]")));
assert_eq!(super::link_text(br"[Intro\[]"), nom::IResult::Done(&b""[..], String::from("Intro[")));
}
}