1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 13:44:57 +00:00

LibMarkdown: Change MD Document parse API to return a RefPtr

Markdown documents are now obtained via the static Document::parse
method, which returns a RefPtr<Document>, or nullptr on failure.
This commit is contained in:
FalseHonesty 2020-05-11 13:55:31 -04:00 committed by Andreas Kling
parent 9a2177437b
commit 7ca562b200
8 changed files with 28 additions and 28 deletions

View file

@ -128,11 +128,10 @@ int main(int argc, char* argv[])
auto buffer = file->read_all(); auto buffer = file->read_all();
StringView source { (const char*)buffer.data(), buffer.size() }; StringView source { (const char*)buffer.data(), buffer.size() };
Markdown::Document md_document; auto md_document = Markdown::Document::parse(source);
bool success = md_document.parse(source); ASSERT(md_document);
ASSERT(success);
String html = md_document.render_to_html(); String html = md_document->render_to_html();
auto html_document = Web::parse_html_document(html); auto html_document = Web::parse_html_document(html);
page_view.set_document(html_document); page_view.set_document(html_document);

View file

@ -560,9 +560,9 @@ void TextEditorWidget::set_markdown_preview_enabled(bool enabled)
void TextEditorWidget::update_markdown_preview() void TextEditorWidget::update_markdown_preview()
{ {
Markdown::Document document; auto document = Markdown::Document::parse(m_editor->text());
if (document.parse(m_editor->text())) { if (document) {
auto html = document.render_to_html(); auto html = document->render_to_html();
auto html_document = Web::parse_html_document(html, URL::create_with_file_protocol(m_path)); auto html_document = Web::parse_html_document(html, URL::create_with_file_protocol(m_path));
m_page_view->set_document(html_document); m_page_view->set_document(html_document);
} }

View file

@ -169,15 +169,14 @@ void Editor::show_documentation_tooltip_if_available(const String& hovered_token
return; return;
} }
Markdown::Document man_document; auto man_document = Markdown::Document::parse(file->read_all());
bool success = man_document.parse(file->read_all());
if (!success) { if (!man_document) {
dbg() << "failed to parse markdown"; dbg() << "failed to parse markdown";
return; return;
} }
auto html_text = man_document.render_to_html(); auto html_text = man_document->render_to_html();
auto html_document = Web::parse_html_document(html_text); auto html_document = Web::parse_html_document(html_text);
if (!html_document) { if (!html_document) {

View file

@ -75,25 +75,29 @@ static bool helper(Vector<StringView>::ConstIterator& lines, NonnullOwnPtrVector
return true; return true;
} }
bool Document::parse(const StringView& str) RefPtr<Document> Document::parse(const StringView& str)
{ {
const Vector<StringView> lines_vec = str.lines(); const Vector<StringView> lines_vec = str.lines();
auto lines = lines_vec.begin(); auto lines = lines_vec.begin();
auto document = adopt(*new Document);
auto& blocks = document->m_blocks;
while (true) { while (true) {
if (lines.is_end()) if (lines.is_end())
return true; break;
if ((*lines).is_empty()) { if ((*lines).is_empty()) {
++lines; ++lines;
continue; continue;
} }
bool any = helper<List>(lines, m_blocks) || helper<Paragraph>(lines, m_blocks) || helper<CodeBlock>(lines, m_blocks) || helper<Heading>(lines, m_blocks); bool any = helper<List>(lines, blocks) || helper<Paragraph>(lines, blocks) || helper<CodeBlock>(lines, blocks) || helper<Heading>(lines, blocks);
if (!any) if (!any)
return false; return nullptr;
} }
return document;
} }
} }

View file

@ -32,12 +32,12 @@
namespace Markdown { namespace Markdown {
class Document final { class Document final : public RefCounted<Document> {
public: public:
String render_to_html() const; String render_to_html() const;
String render_for_terminal() const; String render_for_terminal() const;
bool parse(const StringView&); static RefPtr<Document> parse(const StringView&);
private: private:
NonnullOwnPtrVector<Block> m_blocks; NonnullOwnPtrVector<Block> m_blocks;

View file

@ -332,11 +332,11 @@ void PageView::reload()
static RefPtr<Document> create_markdown_document(const ByteBuffer& data, const URL& url) static RefPtr<Document> create_markdown_document(const ByteBuffer& data, const URL& url)
{ {
Markdown::Document markdown_document; auto markdown_document = Markdown::Document::parse(data);
if (!markdown_document.parse(data)) if (!markdown_document)
return nullptr; return nullptr;
return parse_html_document(markdown_document.render_to_html(), url); return parse_html_document(markdown_document->render_to_html(), url);
} }
static RefPtr<Document> create_text_document(const ByteBuffer& data, const URL& url) static RefPtr<Document> create_text_document(const ByteBuffer& data, const URL& url)

View file

@ -101,10 +101,9 @@ int main(int argc, char* argv[])
printf("%s(%s)\t\tSerenityOS manual\n", name, section); printf("%s(%s)\t\tSerenityOS manual\n", name, section);
Markdown::Document document; auto document = Markdown::Document::parse(source);
bool success = document.parse(source); ASSERT(document);
ASSERT(success);
String rendered = document.render_for_terminal(); String rendered = document->render_for_terminal();
printf("%s", rendered.characters()); printf("%s", rendered.characters());
} }

View file

@ -70,14 +70,13 @@ int main(int argc, char* argv[])
dbg() << "Read size " << buffer.size(); dbg() << "Read size " << buffer.size();
auto input = String::copy(buffer); auto input = String::copy(buffer);
Markdown::Document document; auto document = Markdown::Document::parse(input);
success = document.parse(input);
if (!success) { if (!document) {
fprintf(stderr, "Error parsing\n"); fprintf(stderr, "Error parsing\n");
return 1; return 1;
} }
String res = html ? document.render_to_html() : document.render_for_terminal(); String res = html ? document->render_to_html() : document->render_for_terminal();
printf("%s", res.characters()); printf("%s", res.characters());
} }