fix: list parsing

This commit is contained in:
PoiScript 2019-02-06 18:16:44 +08:00
parent d20d4c2880
commit 54b0f938f9
4 changed files with 173 additions and 109 deletions

View file

@ -1,3 +1,5 @@
use lines::Lines;
#[cfg_attr(test, derive(PartialEq))]
#[derive(Debug)]
pub struct Block;
@ -9,27 +11,25 @@ impl Block {
return None;
}
let args = eol!(src);
let name = until_while!(src, 8, |c| c == b' ' || c == b'\n', |c: u8| c
.is_ascii_alphabetic())?;
let mut lines = Lines::new(src);
let (pre_cont_end, cont_beg, _) = lines.next()?;
let args = if pre_cont_end == name {
None
} else {
Some(&src[name..pre_cont_end])
};
let name = &src[8..name];
let end_line = format!(r"#+END_{}", name);
let mut pre_end = cont_beg;
let mut pos = 0;
let end = format!(r"#+END_{}", &src[8..name]);
for line_end in lines!(src) {
if src[pos..line_end].trim().eq_ignore_ascii_case(&end) {
return Some((
&src[8..name],
if name == args {
None
} else {
Some(&src[name..args])
},
args,
pos,
line_end,
));
while let Some((_, end, line)) = lines.next() {
if line.trim().eq_ignore_ascii_case(&end_line) {
return Some((name, args, cont_beg, pre_end, end));
} else {
pre_end = end;
}
pos = line_end;
}
None
@ -40,7 +40,7 @@ impl Block {
fn parse() {
assert_eq!(
Block::parse("#+BEGIN_SRC\n#+END_SRC"),
Some(("SRC", None, 11, 12, 21))
Some(("SRC", None, 12, 12, 21))
);
assert_eq!(
Block::parse(
@ -52,7 +52,7 @@ fn main() {
#+END_SRC
"#
),
Some(("SRC", Some(" rust"), 16, 104, 114))
Some(("SRC", Some(" rust"), 17, 104, 114))
);
// TODO: more testing
}

View file

@ -125,7 +125,6 @@ impl<'a> Element<'a> {
}
}
// FIXME:
if bytes[pos] == b'\n' {
break (
Some(Element::Paragraph {
@ -154,7 +153,7 @@ impl<'a> Element<'a> {
end: line_beg - start,
}),
start,
Some((list, 1)),
Some((list, 0)),
)
};
}
@ -180,7 +179,7 @@ impl<'a> Element<'a> {
if bytes[pos..].starts_with(b"#+") {
if let Some((name, args, cont_beg, cont_end, end)) = Block::parse(&src[pos..]) {
let cont = &src[pos + cont_beg + 1..pos + cont_end - 1];
let cont = &src[pos + cont_beg..pos + cont_end];
match name.to_uppercase().as_str() {
"COMMENT" => brk!(Element::CommentBlock { args, cont }, end),
"EXAMPLE" => brk!(Element::ExampleBlock { args, cont }, end),
@ -190,16 +189,16 @@ impl<'a> Element<'a> {
"CENTER" => brk!(
Element::CtrBlock {
args,
cont_end,
end,
cont_end: cont_end - cont_beg,
end: end - cont_beg,
},
cont_beg
),
"QUOTE" => brk!(
Element::QteBlock {
args,
cont_end,
end,
cont_end: cont_end - cont_beg,
end: end - cont_beg,
},
cont_beg
),
@ -207,8 +206,8 @@ impl<'a> Element<'a> {
Element::SplBlock {
name,
args,
cont_end,
end
cont_end: cont_end - cont_beg,
end: end - cont_beg,
},
cont_beg
),
@ -371,10 +370,28 @@ fn next_2() {
ident: 0,
ordered: false,
},
1
0
))
)
);
assert_eq!(
Element::next_2("\n\nLorem ipsum dolor sit amet.\n#+BEGIN_QUOTE\nLorem ipsum dolor sit amet.\n#+END_QUOTE\n"),
(
Some(Paragraph {
cont_end: len,
end: len + 1,
}),
2,
Some((
QteBlock {
args: None,
cont_end: len + 1,
end: len + 1 + "#+END_QUOTE\n".len()
},
"#+BEGIN_QUOTE\n".len()
))
)
);
// TODO: more tests
}