fix: handle errors
This commit is contained in:
@@ -63,8 +63,10 @@ pub fn to_tumblr_npf<'a>(ast: &'a Node<'a, RefCell<Ast>>) -> Result<RefCell<NPF>
|
||||
}
|
||||
NodeValue::Text(t) => text.push_str(&format!("{} ", &t)),
|
||||
_ => (),
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Ok::<(), Error>(())
|
||||
})?;
|
||||
|
||||
let text = text.borrow().trim().to_string().replace(" ", " ");
|
||||
let mut block = content_types::Text::from(text);
|
||||
@@ -76,43 +78,40 @@ pub fn to_tumblr_npf<'a>(ast: &'a Node<'a, RefCell<Ast>>) -> Result<RefCell<NPF>
|
||||
};
|
||||
|
||||
npf.borrow_mut().content.push(ContentType::Text(block));
|
||||
|
||||
Ok::<(), Error>(())
|
||||
}
|
||||
NodeValue::BlockQuote => {
|
||||
let block_quote = to_tumblr_npf(node);
|
||||
let block_quote = to_tumblr_npf(node)?;
|
||||
let final_block = RefCell::new(content_types::Text::new());
|
||||
|
||||
block_quote
|
||||
.unwrap()
|
||||
.borrow_mut()
|
||||
.content
|
||||
.iter_mut()
|
||||
.for_each(|b| {
|
||||
if let ContentType::Text(t) = b {
|
||||
let mut fb = final_block.borrow_mut();
|
||||
let text_len = fb.text.chars().count();
|
||||
block_quote.borrow_mut().content.iter_mut().for_each(|b| {
|
||||
if let ContentType::Text(t) = b {
|
||||
let mut fb = final_block.borrow_mut();
|
||||
let text_len = fb.text.chars().count();
|
||||
|
||||
if let Some(formattings) = &t.formatting {
|
||||
for formatting in formattings {
|
||||
match fb.formatting {
|
||||
Some(ref mut v) => v.push(Formatting {
|
||||
if let Some(formattings) = &t.formatting {
|
||||
for formatting in formattings {
|
||||
match fb.formatting {
|
||||
Some(ref mut v) => v.push(Formatting {
|
||||
start: text_len + formatting.start,
|
||||
end: text_len + formatting.end,
|
||||
..formatting.clone()
|
||||
}),
|
||||
None => {
|
||||
fb.formatting = Some(vec![Formatting {
|
||||
start: text_len + formatting.start,
|
||||
end: text_len + formatting.end,
|
||||
..formatting.clone()
|
||||
}),
|
||||
None => {
|
||||
fb.formatting = Some(vec![Formatting {
|
||||
start: text_len + formatting.start,
|
||||
end: text_len + formatting.end,
|
||||
..formatting.clone()
|
||||
}]);
|
||||
}
|
||||
}]);
|
||||
}
|
||||
}
|
||||
}
|
||||
fb.text.push_str(&format!("{}\n\n", &t.text));
|
||||
} else {
|
||||
}
|
||||
});
|
||||
fb.text.push_str(&format!("{}\n\n", &t.text));
|
||||
} else {
|
||||
}
|
||||
});
|
||||
|
||||
println!("{node:#?}");
|
||||
println!("{:#?}", final_block.borrow());
|
||||
@@ -130,6 +129,7 @@ pub fn to_tumblr_npf<'a>(ast: &'a Node<'a, RefCell<Ast>>) -> Result<RefCell<NPF>
|
||||
})
|
||||
.for_each(|b| npf.borrow_mut().content.push(b.clone()));
|
||||
*/
|
||||
Ok(())
|
||||
}
|
||||
NodeValue::Heading(h) => {
|
||||
let mut text = content_types::Text::from(utils::extract_text(node));
|
||||
@@ -157,9 +157,11 @@ pub fn to_tumblr_npf<'a>(ast: &'a Node<'a, RefCell<Ast>>) -> Result<RefCell<NPF>
|
||||
}
|
||||
|
||||
npf.borrow_mut().content.push(ContentType::Text(text));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
_ => (),
|
||||
});
|
||||
_ => Ok(()),
|
||||
})?;
|
||||
|
||||
Ok(npf)
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ fn main() {
|
||||
return;
|
||||
}
|
||||
|
||||
match &cli.command {
|
||||
let _: Result<()> = match &cli.command {
|
||||
Commands::Links {
|
||||
path_root,
|
||||
alias_prop,
|
||||
@@ -124,6 +124,7 @@ fn main() {
|
||||
}
|
||||
};
|
||||
}
|
||||
Ok(())
|
||||
}),
|
||||
Commands::Frontmatter { command } => utils::iter_nodes(&ast, &|node| {
|
||||
if let NodeValue::FrontMatter(ref mut f) = &mut node.data.borrow_mut().value {
|
||||
@@ -162,8 +163,9 @@ fn main() {
|
||||
Err(e) => panic!("{e:#?}"),
|
||||
};
|
||||
}
|
||||
Ok(())
|
||||
}),
|
||||
_ => (),
|
||||
_ => Ok(()),
|
||||
};
|
||||
|
||||
let _ = comrak::format_commonmark(&ast, &mdparser::utils::default_options(), &mut cli.output);
|
||||
|
||||
29
src/utils.rs
29
src/utils.rs
@@ -14,34 +14,25 @@ pub fn default_options() -> comrak::Options {
|
||||
opts
|
||||
}
|
||||
|
||||
pub fn iter_nodes<'a, F>(node: &'a comrak::nodes::AstNode<'a>, f: &F)
|
||||
pub fn iter_nodes<'a, F, E>(node: &'a comrak::nodes::AstNode<'a>, f: &F) -> Result<(), E>
|
||||
where
|
||||
F: Fn(&'a comrak::nodes::AstNode<'a>),
|
||||
F: Fn(&'a comrak::nodes::AstNode<'a>) -> Result<(), E>,
|
||||
{
|
||||
f(node);
|
||||
f(node)?;
|
||||
for c in node.children() {
|
||||
iter_nodes(c, f)
|
||||
iter_nodes(c, f)?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn iter_nodes_shallow<'a, F>(node: &'a comrak::nodes::AstNode<'a>, f: &F)
|
||||
pub fn iter_nodes_shallow<'a, F, E>(node: &'a comrak::nodes::AstNode<'a>, f: &F) -> Result<(), E>
|
||||
where
|
||||
F: Fn(&'a comrak::nodes::AstNode<'a>),
|
||||
F: Fn(&'a comrak::nodes::AstNode<'a>) -> Result<(), E>,
|
||||
{
|
||||
for c in node.children() {
|
||||
f(c)
|
||||
f(c)?
|
||||
}
|
||||
}
|
||||
|
||||
pub fn extract_text<'a>(node: &'a comrak::nodes::AstNode<'a>) -> String {
|
||||
let text = RefCell::new(String::new());
|
||||
iter_nodes(node, &|node| {
|
||||
if let NodeValue::Text(t) = &node.data.borrow().value {
|
||||
text.borrow_mut().push_str(&t);
|
||||
}
|
||||
});
|
||||
let r = text.borrow().to_string();
|
||||
r
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn iter_nodes_r<'a, F, T>(node: &'a comrak::nodes::AstNode<'a>, f: &F) -> Option<T>
|
||||
@@ -60,12 +51,14 @@ where
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn extract_text<'a>(node: &'a comrak::nodes::AstNode<'a>) -> String {
|
||||
let text = RefCell::new(String::new());
|
||||
let _ = iter_nodes(node, &|node| {
|
||||
if let NodeValue::Text(t) = &node.data.borrow().value {
|
||||
text.borrow_mut().push_str(&t);
|
||||
}
|
||||
Ok::<(), std::fmt::Error>(())
|
||||
});
|
||||
let r = text.borrow().to_string();
|
||||
r
|
||||
|
||||
Reference in New Issue
Block a user