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:
parent
9a2177437b
commit
7ca562b200
Notes:
sideshowbarker
2024-07-19 05:59:02 +09:00
Author: https://github.com/FalseHonesty Commit: https://github.com/SerenityOS/serenity/commit/7ca562b200e Pull-request: https://github.com/SerenityOS/serenity/pull/2195 Reviewed-by: https://github.com/bugaevc
8 changed files with 28 additions and 28 deletions
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue