Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mattias-p committed Mar 15, 2018
1 parent 3a18552 commit c5559a9
Showing 1 changed file with 44 additions and 46 deletions.
90 changes: 44 additions & 46 deletions src/linky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,50 +79,6 @@ enum Format {
Markdown,
}

fn parse<'a, R: Read>(
mut reader: R,
content_type: &ContentType,
) -> result::Result<Document<'a>, LookupError> {
let format = match (content_type.type_(), content_type.subtype().as_str()) {
(mime::TEXT, "html") => Format::Html,
(mime::TEXT, "markdown") => Format::Markdown,
_ => {
return Ok(Document {
ids: HashSet::new(),
});
}
};

let charset_hint = content_type
.get_param(mime::CHARSET)
.map(|v| v.as_ref().to_string());
debug!("http charset hint: {:?}", &charset_hint);

let chars = read_chars(&mut reader, charset_hint)?;

let ids = match format {
Format::Markdown => {
let mut headers = Headers::new();
MdAnchorParser::from_buffer(&chars, &GithubId, &mut headers)
.map(|id| Cow::from(id))
.collect()
}
Format::Html => {
let mut result = HashSet::new();
for (_, tag) in htmlstream::tag_iter(&chars) {
for (_, attr) in htmlstream::attr_iter(&tag.attributes) {
if attr.name == "id" || (tag.name == "a" && attr.name == "name") {
result.insert(Cow::from(attr.value));
}
}
}
result
}
};

Ok(Document { ids: ids })
}

pub struct Document<'a> {
pub ids: HashSet<Cow<'a, str>>,
}
Expand All @@ -143,6 +99,48 @@ impl<'a> Document<'a> {
ids: [""].iter().chain(ids).cloned().map(Cow::from).collect(),
}
}

fn parse<R: Read>(
mut reader: R,
content_type: &ContentType,
) -> result::Result<Document<'a>, LookupError> {
let format = match (content_type.type_(), content_type.subtype().as_str()) {
(mime::TEXT, "html") => Format::Html,
(mime::TEXT, "markdown") => Format::Markdown,
_ => {
return Ok(Document::empty());
}
};

let charset_hint = content_type
.get_param(mime::CHARSET)
.map(|v| v.as_ref().to_string());
debug!("http charset hint: {:?}", &charset_hint);

let chars = read_chars(&mut reader, charset_hint)?;

let ids = match format {
Format::Markdown => {
let mut headers = Headers::new();
MdAnchorParser::from_buffer(&chars, &GithubId, &mut headers)
.map(|id| Cow::from(id))
.collect()
}
Format::Html => {
let mut result = HashSet::new();
for (_, tag) in htmlstream::tag_iter(&chars) {
for (_, attr) in htmlstream::attr_iter(&tag.attributes) {
if attr.name == "id" || (tag.name == "a" && attr.name == "name") {
result.insert(Cow::from(attr.value));
}
}
}
result
}
};

Ok(Document { ids: ids })
}
}

pub struct FragResolver<'a> {
Expand Down Expand Up @@ -195,7 +193,7 @@ impl LocalResolver for FilesystemLocalResolver {

let reader = File::open(&path)?;

parse(reader, &MARKDOWN_CONTENT_TYPE)
Document::parse(reader, &MARKDOWN_CONTENT_TYPE)
}
}

Expand All @@ -220,7 +218,7 @@ impl<'a> RemoteResolver for NetworkRemoteResolver<'a> {
.ok_or(ErrorKind::NoMime.into());
let content_type = content_type?;

parse(response, &content_type)
Document::parse(response, &content_type)
};

result
Expand Down

0 comments on commit c5559a9

Please sign in to comment.