diff options
author | Katherine Peeters <katherine.peeters@leagueh.xyz> | 2021-08-08 11:36:34 -0400 |
---|---|---|
committer | Katherine Peeters <katherine.peeters@leagueh.xyz> | 2021-08-08 11:36:34 -0400 |
commit | 5e081c11cc0ea69fcc5bce1696c64d38ede4849d (patch) | |
tree | 85d0a03d264e870ee0fc97f01282a3fdf5647871 | |
parent | 0cbb63ffcca8edaf9d1d0f54cecdca6cc36e251f (diff) | |
download | loh-website-5e081c11cc0ea69fcc5bce1696c64d38ede4849d.tar.gz |
Added gmi2html
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 16 | ||||
-rw-r--r-- | gmi2html.c | 191 |
3 files changed, 208 insertions, 0 deletions
@@ -1,6 +1,7 @@ *.html *.xml favicon.ico +gmi2html !header.html !footer.html @@ -1,3 +1,4 @@ +GEMTEXT=$(shell find -L site -type f -name "*.gmi" -print) MARKDOWN=$(shell find -L site -type f -name "*.md" -print) HTML=$(MARKDOWN:%.md=%.html) RSS=$(shell find -L . -type d -name "*.rss" -print) @@ -30,8 +31,23 @@ all: $(HTML) $(RSS_XML) done cat rss_footer.xml >> $@ +%.html: %.gmi header.html footer.html gmi2html + @echo "GMI2HTML $@" + cp header.html $@ + ./gmi2html < $< > help.txt + ./gmi2html < $< | sed -e 's/\.md/\.html/g ; /^<h2>.*/i </div><div class="content">' >> $@ + echo "</div>" >> $@ + echo "<br />" >> $@ + lsand -H | tail -n +2 >> $@ + cat footer.html >> $@ + +gmi2html: gmi2html.c + @echo "CC $@" + $(CC) $(CFLAGS) $^ -o $@ + .PHONY: clean clean: + rm -f gmi2html rm -f ${HTML} rm -f ${RSS_XML} diff --git a/gmi2html.c b/gmi2html.c new file mode 100644 index 0000000..5708339 --- /dev/null +++ b/gmi2html.c @@ -0,0 +1,191 @@ +#include <ctype.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +static char *lstrip(char *restrict s) { + size_t i; + for(i = 0; s[i] != '\0'; i++) { + if(!isspace(s[i])) { + return &s[i]; + } + } + return &s[i]; +} + +static char *rstrip(char *restrict s) { + char *last_nonspace = s; + for(size_t i = 0; s[i] != '\0'; i++) { + if(!isspace(s[i])) { + last_nonspace = &s[i]; + } + } + if(last_nonspace[0] != '\0') { + last_nonspace[1] = '\0'; + } + return s; +} + +static char *strip(char *restrict s) { + return rstrip(lstrip(s)); +} + +static void print_sanitized(const char *restrict s) { + size_t len = strlen(s); + for(size_t i = 0; i < len; i++) { + if(s[i] == '<') { + printf("<"); + } + else if(s[i] == '>') { + printf(">"); + } + else if(s[i] == '&') { + printf("&"); + } + else if(s[i] == '"') { + printf("""); + } + else { + printf("%c", s[i]); + } + } +} + +int main(int argc, char **argv) { + int list_mode = 0, preformat_mode = 0; + + // Read lines + char *line = NULL; + size_t line_buf_len = 0; + ssize_t line_len; + while((line_len = getline(&line, &line_buf_len, stdin)) != -1) { + line[line_len - 1] = '\0'; + line_len--; + + // Disable list mode + if(list_mode && (line[0] != '*' || line[1] != ' ')) { + list_mode = 0; + printf("</ul>\n"); + } + + // Preformatted mode + if(preformat_mode) { + if(line_len == 3 && line[0] == '`' && line[1] == '`' && line[2] == '`') { + preformat_mode = 0; + printf("</pre>\n"); + } + else { + print_sanitized(line); + printf("\n"); + } + goto loopend; + } + + // Blank lines + if(line[0] == '\0') { + printf("<br />\n"); + } + + // Links + else if(line_len >= 2 && line[0] == '=' && line[1] == '>') { + + // Discard leading whitespace + char *url = lstrip(&line[2]); + + // Get label + char *label = NULL; + for(size_t i = 0; url[i] != '\0'; i++) { + if(isspace(url[i])) { + url[i] = '\0'; + label = &url[i + 1]; + break; + } + } + + // Print link + if(label == NULL) { + label = url; + } + else { + label = strip(label); + } + printf("<a href=\""); + print_sanitized(url); + printf("\">"); + print_sanitized(label); + printf("</a><br />\n"); + } + + // Headings + else if(line[0] == '#') { + unsigned int header_count = 1; + if(line_len >= 2 && line[1] == '#') { + header_count++; + if(line_len >= 3 && line[2] == '#') { + header_count++; + } + } + printf("<h%d>", header_count); + print_sanitized(strip(line + header_count)); + printf("</h%d>\n", header_count); + } + + // Lists + else if(line_len >= 2 && line[0] == '*' && line[1] == ' ') { + if(!list_mode) { + list_mode = 1; + printf("<ul>\n"); + } + printf("<li>"); + print_sanitized(strip(line + 2)); + printf("</li>\n"); + } + + // Blockquotes + else if(line[0] == '>') { + printf("<q>"); + print_sanitized(strip(line + 1)); + printf("</q>\n"); + } + + // Preformatted text + else if(line_len >= 3 && line[0] == '`' && line[1] == '`' && line[2] == '`') { + preformat_mode = 1; + printf("<pre>\n"); + } + + // Regular text + else { + printf("<p>"); + print_sanitized(strip(line)); + printf("</p>\n"); + } + + loopend: + free(line); + line = NULL; + line_buf_len = 0; + } + free(line); + + // Disable list mode + if(list_mode) { + list_mode = 0; + printf("</ul>\n"); + } + + // Preformatted mode + if(preformat_mode) { + preformat_mode = 0; + printf("</pre>\n"); + } + + // Return + if(feof(stdin)) { + return EXIT_SUCCESS; + } + else { + return EXIT_FAILURE; + } +} + |