1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use super::html_link_extractor::HtmlLinkExtractor;
use super::markdown_link_extractor::MarkdownLinkExtractor;
use crate::markup::{MarkupFile, MarkupType};
use std::fmt;
use std::fs;
#[derive(PartialEq, Clone)]
pub struct MarkupLink {
pub source: String,
pub target: String,
pub line: usize,
pub column: usize,
}
impl fmt::Debug for MarkupLink {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} => {} (line {}, column {})",
self.source, self.target, self.line, self.column
)
}
}
#[must_use]
pub fn find_links(file: &MarkupFile) -> Vec<MarkupLink> {
let path = &file.path;
let link_extractor = link_extractor_factory(file.markup_type);
info!("Scan file at path '{}' for links.", path);
match fs::read_to_string(path) {
Ok(text) => {
let mut links = link_extractor.find_links(&text);
for l in &mut links {
l.source = path.to_string();
}
links
}
Err(e) => {
warn!(
"File '{}'. IO Error: \"{}\". Check your file encoding.",
path, e
);
vec![]
}
}
}
fn link_extractor_factory(markup_type: MarkupType) -> Box<dyn LinkExtractor> {
match markup_type {
MarkupType::Markdown => Box::new(MarkdownLinkExtractor()),
MarkupType::Html => Box::new(HtmlLinkExtractor()),
}
}
pub trait LinkExtractor {
fn find_links(&self, text: &str) -> Vec<MarkupLink>;
}