From 72d8b4e6bee0ffac79e96dd649a9da2adb86b507 Mon Sep 17 00:00:00 2001 From: name Date: Wed, 27 Sep 2023 11:34:23 +0000 Subject: 2023-09-26 --- rust/wolfree_sed_in_place/src/include_str.html | 17 ++-- rust/wolfree_sed_in_place/src/include_str.js | 65 ++++++++++++++ rust/wolfree_sed_in_place/src/include_str.tsx | 71 ---------------- rust/wolfree_sed_in_place/src/main.rs | 113 ++++++++----------------- 4 files changed, 110 insertions(+), 156 deletions(-) create mode 100644 rust/wolfree_sed_in_place/src/include_str.js delete mode 100644 rust/wolfree_sed_in_place/src/include_str.tsx (limited to 'rust/wolfree_sed_in_place/src') diff --git a/rust/wolfree_sed_in_place/src/include_str.html b/rust/wolfree_sed_in_place/src/include_str.html index 2fbe0f4..f09b167 100644 --- a/rust/wolfree_sed_in_place/src/include_str.html +++ b/rust/wolfree_sed_in_place/src/include_str.html @@ -1,13 +1,15 @@ + + - + - + + href="/ajax/libs/wolfree/2023.8.31/css/Placeholder.css" /> + href="/ajax/libs/wolfree/2023.8.31/css/PodsParser.css" /> + $0
@@ -72,5 +78,4 @@ $0 Mirror Source -
- \ No newline at end of file + \ No newline at end of file diff --git a/rust/wolfree_sed_in_place/src/include_str.js b/rust/wolfree_sed_in_place/src/include_str.js new file mode 100644 index 0000000..9ef3a31 --- /dev/null +++ b/rust/wolfree_sed_in_place/src/include_str.js @@ -0,0 +1,65 @@ +/* SPDX-License-Identifier: AGPL-3.0-or-later */ + +// @ts-check + +const wolfreeLexicalScopeName = (() => { + /** @type {string[]} */ + const possibleLetters = []; + + for (let i = 32; i < 127; i++) { + possibleLetters.push(String.fromCharCode(i)); + } + + const possibleNames = possibleLetters.flatMap((x) => + possibleLetters.map((y) => x + y) + ); + + const isValid = (name = "") => { + try { + return ( + typeof eval(name).value === "string" && + typeof eval(name).i2d === "boolean" + ); + } catch (error) { + return false; + } + }; + + const wolfreeLexicalScopeName = possibleNames.find(isValid); + + if (typeof wolfreeLexicalScopeName !== "string") { + console.warn({ wolfreeLexicalScopeName }); + return ""; + } + + return wolfreeLexicalScopeName; +})(); + +(() => { + const get = (params = "") => { + return new URLSearchParams(location.search).get(params); + }; + + eval(wolfreeLexicalScopeName).value = get("i") || "topic mathematics"; + eval(wolfreeLexicalScopeName).i2d = get("i2d") === "true"; +})(); + +/** + * Regex in regex - Rust + * https://docs.rs/regex/latest/regex/struct.Regex.html#replacement-string-syntax + */ +// @ts-ignore +$0; + +(async () => { + ( + await import( + // @ts-ignore + "/ajax/libs/wolfree/2023.8.31/js/entrypoint.js" + ) + ).default({ + input: eval(wolfreeLexicalScopeName).value, + ...(eval(wolfreeLexicalScopeName).i2d ? { i2d: true } : {}), + podstate: ["Step-by-step solution", "Step-by-step", "Show all steps"], + }); +})(); diff --git a/rust/wolfree_sed_in_place/src/include_str.tsx b/rust/wolfree_sed_in_place/src/include_str.tsx deleted file mode 100644 index 3c8ff4e..0000000 --- a/rust/wolfree_sed_in_place/src/include_str.tsx +++ /dev/null @@ -1,71 +0,0 @@ -/** - * @license - * SPDX-License-Identifier: AGPL-3.0-or-later - * This file is part of Wolfree. - * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - */ - -// @ts-check - -const wolfreeLexicalScopeName = (() => { - const possibleLetters = Array(); - - for (let i = 32; i < 127; i++) { - possibleLetters.push(String.fromCharCode(i)); - } - - const possibleNames = possibleLetters.flatMap((x) => - possibleLetters.map((y) => x + y) - ); - - const isValid = (name = String()) => { - try { - return ( - typeof eval(name).value === "string" && - typeof eval(name).i2d === "boolean" - ); - } catch (error) { - return false; - } - }; - - const wolfreeLexicalScopeName = possibleNames.find(isValid); - - if (typeof wolfreeLexicalScopeName !== "string") { - console.error({ wolfreeLexicalScopeName }); - return ""; - } - - return wolfreeLexicalScopeName; -})(); - -(() => { - const get = (params = String()) => { - return new URLSearchParams(location.search).get(params); - }; - - eval(wolfreeLexicalScopeName).value = get("i") || "topic mathematics"; - eval(wolfreeLexicalScopeName).i2d = get("i2d") === "true"; -})(); - -/** - * Regex in regex - Rust - * https://docs.rs/regex/latest/regex/struct.Regex.html#replacement-string-syntax - * All instances of $ref in the replacement string are replaced with the substring corresponding to the capture group identified by ref. - * ref may be an integer corresponding to the index of the capture group (counted by order of opening parenthesis where 0 is the entire match) or it can be a name (consisting of letters, digits or underscores) corresponding to a named capture group. - */ -// @ts-ignore -$0; - -(async () => { - ( - await import( - // @ts-ignore - "../../../ajax/libs/wolfree/23.7.8/js/Entrypoint.js" - ) - ).default({ - input: eval(wolfreeLexicalScopeName).value, - i2d: eval(wolfreeLexicalScopeName).i2d, - extraPodstates: [], - }); -})(); diff --git a/rust/wolfree_sed_in_place/src/main.rs b/rust/wolfree_sed_in_place/src/main.rs index 7d56889..f20393f 100644 --- a/rust/wolfree_sed_in_place/src/main.rs +++ b/rust/wolfree_sed_in_place/src/main.rs @@ -1,17 +1,14 @@ -//! The `wolfree_sed_in_place` crate provides a function for performing in-place replacements of patterns in multiple files using regular expressions. +/* SPDX-License-Identifier: AGPL-3.0-or-later */ + +//! This crate provides a function for performing in-place replacements of patterns in multiple files using regular expressions. //! It allows you to recursively process all files within a specified directory and replace occurrences of a given pattern with a specified replacement string. #![allow(clippy::blanket_clippy_restriction_lints)] -#![allow(clippy::exit)] -#![allow(clippy::print_stderr)] #![allow(clippy::implicit_return)] - -/* SPDX-License-Identifier: AGPL-3.0-or-later - * This file is part of Wolfree. - * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - */ +#![allow(clippy::question_mark_used)] use regex::Regex; +use std::error; use std::fs; use walkdir::WalkDir; @@ -21,94 +18,52 @@ use walkdir::WalkDir; /// filters out directories, and applies a regular expression-based replacement /// on the contents of each file. /// -/// # Arguments -/// /// * `directory_path`: A string slice representing the path of the directory to traverse. /// * `pattern`: A string slice containing the regular expression pattern to search for. /// * `replacement`: A string slice representing the replacement for each matched pattern. -/// -/// # Examples -/// -/// ```rust -/// // Replace a specific pattern in the file contents in the "./data/" directory. -/// wolfree_sed_in_place("./data/", r"\bFoo\b", "Bar"); -/// ``` -/// -/// This example replaces all occurrences of the word "Foo" with "Bar" in the files inside the "data" directory. -fn wolfree_sed_in_place(directory_path: &str, pattern: &str, replacement: &str) { - // Compile the regular expression pattern - let regex = match Regex::new(pattern) { - Ok(re) => re, - Err(err) => { - eprintln!("Error: {err}",); - std::process::exit(1); - } - }; - - // Create a directory walker and filter out non-file entries - let walker = WalkDir::new(directory_path) +fn sed( + directory_path: &str, + pattern: &str, + replacement: &str +) -> Result<(), Box> { + // Compile the regular expression pattern. + let regex = Regex::new(pattern)?; + // Create a directory walker and filter out non-file entries. + for entry in WalkDir::new(directory_path) .into_iter() .filter_map(Result::ok) - .filter(|entry| !entry.file_type().is_dir()); - - // Process each file in the directory - for entry in walker { + .filter(|entry| !entry.file_type().is_dir()) { let file_path = entry.path(); - - // Read the file's contents into a string - let file_contents = match fs::read_to_string(file_path) { - Ok(contents) => contents, - Err(err) => { - eprintln!("Error: {err}",); - std::process::exit(1); - } - }; - - // Perform the regex replacement on the file's contents - let modified_contents = regex.replace(&file_contents, replacement); - let modified_contents_as_ref = modified_contents.as_ref(); - - // Write the modified contents back to the file, overwriting its previous contents - match fs::write(file_path, modified_contents_as_ref) { - Ok(_) => (), - Err(err) => { - eprintln!("Error: {err}",); - std::process::exit(1); - } - }; + // Read the file's contents into a string. + // Perform the regex replacement on the file's contents. + // Write the modified contents back to the file, overwriting its previous contents. + fs::write(file_path, &*regex.replace(&fs::read_to_string(file_path)?, replacement))?; } + Ok(()) } /// Entry point of the program. -/// Demonstrates using `wolfree_sed_in_place` function to perform in-place replacements on files in specific directories. -fn main() { - wolfree_sed_in_place( - "./docusaurus/build/input/", - "", - include_str!("include_str.html"), - ); +/// Demonstrates using `sed` function to perform in-place replacements on files in specific directories. +fn main() -> Result<(), Box> { + sed("./docusaurus/static/input/", "", include_str!("include_str.html"))?; - wolfree_sed_in_place( - "./docusaurus/build/_next/static/chunks/", + sed( + "./docusaurus/static/_next/static/chunks/", r"try(.{0,100}?)generateEncodedJSONFromValue(.*?)unescapeForUrl(.*?)catch(.*?)\{}", - include_str!("include_str.tsx"), - ); + include_str!("include_str.js") + )?; // console error: - // _app-daabd76ef4fe402d.js:5 // WebSocket connection to 'wss://localhost/n/v1/api/fetcher/results' failed: - // startWebsocket @ _app-daabd76ef4fe402d.js:5 - // send @ _app-daabd76ef4fe402d.js:5 - // sendMessage @ 1695-ae743311a23f8eb5.js:1 - // newQuery @ 1695-ae743311a23f8eb5.js:78 - // newQuery @ 1695-ae743311a23f8eb5.js:78 // fix: - // Override the hostname in the minified JavaScript source code. - wolfree_sed_in_place( - "./docusaurus/build/_next/static/chunks/pages/", + // override the hostname + sed( + "./docusaurus/static/_next/static/chunks/pages/", "window.location.hostname", - "'www.wolframalpha.com'", - ); + "'www.wolframalpha.com'" + )?; + + Ok(()) } // regex101: build, test, and debug regex -- cgit v1.2.3