Add group parsing.
parent
457d8329ee
commit
976cb8d85d
@ -0,0 +1,87 @@
|
||||
//! Parsing for `/~<group>`.
|
||||
|
||||
use {color_eyre::Result, scraper::Html};
|
||||
|
||||
use crate::{
|
||||
regexes::GROUP_SUBSCRIBERS_RE,
|
||||
selectors::{
|
||||
GROUP_DESCRIPTION, GROUP_NAME, GROUP_SUBSCRIBERS, GROUP_SUB_GROUP_LINKS,
|
||||
GROUP_WIKI_LINKS,
|
||||
},
|
||||
utilities::{
|
||||
extract_anchor_values, parse_regex_match, select_first_element_text,
|
||||
},
|
||||
};
|
||||
|
||||
/// A group's information.
|
||||
#[derive(Debug)]
|
||||
pub struct Group {
|
||||
/// The group description.
|
||||
pub description: Option<String>,
|
||||
|
||||
/// The group name, including leading tilde.
|
||||
pub name: String,
|
||||
|
||||
/// Names of sub-groups.
|
||||
pub sub_groups: Vec<String>,
|
||||
|
||||
/// The amount of subscribers.
|
||||
pub subscribers: i32,
|
||||
|
||||
/// Links to wiki pages.
|
||||
pub wiki_links: Vec<GroupWikiLink>,
|
||||
}
|
||||
|
||||
/// A group's wiki link.
|
||||
#[derive(Debug)]
|
||||
pub struct GroupWikiLink {
|
||||
/// The name of the wiki page.
|
||||
pub name: String,
|
||||
|
||||
/// The URL to the wiki page.
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
impl Group {
|
||||
/// Parses a [`Group`] from a [`scraper::Html`] tree.
|
||||
pub fn from_html(html: &Html) -> Result<Self> {
|
||||
let description =
|
||||
select_first_element_text(html.root_element(), &GROUP_DESCRIPTION);
|
||||
|
||||
let name =
|
||||
select_first_element_text(html.root_element(), &GROUP_NAME).unwrap();
|
||||
|
||||
let subscribers = parse_regex_match(
|
||||
GROUP_SUBSCRIBERS_RE
|
||||
.captures_iter(
|
||||
&select_first_element_text(html.root_element(), &GROUP_SUBSCRIBERS)
|
||||
.unwrap(),
|
||||
)
|
||||
.next()
|
||||
.unwrap()
|
||||
.name("count"),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let sub_groups = html
|
||||
.select(&GROUP_SUB_GROUP_LINKS)
|
||||
.map(|element| extract_anchor_values(element).0)
|
||||
.collect();
|
||||
|
||||
let wiki_links = html
|
||||
.select(&GROUP_WIKI_LINKS)
|
||||
.map(|element| {
|
||||
let (name, url) = extract_anchor_values(element);
|
||||
GroupWikiLink { name, url }
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(Self {
|
||||
description,
|
||||
name,
|
||||
sub_groups,
|
||||
subscribers,
|
||||
wiki_links,
|
||||
})
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
use std::fs::read_to_string;
|
||||
|
||||
use {insta::assert_debug_snapshot, scraper::Html, tildes_parser::Group};
|
||||
|
||||
#[test]
|
||||
fn test_group_parsing() {
|
||||
let html = read_to_string("tests/samples/group.html").unwrap();
|
||||
let html = Html::parse_document(&html);
|
||||
let group = Group::from_html(&html).unwrap();
|
||||
assert_debug_snapshot!(group);
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<title>Sample for group.rs</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<aside id="sidebar">
|
||||
<h3>~group</h3>
|
||||
<div class="group-short-description">Group description.</div>
|
||||
<div class="group-subscription">
|
||||
<span class="group-subscription-count">12345 subscribers</span>
|
||||
</div>
|
||||
|
||||
<ul class="nav">
|
||||
<li>Subgroups</li>
|
||||
|
||||
<ul class="nav">
|
||||
<li class="nav-item">
|
||||
<a href="/~example.sub" class="link-group">~example.sub</a>
|
||||
</li>
|
||||
</ul>
|
||||
</ul>
|
||||
|
||||
<ul class="nav">
|
||||
<li>Group wiki pages</li>
|
||||
|
||||
<ul class="nav">
|
||||
<li class="nav-item">
|
||||
<a href="https://example.org/~example/wiki/index" class="text-bold">
|
||||
index
|
||||
</a>
|
||||
</li>
|
||||
|
||||
<li class="nav-item">
|
||||
<a href="https://example.org/~example/wiki/example_page">
|
||||
Example Page
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</ul>
|
||||
</aside>
|
||||
</body>
|
||||
|
||||
</html>
|
@ -0,0 +1,24 @@
|
||||
---
|
||||
source: tests/group.rs
|
||||
expression: group
|
||||
---
|
||||
Group {
|
||||
description: Some(
|
||||
"Group description.",
|
||||
),
|
||||
name: "~group",
|
||||
sub_groups: [
|
||||
"~example.sub",
|
||||
],
|
||||
subscribers: 12345,
|
||||
wiki_links: [
|
||||
GroupWikiLink {
|
||||
name: "index",
|
||||
url: "https://example.org/~example/wiki/index",
|
||||
},
|
||||
GroupWikiLink {
|
||||
name: "Example Page",
|
||||
url: "https://example.org/~example/wiki/example_page",
|
||||
},
|
||||
],
|
||||
}
|
Loading…
Reference in New Issue