aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Johnson <nick@nicksphere.ch>2022-01-18 00:00:00 +0000
committerNicholas Johnson <nick@nicksphere.ch>2022-01-18 00:00:00 +0000
commit64f8a3d78e57f40f18f9a6e4e972bba083daba2aae1544c5b8759cb4f4257bb0 (patch)
tree3cdedcf914b08b74751ca84404c443aee2101bb081304472afb5d69e9ca87dec
parent6f406279a565f8ca9c7523ca4dda612abc9961a7f63a12588d22b05c168ee46b (diff)
Simplify escape_and_add_tags and document convert_link_line()
-rw-r--r--src/gemini2html.c44
1 files changed, 26 insertions, 18 deletions
diff --git a/src/gemini2html.c b/src/gemini2html.c
index 68175b6..a834f0e 100644
--- a/src/gemini2html.c
+++ b/src/gemini2html.c
@@ -90,14 +90,17 @@ void skip_non_whitespace(const char* str, size_t* pos) {
while (str[*pos] != '\0' && !(str[*pos] == ' ' || str[*pos] == '\t')) (*pos)++;
}
-char* escape_and_add_tags(const char* opening_tag, const char* unescaped, const char* closing_tag, const size_t unescaped_len, const bool append_newline) {
+/* Escapes unescaped string, prepending opening_tag and appending closing tag and a newline.
+ * If unescaped ends in a newline, ignore the newline. Inserting it causes the returned string to line break inappropriately.
+ */
+char* escape_and_add_tags(const char* opening_tag, const char* unescaped, const char* closing_tag, const size_t unescaped_len) {
char* escaped = escape_text(unescaped, unescaped_len);
size_t opening_tag_len = strlen(opening_tag);
size_t escaped_len = strlen(escaped);
size_t closing_tag_len = strlen(closing_tag);
- char* escaped_with_tags = malloc(opening_tag_len + escaped_len + closing_tag_len + 1);
+ char* escaped_with_tags = malloc(opening_tag_len + escaped_len + closing_tag_len + 2);
size_t index = 0;
/* append opening tag */
@@ -110,7 +113,7 @@ char* escape_and_add_tags(const char* opening_tag, const char* unescaped, const
bool is_newline = escaped[escaped_len - 1] == '\n';
if (!is_newline) escaped_with_tags[index++] = escaped[escaped_len - 1];
- /* escaped string is no longer needed, so free it */
+ /* escaped string is no longer needed */
free(escaped);
escaped = NULL;
@@ -118,8 +121,7 @@ char* escape_and_add_tags(const char* opening_tag, const char* unescaped, const
strncpy(escaped_with_tags + index, closing_tag, closing_tag_len);
index += closing_tag_len;
- /* append newline if caller wants one */
- if (append_newline) escaped_with_tags[index++] = '\n';
+ escaped_with_tags[index++] = '\n';
escaped_with_tags[index] = '\0';
return escaped_with_tags;
@@ -141,11 +143,11 @@ enum linetype getlinetype(const char* line, const bool pre_is_toggled) {
char* convert_text_line(const char* line, const size_t size) {
if (line[0] == '\n') return strdup("<br/>\n");
- else return escape_and_add_tags("<p>", line, "</p>", size, true);
+ else return escape_and_add_tags("<p>", line, "</p>", size);
}
char* convert_pre_line(const char* line, const size_t size) {
- return escape_and_add_tags("", line, "", size, true);
+ return escape_and_add_tags("", line, "", size);
}
char* convert_pre_toggle_line(const char* line, const size_t size) {
@@ -153,30 +155,36 @@ char* convert_pre_toggle_line(const char* line, const size_t size) {
}
char* convert_link_line(const char* line, const size_t size) {
- // skip whitespace before link
+ /* skip whitespace before link */
size_t before_link = 2;
skip_whitespace(line, &before_link);
if (line[before_link] == '\0' || line[before_link] == '\n') return strdup("<a></a>\n");
- // find end of link
+ /* find end of link */
size_t after_link = before_link;
skip_non_whitespace(line, &after_link);
- // find beginning of link text
+ /* find beginning of link text */
size_t before_link_text = after_link;
skip_whitespace(line, &before_link_text);
- char* escaped_href = escape_and_add_tags("<a rel=\"noreferrer noopener\" href=\"", line + before_link, "\">", after_link - before_link, false);
+ char* escaped_href = escape_and_add_tags("<a rel=\"noreferrer noopener\" href=\"", line + before_link, "\">", after_link - before_link);
+
+ /* escaped_href will be appended to, so remove newline */
+ escaped_href[strlen(escaped_href) - 1] = '\0';
char* escaped_with_tags = NULL;
+ /* if no link text exists, use the link as link text */
if (line[before_link_text] == '\0') {
- escaped_with_tags = escape_and_add_tags(escaped_href, line + before_link, "</a>", after_link - before_link, true);
+ escaped_with_tags = escape_and_add_tags(escaped_href, line + before_link, "</a>", after_link - before_link);
+ /* otherwise use the link text */
} else {
- escaped_with_tags = escape_and_add_tags(escaped_href, line + before_link_text, "</a>", size - before_link_text, true);
+ escaped_with_tags = escape_and_add_tags(escaped_href, line + before_link_text, "</a>", size - before_link_text);
}
+ /* escaped_href is no longer needed */
free(escaped_href);
escaped_href = NULL;
@@ -189,7 +197,7 @@ char* convert_h1_line(const char* line, const size_t size) {
skip_whitespace(line, &pos);
if (line[pos] == '\0') return strdup("<h1></h1>\n");
- else return escape_and_add_tags("<h1>", line + pos, "</h1>", size - pos, true);
+ else return escape_and_add_tags("<h1>", line + pos, "</h1>", size - pos);
}
char* convert_h2_line(const char* line, const size_t size) {
@@ -198,7 +206,7 @@ char* convert_h2_line(const char* line, const size_t size) {
skip_whitespace(line, &pos);
if (line[pos] == '\0') return strdup("<h2></h2>\n");
- else return escape_and_add_tags("<h2>", line + pos, "</h2>", size - pos, true);
+ else return escape_and_add_tags("<h2>", line + pos, "</h2>", size - pos);
}
char* convert_h3_line(const char* line, const size_t size) {
@@ -207,7 +215,7 @@ char* convert_h3_line(const char* line, const size_t size) {
skip_whitespace(line, &pos);
if (line[pos] == '\0') return strdup("<h3></h3>\n");
- else return escape_and_add_tags("<h3>", line + pos, "</h3>", size - pos, true);
+ else return escape_and_add_tags("<h3>", line + pos, "</h3>", size - pos);
}
char* convert_ul_line(const char* line, const size_t size) {
@@ -216,7 +224,7 @@ char* convert_ul_line(const char* line, const size_t size) {
skip_whitespace(line, &pos);
if (line[pos] == '\0') return strdup("<li></li>\n");
- else return escape_and_add_tags("<li>", line + pos, "</li>", size - pos, true);
+ else return escape_and_add_tags("<li>", line + pos, "</li>", size - pos);
}
char* convert_quote_line(const char* line, const size_t size) {
@@ -225,7 +233,7 @@ char* convert_quote_line(const char* line, const size_t size) {
skip_whitespace(line, &pos);
if (line[pos] == '\0') return strdup("\n");
- else return escape_and_add_tags("", line + pos, "", size - pos, true);
+ else return escape_and_add_tags("", line + pos, "", size - pos);
}
char* convert_line(const char* line, const size_t size, const enum linetype type) {