m_element = NULL;
m_paragraph = NULL;
+ m_end_paragraph_reason = ER_NONE;
parse(text);
}
cursor++;
// If text has begun, don't skip empty line
if (m_paragraph) {
- endParagraph();
+ endParagraph(ER_NEWLINE);
enterElement(ELEMENT_SEPARATOR);
}
escape = false;
if (c == L'\n') { // Unix breaks
// If text has begun, don't skip empty line
if (m_paragraph) {
- endParagraph();
+ endParagraph(ER_NEWLINE);
enterElement(ELEMENT_SEPARATOR);
}
escape = false;
pushChar(c);
}
- endParagraph();
+ endParagraph(ER_NONE);
}
void ParsedText::endElement()
m_element = NULL;
}
-void ParsedText::endParagraph()
+void ParsedText::endParagraph(EndReason reason)
{
if (!m_paragraph)
return;
+ EndReason previous = m_end_paragraph_reason;
+ m_end_paragraph_reason = reason;
+ if (m_empty_paragraph && (reason == ER_TAG ||
+ (reason == ER_NEWLINE && previous == ER_TAG))) {
+ // Ignore last empty paragraph
+ m_paragraph = nullptr;
+ m_paragraphs.pop_back();
+ return;
+ }
endElement();
m_paragraph = NULL;
}
m_paragraphs.emplace_back();
m_paragraph = &m_paragraphs.back();
m_paragraph->setStyle(m_style);
+ m_empty_paragraph = true;
}
}
void ParsedText::pushChar(wchar_t c)
{
// New word if needed
- if (c == L' ' || c == L'\t')
- enterElement(ELEMENT_SEPARATOR);
- else
+ if (c == L' ' || c == L'\t') {
+ if (!m_empty_paragraph)
+ enterElement(ELEMENT_SEPARATOR);
+ else
+ return;
+ } else {
+ m_empty_paragraph = false;
enterElement(ELEMENT_TEXT);
-
+ }
m_element->text += c;
}
} else {
openTag(name, attrs)->style = m_paragraphtags[name];
}
- endParagraph();
+ endParagraph(ER_TAG);
} else
return 0; // Unknown tag
Tag m_root_tag;
protected:
+ typedef enum { ER_NONE, ER_TAG, ER_NEWLINE } EndReason;
+
// Parser functions
void enterElement(ElementType type);
void endElement();
void enterParagraph();
- void endParagraph();
+ void endParagraph(EndReason reason);
void pushChar(wchar_t c);
ParsedText::Tag *newTag(const std::string &name, const AttrsList &attrs);
ParsedText::Tag *openTag(const std::string &name, const AttrsList &attrs);
StyleList m_style;
Element *m_element;
Paragraph *m_paragraph;
+ bool m_empty_paragraph;
+ EndReason m_end_paragraph_reason;
};
class TextDrawer