Bläddra i källkod

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.
FalseHonesty 5 år sedan
förälder
incheckning
7ca562b200

+ 3 - 4
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);
 

+ 3 - 3
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);
     }

+ 3 - 4
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) {

+ 8 - 4
Libraries/LibMarkdown/Document.cpp

@@ -75,25 +75,29 @@ static bool helper(Vector<StringView>::ConstIterator& lines, NonnullOwnPtrVector
     return true;
 }
 
-bool Document::parse(const StringView& str)
+RefPtr<Document> Document::parse(const StringView& str)
 {
     const Vector<StringView> 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<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)
-            return false;
+            return nullptr;
     }
+
+    return document;
 }
 
 }

+ 2 - 2
Libraries/LibMarkdown/Document.h

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

+ 3 - 3
Libraries/LibWeb/PageView.cpp

@@ -332,11 +332,11 @@ void PageView::reload()
 
 static RefPtr<Document> 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<Document> create_text_document(const ByteBuffer& data, const URL& url)

+ 3 - 4
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());
 }

+ 3 - 4
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());
 }