2020-05-16 14:08:13 +03:00
|
|
|
/*
|
2022-02-26 20:55:15 +03:00
|
|
|
* Copyright (c) 2020-2022, the SerenityOS developers.
|
2020-05-16 14:08:13 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-16 14:08:13 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
#include <AK/DeprecatedString.h>
|
2020-05-16 14:08:13 +03:00
|
|
|
#include <AK/Forward.h>
|
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
|
|
|
#include <AK/URL.h>
|
|
|
|
|
|
|
|
namespace Gemini {
|
|
|
|
|
|
|
|
class Line {
|
|
|
|
public:
|
2022-12-04 21:02:33 +03:00
|
|
|
Line(DeprecatedString string)
|
2020-05-16 14:08:13 +03:00
|
|
|
: m_text(move(string))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-02-26 20:55:15 +03:00
|
|
|
virtual ~Line() = default;
|
2020-05-16 14:08:13 +03:00
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
virtual DeprecatedString render_to_html() const = 0;
|
2020-05-16 14:08:13 +03:00
|
|
|
|
|
|
|
protected:
|
2022-12-04 21:02:33 +03:00
|
|
|
DeprecatedString m_text;
|
2020-05-16 14:08:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class Document : public RefCounted<Document> {
|
|
|
|
public:
|
2022-12-04 21:02:33 +03:00
|
|
|
DeprecatedString render_to_html() const;
|
2020-05-16 14:08:13 +03:00
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
static NonnullRefPtr<Document> parse(StringView source, const URL&);
|
2020-05-16 14:08:13 +03:00
|
|
|
|
|
|
|
const URL& url() const { return m_url; };
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit Document(const URL& url)
|
|
|
|
: m_url(url)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
void read_lines(StringView);
|
2020-05-16 14:08:13 +03:00
|
|
|
|
|
|
|
NonnullOwnPtrVector<Line> m_lines;
|
|
|
|
URL m_url;
|
|
|
|
bool m_inside_preformatted_block { false };
|
|
|
|
bool m_inside_unordered_list { false };
|
|
|
|
};
|
|
|
|
|
|
|
|
class Text : public Line {
|
|
|
|
public:
|
2022-12-04 21:02:33 +03:00
|
|
|
Text(DeprecatedString line)
|
2020-05-16 14:08:13 +03:00
|
|
|
: Line(move(line))
|
|
|
|
{
|
|
|
|
}
|
2022-02-26 20:55:15 +03:00
|
|
|
virtual ~Text() override = default;
|
2022-12-04 21:02:33 +03:00
|
|
|
virtual DeprecatedString render_to_html() const override;
|
2020-05-16 14:08:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class Link : public Line {
|
|
|
|
public:
|
2022-12-04 21:02:33 +03:00
|
|
|
Link(DeprecatedString line, Document const&);
|
2022-02-26 20:55:15 +03:00
|
|
|
virtual ~Link() override = default;
|
2022-12-04 21:02:33 +03:00
|
|
|
virtual DeprecatedString render_to_html() const override;
|
2020-05-16 14:08:13 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
URL m_url;
|
2022-12-04 21:02:33 +03:00
|
|
|
DeprecatedString m_name;
|
2020-05-16 14:08:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class Preformatted : public Line {
|
|
|
|
public:
|
2022-12-04 21:02:33 +03:00
|
|
|
Preformatted(DeprecatedString line)
|
2020-05-16 14:08:13 +03:00
|
|
|
: Line(move(line))
|
|
|
|
{
|
|
|
|
}
|
2022-02-26 20:55:15 +03:00
|
|
|
virtual ~Preformatted() override = default;
|
2022-12-04 21:02:33 +03:00
|
|
|
virtual DeprecatedString render_to_html() const override;
|
2020-05-16 14:08:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class UnorderedList : public Line {
|
|
|
|
public:
|
2022-12-04 21:02:33 +03:00
|
|
|
UnorderedList(DeprecatedString line)
|
2020-05-16 14:08:13 +03:00
|
|
|
: Line(move(line))
|
|
|
|
{
|
|
|
|
}
|
2022-02-26 20:55:15 +03:00
|
|
|
virtual ~UnorderedList() override = default;
|
2022-12-04 21:02:33 +03:00
|
|
|
virtual DeprecatedString render_to_html() const override;
|
2020-05-16 14:08:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class Control : public Line {
|
|
|
|
public:
|
|
|
|
enum Kind {
|
|
|
|
UnorderedListStart,
|
|
|
|
UnorderedListEnd,
|
|
|
|
PreformattedStart,
|
|
|
|
PreformattedEnd,
|
|
|
|
};
|
|
|
|
Control(Kind kind)
|
|
|
|
: Line("")
|
|
|
|
, m_kind(kind)
|
|
|
|
{
|
|
|
|
}
|
2022-02-26 20:55:15 +03:00
|
|
|
virtual ~Control() override = default;
|
2022-12-04 21:02:33 +03:00
|
|
|
virtual DeprecatedString render_to_html() const override;
|
2020-05-16 14:08:13 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
Kind m_kind;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Heading : public Line {
|
|
|
|
public:
|
2022-12-04 21:02:33 +03:00
|
|
|
Heading(DeprecatedString line, int level)
|
2020-05-16 14:08:13 +03:00
|
|
|
: Line(move(line))
|
|
|
|
, m_level(level)
|
|
|
|
{
|
|
|
|
}
|
2022-02-26 20:55:15 +03:00
|
|
|
virtual ~Heading() override = default;
|
2022-12-04 21:02:33 +03:00
|
|
|
virtual DeprecatedString render_to_html() const override;
|
2020-05-16 14:08:13 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
int m_level { 1 };
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|