Skip to content

Commit 7ca562b

Browse files
FalseHonestyawesomekling
authored andcommitted
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.
1 parent 9a21774 commit 7ca562b

File tree

8 files changed

+28
-28
lines changed

8 files changed

+28
-28
lines changed

Applications/Help/main.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,10 @@ int main(int argc, char* argv[])
128128
auto buffer = file->read_all();
129129
StringView source { (const char*)buffer.data(), buffer.size() };
130130

131-
Markdown::Document md_document;
132-
bool success = md_document.parse(source);
133-
ASSERT(success);
131+
auto md_document = Markdown::Document::parse(source);
132+
ASSERT(md_document);
134133

135-
String html = md_document.render_to_html();
134+
String html = md_document->render_to_html();
136135
auto html_document = Web::parse_html_document(html);
137136
page_view.set_document(html_document);
138137

Applications/TextEditor/TextEditorWidget.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,9 +560,9 @@ void TextEditorWidget::set_markdown_preview_enabled(bool enabled)
560560

561561
void TextEditorWidget::update_markdown_preview()
562562
{
563-
Markdown::Document document;
564-
if (document.parse(m_editor->text())) {
565-
auto html = document.render_to_html();
563+
auto document = Markdown::Document::parse(m_editor->text());
564+
if (document) {
565+
auto html = document->render_to_html();
566566
auto html_document = Web::parse_html_document(html, URL::create_with_file_protocol(m_path));
567567
m_page_view->set_document(html_document);
568568
}

DevTools/HackStudio/Editor.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,14 @@ void Editor::show_documentation_tooltip_if_available(const String& hovered_token
169169
return;
170170
}
171171

172-
Markdown::Document man_document;
173-
bool success = man_document.parse(file->read_all());
172+
auto man_document = Markdown::Document::parse(file->read_all());
174173

175-
if (!success) {
174+
if (!man_document) {
176175
dbg() << "failed to parse markdown";
177176
return;
178177
}
179178

180-
auto html_text = man_document.render_to_html();
179+
auto html_text = man_document->render_to_html();
181180

182181
auto html_document = Web::parse_html_document(html_text);
183182
if (!html_document) {

Libraries/LibMarkdown/Document.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,29 @@ static bool helper(Vector<StringView>::ConstIterator& lines, NonnullOwnPtrVector
7575
return true;
7676
}
7777

78-
bool Document::parse(const StringView& str)
78+
RefPtr<Document> Document::parse(const StringView& str)
7979
{
8080
const Vector<StringView> lines_vec = str.lines();
8181
auto lines = lines_vec.begin();
82+
auto document = adopt(*new Document);
83+
auto& blocks = document->m_blocks;
8284

8385
while (true) {
8486
if (lines.is_end())
85-
return true;
87+
break;
8688

8789
if ((*lines).is_empty()) {
8890
++lines;
8991
continue;
9092
}
9193

92-
bool any = helper<List>(lines, m_blocks) || helper<Paragraph>(lines, m_blocks) || helper<CodeBlock>(lines, m_blocks) || helper<Heading>(lines, m_blocks);
94+
bool any = helper<List>(lines, blocks) || helper<Paragraph>(lines, blocks) || helper<CodeBlock>(lines, blocks) || helper<Heading>(lines, blocks);
9395

9496
if (!any)
95-
return false;
97+
return nullptr;
9698
}
99+
100+
return document;
97101
}
98102

99103
}

Libraries/LibMarkdown/Document.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232

3333
namespace Markdown {
3434

35-
class Document final {
35+
class Document final : public RefCounted<Document> {
3636
public:
3737
String render_to_html() const;
3838
String render_for_terminal() const;
3939

40-
bool parse(const StringView&);
40+
static RefPtr<Document> parse(const StringView&);
4141

4242
private:
4343
NonnullOwnPtrVector<Block> m_blocks;

Libraries/LibWeb/PageView.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,11 @@ void PageView::reload()
332332

333333
static RefPtr<Document> create_markdown_document(const ByteBuffer& data, const URL& url)
334334
{
335-
Markdown::Document markdown_document;
336-
if (!markdown_document.parse(data))
335+
auto markdown_document = Markdown::Document::parse(data);
336+
if (!markdown_document)
337337
return nullptr;
338338

339-
return parse_html_document(markdown_document.render_to_html(), url);
339+
return parse_html_document(markdown_document->render_to_html(), url);
340340
}
341341

342342
static RefPtr<Document> create_text_document(const ByteBuffer& data, const URL& url)

Userland/man.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,9 @@ int main(int argc, char* argv[])
101101

102102
printf("%s(%s)\t\tSerenityOS manual\n", name, section);
103103

104-
Markdown::Document document;
105-
bool success = document.parse(source);
106-
ASSERT(success);
104+
auto document = Markdown::Document::parse(source);
105+
ASSERT(document);
107106

108-
String rendered = document.render_for_terminal();
107+
String rendered = document->render_for_terminal();
109108
printf("%s", rendered.characters());
110109
}

Userland/md.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,13 @@ int main(int argc, char* argv[])
7070
dbg() << "Read size " << buffer.size();
7171

7272
auto input = String::copy(buffer);
73-
Markdown::Document document;
74-
success = document.parse(input);
73+
auto document = Markdown::Document::parse(input);
7574

76-
if (!success) {
75+
if (!document) {
7776
fprintf(stderr, "Error parsing\n");
7877
return 1;
7978
}
8079

81-
String res = html ? document.render_to_html() : document.render_for_terminal();
80+
String res = html ? document->render_to_html() : document->render_for_terminal();
8281
printf("%s", res.characters());
8382
}

0 commit comments

Comments
 (0)