refactor: remove askama_html dependency
This commit is contained in:
parent
6e77a2198d
commit
082d1c6c92
4 changed files with 27 additions and 17 deletions
|
|
@ -13,11 +13,10 @@ include = ["/src", "README.md", "LICENSE", "!snapshots"]
|
||||||
members = [".", "codegen", "cli"]
|
members = [".", "codegen", "cli"]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["default-tls", "rss"]
|
default = ["default-tls"]
|
||||||
all = ["rss", "html"]
|
all = ["rss"]
|
||||||
|
|
||||||
rss = ["quick-xml"]
|
rss = ["quick-xml"]
|
||||||
html = ["askama_escape"]
|
|
||||||
|
|
||||||
# Reqwest TLS
|
# Reqwest TLS
|
||||||
default-tls = ["reqwest/default-tls"]
|
default-tls = ["reqwest/default-tls"]
|
||||||
|
|
@ -45,7 +44,6 @@ filenamify = "0.1.0"
|
||||||
ress = "0.11.4"
|
ress = "0.11.4"
|
||||||
phf = "0.11.1"
|
phf = "0.11.1"
|
||||||
base64 = "0.13.0"
|
base64 = "0.13.0"
|
||||||
askama_escape = {version = "0.10.3", optional = true}
|
|
||||||
quick-xml = {version = "0.25.0", features = ["serialize"], optional = true}
|
quick-xml = {version = "0.25.0", features = ["serialize"], optional = true}
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::util;
|
||||||
|
|
||||||
use super::UrlTarget;
|
use super::UrlTarget;
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
|
@ -32,8 +34,6 @@ pub trait ToPlaintext {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Trait for converting rich text to html.
|
/// Trait for converting rich text to html.
|
||||||
#[cfg(feature = "html")]
|
|
||||||
#[cfg_attr(docsrs, doc(cfg(feature = "html")))]
|
|
||||||
pub trait ToHtml {
|
pub trait ToHtml {
|
||||||
/// Convert rich text to html.
|
/// Convert rich text to html.
|
||||||
fn to_html(&self) -> String {
|
fn to_html(&self) -> String {
|
||||||
|
|
@ -72,26 +72,22 @@ impl ToPlaintext for TextComponent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "html")]
|
|
||||||
#[cfg_attr(docsrs, doc(cfg(feature = "html")))]
|
|
||||||
impl ToHtml for TextComponent {
|
impl ToHtml for TextComponent {
|
||||||
fn to_html_yt_host(&self, yt_host: &str) -> String {
|
fn to_html_yt_host(&self, yt_host: &str) -> String {
|
||||||
match self {
|
match self {
|
||||||
TextComponent::Text(text) => askama_escape::escape(&text, askama_escape::Html)
|
TextComponent::Text(text) => util::escape_html(text),
|
||||||
.to_string()
|
|
||||||
.replace("\n", "<br>"),
|
|
||||||
TextComponent::Web { text, .. } => {
|
TextComponent::Web { text, .. } => {
|
||||||
format!(
|
format!(
|
||||||
r#"<a href="{}" target="_blank" rel="noreferrer">{}</a>"#,
|
r#"<a href="{}" target="_blank" rel="noreferrer">{}</a>"#,
|
||||||
self.get_url(yt_host),
|
self.get_url(yt_host),
|
||||||
askama_escape::escape(text, askama_escape::Html)
|
util::escape_html(text)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
format!(
|
format!(
|
||||||
r#"<a href="{}">{}</a>"#,
|
r#"<a href="{}">{}</a>"#,
|
||||||
self.get_url(yt_host),
|
self.get_url(yt_host),
|
||||||
askama_escape::escape(self.get_text(), askama_escape::Html)
|
util::escape_html(self.get_text())
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -107,8 +103,6 @@ impl ToPlaintext for RichText {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "html")]
|
|
||||||
#[cfg_attr(docsrs, doc(cfg(feature = "html")))]
|
|
||||||
impl ToHtml for RichText {
|
impl ToHtml for RichText {
|
||||||
fn to_html_yt_host(&self, yt_host: &str) -> String {
|
fn to_html_yt_host(&self, yt_host: &str) -> String {
|
||||||
self.0.iter().map(|c| c.to_html_yt_host(yt_host)).collect()
|
self.0.iter().map(|c| c.to_html_yt_host(yt_host)).collect()
|
||||||
|
|
@ -186,7 +180,6 @@ aespa 에스파 'Black Mamba' MV ℗ SM Entertainment"#
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "html")]
|
|
||||||
#[test]
|
#[test]
|
||||||
fn t_to_html() {
|
fn t_to_html() {
|
||||||
let richtext = RichText::from(TEXT_SOURCE.clone());
|
let richtext = RichText::from(TEXT_SOURCE.clone());
|
||||||
|
|
|
||||||
|
|
@ -312,6 +312,24 @@ where
|
||||||
.ok()
|
.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Replace all html control characters to make a string safe for inserting into HTML.
|
||||||
|
pub fn escape_html(input: &str) -> String {
|
||||||
|
let mut buf = String::with_capacity(input.len());
|
||||||
|
|
||||||
|
for c in input.chars() {
|
||||||
|
match c {
|
||||||
|
'<' => buf.push_str("<"),
|
||||||
|
'>' => buf.push_str(">"),
|
||||||
|
'&' => buf.push_str("&"),
|
||||||
|
'"' => buf.push_str("""),
|
||||||
|
'\'' => buf.push_str("'"),
|
||||||
|
'\n' => buf.push_str("<br>"),
|
||||||
|
_ => buf.push(c),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
buf
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::{fs::File, io::BufReader, path::Path};
|
use std::{fs::File, io::BufReader, path::Path};
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use rstest::rstest;
|
use rstest::rstest;
|
||||||
use time::macros::{date, datetime};
|
use time::macros::date;
|
||||||
use time::OffsetDateTime;
|
use time::OffsetDateTime;
|
||||||
|
|
||||||
use rustypipe::client::{ClientType, RustyPipe};
|
use rustypipe::client::{ClientType, RustyPipe};
|
||||||
|
|
@ -1138,6 +1138,7 @@ async fn channel_not_found(#[case] id: &str) {
|
||||||
#[cfg(feature = "rss")]
|
#[cfg(feature = "rss")]
|
||||||
mod channel_rss {
|
mod channel_rss {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use time::macros::datetime;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn get_channel_rss() {
|
async fn get_channel_rss() {
|
||||||
|
|
|
||||||
Reference in a new issue