feat(elements): cleanup and minor refactor
This commit is contained in:
parent
c1465a6d77
commit
9bc260627b
20 changed files with 802 additions and 835 deletions
|
|
@ -2,71 +2,7 @@ use std::borrow::Cow;
|
|||
|
||||
use nom::{bytes::complete::tag_no_case, character::complete::alpha1, sequence::preceded, IResult};
|
||||
|
||||
use crate::parsers::{take_lines_till, take_until_eol};
|
||||
|
||||
#[cfg_attr(test, derive(PartialEq))]
|
||||
#[derive(Debug)]
|
||||
pub struct Block<'a> {
|
||||
pub name: Cow<'a, str>,
|
||||
pub args: Option<Cow<'a, str>>,
|
||||
}
|
||||
|
||||
impl Block<'_> {
|
||||
#[inline]
|
||||
pub(crate) fn parse(input: &str) -> IResult<&str, (Block<'_>, &str)> {
|
||||
let (input, name) = preceded(tag_no_case("#+BEGIN_"), alpha1)(input)?;
|
||||
let (input, args) = take_until_eol(input)?;
|
||||
let end_line = format!(r"#+END_{}", name);
|
||||
let (input, contents) =
|
||||
take_lines_till(|line| line.eq_ignore_ascii_case(&end_line))(input)?;
|
||||
|
||||
Ok((
|
||||
input,
|
||||
(
|
||||
Block {
|
||||
name: name.into(),
|
||||
args: if args.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(args.into())
|
||||
},
|
||||
},
|
||||
contents,
|
||||
),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse() {
|
||||
assert_eq!(
|
||||
Block::parse("#+BEGIN_SRC\n#+END_SRC"),
|
||||
Ok((
|
||||
"",
|
||||
(
|
||||
Block {
|
||||
name: "SRC".into(),
|
||||
args: None,
|
||||
},
|
||||
""
|
||||
)
|
||||
))
|
||||
);
|
||||
assert_eq!(
|
||||
Block::parse("#+BEGIN_SRC javascript \nconsole.log('Hello World!');\n#+END_SRC\n"),
|
||||
Ok((
|
||||
"",
|
||||
(
|
||||
Block {
|
||||
name: "SRC".into(),
|
||||
args: Some("javascript".into()),
|
||||
},
|
||||
"console.log('Hello World!');\n"
|
||||
)
|
||||
))
|
||||
);
|
||||
// TODO: more testing
|
||||
}
|
||||
use crate::parsers::{line, take_lines_while};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(test, derive(PartialEq))]
|
||||
|
|
@ -129,3 +65,60 @@ pub struct SourceBlock<'a> {
|
|||
pub language: Cow<'a, str>,
|
||||
pub arguments: Cow<'a, str>,
|
||||
}
|
||||
|
||||
pub(crate) fn parse_block_element(input: &str) -> IResult<&str, (&str, Option<&str>, &str)> {
|
||||
let (input, name) = preceded(tag_no_case("#+BEGIN_"), alpha1)(input)?;
|
||||
let (input, args) = line(input)?;
|
||||
let end_line = format!(r"#+END_{}", name);
|
||||
let (input, contents) =
|
||||
take_lines_while(|line| !line.trim().eq_ignore_ascii_case(&end_line))(input)?;
|
||||
let (input, _) = line(input)?;
|
||||
|
||||
Ok((
|
||||
input,
|
||||
(
|
||||
name,
|
||||
if args.trim().is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(args.trim())
|
||||
},
|
||||
contents,
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse() {
|
||||
assert_eq!(
|
||||
parse_block_element(
|
||||
r#"#+BEGIN_SRC
|
||||
#+END_SRC"#
|
||||
),
|
||||
Ok(("", ("SRC".into(), None, "")))
|
||||
);
|
||||
assert_eq!(
|
||||
parse_block_element(
|
||||
r#"#+begin_src
|
||||
#+end_src"#
|
||||
),
|
||||
Ok(("", ("src".into(), None, "")))
|
||||
);
|
||||
assert_eq!(
|
||||
parse_block_element(
|
||||
r#"#+BEGIN_SRC javascript
|
||||
console.log('Hello World!');
|
||||
#+END_SRC
|
||||
"#
|
||||
),
|
||||
Ok((
|
||||
"",
|
||||
(
|
||||
"SRC".into(),
|
||||
Some("javascript".into()),
|
||||
"console.log('Hello World!');\n"
|
||||
)
|
||||
))
|
||||
);
|
||||
// TODO: more testing
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue