Skip to content

Commit

Permalink
feat(quakedown): add parsing function for quake note
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 20, 2021
1 parent f18339e commit fde8dca
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 29 deletions.
71 changes: 71 additions & 0 deletions quake_core/src/markdown/entry_reference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use lazy_static::lazy_static;
use regex::Regex;

lazy_static! {
static ref ENTRY_LINK_RE: Regex =
Regex::new(r#"^(?P<type>[^#|:]+):(?P<id>\d{1,})??(#(?P<section>.+?))??(\|(?P<label>.+?))??\s"(?P<title>[^"]+)"$"#).unwrap();
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Default)]
pub struct EntryReference {
entry_type: String,
entry_id: String,
entry_title: String,
label: Option<String>,
section: Option<String>,
}

impl EntryReference {
#[allow(clippy::all)]
pub fn from_str(text: &str) -> EntryReference {
let captures = ENTRY_LINK_RE
.captures(text)
.expect("note link regex didn't match - bad input?");

let entry_type = captures.name("type").map(|v| v.as_str()).unwrap_or("");
let entry_id = captures.name("id").map(|v| v.as_str()).unwrap_or("");
let entry_title = captures.name("title").map(|v| v.as_str()).unwrap_or("");

let label = captures.name("label").map(|v| v.as_str().to_string());
let section = captures.name("section").map(|v| v.as_str().to_string());

EntryReference {
entry_type: entry_type.to_string(),
entry_id: entry_id.to_string(),
entry_title: entry_title.to_string(),
label,
section,
}
}
}

#[cfg(test)]
mod tests {
use crate::markdown::entry_reference::EntryReference;

#[test]
fn parse_quake_down_link() {
let text = r#"note:0001#Heading|Label "file name""#;
let reference = EntryReference::from_str(text);

assert_eq!("note", reference.entry_type);
assert_eq!("file name", reference.entry_title);
assert_eq!("0001", reference.entry_id);

assert_eq!("Label", reference.label.unwrap());
assert_eq!("Heading", reference.section.unwrap());
}

#[test]
fn parse_normal() {
let text = r#"note:0001 "file name""#;
let reference = EntryReference::from_str(text);

assert_eq!("note", reference.entry_type);
assert_eq!("file name", reference.entry_title);
assert_eq!("0001", reference.entry_id);

assert!(reference.label.is_none());
assert!(reference.section.is_none());
}
}
34 changes: 5 additions & 29 deletions quake_core/src/markdown/md_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::error::Error;
use std::ffi::OsString;
use std::path::PathBuf;

use crate::markdown::entry_reference::EntryReference;
use pulldown_cmark::{CodeBlockKind, CowStr, Event, Options, Parser, Tag};
use pulldown_cmark_to_cmark::cmark_with_options;

Expand All @@ -37,26 +38,19 @@ pub type MarkdownEvents<'a> = Vec<Event<'a>>;
#[derive(Debug, Default)]
pub struct MdProcessor {}

#[derive(Serialize, Deserialize, PartialEq, Debug, Default)]
pub struct PageLink {
entry_type: String,
entry_id: String,
entry_title: String,
}

impl MdProcessor {
pub fn transform(content: &str) -> Result<String, Box<dyn Error>> {
let mut down = MdProcessor::default();
let mut links: Vec<PageLink> = vec![];
let mut links: Vec<EntryReference> = vec![];
let events = down.add_custom_syntax(content, &mut links)?;
let mapping = events.into_iter().map(event_to_owned).collect();

Ok(events_to_text(mapping))
}

pub fn pagelinks(content: &str) -> Result<Vec<PageLink>, Box<dyn Error>> {
pub fn pagelinks(content: &str) -> Result<Vec<EntryReference>, Box<dyn Error>> {
let mut down = MdProcessor::default();
let mut links: Vec<PageLink> = vec![];
let mut links: Vec<EntryReference> = vec![];
let _events = down.add_custom_syntax(content, &mut links)?;

Ok(links)
Expand All @@ -66,7 +60,7 @@ impl MdProcessor {
fn add_custom_syntax<'a>(
&mut self,
content: &'a str,
_links: &mut Vec<PageLink>,
_links: &mut Vec<EntryReference>,
) -> Result<Vec<Event<'a>>, Box<dyn Error>> {
let mut parser_options = Options::empty();
parser_options.insert(Options::ENABLE_TABLES);
Expand Down Expand Up @@ -288,7 +282,6 @@ fn codeblock_kind_to_owned<'a>(codeblock_kind: CodeBlockKind) -> CodeBlockKind<'
#[cfg(test)]
mod tests {
use crate::markdown::md_processor::MdProcessor;
use regex::Regex;
use std::fs;
use std::path::PathBuf;

Expand Down Expand Up @@ -324,21 +317,4 @@ mod tests {
let string = MdProcessor::transform("![[Note:0001#Heading|Label \"file name\"]]").unwrap();
assert_eq!("[Label “file name”](Note:0001)", string);
}

#[test]
fn parse_quake_down_link() {
let page_ref = Regex::new(
r#"^(?P<type>[^#|:]+):(?P<id>\d{1,})??(#(?P<section>.+?))??(\|(?P<label>.+?))??\s"([^"]+)"$"#,
).unwrap();
let text = r#"note:0001#Heading|Label "file name""#;

let captures = page_ref
.captures(text)
.expect("note link regex didn't match - bad input?");
let entry_type = captures.name("type").map(|v| v.as_str());
let _label = captures.name("label").map(|v| v.as_str());
let _section = captures.name("section").map(|v| v.as_str());

println!("{:?}", entry_type);
}
}
1 change: 1 addition & 0 deletions quake_core/src/markdown/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod entry_reference;
pub mod md_processor;
pub mod references;

0 comments on commit fde8dca

Please sign in to comment.