-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
345 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use nom::{bytes::complete::tag, character::complete::space0, IResult}; | ||
|
||
use crate::{ | ||
primitives::{consume_empty_lines, non_empty_line, trim_input_for_rem}, | ||
HasSpan, Span, | ||
}; | ||
|
||
/// An AsciiDoc document may begin with a document header. The document header | ||
/// encapsulates the document title, author and revision information, | ||
/// document-wide attributes, and other document metadata. | ||
#[allow(dead_code)] // TEMPORARY while building | ||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub struct Header<'a> { | ||
title: Option<Span<'a>>, | ||
source: Span<'a>, | ||
} | ||
|
||
impl<'a> Header<'a> { | ||
#[allow(dead_code)] // TEMPORARY | ||
pub(crate) fn parse(i: Span<'a>) -> IResult<Span, Self> { | ||
let source = consume_empty_lines(i); | ||
|
||
// TEMPORARY: Titles are optional, but we're not prepared for that yet. | ||
let (rem, title) = parse_title(source)?; | ||
|
||
let source = trim_input_for_rem(source, rem); | ||
Ok(( | ||
rem, | ||
Self { | ||
title: Some(title), | ||
source, | ||
}, | ||
)) | ||
} | ||
|
||
/// Return a [`Span`] describing the document title, if there was one. | ||
pub fn title(&'a self) -> Option<Span<'a>> { | ||
self.title | ||
} | ||
} | ||
|
||
impl<'a> HasSpan<'a> for Header<'a> { | ||
fn span(&'a self) -> &'a Span<'a> { | ||
&self.source | ||
} | ||
} | ||
|
||
fn parse_title(i: Span<'_>) -> IResult<Span, Span<'_>> { | ||
let (rem, line) = non_empty_line(i)?; | ||
|
||
let (title, _) = tag("= ")(line)?; | ||
let (title, _) = space0(title)?; | ||
|
||
Ok((rem, title)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,6 @@ | |
#[allow(clippy::module_inception)] | ||
mod document; | ||
pub use document::Document; | ||
|
||
mod header; | ||
pub use header::Header; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use pretty_assertions_sorted::assert_eq; | ||
|
||
use crate::{ | ||
document::Header, | ||
tests::fixtures::{document::THeader, TSpan}, | ||
Span, | ||
}; | ||
|
||
#[test] | ||
fn impl_clone() { | ||
// Silly test to mark the #[derive(...)] line as covered. | ||
let h1 = Header::parse(Span::new("= Title", true)).unwrap(); | ||
let h2 = h1.clone(); | ||
assert_eq!(h1, h2); | ||
} | ||
|
||
#[test] | ||
fn only_title() { | ||
let (rem, block) = Header::parse(Span::new("= Just the Title", true)).unwrap(); | ||
|
||
assert_eq!( | ||
rem, | ||
TSpan { | ||
data: "", | ||
line: 1, | ||
col: 17, | ||
offset: 16 | ||
} | ||
); | ||
|
||
assert_eq!( | ||
block, | ||
THeader { | ||
title: Some(TSpan { | ||
data: "Just the Title", | ||
line: 1, | ||
col: 3, | ||
offset: 2, | ||
}), | ||
source: TSpan { | ||
data: "= Just the Title", | ||
line: 1, | ||
col: 1, | ||
offset: 0, | ||
} | ||
} | ||
); | ||
} | ||
|
||
#[test] | ||
fn trims_leading_spaces_in_title() { | ||
// This is totally a judgement call on my part. As far as I can tell, | ||
// the language doesn't describe behavior here. | ||
let (rem, block) = Header::parse(Span::new("= Just the Title", true)).unwrap(); | ||
|
||
assert_eq!( | ||
rem, | ||
TSpan { | ||
data: "", | ||
line: 1, | ||
col: 20, | ||
offset: 19 | ||
} | ||
); | ||
|
||
assert_eq!( | ||
block, | ||
THeader { | ||
title: Some(TSpan { | ||
data: "Just the Title", | ||
line: 1, | ||
col: 6, | ||
offset: 5, | ||
}), | ||
source: TSpan { | ||
data: "= Just the Title", | ||
line: 1, | ||
col: 1, | ||
offset: 0, | ||
} | ||
} | ||
); | ||
} | ||
|
||
#[test] | ||
fn trims_trailing_spaces_in_title() { | ||
let (rem, block) = Header::parse(Span::new("= Just the Title ", true)).unwrap(); | ||
|
||
assert_eq!( | ||
rem, | ||
TSpan { | ||
data: "", | ||
line: 1, | ||
col: 20, | ||
offset: 19 | ||
} | ||
); | ||
|
||
assert_eq!( | ||
block, | ||
THeader { | ||
title: Some(TSpan { | ||
data: "Just the Title", | ||
line: 1, | ||
col: 3, | ||
offset: 2, | ||
}), | ||
source: TSpan { | ||
data: "= Just the Title ", | ||
line: 1, | ||
col: 1, | ||
offset: 0, | ||
} | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
// circumstance. | ||
#[allow(clippy::module_inception)] | ||
mod document; | ||
|
||
mod header; |
Oops, something went wrong.