// match images
('!', '[', ..) => {
- let re = Regex::new(r"!\[(?<caption>[\d\w (\\\[)]*)\]\[(?<path>[\d\w\.\/]*)\]").unwrap();
- match re.captures_at(line, 0) {
+ let re = Regex::new(r"^!\[(?<caption>[\d\w (\\\[)]*)\]\[(?<path>[\d\w\./]*)\]").unwrap();
+ match re.captures(line) {
Some(captures) => {
let caption = captures.name("caption").unwrap().as_str().to_string();
let path = captures.name("path").unwrap().as_str().to_string();
if &string[mat.start()-2..mat.start()-1] == "\\" {false}
else {true}
},
- None => false
+ None => {
+ false
+ }
} {
let new_reference = Reference {
id: capture.name("id").map_or(
let mut new_string = string.to_string();
let re = Regex::new(r"\[[\d\w]+\]").unwrap();
for mat in re.find_iter(&string).collect::<Vec<_>>().iter().rev() {
- if !(&string[mat.start()-1..mat.start()] == "\\") {
+ if !(&string[mat.start().saturating_sub(1)..mat.start()] == "\\") {
let inline_id = &string[mat.start()+1..mat.end()-1].trim();
for reference in &self.list {
if &reference.id == inline_id {
pub fn render_full(&self) -> String {
let mut names: Vec<String> = Vec::new();
for name in self.string.split(" and ") {
- if &name[0..1] == "{" {
+ if !name.is_empty() && &name[0..1] == "{" {
// the case for dealing with organisations
names.push(String::from(&name[1..name.len()-1]));
} else {
// the case for dealing with people with an indeterminate
// amount of names
- let last_name = name.split_whitespace().last().unwrap();
- let mut full_name = String::from(last_name) + ", ";
- name.split_whitespace()
- .filter(|n| n != &last_name)
- .for_each(|n| {
- let initial = format!("{}. ", &n[0..1]);
- full_name.push_str(&initial)
- });
- names.push(full_name);
+ match name.split_whitespace().last() {
+ Some(last_name) => {
+ let mut full_name = String::from(last_name) + ", ";
+ name.split_whitespace()
+ .filter(|n| n != &last_name)
+ .for_each(|n| {
+ let initial = format!("{}. ", &n[0..1]);
+ full_name.push_str(&initial)
+ });
+ names.push(full_name);
+ }
+ None => ()
+ }
}
}
let mut all_names = String::new();
pub fn render_inline(&self) -> String {
let mut names: Vec<&str> = Vec::new();
for name in self.string.split(" and ") {
- if &name[0..1] == "{" {
+ if !name.is_empty() && &name[0..1] == "{" {
// the case for dealing with organisations
names.push(&name[1..name.len()-1]);
} else {
- let last_name = name.split_whitespace().last().unwrap();
- names.push(last_name);
+ match name.split_whitespace().last() {
+ Some(last_name) => {names.push(last_name)}
+ None => ()
+ }
+
}
}
- let mut all_names = String::new();
names.sort_by(|a, b| a.cmp(b));
// et al. if more than 3
- if names.len() > 3 {
- names = names[0..4].to_vec();
- names.push("et al.");
+ match names.len() {
+ 0 => {String::new()}
+ 1 => {names[0].to_string()}
+ 2 => {format!("{} & {}", names[0], names[1])}
+ _ => {format!("{} et al.", names[0])}
}
-
- for name in names {all_names.push_str(&name)};
- all_names
}
}