repo: geminiclient action: commit revision: path_from: revision_from: 0ade454639f0c25fb035d372d7057f1b246649bd: path_to: revision_to:
commit 0ade454639f0c25fb035d372d7057f1b246649bd Author: epochDate: Fri Aug 5 06:23:14 2022 +0000 added header_to_anchor stolen from gemini://nytpu.com/gemlog/files/header_to_anchor.c (thanks nytpu) diff --git a/gemini2html.c b/gemini2html.c
--- a/gemini2html.c +++ b/gemini2html.c @@ -2,14 +2,41 @@ #include#include +#include +#include + #define LINE_ENDING "\r\n" +char * +header_to_anchor(const char *header) +{ + if (header == NULL) return NULL; + + size_t header_len = strlen(header); + // calloc to ensure that the string is always automatically NUL-terminated + char *anchor = calloc(header_len + 1, sizeof(*anchor)); + if (anchor == NULL) return NULL; + + bool prev_is_dash = false; + for (size_t hi = 0, ai = 0; hi < header_len; hi++) { + if (isalnum(header[hi])) { + anchor[ai++] = tolower(header[hi]); + prev_is_dash = false; + } else if (!prev_is_dash && (isspace(header[hi]) || header[hi] == '-')) { + anchor[ai++] = '-'; + prev_is_dash = true; + } + } + return anchor; +} + int main(int argc,char *argv[]) { char line[65536];//65535+null int h=0; char pre=0; char list=0; char *first; + char *tab,*spa; char *second; char *t,*s; int ln=0; @@ -39,17 +66,9 @@ int main(int argc,char *argv[]) { if(!strncmp(line,"## ",3)) h=2; if(!strncmp(line,"### ",4)) h=3; if(h) { - first=""; - s=""; - t=strdup(line+h+1); - if(strchr(t,' ')) { - s=" "; - first=strchr(t,' '); - *first=0; - first++; - } + t=header_to_anchor(line+h+1); if(!ln) printf(" %s %s",h,line+h+1,h,LINE_ENDING); - else printf("%s%s%s %s",h,t,t,t,s,first,h,LINE_ENDING); + else printf("%s %s",h,t,t,line+h+1,h,LINE_ENDING); free(t); continue; } @@ -62,7 +81,22 @@ int main(int argc,char *argv[]) { } if(!strncmp(line,"=> ",3)) { first=line+3; - if((second=strchr(first,' '))) { + tab=strchr(first,'\t'); + spa=strchr(first,' '); + //this shit can probably be shortened. + if(tab && spa) { + second = tab-----END OF PAGE-----