Perfect, you understood it correctly, before moving to text streaming, let's try to fix the current code. So far, I have made two different codes to apply formats, one for the tables which is the newer one and is of better quality, and the previous one which is lower quality and duplicates the functionality as the newer one used on the tables. I will post the code so you can see what I mean. I want to completely remove the old code and move to a similar code used in the tables. There are some missing tags in the tables, but we can find a compromise of adapting it to these specific new use cases not found on the tables.
Here is the code so you can understand and see what I meant, I will skip constants or other utility methods:
wxString Markdown::DetectAndMarkTables(const wxString& message)
{
wxString output;
wxString currentTable;
bool inTable = false;
wxString::size_type pos = 0;
while (pos < message.Length())
{
wxString::size_type lineEnd = message.find('\n', pos);
if (lineEnd == wxString::npos)
{
lineEnd = message.Length();
}
wxString line = message.substr(pos, lineEnd - pos).Trim(false).Trim();
if (line.StartsWith(TABLE_COLUMN_MARKER))
{
if (!inTable)
{
inTable = true;
currentTable.clear();
output << TABLE_MARKER; // Mark the start of a table
}
currentTable << line << '\n';
}
else if (inTable && line.StartsWith(TABLE_HEADER_MARKER))
{
currentTable << line << '\n';
}
else
{
if (inTable)
{
inTable = false;
output << currentTable << TABLE_MARKER; // Mark the end of a table
currentTable.clear();
}
output << line << '\n';
}
pos = lineEnd + 1;
}
if (inTable)
{
output << currentTable << TABLE_MARKER;
}
return output;
}
wxRichTextAttr Markdown::FormatTableMarkdownStyle(wxString& styledText, const wxTextAttr& baseStyle)
{
wxRichTextAttr textAttr = baseStyle;
if (styledText.StartsWith(START_LINK_TEXT_MARKER) && styledText.EndsWith(END_LINK_URL_MARKER))
{
wxString::size_type endLinkText = styledText.find(END_LINK_TEXT_MARKER);
wxString::size_type startLinkUrl = styledText.find(START_LINK_URL_MARKER, endLinkText);
wxString::size_type endLinkUrl = styledText.find(END_LINK_URL_MARKER, startLinkUrl);
if (endLinkText != wxString::npos && startLinkUrl != wxString::npos && endLinkUrl != wxString::npos)
{
wxString linkUrl = styledText.substr(startLinkUrl + 1, endLinkUrl - startLinkUrl - 1);
if (linkUrl.StartsWith("http://") || linkUrl.StartsWith("https://"))
{
textAttr.SetTextColour(*wxBLUE);
textAttr.SetFontUnderlined(true);
textAttr.SetURL(linkUrl);
styledText = styledText.Mid(START_LINK_TEXT_MARKER.Length(), endLinkText - 1);
}
}
}
else
{
bool cont;
do
{
cont = false;
if (styledText.StartsWith(BOLD_MARKER) && styledText.EndsWith(BOLD_MARKER))
{
textAttr.SetFontWeight(wxFONTWEIGHT_BOLD);
styledText = styledText.Mid(BOLD_MARKER.Length(), styledText.Length() - BOLD_MARKER.Length() * 2);
cont = true;
}
if (styledText.StartsWith(ITALIC_MARKER) && styledText.EndsWith(ITALIC_MARKER))
{
textAttr.SetFontStyle(wxFONTSTYLE_ITALIC);
styledText = styledText.Mid(ITALIC_MARKER.Length(), styledText.Length() - ITALIC_MARKER.Length() * 2);
cont = true;
}
if (styledText.StartsWith(UNDERLINE_MARKER) && styledText.EndsWith(UNDERLINE_MARKER))
{
textAttr.SetFontUnderlined(true);
styledText = styledText.Mid(UNDERLINE_MARKER.Length(), styledText.Length() - UNDERLINE_MARKER.Length() * 2);
cont = true;
}
if (styledText.StartsWith(CODE_MARKER) && styledText.EndsWith(CODE_MARKER))
{
textAttr.SetFontWeight(wxFONTWEIGHT_MEDIUM);
textAttr.SetBackgroundColour(baseStyle.GetBackgroundColour().ChangeLightness(60));
textAttr.SetTextColour(baseStyle.GetTextColour().ChangeLightness(140));
styledText = styledText.Mid(CODE_MARKER.Length(), styledText.Length() - CODE_MARKER.Length() * 2);
cont = true;
}
if (styledText.StartsWith(STRIKETHROUGH_MARKER) && styledText.EndsWith(STRIKETHROUGH_MARKER))
{
wxFont font = textAttr.GetFont();
font.MakeStrikethrough();
textAttr.SetFont(font);
styledText = styledText.Mid(STRIKETHROUGH_MARKER.Length(), styledText.Length() - STRIKETHROUGH_MARKER.Length() * 2);
cont = true;
}
}while(cont);
}
return textAttr;
}
void Markdown::CreateAndInsertTable(wxRichTextCtrl* richTextChatBox, const wxString& tableData, const wxTextAttr& style)
{
wxArrayString rows = wxSplit(tableData, '\n');
if (rows.empty()) return;
wxArrayString headers = wxSplit(rows[0], TABLE_COLUMN_MARKER[0]);
headers = FilterEmptyElements(headers);
wxString::size_type colsCount = headers.GetCount();
wxString::size_type rowsCount = rows.GetCount() - 2; // Exclude header underline
if (colsCount < 2)
{
return;
}
wxRichTextTable* table = richTextChatBox->WriteTable(rowsCount, colsCount);
// Headers
for (wxString::size_type col = 0; col < colsCount; ++col)
{
wxString cellText = headers[col].Trim(false).Trim();
wxRichTextAttr cellAttr = FormatTableMarkdownStyle(cellText, style);
cellAttr.SetAlignment(wxTEXT_ALIGNMENT_CENTRE);
cellAttr.SetFontWeight(wxFONTWEIGHT_BOLD);
table->GetCell(0, col)->AddParagraph(cellText, &cellAttr);
}
for (wxString::size_type row = 1; row < rowsCount; ++row)
{
wxArrayString cells = wxSplit(rows[row + 1], TABLE_COLUMN_MARKER[0]); // Skip header row
cells = FilterEmptyElements(cells);
if (cells.GetCount() != colsCount)
{
continue;
}
for (wxString::size_type col = 0; col < colsCount; ++col)
{
wxString cellText = cells[col].Trim(false).Trim();
wxRichTextAttr cellAttr = FormatTableMarkdownStyle(cellText, style);
cellAttr.SetAlignment(wxTEXT_ALIGNMENT_CENTRE);
table->GetCell(row, col)->AddParagraph(cellText, &cellAttr);
}
}
}
wxString Markdown::ReplaceUnpaired(wchar_t character, wchar_t replaceCharacter, const wxString& message)
{
wxString output;
for (wxString::size_type i = 0; i < message.length(); ++i)
{
if (message[i] == '\n' && message[i + 1] == character)
{
wxString::size_type nextChar = message.find(character, i + 2);
if (nextChar == wxString::npos || nextChar >= message.find('\n', i + 2))
{
output << message[i] << replaceCharacter << ' ';
i += 1;
continue;
}
}
output << message[i];
}
return output;
}
wxString Markdown::FormatMessage(const wxString& message)
{
wxString formattedMessage = message;
formattedMessage.Replace(LINE_BREAK_TAG, "\n");
formattedMessage.Replace(START_BOLD_TAG, BOLD_MARKER);
formattedMessage.Replace(END_BOLD_TAG, BOLD_MARKER);
formattedMessage.Replace(START_ITALIC_TAG, ITALIC_MARKER);
formattedMessage.Replace(END_ITALIC_TAG, ITALIC_MARKER);
formattedMessage.Replace(START_LIST_MARKER, LIST_MARKER);
formattedMessage = Markdown::ReplaceUnpaired('*', LIST_MARKER[1], formattedMessage);
formattedMessage = DetectAndMarkTables(formattedMessage);
return formattedMessage;
}
void Markdown::WriteText(wxRichTextCtrl* richTextChatBox, const wxString& message, const wxTextAttr& style)
{
richTextChatBox->BeginStyle(style);
wxString formattedMessage = Markdown::FormatMessage(message);
wxString::size_type pos = 0;
while (pos < formattedMessage.Length() && pos != wxString::npos)
{
wxString::size_type startBoldItalic = formattedMessage.find(BOLD_ITALIC_MARKER, pos);
wxString::size_type startBold = formattedMessage.find(BOLD_MARKER, pos);
wxString::size_type startItalic = formattedMessage.find(ITALIC_MARKER, pos);
wxString::size_type startUnderline = formattedMessage.find(UNDERLINE_MARKER, pos);
wxString::size_type startLinkText = formattedMessage.find(START_LINK_TEXT_MARKER, pos);
wxString::size_type startCode = formattedMessage.find(CODE_MARKER, pos);
wxString::size_type startStrikethrough = formattedMessage.find(STRIKETHROUGH_MARKER, pos);
wxString::size_type startTable = formattedMessage.find(TABLE_MARKER, pos);
wxString::size_type startHeadings[] = {
formattedMessage.find(pos == 0 ? HEADING_MARKERS[0] : ("\n" + HEADING_MARKERS[0]), pos),
formattedMessage.find(pos == 0 ? HEADING_MARKERS[1] : ("\n" + HEADING_MARKERS[1]), pos),
formattedMessage.find(pos == 0 ? HEADING_MARKERS[2] : ("\n" + HEADING_MARKERS[2]), pos),
formattedMessage.find(pos == 0 ? HEADING_MARKERS[3] : ("\n" + HEADING_MARKERS[3]), pos),
formattedMessage.find(pos == 0 ? HEADING_MARKERS[4] : ("\n" + HEADING_MARKERS[4]), pos),
formattedMessage.find(pos == 0 ? HEADING_MARKERS[5] : ("\n" + HEADING_MARKERS[5]), pos)
};
wxString::size_type startBlockquote = formattedMessage.find(pos == 0 ? BLOCKQUOTE_MARKER : ("\n" + BLOCKQUOTE_MARKER), pos);
std::vector<std::pair<wxString::size_type, int>> markers = {
{ startBoldItalic, 3 },
{ startBold, 2 },
{ startItalic, 1 },
{ startUnderline, 2 },
{ startCode, 1 },
{ startHeadings[5], 7 },
{ startHeadings[4], 6 },
{ startHeadings[3], 5 },
{ startHeadings[2], 4 },
{ startHeadings[1], 3 },
{ startHeadings[0], 2 },
{ startBlockquote, 2 },
{ startStrikethrough, 2 },
{ startLinkText, -1 },
{ startTable, 0 }
};
// Find the earliest marker
auto earliestIt = std::min_element(markers.begin(), markers.end(), [](const auto& lhs, const auto& rhs){
return (lhs.first != wxString::npos && (rhs.first == wxString::npos || lhs.first < rhs.first));
});
if (earliestIt->first != wxString::npos)
{
wxString::size_type markerPos = earliestIt->first;
int markerLength = earliestIt->second;
wxString::size_type endMarker;
if (startBoldItalic == markerPos)
{
endMarker = formattedMessage.find(BOLD_ITALIC_MARKER, markerPos + markerLength);
}
else if (startBold == markerPos)
{
endMarker = formattedMessage.find(BOLD_MARKER, markerPos + markerLength);
}
else if (startUnderline == markerPos)
{
endMarker = formattedMessage.find(UNDERLINE_MARKER, markerPos + markerLength);
}
else if (startItalic == markerPos)
{
endMarker = formattedMessage.find(ITALIC_MARKER, markerPos + markerLength);
}
else if (startCode == markerPos)
{
endMarker = formattedMessage.find(CODE_MARKER, markerPos + markerLength);
}
else if (startStrikethrough == markerPos)
{
endMarker = formattedMessage.find(STRIKETHROUGH_MARKER, markerPos + markerLength);
}
else if (startHeadings[0] == markerPos || startHeadings[1] == markerPos || startHeadings[2] == markerPos || startHeadings[3] == markerPos || startHeadings[4] == markerPos || startHeadings[5] == markerPos || startBlockquote == markerPos)
{
endMarker = formattedMessage.find("\n", markerPos + markerLength);
}
else if (startTable == markerPos)
{
wxString::size_type tableEnd = formattedMessage.find(TABLE_MARKER, markerPos + TABLE_MARKER.length());
if (tableEnd != wxString::npos)
{
wxString tableData = formattedMessage.substr(markerPos + TABLE_MARKER.length(), tableEnd - (markerPos + TABLE_MARKER.length()));
CreateAndInsertTable(richTextChatBox, tableData, style);
pos = tableEnd + TABLE_MARKER.length();
continue;
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
continue;
}
}
else if (startLinkText == markerPos)
{
wxString::size_type endLinkText = formattedMessage.find(END_LINK_TEXT_MARKER, markerPos);
wxString::size_type startLinkUrl = formattedMessage.find(START_LINK_URL_MARKER, endLinkText);
wxString::size_type endLinkUrl = formattedMessage.find(END_LINK_URL_MARKER, startLinkUrl);
if (endLinkText != wxString::npos && startLinkUrl != wxString::npos && endLinkUrl != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
wxString linkText = formattedMessage.substr(markerPos + 1, endLinkText - markerPos - 1);
wxString linkUrl = formattedMessage.substr(startLinkUrl + 1, endLinkUrl - startLinkUrl - 1);
if (linkUrl.StartsWith("http://") || linkUrl.StartsWith("https://"))
{
richTextChatBox->WriteText(beforeMarker);
wxRichTextAttr urlStyle;
urlStyle.SetTextColour(*wxBLUE);
urlStyle.SetFontUnderlined(true);
richTextChatBox->BeginStyle(urlStyle);
richTextChatBox->BeginURL(linkUrl);
richTextChatBox->WriteText(linkText);
richTextChatBox->EndURL();
richTextChatBox->EndStyle();
pos = endLinkUrl + 1;
continue;
}
else
{
wxString invalidLinkText = formattedMessage.substr(markerPos, endLinkUrl - markerPos + 1);
richTextChatBox->WriteText(beforeMarker + invalidLinkText);
pos = endLinkUrl + 1;
continue;
}
}
}
if (endMarker != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
richTextChatBox->WriteText(beforeMarker);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
if (startBoldItalic == markerPos)
{
wxRichTextAttr textAttr = style;
textAttr.SetFontWeight(wxFONTWEIGHT_BOLD);
textAttr.SetFontStyle(wxFONTSTYLE_ITALIC);
richTextChatBox->BeginStyle(textAttr);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
else if (startBold == markerPos)
{
wxRichTextAttr textAttr = style;
textAttr.SetFontWeight(wxFONTWEIGHT_BOLD);
richTextChatBox->BeginStyle(textAttr);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
else if (startItalic == markerPos)
{
wxRichTextAttr textAttr = style;
textAttr.SetFontStyle(wxFONTSTYLE_ITALIC);
richTextChatBox->BeginStyle(textAttr);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
else if (startUnderline == markerPos)
{
wxRichTextAttr textAttr = style;
textAttr.SetFontUnderlined(true);
richTextChatBox->BeginStyle(textAttr);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
else if (startCode == markerPos)
{
wxRichTextAttr textAttr = style;
textAttr.SetFontWeight(wxFONTWEIGHT_MEDIUM);
textAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(60));
textAttr.SetTextColour(style.GetTextColour().ChangeLightness(140));
richTextChatBox->BeginStyle(textAttr);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
else if (startStrikethrough == markerPos)
{
wxRichTextAttr textAttr = style;
wxFont font = richTextChatBox->GetFont();
font.MakeStrikethrough();
textAttr.SetFont(font);
richTextChatBox->BeginStyle(textAttr);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
else if (startBlockquote == markerPos)
{
wxTextAttr textAttr;
textAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(50));
textAttr.SetTextColour(wxColour(220, 220, 220));
richTextChatBox->BeginStyle(textAttr);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n')
{
richTextChatBox->WriteText("\n");
}
}
else if (startHeadings[0] == markerPos || startHeadings[1] == markerPos || startHeadings[2] == markerPos || startHeadings[3] == markerPos || startHeadings[4] == markerPos || startHeadings[5] == markerPos) // Headings
{
wxFont font = richTextChatBox->GetFont();
int pointSize = font.GetPointSize();
if (startHeadings[0] == markerPos)
{
pointSize *= 2;
}
else if (startHeadings[1] == markerPos)
{
pointSize *= 1.7;
}
else if (startHeadings[2] == markerPos)
{
pointSize *= 1.5;
}
else if (startHeadings[3] == markerPos)
{
pointSize *= 1.3;
}
else if (startHeadings[4] == markerPos)
{
pointSize *= 1.2;
}
else if (startHeadings[5] == markerPos)
{
pointSize *= 1.1;
}
richTextChatBox->WriteText("\n");
wxRichTextAttr textAttr = style;
font.SetPointSize(pointSize);
textAttr.SetFont(font);
richTextChatBox->BeginStyle(textAttr);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
richTextChatBox->WriteText("\n");
}
if (startHeadings[0] == markerPos || startHeadings[1] == markerPos || startHeadings[2] == markerPos || startHeadings[3] == markerPos || startHeadings[4] == markerPos || startHeadings[5] == markerPos)
{
pos = endMarker + 1;
}
else
{
pos = endMarker + markerLength;
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
richTextChatBox->EndStyle();
}