Move anki.utils.html_to_text_line() to backend (#2816)

This commit is contained in:
Abdo 2023-11-09 02:57:23 +03:00 committed by GitHub
parent ae6a03942f
commit 6a2d1f94d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 10 deletions

View file

@ -26,6 +26,7 @@ service CardRenderingService {
rpc EncodeIriPaths(generic.String) returns (generic.String); rpc EncodeIriPaths(generic.String) returns (generic.String);
rpc DecodeIriPaths(generic.String) returns (generic.String); rpc DecodeIriPaths(generic.String) returns (generic.String);
rpc StripHtml(StripHtmlRequest) returns (generic.String); rpc StripHtml(StripHtmlRequest) returns (generic.String);
rpc HtmlToTextLine(HtmlToTextLineRequest) returns (generic.String);
rpc CompareAnswer(CompareAnswerRequest) returns (generic.String); rpc CompareAnswer(CompareAnswerRequest) returns (generic.String);
rpc ExtractClozeForTyping(ExtractClozeForTypingRequest) rpc ExtractClozeForTyping(ExtractClozeForTypingRequest)
returns (generic.String); returns (generic.String);
@ -156,6 +157,11 @@ message StripHtmlRequest {
Mode mode = 2; Mode mode = 2;
} }
message HtmlToTextLineRequest {
string text = 1;
bool preserve_media_filenames = 2;
}
message CompareAnswerRequest { message CompareAnswerRequest {
string expected = 1; string expected = 1;
string provided = 2; string provided = 2;

View file

@ -7,7 +7,6 @@ import json as _json
import os import os
import platform import platform
import random import random
import re
import shutil import shutil
import string import string
import subprocess import subprocess
@ -69,15 +68,11 @@ def strip_html_media(txt: str) -> str:
def html_to_text_line(txt: str) -> str: def html_to_text_line(txt: str) -> str:
txt = txt.replace("<br>", " ") import anki.lang
txt = txt.replace("<br />", " ")
txt = txt.replace("<div>", " ") return anki.lang.current_i18n.html_to_text_line(
txt = txt.replace("\n", " ") text=txt, preserve_media_filenames=True
txt = re.sub(r"\[sound:[^]]+\]", "", txt) )
txt = re.sub(r"\[\[type:[^]]+\]\]", "", txt)
txt = strip_html_media(txt)
txt = txt.strip()
return txt
# IDs # IDs

View file

@ -20,6 +20,7 @@ use crate::notetype::RenderCardOutput;
use crate::template::RenderedNode; use crate::template::RenderedNode;
use crate::text::decode_iri_paths; use crate::text::decode_iri_paths;
use crate::text::encode_iri_paths; use crate::text::encode_iri_paths;
use crate::text::html_to_text_line;
use crate::text::sanitize_html_no_images; use crate::text::sanitize_html_no_images;
use crate::text::strip_html; use crate::text::strip_html;
use crate::text::strip_html_preserving_media_filenames; use crate::text::strip_html_preserving_media_filenames;
@ -151,6 +152,17 @@ impl crate::services::CardRenderingService for Collection {
strip_html_proto(input) strip_html_proto(input)
} }
fn html_to_text_line(
&mut self,
input: anki_proto::card_rendering::HtmlToTextLineRequest,
) -> Result<generic::String> {
Ok(
html_to_text_line(&input.text, input.preserve_media_filenames)
.to_string()
.into(),
)
}
fn compare_answer( fn compare_answer(
&mut self, &mut self,
input: anki_proto::card_rendering::CompareAnswerRequest, input: anki_proto::card_rendering::CompareAnswerRequest,