From 46eaf7d134d8c2fd241cb714c7a2af9ed7159084 Mon Sep 17 00:00:00 2001
From: Inqiyad Sabr
Date: Tue, 4 Nov 2025 20:47:51 +0600
Subject: Added Comments and fixed lengthened the codeblocks (for clarity)
---
g2x.c | 88 +++++++++++++++++++++++++++++++++++++++++++++----------------------
1 file changed, 59 insertions(+), 29 deletions(-)
(limited to 'g2x.c')
diff --git a/g2x.c b/g2x.c
index 7fdebb7..05b64fc 100644
--- a/g2x.c
+++ b/g2x.c
@@ -3,7 +3,6 @@
#define BUFSIZE 4096
#define html(s) fputs(s,stdout)
-#define getbuf() fgets(buf,BUFSIZE,stdin)
#define xmlput(n)\
for(pos = n; buf[pos] != '\n'; pos++) {\
switch(buf[pos]) {\
@@ -30,45 +29,76 @@ int main(void)
"\n"
);
- while (getbuf() != NULL)
+ // Reading a line from stdin into buf
+ while (fgets(buf,BUFSIZE,stdin) != NULL)
{
- if (!strncmp(buf,"###",3)) {
- html(""); xmlput(4); html("
\n");
- } else if (!strncmp(buf,"##",2)) {
- html(""); xmlput(3); html("
\n");
- } else if (buf[0] == '#') {
- html(""); xmlput(2); html("
\n");
+ if (!strncmp(buf,"###",3)) // Match ###
+ {
+ html("");
+ xmlput(4);
+ html("
\n");
}
- else if (buf[0] == '>') {
+ else if (!strncmp(buf,"##",2)) // Match ##
+ {
+ html("");
+ xmlput(3);
+ html("
\n");
+ }
+ else if (buf[0] == '#') // Match #
+ {
+ html("");
+ xmlput(2);
+ html("
\n");
+ }
+ else if (buf[0] == '>') // Match > for blockquote
+ {
html(""); xmlput(2);
html("
\n");
}
- else if (buf[0] == '*') {
+ else if (buf[0] == '*') // Match lists
+ {
if (list_state == 0) html("\n");
html("- "); xmlput(2); html("
\n");
- list_state = 1;
- } else if (list_state == 1) {
+ list_state = 1; // in a list
+ }
+ else if (list_state == 1)
+ {
html("
\n");
- list_state = 0;
+ list_state = 0; // out of a list
}
- else if (!strncmp(buf,"=>",2)) {
+ else if (!strncmp(buf,"=>",2)) // Match links
+ {
html(""); xmlput(pos+1); html("
\n");
+ // read entire URL
+ if (
+ buf[pos-3] == 'g' &&
+ buf[pos-2] == 'm' &&
+ buf[pos-1] == 'i' // check for .gmi
+ ){
+ fseek(stdout,-3,1); // move back 3 chars
+ html("html"); // put .html
+ }
+ html("\">"); xmlput(pos+1); html("\n");
+ }
+ else if (!strncmp(buf,"```",3)) // Match codeblocks
+ {
+ fgets(buf,BUFSIZE,stdin); html("");
+ while (strncmp(buf,"```",3))
+ {
+ xmlput(0);
+ putchar('\n');
+ fgets(buf,BUFSIZE,stdin);
+ }
+ html("
\n");
+ }
+ else // Match paragraphs
+ {
+ html("");
+ xmlput(0);
+ html("
\n");
}
- else if (!strncmp(buf,"```",3)) {
- getbuf(); html("");
- while (strncmp(buf,"```",3)) {
- xmlput(0); putchar('\n'); getbuf();
- } html("
\n");
- } else { html(""); xmlput(0); html("
\n"); }
} html(""); html("