refactor: procedure that convert_pages() into a func (#934)

Extract a procedure that calls convert_pages() to process HTML files
into a function. It is used in both install/store.rs and
install/local_extension.rs, doing this avoids code duplication.
This commit is contained in:
SteveLauC
2025-10-20 18:23:53 +08:00
committed by GitHub
parent 8e49455acf
commit 5c92b5acab
4 changed files with 69 additions and 105 deletions

View File

@@ -39,6 +39,8 @@ refactor(calculator): skip evaluation if expr is in form "num => num" #929
chore: use a custom log directory #930 chore: use a custom log directory #930
chore: bump tauri_nspanel to v2.1 #933 chore: bump tauri_nspanel to v2.1 #933
refactor: show_coco/hide_coco now use NSPanel's function on macOS #933 refactor: show_coco/hide_coco now use NSPanel's function on macOS #933
refactor: procedure that convert_pages() into a func #934
## 0.8.0 (2025-09-28) ## 0.8.0 (2025-09-28)

View File

@@ -1,6 +1,7 @@
use crate::extension::PLUGIN_JSON_FILE_NAME;
use crate::extension::third_party::check::general_check; use crate::extension::third_party::check::general_check;
use crate::extension::third_party::install::{ use crate::extension::third_party::install::{
convert_page, filter_out_incompatible_sub_extensions, is_extension_installed, filter_out_incompatible_sub_extensions, is_extension_installed, view_extension_convert_pages,
}; };
use crate::extension::third_party::{ use crate::extension::third_party::{
THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE, get_third_party_extension_directory, THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE, get_third_party_extension_directory,
@@ -8,7 +9,6 @@ use crate::extension::third_party::{
use crate::extension::{ use crate::extension::{
Extension, canonicalize_relative_icon_path, canonicalize_relative_page_path, Extension, canonicalize_relative_icon_path, canonicalize_relative_page_path,
}; };
use crate::extension::{ExtensionType, PLUGIN_JSON_FILE_NAME};
use crate::util::platform::Platform; use crate::util::platform::Platform;
use serde_json::Value as Json; use serde_json::Value as Json;
use std::path::Path; use std::path::Path;
@@ -227,55 +227,7 @@ pub(crate) async fn install_local_extension(
* *
* HTTP links will be skipped. * HTTP links will be skipped.
*/ */
let pages: Vec<&str> = { view_extension_convert_pages(&extension, &dest_dir).await?;
if extension.r#type == ExtensionType::View {
let page = extension
.page
.as_ref()
.expect("View extension should set its page field");
vec![page.as_str()]
} else if extension.r#type.contains_sub_items()
&& let Some(ref views) = extension.views
{
let mut pages = Vec::with_capacity(views.len());
for view in views.iter() {
let page = view
.page
.as_ref()
.expect("View extension should set its page field");
pages.push(page.as_str());
}
pages
} else {
// No pages in this extension
Vec::new()
}
};
fn canonicalize_page_path(page_path: &Path, extension_root: &Path) -> PathBuf {
if page_path.is_relative() {
// It is relative to the extension root directory
extension_root.join(page_path)
} else {
page_path.into()
}
}
for page in pages {
/*
* Skip HTTP links
*/
if let Ok(url) = url::Url::parse(page)
&& ["http", "https"].contains(&url.scheme())
{
continue;
}
let path = canonicalize_page_path(Path::new(page), &dest_dir);
convert_page(&path).await?;
}
// Canonicalize relative icon and page paths // Canonicalize relative icon and page paths
canonicalize_relative_icon_path(&dest_dir, &mut extension)?; canonicalize_relative_icon_path(&dest_dir, &mut extension)?;

View File

@@ -42,8 +42,10 @@ pub(crate) mod local_extension;
pub(crate) mod store; pub(crate) mod store;
use crate::extension::Extension; use crate::extension::Extension;
use crate::extension::ExtensionType;
use crate::util::platform::Platform; use crate::util::platform::Platform;
use std::path::Path; use std::path::Path;
use std::path::PathBuf;
use super::THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE; use super::THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE;
@@ -228,6 +230,63 @@ fn _convert_page(page_content: &str, absolute_page_path: &Path) -> Result<String
Ok(modified_html) Ok(modified_html)
} }
async fn view_extension_convert_pages(
extension: &Extension,
extension_directory: &Path,
) -> Result<(), String> {
let pages: Vec<&str> = {
if extension.r#type == ExtensionType::View {
let page = extension
.page
.as_ref()
.expect("View extension should set its page field");
vec![page.as_str()]
} else if extension.r#type.contains_sub_items()
&& let Some(ref views) = extension.views
{
let mut pages = Vec::with_capacity(views.len());
for view in views.iter() {
let page = view
.page
.as_ref()
.expect("View extension should set its page field");
pages.push(page.as_str());
}
pages
} else {
// No pages in this extension
Vec::new()
}
};
fn canonicalize_page_path(page_path: &Path, extension_root: &Path) -> PathBuf {
if page_path.is_relative() {
// It is relative to the extension root directory
extension_root.join(page_path)
} else {
page_path.into()
}
}
for page in pages {
/*
* Skip HTTP links
*/
if let Ok(url) = url::Url::parse(page)
&& ["http", "https"].contains(&url.scheme())
{
continue;
}
let path = canonicalize_page_path(Path::new(page), &extension_directory);
convert_page(&path).await?;
}
Ok(())
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@@ -10,15 +10,14 @@ use crate::common::search::QuerySource;
use crate::common::search::SearchQuery; use crate::common::search::SearchQuery;
use crate::common::traits::SearchSource; use crate::common::traits::SearchSource;
use crate::extension::Extension; use crate::extension::Extension;
use crate::extension::ExtensionType;
use crate::extension::PLUGIN_JSON_FILE_NAME; use crate::extension::PLUGIN_JSON_FILE_NAME;
use crate::extension::THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE; use crate::extension::THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE;
use crate::extension::canonicalize_relative_icon_path; use crate::extension::canonicalize_relative_icon_path;
use crate::extension::canonicalize_relative_page_path; use crate::extension::canonicalize_relative_page_path;
use crate::extension::third_party::check::general_check; use crate::extension::third_party::check::general_check;
use crate::extension::third_party::get_third_party_extension_directory; use crate::extension::third_party::get_third_party_extension_directory;
use crate::extension::third_party::install::convert_page;
use crate::extension::third_party::install::filter_out_incompatible_sub_extensions; use crate::extension::third_party::install::filter_out_incompatible_sub_extensions;
use crate::extension::third_party::install::view_extension_convert_pages;
use crate::server::http_client::HttpClient; use crate::server::http_client::HttpClient;
use crate::util::platform::Platform; use crate::util::platform::Platform;
use async_trait::async_trait; use async_trait::async_trait;
@@ -26,8 +25,6 @@ use reqwest::StatusCode;
use serde_json::Map as JsonObject; use serde_json::Map as JsonObject;
use serde_json::Value as Json; use serde_json::Value as Json;
use std::io::Read; use std::io::Read;
use std::path::Path;
use std::path::PathBuf;
use tauri::AppHandle; use tauri::AppHandle;
const DATA_SOURCE_ID: &str = "Extension Store"; const DATA_SOURCE_ID: &str = "Extension Store";
@@ -401,57 +398,11 @@ pub(crate) async fn install_extension_from_store(
/* /*
* Call convert_page() to update the page files. This has to be done after * Call convert_page() to update the page files. This has to be done after
* writing the extension files * writing the extension files because we will edit them.
*
* HTTP links will be skipped.
*/ */
let pages: Vec<&str> = { view_extension_convert_pages(&extension, &extension_directory).await?;
if extension.r#type == ExtensionType::View {
let page = extension
.page
.as_ref()
.expect("View extension should set its page field");
vec![path.as_str()]
} else if extension.r#type.contains_sub_items()
&& let Some(ref views) = extension.views
{
let mut pages = Vec::with_capacity(views.len());
for view in views.iter() {
let page = view
.page
.as_ref()
.expect("View extension should set its page field");
pages.push(page.as_str());
}
pages
} else {
// No pages in this extension
Vec::new()
}
};
fn canonicalize_page_path(page_path: &Path, extension_root: &Path) -> PathBuf {
if page_path.is_relative() {
// It is relative to the extension root directory
extension_root.join(page_path)
} else {
page_path.into()
}
}
for page in pages {
/*
* Skip HTTP links
*/
if let Ok(url) = url::Url::parse(page)
&& ["http", "https"].contains(&url.scheme())
{
continue;
}
let path = canonicalize_page_path(Path::new(page), &extension_directory);
convert_page(&path).await?;
}
// Canonicalize relative icon and page paths // Canonicalize relative icon and page paths
canonicalize_relative_icon_path(&extension_directory, &mut extension)?; canonicalize_relative_icon_path(&extension_directory, &mut extension)?;