gmni

a gemini line mode client
git clone https://git.clttr.info/gmni.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

commit dc4bd11aee8eeceab63bce2956e74a48febcd0e7
parent af1f6baf5e7f8f08a48e22fa48b1320d72c6b132
Author: Ondřej Fiala <ofiala@airmail.cc>
Date:   Wed, 27 Dec 2023 01:02:01 +0100

parser: remove whitespace at the beginning of tokens

Diffstat:
Minclude/util.h | 1+
Msrc/gmnlm.c | 19++++++-------------
Msrc/parser.c | 28+++++++++++++---------------
Msrc/util.c | 8++++++++
4 files changed, 28 insertions(+), 28 deletions(-)

diff --git a/include/util.h b/include/util.h @@ -8,6 +8,7 @@ struct pathspec { const char *path; }; +char *trim_ws(char *in); char *getpath(const struct pathspec *paths, size_t npaths); void posix_dirname(char *path, char *dname); int mkdirs(char *path, mode_t mode); diff --git a/src/gmnlm.c b/src/gmnlm.c @@ -149,13 +149,6 @@ get_data_pathfmt() return getpath(paths, sizeof(paths) / sizeof(paths[0])); } -static char * -trim_ws(char *in) -{ - while (*in && isspace(*in)) ++in; - return in; -} - static void save_bookmark(struct browser *browser, const char *title) { @@ -613,7 +606,7 @@ do_prompts(const char *prompt, struct browser *browser) goto exit; } char *title = in[1] ? &in[1] : browser->page_title; - save_bookmark(browser, title ? trim_ws(title) : title); + save_bookmark(browser, title); goto exit; case 'M': if (in[1]) { @@ -862,7 +855,7 @@ display_document(struct browser *browser, struct gemini_response *resp, while (parser_next(&p, &tok) == 0) { if (tok.token != GEMINI_LINK) goto skip; *next = calloc(1, sizeof(struct link)); - (*next)->url = strdup(trim_ws(tok.link.url)); + (*next)->url = strdup(tok.link.url); next = &(*next)->next; skip: gemini_token_finish(&tok); @@ -927,7 +920,7 @@ repeat: case GEMINI_LINK: if (text == NULL) { col += fprintf(out, "%d) ", nlinks++); - text = trim_ws(tok.link.text ? tok.link.text : tok.link.url); + text = tok.link.text ? tok.link.text : tok.link.url; } else { col += fprintf(out, " "); } @@ -959,7 +952,7 @@ repeat: col += fprintf(out, " "); break; } - text = trim_ws(tok.heading.title); + text = tok.heading.title; } else { col += fprintf(out, " "); } @@ -968,7 +961,7 @@ repeat: if (text == NULL) { col += fprintf(out, " %s ", browser->unicode ? "•" : "*"); - text = trim_ws(tok.list_item); + text = tok.list_item; } else { col += fprintf(out, " "); } @@ -977,7 +970,7 @@ repeat: col += fprintf(out, " %s ", browser->unicode ? "┃" : ">"); if (text == NULL) { - text = trim_ws(tok.quote_text); + text = tok.quote_text; } break; } diff --git a/src/parser.c b/src/parser.c @@ -6,6 +6,7 @@ #include <stdlib.h> #include <string.h> #include <gmni/gmni.h> +#include "util.h" void gemini_parser_init(struct gemini_parser *p, char *body) @@ -72,22 +73,19 @@ gemini_parser_next(struct gemini_parser *p, struct gemini_token *tok) } } else if (strncmp(ln, "=>", 2) == 0) { tok->token = GEMINI_LINK; - int i = 2; - while (isspace(ln[i])) i++; - tok->link.url = strdup(&ln[i]); + tok->link.url = strdup(trim_ws(&ln[2])); - for (char *p = tok->link.url; *p; p++) { - if (isspace(*p)) { - *p = '\0'; - do { p++; } while (isspace(*p)); - if (*p) tok->link.text = strdup(p); - break; - } + char *p = tok->link.url; + while (*p && !isspace(*p)) p++; + if (*p) { + *p = '\0'; + p++; + tok->link.text = strdup(trim_ws(p)); } } else if (strncmp(ln, "```", 3) == 0) { tok->token = GEMINI_PREFORMATTED_BEGIN; if (ln[3]) { - tok->preformatted = strdup(&ln[3]); + tok->preformatted = strdup(trim_ws(&ln[3])); } p->preformatted = true; } else if (ln[0] == '#') { @@ -97,16 +95,16 @@ gemini_parser_next(struct gemini_parser *p, struct gemini_token *tok) ++level; } tok->heading.level = level; - tok->heading.title = strdup(&ln[level]); + tok->heading.title = strdup(trim_ws(&ln[level])); } else if (ln[0] == '*') { tok->token = GEMINI_LIST_ITEM; - tok->list_item = strdup(&ln[1]); + tok->list_item = strdup(trim_ws(&ln[1])); } else if (ln[0] == '>') { tok->token = GEMINI_QUOTE; - tok->quote_text = strdup(&ln[1]); + tok->quote_text = strdup(trim_ws(&ln[1])); } else { tok->token = GEMINI_TEXT; - tok->text = strdup(ln); + tok->text = strdup(trim_ws(ln)); } ln[len] = c; diff --git a/src/util.c b/src/util.c @@ -1,5 +1,6 @@ #include <assert.h> #include <bearssl.h> +#include <ctype.h> #include <errno.h> #include <gmni/gmni.h> #include <libgen.h> @@ -13,6 +14,13 @@ #include <unistd.h> #include "util.h" +char * +trim_ws(char *in) +{ + while (isspace(*in)) ++in; + return in; +} + void posix_dirname(char *path, char *dname) {