From 8971588486b6ffa9269344b4bda71de86af9d908 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 17 May 2023 14:31:55 +0200 Subject: Externalize contributor fetching --- docs/src/contribs.rs | 25 +++++++------------------ docs/src/html.rs | 2 +- docs/src/lib.rs | 10 +++++++++- 3 files changed, 17 insertions(+), 20 deletions(-) (limited to 'docs/src') diff --git a/docs/src/contribs.rs b/docs/src/contribs.rs index 7e43249a..901b5f69 100644 --- a/docs/src/contribs.rs +++ b/docs/src/contribs.rs @@ -4,23 +4,15 @@ use std::fmt::Write; use serde::Deserialize; -use super::Html; +use super::{Html, Resolver}; /// Build HTML detailing the contributors between two tags. -pub fn contributors(from: &str, to: &str) -> Option { +pub fn contributors(resolver: &dyn Resolver, from: &str, to: &str) -> Option { let staff = ["laurmaedje", "reknih"]; - let url = format!("https://api.github.com/repos/typst/typst/compare/{from}...{to}"); - let response: Response = ureq::get(&url) - .set("X-GitHub-Api-Version", "2022-11-28") - .call() - .ok()? - .into_json() - .ok()?; - // Determine number of contributions per person. let mut contributors = HashMap::::new(); - for commit in response.commits { + for commit in resolver.commits(from, to) { contributors .entry(commit.author.login.clone()) .or_insert_with(|| Contributor { @@ -80,18 +72,15 @@ struct Contributor { contributions: usize, } +/// A commit on the `typst` repository. #[derive(Debug, Deserialize)] -struct Response { - commits: Vec, -} - -#[derive(Debug, Deserialize)] -struct Commit { +pub struct Commit { author: Author, } +/// A commit author. #[derive(Debug, Deserialize)] -struct Author { +pub struct Author { login: String, avatar_url: String, } diff --git a/docs/src/html.rs b/docs/src/html.rs index d3566b07..cd47d75b 100644 --- a/docs/src/html.rs +++ b/docs/src/html.rs @@ -121,7 +121,7 @@ impl<'a> Handler<'a> { md::Event::Html(html) if html.starts_with(" { let from = html_attr(html, "from").unwrap(); let to = html_attr(html, "to").unwrap(); - let Some(output) = contributors(from, to) else { return false }; + let Some(output) = contributors(self.resolver, from, to) else { return false }; *html = output.raw.into(); } diff --git a/docs/src/lib.rs b/docs/src/lib.rs index 3fcfecd1..fcf3610c 100644 --- a/docs/src/lib.rs +++ b/docs/src/lib.rs @@ -3,7 +3,7 @@ mod contribs; mod html; -pub use contribs::contributors; +pub use contribs::{contributors, Author, Commit}; pub use html::Html; use std::fmt::{self, Debug, Formatter}; @@ -72,6 +72,9 @@ pub trait Resolver { /// Produce HTML for an example. fn example(&self, source: Html, frames: &[Frame]) -> Html; + + /// Determine the commits between two tags. + fn commits(&self, from: &str, to: &str) -> Vec; } /// Details about a documentation page and its children. @@ -81,6 +84,7 @@ pub struct PageModel { pub title: String, pub description: String, pub part: Option<&'static str>, + pub outline: Vec, pub body: BodyModel, pub children: Vec, } @@ -784,5 +788,9 @@ mod tests { fn image(&self, _: &str, _: &[u8]) -> String { String::new() } + + fn commits(&self, _: &str, _: &str) -> Vec { + vec![] + } } } -- cgit v1.2.3