LibMarkdown: Drop MD prefixes and move into "Markdown" namespace :^)

This commit is contained in:
Andreas Kling 2020-04-28 21:04:25 +02:00
parent 104969a9f5
commit ea204ef05b
Notes: sideshowbarker 2024-07-19 07:12:45 +09:00
18 changed files with 131 additions and 78 deletions

View File

@ -41,7 +41,7 @@
#include <LibGUI/ToolBarContainer.h>
#include <LibGUI/TreeView.h>
#include <LibGUI/Window.h>
#include <LibMarkdown/MDDocument.h>
#include <LibMarkdown/Document.h>
#include <LibWeb/HtmlView.h>
#include <LibWeb/Layout/LayoutNode.h>
#include <LibWeb/Parser/CSSParser.h>
@ -128,7 +128,7 @@ int main(int argc, char* argv[])
auto buffer = file->read_all();
StringView source { (const char*)buffer.data(), buffer.size() };
MDDocument md_document;
Markdown::Document md_document;
bool success = md_document.parse(source);
ASSERT(success);

View File

@ -36,7 +36,7 @@
#include <LibGUI/ScrollBar.h>
#include <LibGUI/SyntaxHighlighter.h>
#include <LibGUI/Window.h>
#include <LibMarkdown/MDDocument.h>
#include <LibMarkdown/Document.h>
#include <LibWeb/DOM/ElementFactory.h>
#include <LibWeb/DOM/HTMLHeadElement.h>
#include <LibWeb/DOM/Text.h>
@ -166,7 +166,7 @@ void Editor::show_documentation_tooltip_if_available(const String& hovered_token
return;
}
MDDocument man_document;
Markdown::Document man_document;
bool success = man_document.parse(file->read_all());
if (!success) {

View File

@ -26,13 +26,18 @@
#pragma once
#include <AK/StringView.h>
#include <AK/Vector.h>
class MDBlock {
namespace Markdown {
class Block {
public:
virtual ~MDBlock() {}
virtual ~Block() {}
virtual String render_to_html() const = 0;
virtual String render_for_terminal() const = 0;
virtual bool parse(Vector<StringView>::ConstIterator& lines) = 0;
};
}

View File

@ -25,28 +25,30 @@
*/
#include <AK/StringBuilder.h>
#include <LibMarkdown/MDCodeBlock.h>
#include <LibMarkdown/CodeBlock.h>
MDText::Style MDCodeBlock::style() const
namespace Markdown {
Text::Style CodeBlock::style() const
{
if (m_style_spec.spans().is_empty())
return {};
return m_style_spec.spans()[0].style;
}
String MDCodeBlock::style_language() const
String CodeBlock::style_language() const
{
if (m_style_spec.spans().is_empty())
return {};
return m_style_spec.spans()[0].text;
}
String MDCodeBlock::render_to_html() const
String CodeBlock::render_to_html() const
{
StringBuilder builder;
String style_language = this->style_language();
MDText::Style style = this->style();
Text::Style style = this->style();
if (style.strong)
builder.append("<b>");
@ -81,11 +83,11 @@ String MDCodeBlock::render_to_html() const
return builder.build();
}
String MDCodeBlock::render_for_terminal() const
String CodeBlock::render_for_terminal() const
{
StringBuilder builder;
MDText::Style style = this->style();
Text::Style style = this->style();
bool needs_styling = style.strong || style.emph;
if (needs_styling) {
builder.append("\033[");
@ -112,7 +114,7 @@ String MDCodeBlock::render_for_terminal() const
return builder.build();
}
bool MDCodeBlock::parse(Vector<StringView>::ConstIterator& lines)
bool CodeBlock::parse(Vector<StringView>::ConstIterator& lines)
{
if (lines.is_end())
return false;
@ -159,3 +161,5 @@ bool MDCodeBlock::parse(Vector<StringView>::ConstIterator& lines)
m_code = builder.build();
return true;
}
}

View File

@ -26,12 +26,14 @@
#pragma once
#include <LibMarkdown/MDBlock.h>
#include <LibMarkdown/MDText.h>
#include <LibMarkdown/Block.h>
#include <LibMarkdown/Text.h>
class MDCodeBlock final : public MDBlock {
namespace Markdown {
class CodeBlock final : public Block {
public:
virtual ~MDCodeBlock() override {}
virtual ~CodeBlock() override {}
virtual String render_to_html() const override;
virtual String render_for_terminal() const override;
@ -39,8 +41,10 @@ public:
private:
String style_language() const;
MDText::Style style() const;
Text::Style style() const;
String m_code;
MDText m_style_spec;
Text m_style_spec;
};
}

View File

@ -25,13 +25,15 @@
*/
#include <AK/StringBuilder.h>
#include <LibMarkdown/MDCodeBlock.h>
#include <LibMarkdown/MDDocument.h>
#include <LibMarkdown/MDHeading.h>
#include <LibMarkdown/MDList.h>
#include <LibMarkdown/MDParagraph.h>
#include <LibMarkdown/CodeBlock.h>
#include <LibMarkdown/Document.h>
#include <LibMarkdown/Heading.h>
#include <LibMarkdown/List.h>
#include <LibMarkdown/Paragraph.h>
String MDDocument::render_to_html() const
namespace Markdown {
String Document::render_to_html() const
{
StringBuilder builder;
@ -50,7 +52,7 @@ String MDDocument::render_to_html() const
return builder.build();
}
String MDDocument::render_for_terminal() const
String Document::render_for_terminal() const
{
StringBuilder builder;
@ -62,10 +64,10 @@ String MDDocument::render_for_terminal() const
return builder.build();
}
template<typename Block>
static bool helper(Vector<StringView>::ConstIterator& lines, NonnullOwnPtrVector<MDBlock>& blocks)
template<typename BlockType>
static bool helper(Vector<StringView>::ConstIterator& lines, NonnullOwnPtrVector<Block>& blocks)
{
NonnullOwnPtr<Block> block = make<Block>();
NonnullOwnPtr<BlockType> block = make<BlockType>();
bool success = block->parse(lines);
if (!success)
return false;
@ -73,7 +75,7 @@ static bool helper(Vector<StringView>::ConstIterator& lines, NonnullOwnPtrVector
return true;
}
bool MDDocument::parse(const StringView& str)
bool Document::parse(const StringView& str)
{
const Vector<StringView> lines_vec = str.lines();
auto lines = lines_vec.begin();
@ -87,9 +89,11 @@ bool MDDocument::parse(const StringView& str)
continue;
}
bool any = helper<MDList>(lines, m_blocks) || helper<MDParagraph>(lines, m_blocks) || helper<MDCodeBlock>(lines, m_blocks) || helper<MDHeading>(lines, m_blocks);
bool any = helper<List>(lines, m_blocks) || helper<Paragraph>(lines, m_blocks) || helper<CodeBlock>(lines, m_blocks) || helper<Heading>(lines, m_blocks);
if (!any)
return false;
}
}
}

View File

@ -28,9 +28,11 @@
#include <AK/NonnullOwnPtrVector.h>
#include <AK/String.h>
#include <LibMarkdown/MDBlock.h>
#include <LibMarkdown/Block.h>
class MDDocument final {
namespace Markdown {
class Document final {
public:
String render_to_html() const;
String render_for_terminal() const;
@ -38,5 +40,7 @@ public:
bool parse(const StringView&);
private:
NonnullOwnPtrVector<MDBlock> m_blocks;
NonnullOwnPtrVector<Block> m_blocks;
};
}

View File

@ -25,9 +25,11 @@
*/
#include <AK/StringBuilder.h>
#include <LibMarkdown/MDHeading.h>
#include <LibMarkdown/Heading.h>
String MDHeading::render_to_html() const
namespace Markdown {
String Heading::render_to_html() const
{
StringBuilder builder;
builder.appendf("<h%d>", m_level);
@ -36,7 +38,7 @@ String MDHeading::render_to_html() const
return builder.build();
}
String MDHeading::render_for_terminal() const
String Heading::render_for_terminal() const
{
StringBuilder builder;
@ -57,7 +59,7 @@ String MDHeading::render_for_terminal() const
return builder.build();
}
bool MDHeading::parse(Vector<StringView>::ConstIterator& lines)
bool Heading::parse(Vector<StringView>::ConstIterator& lines)
{
if (lines.is_end())
return false;
@ -78,3 +80,5 @@ bool MDHeading::parse(Vector<StringView>::ConstIterator& lines)
++lines;
return true;
}
}

View File

@ -28,18 +28,22 @@
#include <AK/StringView.h>
#include <AK/Vector.h>
#include <LibMarkdown/MDBlock.h>
#include <LibMarkdown/MDText.h>
#include <LibMarkdown/Block.h>
#include <LibMarkdown/Text.h>
class MDHeading final : public MDBlock {
namespace Markdown {
class Heading final : public Block {
public:
virtual ~MDHeading() override {}
virtual ~Heading() override {}
virtual String render_to_html() const override;
virtual String render_for_terminal() const override;
virtual bool parse(Vector<StringView>::ConstIterator& lines) override;
private:
MDText m_text;
Text m_text;
int m_level { -1 };
};
}

View File

@ -25,9 +25,11 @@
*/
#include <AK/StringBuilder.h>
#include <LibMarkdown/MDList.h>
#include <LibMarkdown/List.h>
String MDList::render_to_html() const
namespace Markdown {
String List::render_to_html() const
{
StringBuilder builder;
@ -45,7 +47,7 @@ String MDList::render_to_html() const
return builder.build();
}
String MDList::render_for_terminal() const
String List::render_for_terminal() const
{
StringBuilder builder;
@ -64,7 +66,7 @@ String MDList::render_for_terminal() const
return builder.build();
}
bool MDList::parse(Vector<StringView>::ConstIterator& lines)
bool List::parse(Vector<StringView>::ConstIterator& lines)
{
bool first = true;
while (true) {
@ -104,7 +106,7 @@ bool MDList::parse(Vector<StringView>::ConstIterator& lines)
return false;
first = false;
MDText text;
Text text;
bool success = text.parse(line.substring_view(offset, line.length() - offset));
ASSERT(success);
m_items.append(move(text));
@ -113,3 +115,5 @@ bool MDList::parse(Vector<StringView>::ConstIterator& lines)
return !first;
}
}

View File

@ -27,12 +27,14 @@
#pragma once
#include <AK/Vector.h>
#include <LibMarkdown/MDBlock.h>
#include <LibMarkdown/MDText.h>
#include <LibMarkdown/Block.h>
#include <LibMarkdown/Text.h>
class MDList final : public MDBlock {
namespace Markdown {
class List final : public Block {
public:
virtual ~MDList() override {}
virtual ~List() override {}
virtual String render_to_html() const override;
virtual String render_for_terminal() const override;
@ -40,6 +42,8 @@ public:
private:
// TODO: List items should be considered blocks of their own kind.
Vector<MDText> m_items;
Vector<Text> m_items;
bool m_is_ordered { false };
};
}

View File

@ -1,10 +1,10 @@
OBJS = \
MDDocument.o \
MDParagraph.o \
MDHeading.o \
MDCodeBlock.o \
MDList.o \
MDText.o
Document.o \
Paragraph.o \
Heading.o \
CodeBlock.o \
List.o \
Text.o
LIBRARY = libmarkdown.a

View File

@ -25,9 +25,11 @@
*/
#include <AK/StringBuilder.h>
#include <LibMarkdown/MDParagraph.h>
#include <LibMarkdown/Paragraph.h>
String MDParagraph::render_to_html() const
namespace Markdown {
String Paragraph::render_to_html() const
{
StringBuilder builder;
builder.appendf("<p>");
@ -36,7 +38,7 @@ String MDParagraph::render_to_html() const
return builder.build();
}
String MDParagraph::render_for_terminal() const
String Paragraph::render_for_terminal() const
{
StringBuilder builder;
builder.append(m_text.render_for_terminal());
@ -44,7 +46,7 @@ String MDParagraph::render_for_terminal() const
return builder.build();
}
bool MDParagraph::parse(Vector<StringView>::ConstIterator& lines)
bool Paragraph::parse(Vector<StringView>::ConstIterator& lines)
{
if (lines.is_end())
return false;
@ -90,3 +92,5 @@ bool MDParagraph::parse(Vector<StringView>::ConstIterator& lines)
ASSERT(success);
return true;
}
}

View File

@ -26,17 +26,21 @@
#pragma once
#include <LibMarkdown/MDBlock.h>
#include <LibMarkdown/MDText.h>
#include <LibMarkdown/Block.h>
#include <LibMarkdown/Text.h>
class MDParagraph final : public MDBlock {
namespace Markdown {
class Paragraph final : public Block {
public:
virtual ~MDParagraph() override {}
virtual ~Paragraph() override {}
virtual String render_to_html() const override;
virtual String render_for_terminal() const override;
virtual bool parse(Vector<StringView>::ConstIterator& lines) override;
private:
MDText m_text;
Text m_text;
};
}

View File

@ -25,9 +25,11 @@
*/
#include <AK/StringBuilder.h>
#include <LibMarkdown/MDText.h>
#include <LibMarkdown/Text.h>
#include <string.h>
namespace Markdown {
static String unescape(const StringView& text)
{
StringBuilder builder;
@ -42,7 +44,7 @@ static String unescape(const StringView& text)
return builder.build();
}
String MDText::render_to_html() const
String Text::render_to_html() const
{
StringBuilder builder;
@ -110,7 +112,7 @@ String MDText::render_to_html() const
return builder.build();
}
String MDText::render_for_terminal() const
String Text::render_for_terminal() const
{
StringBuilder builder;
@ -149,7 +151,7 @@ String MDText::render_for_terminal() const
return builder.build();
}
bool MDText::parse(const StringView& str)
bool Text::parse(const StringView& str)
{
Style current_style;
size_t current_span_start = 0;
@ -229,3 +231,5 @@ bool MDText::parse(const StringView& str)
return true;
}
}

View File

@ -29,7 +29,9 @@
#include <AK/String.h>
#include <AK/Vector.h>
class MDText final {
namespace Markdown {
class Text final {
public:
struct Style {
bool emph { false };
@ -53,3 +55,5 @@ public:
private:
Vector<Span> m_spans;
};
}

View File

@ -28,7 +28,7 @@
#include <AK/String.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibMarkdown/MDDocument.h>
#include <LibMarkdown/Document.h>
#include <stdio.h>
#include <unistd.h>
@ -101,7 +101,7 @@ int main(int argc, char* argv[])
printf("%s(%s)\t\tSerenityOS manual\n", name, section);
MDDocument document;
Markdown::Document document;
bool success = document.parse(source);
ASSERT(success);

View File

@ -27,7 +27,7 @@
#include <AK/ByteBuffer.h>
#include <AK/String.h>
#include <LibCore/File.h>
#include <LibMarkdown/MDDocument.h>
#include <LibMarkdown/Document.h>
#include <stdio.h>
#include <string.h>
@ -70,7 +70,7 @@ int main(int argc, char* argv[])
dbg() << "Read size " << buffer.size();
auto input = String::copy(buffer);
MDDocument document;
Markdown::Document document;
success = document.parse(input);
if (!success) {