mirror of
https://codeberg.org/postscriptum/snac2nex.git
synced 2026-03-31 21:25:28 +00:00
improve template formatter
This commit is contained in:
parent
8b0f28a6c3
commit
a04ccbb476
1 changed files with 39 additions and 16 deletions
|
|
@ -25,28 +25,44 @@ impl Template {
|
|||
tags: Option<Vec<String>>,
|
||||
attachments: Option<Vec<(String, Option<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
|
||||
.replace(
|
||||
"{content}",
|
||||
&if matches!(self.format, Format::Gemtext) || self.pattern.contains("{tags}") {
|
||||
content.replace('#', "")
|
||||
if matches!(self.format, Format::Gemtext) || self.pattern.contains("{tags}") {
|
||||
c.replace('#', "")
|
||||
} else {
|
||||
content
|
||||
},
|
||||
c
|
||||
}
|
||||
.trim(),
|
||||
)
|
||||
.replace(
|
||||
"{attachments}",
|
||||
&attachments
|
||||
.filter(|t| !t.is_empty())
|
||||
.map(|a| {
|
||||
let mut b = Vec::with_capacity(a.len());
|
||||
b.push("\n".to_string());
|
||||
b.push('\n'.to_string());
|
||||
for (link, alt) in a {
|
||||
let mut t = Vec::with_capacity(2);
|
||||
t.push(if matches!(self.format, Format::Dir) {
|
||||
format!("=> ../{link}")
|
||||
} else {
|
||||
format!("=> {link}")
|
||||
t.push(match self.format {
|
||||
Format::Dir => format!("=> ../{link}"),
|
||||
Format::Gemtext => format!("=> {link}"),
|
||||
Format::Plain => link,
|
||||
});
|
||||
if let Some(text) = alt {
|
||||
t.push(text)
|
||||
|
|
@ -60,25 +76,32 @@ impl Template {
|
|||
.replace(
|
||||
"{tags}",
|
||||
&tags
|
||||
.filter(|t| !t.is_empty())
|
||||
.map(|t| {
|
||||
format!(
|
||||
"\n\n{}",
|
||||
// trim gemtext special chars
|
||||
// trim special chars (markdown entries)
|
||||
if matches!(self.format, Format::Gemtext) {
|
||||
let mut b = Vec::with_capacity(t.len());
|
||||
for this in t {
|
||||
b.push(this.replace('#', ""))
|
||||
b.push(this.replace('#', "*"));
|
||||
}
|
||||
b
|
||||
b.join("\n")
|
||||
} else {
|
||||
t
|
||||
t.join(", ")
|
||||
}
|
||||
.join(", ")
|
||||
)
|
||||
})
|
||||
.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(
|
||||
"{updated}",
|
||||
&updated
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue