fn formatted_title_and_file_of_record(
    pathbuf_of_record: &PathBuf,
    base_path: &PathBuf,
    format: &String,
) -> Result<String, Error> {
    // Define Regexes
    let re_title_md = Regex::new(r"^# (\d+\.?\s+.*)\s?$").unwrap();
    let re_title_rst = Regex::new(r"^\s*[\*#]\s*+$").unwrap();

    // Set flags
    let mut past_delimiter: bool = false; // Used for RST only

    // Calculate the relative path from the pathbuf to the base path
    let relative_path_to = diff_paths(pathbuf_of_record, base_path)
        .unwrap()
        .display()
        .to_string();

    // Build a default "title" to return
    let mut link_text: String = String::from(&relative_path_to);

    // Replace it with a formatted version, if we are working with a known file format
    if format == "md" {
        link_text = String::from("[");
        link_text.push_str(&relative_path_to);
        link_text.push_str("](");
        link_text.push_str(&relative_path_to);
        link_text.push_str(")");
    } else if format == "rst" {
        link_text = String::from(":doc:`");
        link_text.push_str(&relative_path_to);
        link_text.push_str(" <");
        link_text.push_str(&relative_path_to);
        link_text.push_str(">`");
    }

    // Then loop through the pathbuf to replace the file format
    if let Ok(lines) = get_lines_from_a_file(pathbuf_of_record) {
        for line in lines {
            if let Ok(line) = line {
                if format == "md" {
                    if re_title_md.is_match(&line) {
                        link_text = re_title_md.replace(&line, "[$1](").to_string();
                        link_text.push_str(&relative_path_to);
                        link_text.push_str(")");
                        return Ok(link_text);
                    }
                } else if format == "rst" {
                    if past_delimiter {
                        link_text = String::from(":doc:`");
                        link_text.push_str(&line);
                        link_text.push_str(" <");
                        link_text.push_str(&relative_path_to);
                        link_text.push_str(">`");
                        return Ok(link_text);
                    } else if re_title_rst.is_match(&line) {
                        past_delimiter = true;
                    }
                }
            }
        }
    }

    // If we couldn't find the title, return at least a formatted link string
    return Ok(link_text);
}
// This function turns a PathBuf object into a markdown link. If the Object is readable as a Markdown link or a Restructured Text
// file, with the title as the first heading, then it will encapsulate the title in the relevant formatted link.
This function turns a PathBuf object into a markdown link. If the Object is readable as a Markdown link or a Restructured Text
file, with the title as the first heading, then it will encapsulate the title in the relevant formatted link.
This function turns a PathBuf object into a markdown link . If the Object is readable as a Markdown link or a Restructured Text file with the title as the first heading then it will encapsulate the title in the relevant formatted link .
fn formatted_title_and_file_of_record(
    pathbuf_of_record: &PathBuf,
    base_path: &PathBuf,
    format: &String,
) -> Result<String, Error> {
    // Define Regexes
    let re_title_md = Regex::new(r"^# (\d+\.?\s+.*)\s?$").unwrap();
    let re_title_rst = Regex::new(r"^\s*[\*#]\s*+$").unwrap();
    // Set flags
    let mut past_delimiter: bool = false; // Used for RST only
    // Calculate the relative path from the pathbuf to the base path
    let relative_path_to = diff_paths(pathbuf_of_record, base_path)
        .unwrap()
        .display()
        .to_string();
    // Build a default "title" to return
    let mut link_text: String = String::from(&relative_path_to);
    // Replace it with a formatted version, if we are working with a known file format
    if format == "md" {
        link_text = String::from("[");
        link_text.push_str(&relative_path_to);
        link_text.push_str("](");
        link_text.push_str(&relative_path_to);
        link_text.push_str(")");
    } else if format == "rst" {
        link_text = String::from(":doc:`");
        link_text.push_str(&relative_path_to);
        link_text.push_str(" <");
        link_text.push_str(&relative_path_to);
        link_text.push_str(">`");
    }
    // Then loop through the pathbuf to replace the file format
    if let Ok(lines) = get_lines_from_a_file(pathbuf_of_record) {
        for line in lines {
            if let Ok(line) = line {
                if format == "md" {
                    if re_title_md.is_match(&line) {
                        link_text = re_title_md.replace(&line, "[$1](").to_string();
                        link_text.push_str(&relative_path_to);
                        link_text.push_str(")");
                        return Ok(link_text);
                    }
                } else if format == "rst" {
                    if past_delimiter {
                        link_text = String::from(":doc:`");
                        link_text.push_str(&line);
                        link_text.push_str(" <");
                        link_text.push_str(&relative_path_to);
                        link_text.push_str(">`");
                        return Ok(link_text);
                    } else if re_title_rst.is_match(&line) {
                        past_delimiter = true;
                    }
                }
            }
        }
    }
    // If we couldn't find the title, return at least a formatted link string
    return Ok(link_text);
}
fn formatted_title_and_file_of_record ( pathbuf_of_record : & PathBuf , base_path : & PathBuf , format : & String , ) -> Result < String , Error > { // Define Regexes let re_title_md = Regex :: new ( r"^# (\d+\.?\s+.*)\s?$" ) . unwrap ( ) ; let re_title_rst = Regex :: new ( r"^\s*[\*#]\s*+$" ) . unwrap ( ) ; // Set flags let mut past_delimiter : bool = false ; // Used for RST only // Calculate the relative path from the pathbuf to the base path let relative_path_to = diff_paths ( pathbuf_of_record , base_path ) . unwrap ( ) . display ( ) . to_string ( ) ; // Build a default "title" to return let mut link_text : String = String :: from ( & relative_path_to ) ; // Replace it with a formatted version, if we are working with a known file format if format == " " { link_text = String :: from ( " " ) ; link_text . push_str ( & relative_path_to ) ; link_text . push_str ( " " ) ; link_text . push_str ( & relative_path_to ) ; link_text . push_str ( " " ) ; } else if format == " " { link_text = String :: from ( " " ) ; link_text . push_str ( & relative_path_to ) ; link_text . push_str ( " " ) ; link_text . push_str ( & relative_path_to ) ; link_text . push_str ( " " ) ; } // Then loop through the pathbuf to replace the file format if let Ok ( lines ) = get_lines_from_a_file ( pathbuf_of_record ) { for line in lines { if let Ok ( line ) = line { if format == " " { if re_title_md . is_match ( & line ) { link_text = re_title_md . replace ( & line , " " ) . to_string ( ) ; link_text . push_str ( & relative_path_to ) ; link_text . push_str ( " " ) ; return Ok ( link_text ) ; } } else if format == " " { if past_delimiter { link_text = String :: from ( " " ) ; link_text . push_str ( & line ) ; link_text . push_str ( " " ) ; link_text . push_str ( & relative_path_to ) ; link_text . push_str ( " " ) ; return Ok ( link_text ) ; } else if re_title_rst . is_match ( & line ) { past_delimiter = true ; } } } } } // If we couldn't find the title, return at least a formatted link string return Ok ( link_text ) ; }
This function turns a PathBuf object into a markdown link.
This function turns a PathBuf object into a markdown link .
pathbuf_of_record
&PathBuf
base_path
&PathBuf
format
&String
pathbuf_of_record
&PathBuf
base_path
&PathBuf
format
&String