From b7d2ea8a43e49c29dd3290bc0f4c8e5b7f271acd Mon Sep 17 00:00:00 2001 From: LDA Date: Thu, 1 Aug 2024 18:30:08 +0200 Subject: [PATCH] [MOD] Fix quotes a bit --- src/StrSplit.c | 49 +++++++++++++++++++------------- src/XEP-0393.c | 60 ++++++++++++++++++++++++--------------- src/include/StringSplit.h | 4 +-- 3 files changed, 69 insertions(+), 44 deletions(-) diff --git a/src/StrSplit.c b/src/StrSplit.c index af4545e..0139be4 100644 --- a/src/StrSplit.c +++ b/src/StrSplit.c @@ -115,24 +115,28 @@ StrFullRect(char **split) } char -StrGet(StringRect rect, int line, int col) +StrGet(StringRect *rect, int line, int col) { int actual_line, actual_col; char *linep; - if (!rect.source_lines) + if (!rect || !rect->source_lines) { return '\0'; } - actual_line = rect.start_line + line; - actual_col = rect.start_char + col; + actual_line = rect->start_line + line; + actual_col = rect->start_char + col; - if (actual_line > rect.end_line) + if (actual_line > rect->end_line) + { + return '\0'; + } + if (actual_line >= StrLines(rect->source_lines)) { return '\0'; } - if (!(linep = rect.source_lines[actual_line])) + if (!(linep = rect->source_lines[actual_line])) { return '\0'; } @@ -168,27 +172,34 @@ StrViewChars(StringRect rect, int line) } StringRect -StrGetl(StringRect rect, int line, bool extend) +StrGetl(StringRect *rect, int line, bool extend) { int actual_line; - if (!rect.source_lines) + StringRect ret; + if (!rect->source_lines) + { + return StrFullRect(NULL); + } + + ret = *rect; + + actual_line = rect->start_line + line; + + if (actual_line > rect->end_line) + { + return StrFullRect(NULL); + } + if (actual_line >= StrLines(rect->source_lines)) { return StrFullRect(NULL); } - actual_line = rect.start_line + line; - - if (actual_line > rect.end_line) - { - return StrFullRect(NULL); - } - - rect.start_line = actual_line; + ret.start_line = actual_line; if (!extend) { - rect.end_line = actual_line; + ret.end_line = actual_line; } - return rect; + return ret; } StringRect StrShift(StringRect rect, int n) @@ -229,7 +240,7 @@ PrintRect(StringRect rect) char cbuf[2] = { 0, '\0' }; size_t chi = 0; - while ((*cbuf = StrGet(rect, i, chi)) != '\0' && + while ((*cbuf = StrGet(&rect, i, chi)) != '\0' && chi++ <= StrViewChars(rect, i)) { tmp = line; diff --git a/src/XEP-0393.c b/src/XEP-0393.c index 339ada6..0c6cabc 100644 --- a/src/XEP-0393.c +++ b/src/XEP-0393.c @@ -72,32 +72,29 @@ DecodeQuote(StringRect rect, size_t *skip) int lines = 0; /* C abuse of chaining operations */ - while ((StrGet(rect, lines, 0) == '>') && ++lines) + while ((StrGet(&rect, lines, 0) == '>') && ++lines) { if (!ret.source_lines) { int shift_by = 1, ch; ret = rect; - ret.end_line = 0; + ret.end_line--; - while ((ch = StrGet(rect, lines - 1, shift_by)) && isspace(ch)) + while ((ch = StrGet(&rect, lines - 1, shift_by)) && isspace(ch)) { shift_by++; } - if (ch) - { - ret = StrShift(ret, shift_by); - } + ret = StrShift(ret, shift_by); continue; } - ret.end_line++; } if (!lines) { return StrFullRect(NULL); } + ret.end_line = ret.start_line + lines - 1; if (skip) { @@ -113,7 +110,7 @@ DecodeSpan(StringRect rect, char del, size_t *skip) int chars = 0; char c; - if (StrGet(rect, 0, 0) != del) + if (StrGet(&rect, 0, 0) != del) { return ret; } @@ -121,7 +118,7 @@ DecodeSpan(StringRect rect, char del, size_t *skip) rect = StrShift(rect, 1); /* C abuse of chaining operations */ - while (((c = StrGet(rect, 0, chars)) != del) && ++chars) + while (((c = StrGet(&rect, 0, chars)) != del) && ++chars) { if (!c) { @@ -153,7 +150,7 @@ DecodeSpan(StringRect rect, char del, size_t *skip) size_t i; for (i = 0; i < StrViewChars(ret, 0); i++) { - *chara = StrGet(ret, 0, i); + *chara = StrGet(&ret, 0, i); if (!*chara) { break; @@ -186,10 +183,11 @@ ParseLine(XEP393Element *elem, StringRect line) size_t ch_idx, chars = StrViewChars(line, 0); size_t text_start = 0; size_t i; + bool managed = false; for (ch_idx = 0; ch_idx < chars; ch_idx++) { - char curr = StrGet(line, 0, ch_idx); + char curr = StrGet(&line, 0, ch_idx); StringRect span; shifted = line; shifted.start_char += ch_idx; @@ -204,7 +202,7 @@ ParseLine(XEP393Element *elem, StringRect line) char *temp, *gen = NULL, chara[2] = { 0, '\0' }; \ for (i = text_start; i < text_end; i++) \ { \ - *chara = StrGet(line, 0, i); \ + *chara = StrGet(&line, 0, i); \ \ temp = gen; \ gen = StrConcat(2, gen, chara); \ @@ -212,6 +210,7 @@ ParseLine(XEP393Element *elem, StringRect line) } \ line_item = CreateElementVessel(elem, XEP393_TEXT); \ line_item->text_data = gen; \ + managed = true; \ } \ \ span_item = CreateElementVessel(elem, sym); \ @@ -225,12 +224,15 @@ ParseLine(XEP393Element *elem, StringRect line) HandleSpan('_', XEP393_ITALIC); HandleSpan('~', XEP393_SRKE); HandleSpan('`', XEP393_MONO); + /* TODO: Can we troll and introduce | as a Parsee extension? */ } + + if (!managed) { char *temp, *gen = NULL, chara[2] = { 0, '\0' }; for (i = text_start; i < chars; i++) { - *chara = StrGet(line, 0, i); + *chara = StrGet(&line, 0, i); temp = gen; gen = StrConcat(2, gen, chara); @@ -247,22 +249,25 @@ XEP393Parse(XEP393Element *root, StringRect region, int flags) for (i = 0; i < lines; i++) { - StringRect extend_line = StrGetl(region, i, true); - StringRect single_line = StrGetl(region, i, false); + StringRect extend_line = StrGetl(®ion, i, true); + StringRect single_line = StrGetl(®ion, i, false); size_t jump_by = 0; XEP393Element *sub; - if ((flags & BLOCK_QUOTE) && (StrGet(single_line, 0, 0) == '>')) + if ((flags & BLOCK_QUOTE) && (StrGet(&single_line, 0, 0) == '>')) { StringRect quote = DecodeQuote(extend_line, &jump_by); - sub = CreateElementVessel(root, XEP393_QUOT); - XEP393Parse(sub, quote, flags); + if (quote.source_lines) + { + sub = CreateElementVessel(root, XEP393_QUOT); + XEP393Parse(sub, quote, flags); - i += jump_by - 1; - continue; + i += jump_by - 1; + continue; + } } + /* TODO: Codeblocks, this shouldn't be too hard. */ - /* TODO: Parse the single line properly. */ if (!(flags & BLOCK_CODES)) { sub = CreateElementVessel(root, XEP393_LINE); @@ -325,7 +330,8 @@ ShoveXML(XEP393Element *element, XMLElement *xmlparent) break; default: break; } - if (ArraySize(element->children) == 0 && element->text_data) + if (ArraySize(element->children) == 0 && + element->text_data) { XMLElement *text = XMLCreateText(element->text_data); XMLAddChild(head, text); @@ -334,6 +340,14 @@ ShoveXML(XEP393Element *element, XMLElement *xmlparent) for (i = 0; i < ArraySize(element->children); i++) { XEP393Element *sub = ArrayGet(element->children, i); + if (sub->type == XEP393_TEXT && !sub->text_data) + { + XMLFreeElement(ArrayDelete( + xmlparent->children, + ArraySize(xmlparent->children) - 1 + )); + continue; + } ShoveXML(sub, head); } } diff --git a/src/include/StringSplit.h b/src/include/StringSplit.h index 93c5bb9..7160ac4 100644 --- a/src/include/StringSplit.h +++ b/src/include/StringSplit.h @@ -38,14 +38,14 @@ extern StringRect StrFullRect(char **split); * -------------- * Returns: A character at {line}, {col} in C-indices | '\0' * Modifies: NOTHING */ -extern char StrGet(StringRect rect, int line, int col); +extern char StrGet(StringRect *rect, int line, int col); /** * Retrieves a line from a string rectview, or a NULL one * -------------- * Returns: A stringview that lives along the original * Modifies: NOTHING */ -extern StringRect StrGetl(StringRect rect, int line, bool extend); +extern StringRect StrGetl(StringRect *rect, int line, bool extend); /** * Returns a new stringrect, shifted by N chars.