Skip to content

Commit 0919d7d

Browse files
committed
feat(templs): many
- inheritance diagram - web ext examples and fixes
1 parent 1e5480d commit 0919d7d

24 files changed

+485
-84
lines changed

Diff for: crates/rari-deps/src/external_json.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use std::fs::{self, File};
2+
use std::io::{BufWriter, Write};
3+
use std::path::{Path, PathBuf};
4+
5+
use chrono::{DateTime, Duration, Utc};
6+
use serde::{Deserialize, Serialize};
7+
8+
use crate::error::DepsError;
9+
10+
#[derive(Deserialize, Serialize, Default, Debug)]
11+
pub struct LastChecked {
12+
pub latest_last_check: Option<DateTime<Utc>>,
13+
}
14+
15+
pub fn get_json(name: &str, url: &str, out_path: &Path) -> Result<Option<PathBuf>, DepsError> {
16+
let package_path = out_path.join(name);
17+
let last_check_path = package_path.join("last_check.json");
18+
let now = Utc::now();
19+
let current = fs::read_to_string(last_check_path)
20+
.ok()
21+
.and_then(|current| serde_json::from_str::<LastChecked>(&current).ok())
22+
.unwrap_or_default();
23+
if current.latest_last_check.unwrap_or_default() < now - Duration::days(1) {
24+
if package_path.exists() {
25+
fs::remove_dir_all(&package_path)?;
26+
}
27+
fs::create_dir_all(&package_path)?;
28+
let buf = reqwest::blocking::get(url)?.bytes()?;
29+
30+
let out_file = package_path.join("data.json");
31+
let file = File::create(out_file).unwrap();
32+
let mut buffed = BufWriter::new(file);
33+
buffed.write_all(buf.as_ref())?;
34+
35+
fs::write(
36+
package_path.join("last_check.json"),
37+
serde_json::to_string_pretty(&LastChecked {
38+
latest_last_check: Some(now),
39+
})?,
40+
)?;
41+
}
42+
Ok(None)
43+
}

Diff for: crates/rari-deps/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
pub mod bcd;
22
pub mod error;
3+
pub mod external_json;
34
pub mod mdn_data;
45
pub mod npm;
6+
pub mod web_ext_examples;
57
pub mod web_features;
68
pub mod webref_css;

Diff for: crates/rari-deps/src/web_ext_examples.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::path::Path;
2+
3+
use crate::error::DepsError;
4+
use crate::external_json::get_json;
5+
6+
pub fn update_web_ext_examples(base_path: &Path) -> Result<(), DepsError> {
7+
get_json(
8+
"web_ext_examples",
9+
"https://raw.githubusercontent.com/mdn/webextensions-examples/main/examples.json",
10+
base_path,
11+
)?;
12+
Ok(())
13+
}

Diff for: crates/rari-doc/src/helpers/api_inheritance.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use super::json_data::json_data_interface;
2+
3+
pub fn inheritance(main_if: &str) -> Vec<&str> {
4+
let web_api_data = json_data_interface();
5+
let mut inherited = vec![];
6+
7+
let mut interface = main_if;
8+
while let Some(inherited_data) = web_api_data
9+
.get(interface)
10+
.map(|data| data.inh.as_str())
11+
.filter(|ihn| !ihn.is_empty())
12+
{
13+
inherited.push(inherited_data);
14+
interface = inherited_data;
15+
}
16+
inherited
17+
}

Diff for: crates/rari-doc/src/helpers/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
pub mod api_inheritance;
12
pub mod css_info;
23
pub mod json_data;
34
pub mod subpages;
45
pub mod titles;
6+
pub mod web_ext_examples;

Diff for: crates/rari-doc/src/helpers/web_ext_examples.rs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use std::collections::HashMap;
2+
use std::fs::read_to_string;
3+
4+
use once_cell::sync::Lazy;
5+
use rari_types::globals::data_dir;
6+
use serde::Deserialize;
7+
use tracing::warn;
8+
9+
use crate::error::DocError;
10+
11+
#[derive(Deserialize, Debug, Clone)]
12+
pub struct WebExtExample {
13+
pub description: String,
14+
pub javascript_apis: Vec<String>,
15+
pub name: String,
16+
}
17+
18+
pub struct WebExtExamplesData {
19+
pub by_module: HashMap<&'static str, Vec<&'static WebExtExample>>,
20+
pub by_module_and_api: HashMap<&'static str, Vec<&'static WebExtExample>>,
21+
}
22+
23+
pub static WEB_EXT_EXAMPLES_JSON: Lazy<Vec<WebExtExample>> = Lazy::new(|| {
24+
match read_to_string(data_dir().join("web_ext_examples/data.json"))
25+
.map_err(DocError::from)
26+
.and_then(|s| serde_json::from_str(&s).map_err(DocError::from))
27+
{
28+
Ok(data) => data,
29+
Err(e) => {
30+
warn!("Error loading mdn/data: {e}");
31+
Default::default()
32+
}
33+
}
34+
});
35+
36+
pub fn web_ext_examples_json() -> &'static [WebExtExample] {
37+
&WEB_EXT_EXAMPLES_JSON
38+
}
39+
40+
pub static WEB_EXT_EXAMPLES_DATA: Lazy<WebExtExamplesData> = Lazy::new(|| {
41+
let mut by_module = HashMap::new();
42+
for example in web_ext_examples_json() {
43+
for js_api in &example.javascript_apis {
44+
let js_api = &js_api[..js_api.find('.').unwrap_or(js_api.len())];
45+
by_module
46+
.entry(js_api)
47+
.and_modify(|e: &mut Vec<_>| e.push(example))
48+
.or_insert(vec![example]);
49+
}
50+
}
51+
let mut by_module_and_api = HashMap::new();
52+
for example in web_ext_examples_json() {
53+
for js_api in &example.javascript_apis {
54+
by_module_and_api
55+
.entry(js_api.as_str())
56+
.and_modify(|e: &mut Vec<_>| e.push(example))
57+
.or_insert(vec![example]);
58+
}
59+
}
60+
WebExtExamplesData {
61+
by_module,
62+
by_module_and_api,
63+
}
64+
});

Diff for: crates/rari-doc/src/html/modifier.rs

+29-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use ego_tree::NodeId;
22
use html5ever::{namespace_url, ns, QualName};
33
use rari_md::anchor::anchorize;
4-
use scraper::{Html, Node, Selector};
4+
use scraper::{ElementRef, Html, Node, Selector};
55

66
use crate::error::DocError;
77

@@ -20,25 +20,37 @@ pub fn add_attribute(html: &mut Html, node_id: NodeId, key: &str, value: &str) {
2020
}
2121
}
2222

23+
pub fn remove_attribute(html: &mut Html, node_id: NodeId, key: &str) {
24+
if let Some(mut details) = html.tree.get_mut(node_id) {
25+
if let Node::Element(ref mut el) = details.value() {
26+
el.attrs.swap_remove(&QualName {
27+
prefix: None,
28+
ns: ns!(),
29+
local: key.into(),
30+
});
31+
}
32+
}
33+
}
34+
2335
pub fn add_missing_ids(html: &mut Html) -> Result<(), DocError> {
24-
let a_selector = Selector::parse("*[id=---update-id]").unwrap();
25-
let subs = html
26-
.select(&a_selector)
27-
.map(|el| {
28-
let text = if let Some(text) = el
29-
.first_child()
30-
.and_then(|child| child.value().as_text().map(|t| t.to_string()))
31-
{
32-
text
33-
} else {
34-
el.text().collect::<String>()
35-
};
36-
let id = anchorize(&text);
37-
(el.id(), id)
38-
})
39-
.collect::<Vec<_>>();
36+
let selector = Selector::parse("*[data-update-id], h2:not([id]), h3:not([id])").unwrap();
37+
let subs =
38+
html.select(&selector)
39+
.map(|el| {
40+
let text = if let Some(text) = el.first_child().and_then(|child| {
41+
ElementRef::wrap(child).map(|el| el.text().collect::<String>())
42+
}) {
43+
text
44+
} else {
45+
el.text().collect::<String>()
46+
};
47+
let id = anchorize(&text);
48+
(el.id(), id)
49+
})
50+
.collect::<Vec<_>>();
4051
for (el_id, id) in subs {
4152
add_attribute(html, el_id, "id", &id);
53+
remove_attribute(html, el_id, "data-update-id");
4254
}
4355
Ok(())
4456
}

Diff for: crates/rari-doc/src/sidebars/apiref.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use tracing::error;
66
use crate::docs::doc::Doc;
77
use crate::docs::page::{Page, PageLike};
88
use crate::error::DocError;
9-
use crate::helpers::json_data::{json_data_group, json_data_interface};
9+
use crate::helpers::api_inheritance::inheritance;
10+
use crate::helpers::json_data::json_data_group;
1011
use crate::helpers::subpages::{get_sub_pages, SubPagesSorter};
1112
use crate::helpers::titles::api_page_title;
1213
use crate::html::sidebar::{
@@ -37,7 +38,6 @@ pub fn sidebar(slug: &str, group: Option<&str>, locale: Locale) -> Result<MetaSi
3738
return Err(DocError::InvalidSlugForX(slug.to_string()));
3839
}
3940

40-
let web_api_data = json_data_interface();
4141
let web_api_groups = group.and_then(|group| json_data_group().get(group));
4242

4343
let main_if_pages = get_sub_pages(
@@ -82,17 +82,7 @@ pub fn sidebar(slug: &str, group: Option<&str>, locale: Locale) -> Result<MetaSi
8282
v.push(page);
8383
}
8484

85-
let mut inherited = vec![];
86-
87-
let mut interface = main_if;
88-
while let Some(inherited_data) = web_api_data
89-
.get(interface)
90-
.map(|data| data.inh.as_str())
91-
.filter(|ihn| !ihn.is_empty())
92-
{
93-
inherited.push(inherited_data);
94-
interface = inherited_data;
95-
}
85+
let inherited = inheritance(main_if);
9686

9787
let mut entries = vec![];
9888

Diff for: crates/rari-doc/src/templ/templs/badges.rs

-14
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ pub fn deprecated() -> Result<String, DocError> {
2424
Ok(out)
2525
}
2626

27-
#[rari_f]
28-
pub fn readonly() -> Result<String, DocError> {
29-
let mut out = String::new();
30-
write_readonly(&mut out, env.locale)?;
31-
Ok(out)
32-
}
3327
#[rari_f]
3428
pub fn optional() -> Result<String, DocError> {
3529
let str = rari_l10n::l10n_json_data("Template", "optional", env.locale)?;
@@ -62,14 +56,6 @@ pub fn write_deprecated(out: &mut impl std::fmt::Write, locale: Locale) -> Resul
6256
Ok(write_badge(out, title, abbreviation, "deprecated")?)
6357
}
6458

65-
pub fn write_readonly(out: &mut impl std::fmt::Write, locale: Locale) -> Result<(), DocError> {
66-
let title = rari_l10n::l10n_json_data("Template", "readonly_badge_title", locale)?;
67-
let abbreviation =
68-
rari_l10n::l10n_json_data("Template", "readonly_badge_abbreviation", locale)?;
69-
70-
Ok(write_badge(out, title, abbreviation, "readonly")?)
71-
}
72-
7359
pub fn write_badge(
7460
out: &mut impl std::fmt::Write,
7561
title: &str,

Diff for: crates/rari-doc/src/templ/templs/banners.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ pub fn deprecated_header(version: Option<AnyArg>) -> Result<String, DocError> {
1313
let copy = rari_l10n::l10n_json_data("Template", "deprecated_header_copy", env.locale)?;
1414

1515
Ok([
16-
r#"<div class="notecard deprecated"><strong>"#,
16+
r#"<div class="notecard deprecated"><p><strong>"#,
1717
title,
1818
":</strong> ",
1919
copy,
20-
"</div>",
20+
"</p></div>",
2121
]
2222
.join(""))
2323
}

0 commit comments

Comments
 (0)