Fix toml parse error line number (kind of) (#1138)
- Minor change to how frontmatter is detected so that toml issues correct line numbers - split_content now returns (&str, &str)
This commit is contained in:
parent
05646ab573
commit
159ce0f672
|
@ -13,7 +13,7 @@ pub use section::SectionFrontMatter;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref PAGE_RE: Regex =
|
static ref PAGE_RE: Regex =
|
||||||
Regex::new(r"^[[:space:]]*\+\+\+\r?\n((?s).*?(?-s))\+\+\+\r?\n?((?s).*(?-s))$").unwrap();
|
Regex::new(r"^[[:space:]]*\+\+\+(\r?\n(?s).*?(?-s))\+\+\+\r?\n?((?s).*(?-s))$").unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
|
@ -37,7 +37,7 @@ pub enum InsertAnchor {
|
||||||
|
|
||||||
/// Split a file between the front matter and its content
|
/// Split a file between the front matter and its content
|
||||||
/// Will return an error if the front matter wasn't found
|
/// Will return an error if the front matter wasn't found
|
||||||
fn split_content(file_path: &Path, content: &str) -> Result<(String, String)> {
|
fn split_content<'c>(file_path: &Path, content: &'c str) -> Result<(&'c str, &'c str)> {
|
||||||
if !PAGE_RE.is_match(content) {
|
if !PAGE_RE.is_match(content) {
|
||||||
bail!(
|
bail!(
|
||||||
"Couldn't find front matter in `{}`. Did you forget to add `+++`?",
|
"Couldn't find front matter in `{}`. Did you forget to add `+++`?",
|
||||||
|
@ -50,15 +50,15 @@ fn split_content(file_path: &Path, content: &str) -> Result<(String, String)> {
|
||||||
// caps[0] is the full match
|
// caps[0] is the full match
|
||||||
// caps[1] => front matter
|
// caps[1] => front matter
|
||||||
// caps[2] => content
|
// caps[2] => content
|
||||||
Ok((caps[1].to_string(), caps[2].to_string()))
|
Ok((caps.get(1).unwrap().as_str(), caps.get(2).unwrap().as_str()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Split a file between the front matter and its content.
|
/// Split a file between the front matter and its content.
|
||||||
/// Returns a parsed `SectionFrontMatter` and the rest of the content
|
/// Returns a parsed `SectionFrontMatter` and the rest of the content
|
||||||
pub fn split_section_content(
|
pub fn split_section_content<'c>(
|
||||||
file_path: &Path,
|
file_path: &Path,
|
||||||
content: &str,
|
content: &'c str,
|
||||||
) -> Result<(SectionFrontMatter, String)> {
|
) -> Result<(SectionFrontMatter, &'c str)> {
|
||||||
let (front_matter, content) = split_content(file_path, content)?;
|
let (front_matter, content) = split_content(file_path, content)?;
|
||||||
let meta = SectionFrontMatter::parse(&front_matter).map_err(|e| {
|
let meta = SectionFrontMatter::parse(&front_matter).map_err(|e| {
|
||||||
Error::chain(
|
Error::chain(
|
||||||
|
@ -71,7 +71,7 @@ pub fn split_section_content(
|
||||||
|
|
||||||
/// Split a file between the front matter and its content
|
/// Split a file between the front matter and its content
|
||||||
/// Returns a parsed `PageFrontMatter` and the rest of the content
|
/// Returns a parsed `PageFrontMatter` and the rest of the content
|
||||||
pub fn split_page_content(file_path: &Path, content: &str) -> Result<(PageFrontMatter, String)> {
|
pub fn split_page_content<'c>(file_path: &Path, content: &'c str) -> Result<(PageFrontMatter, &'c str)> {
|
||||||
let (front_matter, content) = split_content(file_path, content)?;
|
let (front_matter, content) = split_content(file_path, content)?;
|
||||||
let meta = PageFrontMatter::parse(&front_matter).map_err(|e| {
|
let meta = PageFrontMatter::parse(&front_matter).map_err(|e| {
|
||||||
Error::chain(
|
Error::chain(
|
||||||
|
|
|
@ -136,7 +136,7 @@ impl Page {
|
||||||
|
|
||||||
page.lang = page.file.find_language(config)?;
|
page.lang = page.file.find_language(config)?;
|
||||||
|
|
||||||
page.raw_content = content;
|
page.raw_content = content.to_string();
|
||||||
let (word_count, reading_time) = get_reading_analytics(&page.raw_content);
|
let (word_count, reading_time) = get_reading_analytics(&page.raw_content);
|
||||||
page.word_count = Some(word_count);
|
page.word_count = Some(word_count);
|
||||||
page.reading_time = Some(reading_time);
|
page.reading_time = Some(reading_time);
|
||||||
|
|
|
@ -107,7 +107,7 @@ impl Section {
|
||||||
let (meta, content) = split_section_content(file_path, content)?;
|
let (meta, content) = split_section_content(file_path, content)?;
|
||||||
let mut section = Section::new(file_path, meta, base_path);
|
let mut section = Section::new(file_path, meta, base_path);
|
||||||
section.lang = section.file.find_language(config)?;
|
section.lang = section.file.find_language(config)?;
|
||||||
section.raw_content = content;
|
section.raw_content = content.to_string();
|
||||||
let (word_count, reading_time) = get_reading_analytics(§ion.raw_content);
|
let (word_count, reading_time) = get_reading_analytics(§ion.raw_content);
|
||||||
section.word_count = Some(word_count);
|
section.word_count = Some(word_count);
|
||||||
section.reading_time = Some(reading_time);
|
section.reading_time = Some(reading_time);
|
||||||
|
|
Loading…
Reference in New Issue