From b310fce41b551a12eb59e7348878f6069b8351f1 Mon Sep 17 00:00:00 2001
From: Inqiyad Sabr
Date: Thu, 13 Nov 2025 00:05:21 +0600
Subject: Optimised for better header checks
And removed the rsync flag for updating
---
c2x | 2 +-
g2x.c | 47 ++++++++++++++++++++++++-----------------------
2 files changed, 25 insertions(+), 24 deletions(-)
diff --git a/c2x b/c2x
index 7d6a1b0..195c922 100755
--- a/c2x
+++ b/c2x
@@ -1,5 +1,5 @@
#!/bin/sh
-rsync -avz ./"${1}" ./"${1}_xhtml"
+rsync ./"${1}" ./"${1}_xhtml"
cd ./"${1}_xhtml" || exit
for file in $(find ./* | grep ".gmi" | sed 's/.gmi//'); do
g2x < "./${file}.gmi" > "./${file}.html"
diff --git a/g2x.c b/g2x.c
index 05b64fc..f6e4f33 100644
--- a/g2x.c
+++ b/g2x.c
@@ -32,46 +32,47 @@ int main(void)
// Reading a line from stdin into buf
while (fgets(buf,BUFSIZE,stdin) != NULL)
{
- if (!strncmp(buf,"###",3)) // Match ###
- {
- html("");
- xmlput(4);
- html("
\n");
+ if (!strncmp(buf,"# ",2))
+ {// Match # for first header
+ html("");
+ xmlput(2);
+ html("
\n");
}
- else if (!strncmp(buf,"##",2)) // Match ##
- {
+ else if (!strncmp(buf,"## ",3))
+ {// Match ## for second header
html("");
xmlput(3);
html("
\n");
}
- else if (buf[0] == '#') // Match #
- {
- html("");
- xmlput(2);
- html("
\n");
+ else if (!strncmp(buf,"### ",4))
+ {// Match ### for third header
+ html("");
+ xmlput(4);
+ html("
\n");
}
- else if (buf[0] == '>') // Match > for blockquote
- {
- html(""); xmlput(2);
+ else if (!strncmp(buf,"> ",2))
+ {// Match "> " for blockquote
+ html("");
+ xmlput(2);
html("
\n");
}
- else if (buf[0] == '*') // Match lists
- {
+ else if (!strncmp(buf,"* ",2))
+ {// Match * for lists
if (list_state == 0) html("\n");
html("- "); xmlput(2); html("
\n");
list_state = 1; // in a list
}
else if (list_state == 1)
- {
+ {// In a set of lists
html("
\n");
list_state = 0; // out of a list
}
else if (!strncmp(buf,"=>",2)) // Match links
- {
+ {// Match => for hyperlinks
html(""); xmlput(pos+1); html("
\n");
}
- else if (!strncmp(buf,"```",3)) // Match codeblocks
- {
+ else if (!strncmp(buf,"```",3))
+ {// Match ``` codeblocks, ignore the block-name
fgets(buf,BUFSIZE,stdin); html("");
while (strncmp(buf,"```",3))
{
@@ -100,5 +101,5 @@ int main(void)
html("\n");
}
} html("