~callum/gmsfn

bbcd4b8369671c348e832d4101d6b50a8c93343e — Callum Brown 1 year, 3 months ago 68930d0
Output gemini links to files specified in config

Output file paths are given by third level headings.
e.g. ### /home/user/output.gmi
New links on each page linked before the next ### are output to that file.
Output file defaults to stdout.
1 files changed, 36 insertions(+), 6 deletions(-)

M src/gemlogger.c
M src/gemlogger.c => src/gemlogger.c +36 -6
@@ 60,6 60,15 @@ tofu_callback(enum tofu_error error, const char *fingerprint,
}


/* From gmnlm.c */
static char *
trim_ws(char *in)
{
	while (*in && isspace(*in)) ++in;
	return in;
}


/* Determine whether the response can be used. */
const bool
bad_response(enum gemini_result r, struct gemini_response *resp)


@@ 202,11 211,28 @@ main(int argc, char *argv[])
	config_resp.ssl_ctx = NULL;

	/* Parse config file */
	FILE *out_fp = stdout;
	bool good_output_file = true;
	struct gemini_parser config_parser;
	gemini_parser_init(&config_parser, config_resp.bio);
	struct gemini_token config_tok;
	while (gemini_parser_next(&config_parser, &config_tok) == 0) {
		if (token_is_good_link(config_tok)) {
		if (config_tok.token == GEMINI_HEADING) {
			if (config_tok.heading.level == 3) {
				/* Change output file */
				char *path = trim_ws(config_tok.heading.title);
				FILE *new_out_fp = fopen(path, "w+");
				if (!new_out_fp) {
					printf("Bad output file: %s\n", config_tok.heading.title);
					/* Skip links until a new output file is given */
					good_output_file = false;
					continue;
				}
				out_fp = new_out_fp;
				good_output_file = true;
			}
		} else if (good_output_file && token_is_good_link(config_tok)) {
			/* Make requests and output links */
			/* Get gemlog index page */
			struct gemini_response resp;
			enum gemini_result r = gemini_request(config_tok.link.url, &opts, &resp);


@@ 218,7 244,6 @@ main(int argc, char *argv[])

			char *base_url = get_url_without_filename(config_tok.link.url);
			char *abs_url;
			FILE *out = stdout;

			/* Parse gemlog index page */
			struct gemini_parser p;


@@ 227,10 252,14 @@ main(int argc, char *argv[])
			while (gemini_parser_next(&p, &tok) == 0) {
				if (token_is_good_link(tok)) {
					abs_url = get_absolute_url(tok.link.url, base_url);
					fprintf(out, "link text: %s\n", tok.link.text);
					fprintf(out, "link url: %s\n", tok.link.url);
					fprintf(out, "abs  url: %s\n", abs_url);
					fprintf(out, "\n");
					fprintf(out_fp, "=> %s", abs_url);
					if (config_tok.link.text)
						fprintf(out_fp, " %s", config_tok.link.text);
					if (config_tok.link.text && tok.link.text)
						fprintf(out_fp, ":");
					if (tok.link.text)
						fprintf(out_fp, " %s", tok.link.text);
					fprintf(out_fp, "\n");
					free(abs_url);
				}
			}


@@ 240,6 269,7 @@ main(int argc, char *argv[])
		}
	}

	fclose(out_fp);
	gemini_token_finish(&config_tok);
	gemini_parser_finish(&config_parser);
	gemini_response_finish(&config_resp);