[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

@ -115,24 +115,28 @@ StrFullRect(char **split)
} }
char char
StrGet(StringRect rect, int line, int col) StrGet(StringRect *rect, int line, int col)
{ {
int actual_line, actual_col; int actual_line, actual_col;
char *linep; char *linep;
if (!rect.source_lines) if (!rect || !rect->source_lines)
{ {
return '\0'; return '\0';
} }
actual_line = rect.start_line + line; actual_line = rect->start_line + line;
actual_col = rect.start_char + col; 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'; return '\0';
} }
if (!(linep = rect.source_lines[actual_line])) if (!(linep = rect->source_lines[actual_line]))
{ {
return '\0'; return '\0';
} }
@ -168,27 +172,34 @@ StrViewChars(StringRect rect, int line)
} }
StringRect StringRect
StrGetl(StringRect rect, int line, bool extend) StrGetl(StringRect *rect, int line, bool extend)
{ {
int actual_line; int actual_line;
if (!rect.source_lines) StringRect ret;
if (!rect->source_lines)
{ {
return StrFullRect(NULL); return StrFullRect(NULL);
} }
actual_line = rect.start_line + line; ret = *rect;
if (actual_line > rect.end_line) 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); return StrFullRect(NULL);
} }
rect.start_line = actual_line; ret.start_line = actual_line;
if (!extend) if (!extend)
{ {
rect.end_line = actual_line; ret.end_line = actual_line;
} }
return rect; return ret;
} }
StringRect StringRect
StrShift(StringRect rect, int n) StrShift(StringRect rect, int n)
@ -229,7 +240,7 @@ PrintRect(StringRect rect)
char cbuf[2] = { 0, '\0' }; char cbuf[2] = { 0, '\0' };
size_t chi = 0; size_t chi = 0;
while ((*cbuf = StrGet(rect, i, chi)) != '\0' && while ((*cbuf = StrGet(&rect, i, chi)) != '\0' &&
chi++ <= StrViewChars(rect, i)) chi++ <= StrViewChars(rect, i))
{ {
tmp = line; tmp = line;

View file

@ -72,32 +72,29 @@ DecodeQuote(StringRect rect, size_t *skip)
int lines = 0; int lines = 0;
/* C abuse of chaining operations */ /* C abuse of chaining operations */
while ((StrGet(rect, lines, 0) == '>') && ++lines) while ((StrGet(&rect, lines, 0) == '>') && ++lines)
{ {
if (!ret.source_lines) if (!ret.source_lines)
{ {
int shift_by = 1, ch; int shift_by = 1, ch;
ret = rect; 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++; shift_by++;
} }
if (ch)
{
ret = StrShift(ret, shift_by); ret = StrShift(ret, shift_by);
}
continue; continue;
} }
ret.end_line++;
} }
if (!lines) if (!lines)
{ {
return StrFullRect(NULL); return StrFullRect(NULL);
} }
ret.end_line = ret.start_line + lines - 1;
if (skip) if (skip)
{ {
@ -113,7 +110,7 @@ DecodeSpan(StringRect rect, char del, size_t *skip)
int chars = 0; int chars = 0;
char c; char c;
if (StrGet(rect, 0, 0) != del) if (StrGet(&rect, 0, 0) != del)
{ {
return ret; return ret;
} }
@ -121,7 +118,7 @@ DecodeSpan(StringRect rect, char del, size_t *skip)
rect = StrShift(rect, 1); rect = StrShift(rect, 1);
/* C abuse of chaining operations */ /* C abuse of chaining operations */
while (((c = StrGet(rect, 0, chars)) != del) && ++chars) while (((c = StrGet(&rect, 0, chars)) != del) && ++chars)
{ {
if (!c) if (!c)
{ {
@ -153,7 +150,7 @@ DecodeSpan(StringRect rect, char del, size_t *skip)
size_t i; size_t i;
for (i = 0; i < StrViewChars(ret, 0); i++) for (i = 0; i < StrViewChars(ret, 0); i++)
{ {
*chara = StrGet(ret, 0, i); *chara = StrGet(&ret, 0, i);
if (!*chara) if (!*chara)
{ {
break; break;
@ -186,10 +183,11 @@ ParseLine(XEP393Element *elem, StringRect line)
size_t ch_idx, chars = StrViewChars(line, 0); size_t ch_idx, chars = StrViewChars(line, 0);
size_t text_start = 0; size_t text_start = 0;
size_t i; size_t i;
bool managed = false;
for (ch_idx = 0; ch_idx < chars; ch_idx++) 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; StringRect span;
shifted = line; shifted = line;
shifted.start_char += ch_idx; shifted.start_char += ch_idx;
@ -204,7 +202,7 @@ ParseLine(XEP393Element *elem, StringRect line)
char *temp, *gen = NULL, chara[2] = { 0, '\0' }; \ char *temp, *gen = NULL, chara[2] = { 0, '\0' }; \
for (i = text_start; i < text_end; i++) \ for (i = text_start; i < text_end; i++) \
{ \ { \
*chara = StrGet(line, 0, i); \ *chara = StrGet(&line, 0, i); \
\ \
temp = gen; \ temp = gen; \
gen = StrConcat(2, gen, chara); \ gen = StrConcat(2, gen, chara); \
@ -212,6 +210,7 @@ ParseLine(XEP393Element *elem, StringRect line)
} \ } \
line_item = CreateElementVessel(elem, XEP393_TEXT); \ line_item = CreateElementVessel(elem, XEP393_TEXT); \
line_item->text_data = gen; \ line_item->text_data = gen; \
managed = true; \
} \ } \
\ \
span_item = CreateElementVessel(elem, sym); \ span_item = CreateElementVessel(elem, sym); \
@ -225,12 +224,15 @@ ParseLine(XEP393Element *elem, StringRect line)
HandleSpan('_', XEP393_ITALIC); HandleSpan('_', XEP393_ITALIC);
HandleSpan('~', XEP393_SRKE); HandleSpan('~', XEP393_SRKE);
HandleSpan('`', XEP393_MONO); HandleSpan('`', XEP393_MONO);
/* TODO: Can we troll and introduce | as a Parsee extension? */
} }
if (!managed)
{ {
char *temp, *gen = NULL, chara[2] = { 0, '\0' }; char *temp, *gen = NULL, chara[2] = { 0, '\0' };
for (i = text_start; i < chars; i++) for (i = text_start; i < chars; i++)
{ {
*chara = StrGet(line, 0, i); *chara = StrGet(&line, 0, i);
temp = gen; temp = gen;
gen = StrConcat(2, gen, chara); gen = StrConcat(2, gen, chara);
@ -247,22 +249,25 @@ XEP393Parse(XEP393Element *root, StringRect region, int flags)
for (i = 0; i < lines; i++) for (i = 0; i < lines; i++)
{ {
StringRect extend_line = StrGetl(region, i, true); StringRect extend_line = StrGetl(&region, i, true);
StringRect single_line = StrGetl(region, i, false); StringRect single_line = StrGetl(&region, i, false);
size_t jump_by = 0; size_t jump_by = 0;
XEP393Element *sub; 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); StringRect quote = DecodeQuote(extend_line, &jump_by);
if (quote.source_lines)
{
sub = CreateElementVessel(root, XEP393_QUOT); sub = CreateElementVessel(root, XEP393_QUOT);
XEP393Parse(sub, quote, flags); XEP393Parse(sub, quote, flags);
i += jump_by - 1; i += jump_by - 1;
continue; continue;
} }
}
/* TODO: Codeblocks, this shouldn't be too hard. */
/* TODO: Parse the single line properly. */
if (!(flags & BLOCK_CODES)) if (!(flags & BLOCK_CODES))
{ {
sub = CreateElementVessel(root, XEP393_LINE); sub = CreateElementVessel(root, XEP393_LINE);
@ -325,7 +330,8 @@ ShoveXML(XEP393Element *element, XMLElement *xmlparent)
break; break;
default: 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); XMLElement *text = XMLCreateText(element->text_data);
XMLAddChild(head, text); XMLAddChild(head, text);
@ -334,6 +340,14 @@ ShoveXML(XEP393Element *element, XMLElement *xmlparent)
for (i = 0; i < ArraySize(element->children); i++) for (i = 0; i < ArraySize(element->children); i++)
{ {
XEP393Element *sub = ArrayGet(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); ShoveXML(sub, head);
} }
} }

View file

@ -38,14 +38,14 @@ extern StringRect StrFullRect(char **split);
* -------------- * --------------
* Returns: A character at {line}, {col} in C-indices | '\0' * Returns: A character at {line}, {col} in C-indices | '\0'
* Modifies: NOTHING */ * 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 * Retrieves a line from a string rectview, or a NULL one
* -------------- * --------------
* Returns: A stringview that lives along the original * Returns: A stringview that lives along the original
* Modifies: NOTHING */ * 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. * Returns a new stringrect, shifted by N chars.