OK! Perfect, I have a method to assign an style to a cell based on how it starts and it works correctly for only one marker, but doesn't support multiple markers, so the problems it has are:
- Doesn't support multiple markers concurrently, i.e. Bold Text then Italic Text.
- Doesn't support multiple markers at the same time, i.e. Underline and Italic Text.
Here is my current implementation, let's find a way to crack this problem, starting with the stage 1, and then moving to the next stage when we make sure it works correctly. Also take into account I use constants, so don't use markdown text but use the constants instead. Here is the code:
wxArrayString Markdown::FilterEmptyElements(const wxArrayString& arrayString)
{
wxArrayString filteredArrayString;
for (const wxString& s : arrayString)
{
if (!s.IsEmpty())
{
filteredArrayString.Add(wxString(s).Trim(false).Trim());
}
}
return filteredArrayString;
}
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, bool header)
{
wxRichTextAttr textAttr = baseStyle;
if (header)
{
textAttr.SetFontWeight(wxFONTWEIGHT_BOLD);
}
if (styledText.StartsWith(BOLD_ITALIC_MARKER) && styledText.EndsWith(BOLD_ITALIC_MARKER))
{
textAttr.SetFontWeight(wxFONTWEIGHT_BOLD);
textAttr.SetFontStyle(wxFONTSTYLE_ITALIC);
styledText = styledText.Mid(BOLD_ITALIC_MARKER.Length(), styledText.Length() - BOLD_ITALIC_MARKER.Length() * 2);
}
else 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);
}
else 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);
}
else if (styledText.StartsWith(UNDERLINE_MARKER) && styledText.EndsWith(UNDERLINE_MARKER))
{
textAttr.SetFontUnderlined(true);
styledText = styledText.Mid(UNDERLINE_MARKER.Length(), styledText.Length() - UNDERLINE_MARKER.Length() * 2);
}
else 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);
}
else 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);
}
else 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);
}
}
}
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, true);
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);
}
}
}