diff --git a/Applications/Help/main.cpp b/Applications/Help/main.cpp index 3455d9e7d9..ffe41c3e69 100644 --- a/Applications/Help/main.cpp +++ b/Applications/Help/main.cpp @@ -128,11 +128,10 @@ int main(int argc, char* argv[]) auto buffer = file->read_all(); StringView source { (const char*)buffer.data(), buffer.size() }; - Markdown::Document md_document; - bool success = md_document.parse(source); - ASSERT(success); + auto md_document = Markdown::Document::parse(source); + ASSERT(md_document); - String html = md_document.render_to_html(); + String html = md_document->render_to_html(); auto html_document = Web::parse_html_document(html); page_view.set_document(html_document); diff --git a/Applications/TextEditor/TextEditorWidget.cpp b/Applications/TextEditor/TextEditorWidget.cpp index 6884335e2c..2f550507c2 100644 --- a/Applications/TextEditor/TextEditorWidget.cpp +++ b/Applications/TextEditor/TextEditorWidget.cpp @@ -560,9 +560,9 @@ void TextEditorWidget::set_markdown_preview_enabled(bool enabled) void TextEditorWidget::update_markdown_preview() { - Markdown::Document document; - if (document.parse(m_editor->text())) { - auto html = document.render_to_html(); + auto document = Markdown::Document::parse(m_editor->text()); + if (document) { + auto html = document->render_to_html(); auto html_document = Web::parse_html_document(html, URL::create_with_file_protocol(m_path)); m_page_view->set_document(html_document); } diff --git a/DevTools/HackStudio/Editor.cpp b/DevTools/HackStudio/Editor.cpp index 6383cb90cd..697d811ceb 100644 --- a/DevTools/HackStudio/Editor.cpp +++ b/DevTools/HackStudio/Editor.cpp @@ -169,15 +169,14 @@ void Editor::show_documentation_tooltip_if_available(const String& hovered_token return; } - Markdown::Document man_document; - bool success = man_document.parse(file->read_all()); + auto man_document = Markdown::Document::parse(file->read_all()); - if (!success) { + if (!man_document) { dbg() << "failed to parse markdown"; 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); if (!html_document) { diff --git a/Libraries/LibMarkdown/Document.cpp b/Libraries/LibMarkdown/Document.cpp index 408bcf1435..841b5a5ef6 100644 --- a/Libraries/LibMarkdown/Document.cpp +++ b/Libraries/LibMarkdown/Document.cpp @@ -75,25 +75,29 @@ static bool helper(Vector::ConstIterator& lines, NonnullOwnPtrVector return true; } -bool Document::parse(const StringView& str) +RefPtr Document::parse(const StringView& str) { const Vector lines_vec = str.lines(); auto lines = lines_vec.begin(); + auto document = adopt(*new Document); + auto& blocks = document->m_blocks; while (true) { if (lines.is_end()) - return true; + break; if ((*lines).is_empty()) { ++lines; continue; } - bool any = helper(lines, m_blocks) || helper(lines, m_blocks) || helper(lines, m_blocks) || helper(lines, m_blocks); + bool any = helper(lines, blocks) || helper(lines, blocks) || helper(lines, blocks) || helper(lines, blocks); if (!any) - return false; + return nullptr; } + + return document; } } diff --git a/Libraries/LibMarkdown/Document.h b/Libraries/LibMarkdown/Document.h index 3cf4c769cb..58733db452 100644 --- a/Libraries/LibMarkdown/Document.h +++ b/Libraries/LibMarkdown/Document.h @@ -32,12 +32,12 @@ namespace Markdown { -class Document final { +class Document final : public RefCounted { public: String render_to_html() const; String render_for_terminal() const; - bool parse(const StringView&); + static RefPtr parse(const StringView&); private: NonnullOwnPtrVector m_blocks; diff --git a/Libraries/LibWeb/PageView.cpp b/Libraries/LibWeb/PageView.cpp index e5b1398166..88304be1b5 100644 --- a/Libraries/LibWeb/PageView.cpp +++ b/Libraries/LibWeb/PageView.cpp @@ -332,11 +332,11 @@ void PageView::reload() static RefPtr create_markdown_document(const ByteBuffer& data, const URL& url) { - Markdown::Document markdown_document; - if (!markdown_document.parse(data)) + auto markdown_document = Markdown::Document::parse(data); + if (!markdown_document) return nullptr; - return parse_html_document(markdown_document.render_to_html(), url); + return parse_html_document(markdown_document->render_to_html(), url); } static RefPtr create_text_document(const ByteBuffer& data, const URL& url) diff --git a/Userland/man.cpp b/Userland/man.cpp index 36900a984f..feb4887aa5 100644 --- a/Userland/man.cpp +++ b/Userland/man.cpp @@ -101,10 +101,9 @@ int main(int argc, char* argv[]) printf("%s(%s)\t\tSerenityOS manual\n", name, section); - Markdown::Document document; - bool success = document.parse(source); - ASSERT(success); + auto document = Markdown::Document::parse(source); + ASSERT(document); - String rendered = document.render_for_terminal(); + String rendered = document->render_for_terminal(); printf("%s", rendered.characters()); } diff --git a/Userland/md.cpp b/Userland/md.cpp index 17187fc06e..45a6041854 100644 --- a/Userland/md.cpp +++ b/Userland/md.cpp @@ -70,14 +70,13 @@ int main(int argc, char* argv[]) dbg() << "Read size " << buffer.size(); auto input = String::copy(buffer); - Markdown::Document document; - success = document.parse(input); + auto document = Markdown::Document::parse(input); - if (!success) { + if (!document) { fprintf(stderr, "Error parsing\n"); 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()); }