feat: ability to categorize

This commit is contained in:
Mark
2026-03-02 00:28:51 +01:00
parent 19f51c5732
commit b86cbd5cf4
6 changed files with 246 additions and 123 deletions

View File

@@ -46,12 +46,19 @@ span {
<!-- t: end -->
<!-- t: // running, not ignored -->
<!-- t: if running -->
<!-- t: set heading true -->
<!-- t: for running -->
<!-- t: ifline id ignore -->
<!-- t: else -->
<!-- t: if heading -->
<!-- t: set heading -->
<h3>things running on the server</h3>
<ul>
<!-- t: for running -->
<!-- t: end -->
<li>
<!-- t: if info -->
<a href="/info/<!-- t: raw info -->">
@@ -82,8 +89,7 @@ span {
<!-- t: set linebreak -->
<!-- t: for line additional -->
<!-- t: raw linebreak -->
<!-- t: set linebreak <br>
-->
<!-- t: set linebreak <br> -->
<!-- t: safe line -->
<!-- t: end -->
</div>
@@ -95,17 +101,83 @@ span {
<!-- t: end -->
</li>
<!-- t: end -->
<!-- t: end -->
</ul>
<!-- t: end -->
<!-- t: // not running, pinned -->
<!-- t: if static -->
<!-- t: set heading true -->
<!-- t: for static -->
<!-- t: ifline id pinned -->
<!-- t: if heading -->
<!-- t: set heading -->
<h3>pinned stuff</h3>
<ul>
<!-- t: end -->
<li>
<!-- t: if info -->
<a href="/info/<!-- t: raw info -->">
<!-- t: else -->
<!-- t: if redirect -->
<a href="/<!-- t: raw redirect -->">
<!-- t: else -->
<span>
<!-- t: end -->
<!-- t: end -->
<!-- t: safe id -->
<!-- t: if info -->
</a>
<!-- t: else -->
<!-- t: if redirect -->
</a>
<!-- t: else -->
</span>
<!-- t: end -->
<!-- t: end -->
: <!-- t: // comment to keep editors from removing the trailing space -->
<!-- t: if additional -->
<em class="oht">
<!-- t: end -->
<!-- t: safe status -->
<!-- t: if additional -->
</em><div class="ohs">
<!-- t: set linebreak -->
<!-- t: for line additional -->
<!-- t: raw linebreak -->
<!-- t: set linebreak <br> -->
<!-- t: safe line -->
<!-- t: end -->
</div>
<!-- t: end -->
<!-- t: if debug -->
<!-- t: if duration -->
| <!-- t: safe duration -->ms
<!-- t: end -->
<!-- t: end -->
</li>
<!-- t: end -->
<!-- t: end -->
</ul>
<!-- t: end -->
<!-- t: // not running, not pinned -->
<!-- t: if static -->
<!-- t: set heading true -->
<!-- t: for static -->
<!-- t: ifline id pinned -->
<!-- t: else -->
<!-- t: if heading -->
<!-- t: set heading -->
<h3>everything else</h3>
<ul>
<!-- t: for static -->
<!-- t: end -->
<li>
<!-- t: if info -->
<a href="/info/<!-- t: raw info -->">
@@ -136,8 +208,7 @@ span {
<!-- t: set linebreak -->
<!-- t: for line additional -->
<!-- t: raw linebreak -->
<!-- t: set linebreak <br>
-->
<!-- t: set linebreak <br> -->
<!-- t: safe line -->
<!-- t: end -->
</div>
@@ -149,9 +220,9 @@ span {
<!-- t: end -->
</li>
<!-- t: end -->
<!-- t: end -->
</ul>
<!-- t: end -->
<!-- t: if docslink -->

View File

@@ -1,5 +1,4 @@
<!DOCTYPE html>
<!-- t: // fill out the following information -->
<!-- t: set domain -->
<!-- t: set sitename unconfigured tomatenmharksite -->
@@ -7,8 +6,17 @@
<!-- t: set ownername CHANGEME -->
<!-- t: set ownerpage /info/me/ -->
<!-- t: // don't show these ids as running -->
<!-- t: set ignore
-->
<!-- t: // put these ids in a section between running and everything else -->
<!-- t: set pinned
-->
<!-- t: // remove this to disable the "tomatenmharksite documentation" link -->
<!-- t: set docslink true -->
<html>
<head>
<meta charset="UTF-8">

View File

@@ -32,7 +32,7 @@ impl Data {
status_updated: Mutex::new((Instant::now(), false)),
}
}
pub fn status_sync(&self, dbg: bool) -> MutexGuard<Status> {
pub fn status_sync(&'_ self, dbg: bool) -> MutexGuard<'_, Status> {
let mut updated = self.status_updated.blocking_lock();
let now = Instant::now();
if (now - updated.0).as_secs_f32() > 3.0 || (dbg && !updated.1) {
@@ -47,7 +47,7 @@ impl Data {
self.status.blocking_lock()
}
}
pub async fn status_async(&self, dbg: bool) -> MutexGuard<Status> {
pub async fn status_async(&'_ self, dbg: bool) -> MutexGuard<'_, Status> {
let mut updated = self.status_updated.lock().await;
let now = Instant::now();
if (now - updated.0).as_secs_f32() > 3.0 || (dbg && !updated.1) {

View File

@@ -26,7 +26,7 @@ async fn index_dbg(data: &State<Data>) -> RawHtml<String> {
async fn index_gen(data: &State<Data>, dbg: bool) -> RawHtml<String> {
let status = data.status_async(dbg).await;
return RawHtml(data.index.gen(&status, dbg, data.globals.clone()));
RawHtml(data.index.gen(&status, dbg, data.globals.clone()))
}
#[get("/int")]

View File

@@ -2,8 +2,8 @@ use std::{collections::BTreeMap, os::unix::fs::PermissionsExt, path::Path, time:
use tokio::time::Instant;
const SLASHINFO: &'static str = "/srv/tomatenmhark-slashinfo/";
const REDIRECT: &'static str = "/srv/tomatenmhark-redirect/";
const SLASHINFO: &str = "/srv/tomatenmhark-slashinfo/";
const REDIRECT: &str = "/srv/tomatenmhark-redirect/";
pub struct Status(
pub BTreeMap<String, (bool, bool, String, Option<String>, Option<Duration>)>,
@@ -40,36 +40,31 @@ impl Status {
);
let mut rest = BTreeMap::new();
if let Ok(rd) = std::fs::read_dir(SLASHINFO) {
for f in rd {
if let Ok(f) = f {
if let Some(id) = f.file_name().to_str() {
if !map.contains_key(id) {
let mut p = f.path();
p.push("desc");
if let Ok(desc) = std::fs::read_to_string(&p) {
let info = Path::new(SLASHINFO).join(id).join("index.html");
let info = info.starts_with(SLASHINFO)
&& info.try_exists().ok() == Some(true);
let redirect = Path::new(REDIRECT).join(id);
let redirect = redirect.starts_with(REDIRECT)
&& redirect.try_exists().ok() == Some(true);
let desc = desc.trim();
if let Some(i) = desc.find('\n') {
rest.insert(
id.to_owned(),
(
info,
redirect,
desc[0..i].trim().to_owned(),
Some(desc[i + 1..].trim().to_owned()),
),
);
} else {
rest.insert(
id.to_owned(),
(info, redirect, desc.to_owned(), None),
);
}
for f in rd.filter_map(Result::ok) {
if let Some(id) = f.file_name().to_str() {
if !map.contains_key(id) {
let mut p = f.path();
p.push("desc");
if let Ok(desc) = std::fs::read_to_string(&p) {
let info = Path::new(SLASHINFO).join(id).join("index.html");
let info =
info.starts_with(SLASHINFO) && info.try_exists().ok() == Some(true);
let redirect = Path::new(REDIRECT).join(id);
let redirect = redirect.starts_with(REDIRECT)
&& redirect.try_exists().ok() == Some(true);
let desc = desc.trim();
if let Some(i) = desc.find('\n') {
rest.insert(
id.to_owned(),
(
info,
redirect,
desc[0..i].trim().to_owned(),
Some(desc[i + 1..].trim().to_owned()),
),
);
} else {
rest.insert(id.to_owned(), (info, redirect, desc.to_owned(), None));
}
}
}
@@ -143,26 +138,25 @@ impl Status {
fn query_status_sync(mut func: impl FnMut(&str, bool, bool, &str, Option<Duration>), dbg: bool) {
if let Ok(rd) = std::fs::read_dir("/tmp/") {
for f in rd {
if let Ok(f) = f {
if let Some(name) = f.file_name().to_str() {
if name.starts_with("tomatenmhark-status-") {
let id = name["tomatenmhark-status-".len()..].trim();
if !id.is_empty() {
if let Ok(status) = std::fs::read_to_string(f.path())
.as_ref()
.map(|v| v.trim_end())
{
if !status.is_empty() {
let info = Path::new(SLASHINFO).join(id).join("index.html");
let info = info.starts_with(SLASHINFO)
&& info.try_exists().ok() == Some(true);
let redirect = Path::new(REDIRECT).join(id);
let redirect = redirect.starts_with(REDIRECT)
&& redirect.try_exists().ok() == Some(true);
func(id, info, redirect, status, None);
}
}
for f in rd.filter_map(Result::ok) {
if let Some(name) = f.file_name().to_str() {
if let Some(id) = name
.strip_prefix("tomatenmhark-status-")
.map(|v| v.trim())
.filter(|v| !v.is_empty())
{
if let Ok(status) = std::fs::read_to_string(f.path())
.as_ref()
.map(|v| v.trim_end())
{
if !status.is_empty() {
let info = Path::new(SLASHINFO).join(id).join("index.html");
let info =
info.starts_with(SLASHINFO) && info.try_exists().ok() == Some(true);
let redirect = Path::new(REDIRECT).join(id);
let redirect = redirect.starts_with(REDIRECT)
&& redirect.try_exists().ok() == Some(true);
func(id, info, redirect, status, None);
}
}
}
@@ -170,28 +164,26 @@ fn query_status_sync(mut func: impl FnMut(&str, bool, bool, &str, Option<Duratio
}
}
if let Ok(rd) = std::fs::read_dir("/srv/tomatenmhark-dystatus/") {
for f in rd {
if let Ok(f) = f {
if let Some(name) = f.file_name().to_str() {
if f.metadata()
.is_ok_and(|meta| meta.is_file() && meta.permissions().mode() & 1 == 1)
{
let id = name.trim();
if !id.is_empty() {
let start = dbg.then(Instant::now);
if let Ok(output) = std::process::Command::new(f.path()).output() {
let elapsed = start.map(|v| v.elapsed());
let out = String::from_utf8_lossy(&output.stdout);
let out = out.trim_end();
if dbg || !out.is_empty() {
let info = Path::new(SLASHINFO).join(id).join("index.html");
let info = info.starts_with(SLASHINFO)
&& info.try_exists().ok() == Some(true);
let redirect = Path::new(REDIRECT).join(id);
let redirect = redirect.starts_with(REDIRECT)
&& redirect.try_exists().ok() == Some(true);
func(id, info, redirect, out, elapsed);
}
for f in rd.filter_map(Result::ok) {
if let Some(name) = f.file_name().to_str() {
if f.metadata()
.is_ok_and(|meta| meta.is_file() && meta.permissions().mode() & 1 == 1)
{
let id = name.trim();
if !id.is_empty() {
let start = dbg.then(Instant::now);
if let Ok(output) = std::process::Command::new(f.path()).output() {
let elapsed = start.map(|v| v.elapsed());
let out = String::from_utf8_lossy(&output.stdout);
let out = out.trim_end();
if dbg || !out.is_empty() {
let info = Path::new(SLASHINFO).join(id).join("index.html");
let info = info.starts_with(SLASHINFO)
&& info.try_exists().ok() == Some(true);
let redirect = Path::new(REDIRECT).join(id);
let redirect = redirect.starts_with(REDIRECT)
&& redirect.try_exists().ok() == Some(true);
func(id, info, redirect, out, elapsed);
}
}
}
@@ -207,23 +199,24 @@ async fn query_status_async(
if let Ok(mut rd) = tokio::fs::read_dir("/tmp/").await {
while let Ok(Some(f)) = rd.next_entry().await {
if let Some(name) = f.file_name().to_str() {
if name.starts_with("tomatenmhark-status-") {
let id = name["tomatenmhark-status-".len()..].trim();
if !id.is_empty() {
if let Ok(status) = tokio::fs::read_to_string(f.path())
.await
.as_ref()
.map(|v| v.trim_end())
{
if !status.is_empty() {
let info = Path::new(SLASHINFO).join(id).join("index.html");
let info = info.starts_with(SLASHINFO)
&& tokio::fs::try_exists(info).await.ok() == Some(true);
let redirect = Path::new(REDIRECT).join(id);
let redirect = redirect.starts_with(REDIRECT)
&& tokio::fs::try_exists(redirect).await.ok() == Some(true);
func(id, info, redirect, status, None);
}
if let Some(id) = name
.strip_prefix("tomatenmhark-status-")
.map(|v| v.trim())
.filter(|v| !v.is_empty())
{
if let Ok(status) = tokio::fs::read_to_string(f.path())
.await
.as_ref()
.map(|v| v.trim_end())
{
if !status.is_empty() {
let info = Path::new(SLASHINFO).join(id).join("index.html");
let info = info.starts_with(SLASHINFO)
&& tokio::fs::try_exists(info).await.ok() == Some(true);
let redirect = Path::new(REDIRECT).join(id);
let redirect = redirect.starts_with(REDIRECT)
&& tokio::fs::try_exists(redirect).await.ok() == Some(true);
func(id, info, redirect, status, None);
}
}
}

View File

@@ -13,6 +13,8 @@ pub enum TemplateType {
Set(usize, String),
Concat(usize, usize),
If(usize, Templates, Option<Templates>),
IfEq(usize, usize, Templates, Option<Templates>),
IfLine(usize, usize, Templates, Option<Templates>),
For(Vec<TemplateLoop>, Templates),
}
@@ -94,8 +96,8 @@ pub struct TemplateInfo {
last_end_was_else: Option<bool>,
}
const COMMENT_START: &'static str = "<!-- t: ";
const COMMENT_END: &'static str = "-->";
const COMMENT_START: &str = "<!-- t: ";
const COMMENT_END: &str = " -->";
impl Template {
pub fn parse(
mut src: &str,
@@ -137,13 +139,14 @@ impl Templates {
if let Some(comment_end) = src.find(COMMENT_END) {
// extract comment content
let comment_og = src[..comment_end].trim();
*src = &src[comment_end + COMMENT_END.len()..].trim_start_matches('\r');
let comment_og = src[..comment_end].trim_start();
*src = src[comment_end + COMMENT_END.len()..].trim_start_matches('\r');
*src = src.strip_prefix('\n').unwrap_or(src);
// do template things
let comment = comment_og.to_lowercase();
let mut comment = comment.split(char::is_whitespace);
match comment.next().unwrap_or("") {
let comment_match = comment.next().unwrap_or("");
match comment_match {
"//" | "#" => {}
"end" => {
break 'parsing;
@@ -170,7 +173,8 @@ impl Templates {
let var_name = comment
.next()
.ok_or(TemplateParseError::MissingVariable("set"))?;
let value = comment_og[3..].trim_start()[var_name.len()..].trim_start();
let value = &comment_og[3..].trim_start()[var_name.len()..];
let value = value.strip_prefix(char::is_whitespace).unwrap_or(value);
out.push(TemplateType::Set(info.var(var_name), value.to_owned()));
}
"concat" => {
@@ -187,22 +191,55 @@ impl Templates {
),
));
}
"if" => {
"if" | "ifeq" | "ifline" => {
let prev_lewe = info.last_end_was_else;
info.last_end_was_else = Some(false);
out.push(TemplateType::If(
info.var(
comment
.next()
.ok_or(TemplateParseError::MissingVariable("if"))?,
),
let (t_if, t_else) = (
Templates::parse(src, info)?,
if info.last_end_was_else.is_some_and(|v| v) {
Some(Templates::parse(src, info)?)
} else {
None
},
));
);
out.push(match comment_match {
"if" => TemplateType::If(
info.var(
comment
.next()
.ok_or(TemplateParseError::MissingVariable("if"))?,
),
t_if,
t_else,
),
"ifeq" => TemplateType::IfEq(
info.var(
comment
.next()
.ok_or(TemplateParseError::MissingVariable("ifeq"))?,
),
info.var(
comment
.next()
.ok_or(TemplateParseError::MissingVariable("ifeq"))?,
),
t_if,
t_else,
),
"ifline" => {
TemplateType::IfLine(
info.var(comment.next().ok_or(
TemplateParseError::MissingVariable("ifline"),
)?),
info.var(comment.next().ok_or(
TemplateParseError::MissingVariable("ifline"),
)?),
t_if,
t_else,
)
}
_ => unreachable!(),
});
info.last_end_was_else = prev_lewe;
}
"for" => {
@@ -256,7 +293,7 @@ pub enum TemplateParseError {
impl Display for TemplateParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::UnclosedComment => write!(f, "There is an unclosed comment. Make sure all `<!-- t: ` comments are closed by a `-->`"),
Self::UnclosedComment => write!(f, "There is an unclosed comment. Make sure all `<!-- t: ` comments are closed by a ` -->`"),
Self::InvalidTemplateKeyword(kw) => write!(f, "Invalid keyword `{kw}` after `<!-- t: `"),
Self::UnknownForSource(s) => write!(f, "For loop can only be used with one or more of `running`, `static`, or `line varname`, not `{s}`"),
Self::DidNotReachEOF(s) => write!(f, "Stopped parsing before the end of the file. Remaining, unparsed text: {s}"),
@@ -296,16 +333,16 @@ impl TemplateType {
Self::Concat(v, a) => {
if *v == *a {
let a = &mut vars[*v];
a.reserve(a.len());
let len = a.len();
a.reserve(len);
let mut i = 0;
while let Some(ch) = a[i..].chars().next() {
while let Some(ch) = a[i..len].chars().next() {
i += ch.len_utf8();
a.push(ch);
}
} else {
let a_temp = std::mem::replace(&mut vars[*a], String::new());
vars[*v].push_str(&a_temp);
vars[*a] = a_temp;
let [a, v] = vars.get_disjoint_mut([*a, *v]).unwrap();
v.push_str(a);
}
}
Self::If(v, template, else_template) => {
@@ -315,6 +352,20 @@ impl TemplateType {
else_template.gen(status, debug, vars, out);
}
}
Self::IfEq(x, y, template, else_template) => {
if vars[*x] == vars[*y] {
template.gen(status, debug, vars, out);
} else if let Some(else_template) = else_template {
else_template.gen(status, debug, vars, out);
}
}
Self::IfLine(x, y, template, else_template) => {
if vars[*y].lines().any(|line| line == vars[*x]) {
template.gen(status, debug, vars, out);
} else if let Some(else_template) = else_template {
else_template.gen(status, debug, vars, out);
}
}
Self::For(src, template) => {
for src in src {
match src {