[MOD] Fix quotes a bit

This commit is contained in:
LDA 2024-08-01 18:30:08 +02:00
commit b7d2ea8a43
3 changed files with 64 additions and 39 deletions

View file

@ -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(&region, i, true);
StringRect single_line = StrGetl(&region, 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);
}
}