improve template formatter

This commit is contained in:
postscriptum 2025-07-04 20:47:13 +03:00
parent 8b0f28a6c3
commit a04ccbb476

View file

@ -25,28 +25,44 @@ impl Template {
tags: Option<Vec<String>>, tags: Option<Vec<String>>,
attachments: Option<Vec<(String, Option<String>)>>, attachments: Option<Vec<(String, Option<String>)>>,
) -> String { ) -> String {
// implement separated templates @TODO let mut c = String::with_capacity(content.len() + 128);
for l in content.lines() {
if (l.starts_with("http://")
|| l.starts_with("https://")
|| l.starts_with("nex://")
|| l.starts_with("gopher://")
|| l.starts_with("gemini://"))
&& !l.contains(' ')
&& !matches!(self.format, Format::Plain)
{
c.push_str("=> ")
}
c.push_str(l);
c.push('\n')
}
self.pattern self.pattern
.replace( .replace(
"{content}", "{content}",
&if matches!(self.format, Format::Gemtext) || self.pattern.contains("{tags}") { if matches!(self.format, Format::Gemtext) || self.pattern.contains("{tags}") {
content.replace('#', "") c.replace('#', "")
} else { } else {
content c
}, }
.trim(),
) )
.replace( .replace(
"{attachments}", "{attachments}",
&attachments &attachments
.filter(|t| !t.is_empty())
.map(|a| { .map(|a| {
let mut b = Vec::with_capacity(a.len()); let mut b = Vec::with_capacity(a.len());
b.push("\n".to_string()); b.push('\n'.to_string());
for (link, alt) in a { for (link, alt) in a {
let mut t = Vec::with_capacity(2); let mut t = Vec::with_capacity(2);
t.push(if matches!(self.format, Format::Dir) { t.push(match self.format {
format!("=> ../{link}") Format::Dir => format!("=> ../{link}"),
} else { Format::Gemtext => format!("=> {link}"),
format!("=> {link}") Format::Plain => link,
}); });
if let Some(text) = alt { if let Some(text) = alt {
t.push(text) t.push(text)
@ -60,25 +76,32 @@ impl Template {
.replace( .replace(
"{tags}", "{tags}",
&tags &tags
.filter(|t| !t.is_empty())
.map(|t| { .map(|t| {
format!( format!(
"\n\n{}", "\n\n{}",
// trim gemtext special chars // trim special chars (markdown entries)
if matches!(self.format, Format::Gemtext) { if matches!(self.format, Format::Gemtext) {
let mut b = Vec::with_capacity(t.len()); let mut b = Vec::with_capacity(t.len());
for this in t { for this in t {
b.push(this.replace('#', "")) b.push(this.replace('#', "*"));
} }
b b.join("\n")
} else { } else {
t t.join(", ")
} }
.join(", ")
) )
}) })
.unwrap_or_default(), .unwrap_or_default(),
) )
.replace("{link}", &format!("\n\n=> {link}")) .replace(
"{link}",
&if matches!(self.format, Format::Plain) {
format!("\n\n{link}")
} else {
format!("\n\n=> {link}")
},
)
.replace( .replace(
"{updated}", "{updated}",
&updated &updated