Skip to content

Commit

Permalink
feat(generic_pages): add support for generic pages
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Sep 9, 2024
1 parent a1ccbb1 commit 971d92c
Show file tree
Hide file tree
Showing 18 changed files with 359 additions and 17 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ jobs:
include:
- target: x86_64-unknown-linux-musl
os: ubuntu-latest
build-tool: cargo-zigbuild
- target: aarch64-unknown-linux-musl
os: ubuntu-latest
build-tool: cargo-zigbuild
- target: aarch64-apple-darwin
os: macos-latest
- target: x86_64-pc-windows-msvc
Expand Down
17 changes: 14 additions & 3 deletions crates/rari-doc/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use rari_types::globals::build_out_root;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use tracing::{error, span, Level};

use crate::cached_readers::{blog_files, curriculum_files};
use crate::cached_readers::{blog_files, curriculum_files, generic_pages_files};
use crate::error::DocError;
use crate::pages::build::{
build_blog_post, build_contributor_spotlight, build_curriculum, build_doc, build_spa,
copy_additional_files,
build_blog_post, build_contributor_spotlight, build_curriculum, build_doc, build_generic_page,
build_spa, copy_additional_files,
};
use crate::pages::page::{Page, PageLike};
use crate::pages::types::spa::SPA;
Expand All @@ -28,6 +28,7 @@ pub fn build_single_page(page: &Page) {
Page::SPA(spa) => build_spa(spa),
Page::Curriculum(curriculum) => build_curriculum(curriculum),
Page::ContributorSpotlight(cs) => build_contributor_spotlight(cs),
Page::GenericPage(generic) => build_generic_page(generic),
};
match built_page {
Ok(built_page) => {
Expand Down Expand Up @@ -83,3 +84,13 @@ pub fn build_blog_pages() -> Result<Vec<Cow<'static, str>>, DocError> {
})
.collect())
}

pub fn build_generic_pages() -> Result<Vec<Cow<'static, str>>, DocError> {
Ok(generic_pages_files()
.values()
.map(|page| {
build_single_page(page);
Cow::Owned(page.url().to_string())
})
.collect())
}
43 changes: 43 additions & 0 deletions crates/rari-doc/src/cached_readers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::{Arc, LazyLock, OnceLock, RwLock};

use rari_types::globals::{
blog_root, cache_content, content_root, content_translated_root, curriculum_root,
generic_pages_root,
};
use rari_types::locale::Locale;
use rari_utils::io::read_to_string;
Expand All @@ -16,6 +17,7 @@ use crate::pages::page::{Page, PageLike};
use crate::pages::types::blog::{Author, AuthorFrontmatter, BlogPost, BlogPostBuildMeta};
use crate::pages::types::curriculum::{CurriculumIndexEntry, CurriculumPage};
use crate::pages::types::doc::Doc;
use crate::pages::types::generic::GenericPage;
use crate::reader::read_docs_parallel;
use crate::sidebars::jsref;
use crate::translations::init_translations_from_static_docs;
Expand All @@ -29,6 +31,7 @@ type SidebarFilesCache = Arc<RwLock<HashMap<(String, Locale), Arc<MetaSidebar>>>
pub static CACHED_SIDEBAR_FILES: LazyLock<SidebarFilesCache> =
LazyLock::new(|| Arc::new(RwLock::new(HashMap::new())));
pub static CACHED_CURRICULUM: OnceLock<CurriculumFiles> = OnceLock::new();
pub static GENERIC_PAGES_FILES: OnceLock<GenericPagesFiles> = OnceLock::new();

#[derive(Debug, Default, Clone)]
pub struct BlogFiles {
Expand Down Expand Up @@ -97,6 +100,30 @@ pub fn gather_blog_posts() -> Result<HashMap<String, Page>, DocError> {
}
}

pub fn gather_generic_pages() -> Result<HashMap<String, Page>, DocError> {
if let Some(root) = generic_pages_root() {
Ok(read_docs_parallel::<GenericPage>(&[root], Some("*.md"))?
.into_iter()
.filter_map(|page| {
if let Page::GenericPage(generic) = page {
Some(generic)
} else {
None
}
})
.flat_map(|generic| {
Locale::all()
.iter()
.map(|locale| Page::GenericPage(Arc::new(generic.as_locale(*locale))))
.collect::<Vec<_>>()
})
.map(|page| (page.url().to_ascii_lowercase(), page))
.collect())
} else {
Err(DocError::NoGenericPagesRoot)
}
}

pub fn gather_curriculum() -> Result<CurriculumFiles, DocError> {
if let Some(curriculum_root) = curriculum_root() {
let curriculum_root = curriculum_root.join("curriculum");
Expand Down Expand Up @@ -275,3 +302,19 @@ pub fn read_and_cache_doc_pages() -> Result<Vec<Page>, DocError> {
init_translations_from_static_docs();
Ok(docs)
}

pub type GenericPagesFiles = HashMap<String, Page>;

pub fn generic_pages_files() -> Cow<'static, GenericPagesFiles> {
fn gather() -> GenericPagesFiles {
gather_generic_pages().unwrap_or_else(|e| {
error!("{e}");
Default::default()
})
}
if cache_content() {
Cow::Borrowed(GENERIC_PAGES_FILES.get_or_init(gather))
} else {
Cow::Owned(gather())
}
}
2 changes: 2 additions & 0 deletions crates/rari-doc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub enum DocError {
NoSuchPrefix(#[from] StripPrefixError),
#[error("No curricm root set")]
NoCurriculumRoot,
#[error("No generic pages roots set")]
NoGenericPagesRoot,
#[error("No H1 found")]
NoH1,
#[error(transparent)]
Expand Down
19 changes: 17 additions & 2 deletions crates/rari-doc/src/pages/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ use std::borrow::Cow;
use std::fs;
use std::path::Path;

use concat_in_place::strcat;
use rari_types::fm_types::PageType;
use rari_types::globals::{base_url, content_branch, git_history, popularities};
use rari_types::locale::Locale;
use scraper::Html;

use super::json::{
BuiltDocy, Compat, ContributorSpotlightHyData, HyData, JsonBlogPost, JsonBlogPostDoc,
JsonCurriculum, JsonDoADoc, JsonDoc, Prose, Section, Source, SpecificationSection, TocEntry,
Translation,
JsonCurriculum, JsonDoADoc, JsonDoc, JsonGenericHyData, JsonGenericPage, Prose, Section,
Source, SpecificationSection, TocEntry, Translation,
};
use super::page::PageLike;
use super::parents::parents;
use super::title::{page_title, transform_title};
use super::types::contributors::ContributorSpotlight;
use super::types::generic::GenericPage;
use crate::baseline::get_baseline;
use crate::error::DocError;
use crate::html::bubble_up::bubble_up_curriculum_page;
Expand Down Expand Up @@ -319,6 +321,19 @@ pub fn build_blog_post(post: &BlogPost) -> Result<BuiltDocy, DocError> {
})))
}

pub fn build_generic_page(page: &GenericPage) -> Result<BuiltDocy, DocError> {
let PageContent { body, toc, .. } = build_content(page)?;
Ok(BuiltDocy::GenericPage(Box::new(JsonGenericPage {
hy_data: JsonGenericHyData {
sections: body,
title: page.meta.title.clone(),
toc,
},
page_title: strcat!(page.meta.title.as_str() " | " page.meta.title_suffix.as_str()),
url: page.meta.url.clone(),
})))
}

pub fn build_spa(spa: &SPA) -> Result<BuiltDocy, DocError> {
spa.as_built_doc()
}
Expand Down
17 changes: 17 additions & 0 deletions crates/rari-doc/src/pages/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ pub enum BuiltDocy {
BlogPost(Box<JsonBlogPost>),
ContributorSpotlight(Box<JsonContributorSpotlight>),
BasicSPA(Box<JsonBasicSPA>),
GenericPage(Box<JsonGenericPage>),
}

#[derive(Deserialize, Serialize, Clone, Debug, Default)]
Expand Down Expand Up @@ -289,3 +290,19 @@ pub struct JsonBasicSPA {
pub no_indexing: bool,
pub url: String,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct JsonGenericHyData {
pub sections: Vec<Section>,
pub title: String,
pub toc: Vec<TocEntry>,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct JsonGenericPage {
pub hy_data: JsonGenericHyData,
pub page_title: String,
pub url: String,
}
7 changes: 7 additions & 0 deletions crates/rari-doc/src/pages/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rari_types::locale::Locale;
use rari_types::RariEnv;

use super::types::contributors::contributor_spotlight_from_url;
use super::types::generic::GenericPage;
use crate::cached_readers::{blog_from_url, curriculum_from_url};
use crate::error::DocError;
use crate::pages::types::blog::BlogPost;
Expand All @@ -26,6 +27,7 @@ pub enum Page {
SPA(Arc<SPA>),
Curriculum(Arc<CurriculumPage>),
ContributorSpotlight(Arc<ContributorSpotlight>),
GenericPage(Arc<GenericPage>),
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
Expand All @@ -35,6 +37,7 @@ pub enum PageCategory {
SPA,
Curriculum,
ContributorSpotlight,
GenericPage,
}

impl Page {
Expand Down Expand Up @@ -95,6 +98,7 @@ impl PageReader for Page {
PageCategory::SPA => SPA::read(path, locale),
PageCategory::Curriculum => CurriculumPage::read(path, locale),
PageCategory::ContributorSpotlight => ContributorSpotlight::read(path, locale),
PageCategory::GenericPage => GenericPage::read(path, locale),
}
}
}
Expand Down Expand Up @@ -142,6 +146,9 @@ pub fn url_path_to_page_with_other_locale_and_fallback(
url_path.to_string(),
PageCategory::ContributorSpotlight,
)),
PageCategory::GenericPage => GenericPage::from_slug(slug, locale).ok_or(
DocError::PageNotFound(url_path.to_string(), PageCategory::GenericPage),
),
}
}

Expand Down
Loading

0 comments on commit 971d92c

Please sign in to comment.