aboutsummaryrefslogtreecommitdiff
path: root/src/modules/router/translate.rs
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-03-30 20:11:20 -0700
committerFuwn <[email protected]>2023-03-30 20:11:20 -0700
commita63b2174be4bb897d717034ed42e2b7676645deb (patch)
treecbd89ccadc6fbaef751fd6f6021050ee9929ee6c /src/modules/router/translate.rs
parentfeat(translate): minimize translation passes (diff)
downloadlocus-a63b2174be4bb897d717034ed42e2b7676645deb.tar.xz
locus-a63b2174be4bb897d717034ed42e2b7676645deb.zip
feat(translation): add translation header
Diffstat (limited to 'src/modules/router/translate.rs')
-rw-r--r--src/modules/router/translate.rs59
1 files changed, 3 insertions, 56 deletions
diff --git a/src/modules/router/translate.rs b/src/modules/router/translate.rs
index fe94af4..8f24a9b 100644
--- a/src/modules/router/translate.rs
+++ b/src/modules/router/translate.rs
@@ -16,60 +16,7 @@
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only
-use crate::modules::router::deepl::translate;
+mod deepl;
+mod module;
-pub fn module(
- cc: &windmark::returnable::CallbackContext<'_>,
- content: &mut String,
-) {
- if let Some(language) =
- windmark::utilities::queries_from_url(cc.url).get("translate")
- {
- let lines = content
- .lines()
- .map(ToString::to_string)
- .collect::<Vec<String>>();
- let mut preformatted = false;
- let mut saved_lines = std::collections::HashMap::new();
- let mut fully_translated = Vec::new();
-
- for (i, line) in &mut lines.iter().enumerate() {
- if line == "```" {
- preformatted = !preformatted;
- }
-
- if line.starts_with("=>") {
- if let Some(germ::ast::Node::Link {
- to,
- text,
- }) = germ::ast::Ast::from_string(line).inner().get(0)
- {
- saved_lines.insert(
- i,
- format!(
- "=> {to}?translate={language}{}",
- text.clone().map_or_else(
- || "".to_string(),
- |text| { format!(" {}", translate(&text, language).text()) }
- )
- ),
- );
- }
- } else if preformatted {
- saved_lines.insert(i, line.to_string());
- }
- }
-
- let translated = translate(content, language).text();
-
- for (i, line) in translated.lines().enumerate() {
- if saved_lines.contains_key(&i) {
- fully_translated.push(saved_lines.get(&i).unwrap().to_string());
- } else {
- fully_translated.push(line.to_string());
- }
- }
-
- *content = fully_translated.join("\n");
- }
-}
+pub use module::module;