/src/serenity/Userland/Libraries/LibGemini/Document.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020-2022, the SerenityOS developers. |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/ByteString.h> |
10 | | #include <AK/Forward.h> |
11 | | #include <AK/NonnullOwnPtr.h> |
12 | | #include <LibURL/URL.h> |
13 | | |
14 | | namespace Gemini { |
15 | | |
16 | | class Line { |
17 | | public: |
18 | | Line(ByteString string) |
19 | 94.2M | : m_text(move(string)) |
20 | 94.2M | { |
21 | 94.2M | } |
22 | | |
23 | 94.2M | virtual ~Line() = default; |
24 | | |
25 | | virtual ByteString render_to_html() const = 0; |
26 | | |
27 | | protected: |
28 | | ByteString m_text; |
29 | | }; |
30 | | |
31 | | class Document : public RefCounted<Document> { |
32 | | public: |
33 | | ByteString render_to_html() const; |
34 | | |
35 | | static NonnullRefPtr<Document> parse(StringView source, const URL::URL&); |
36 | | |
37 | 16.2k | const URL::URL& url() const { return m_url; } |
38 | | |
39 | | private: |
40 | | explicit Document(const URL::URL& url) |
41 | 1.33k | : m_url(url) |
42 | 1.33k | { |
43 | 1.33k | } |
44 | | |
45 | | void read_lines(StringView); |
46 | | |
47 | | Vector<NonnullOwnPtr<Line>> m_lines; |
48 | | URL::URL m_url; |
49 | | bool m_inside_preformatted_block { false }; |
50 | | bool m_inside_unordered_list { false }; |
51 | | }; |
52 | | |
53 | | class Text : public Line { |
54 | | public: |
55 | | Text(ByteString line) |
56 | 49.7M | : Line(move(line)) |
57 | 49.7M | { |
58 | 49.7M | } |
59 | | virtual ~Text() override = default; |
60 | | virtual ByteString render_to_html() const override; |
61 | | }; |
62 | | |
63 | | class Link : public Line { |
64 | | public: |
65 | | Link(ByteString line, Document const&); |
66 | 16.2k | virtual ~Link() override = default; |
67 | | virtual ByteString render_to_html() const override; |
68 | | |
69 | | private: |
70 | | URL::URL m_url; |
71 | | ByteString m_name; |
72 | | }; |
73 | | |
74 | | class Preformatted : public Line { |
75 | | public: |
76 | | Preformatted(ByteString line) |
77 | 6.25M | : Line(move(line)) |
78 | 6.25M | { |
79 | 6.25M | } |
80 | | virtual ~Preformatted() override = default; |
81 | | virtual ByteString render_to_html() const override; |
82 | | }; |
83 | | |
84 | | class UnorderedList : public Line { |
85 | | public: |
86 | | UnorderedList(ByteString line) |
87 | 12.7M | : Line(move(line)) |
88 | 12.7M | { |
89 | 12.7M | } |
90 | | virtual ~UnorderedList() override = default; |
91 | | virtual ByteString render_to_html() const override; |
92 | | }; |
93 | | |
94 | | class Control : public Line { |
95 | | public: |
96 | | enum Kind { |
97 | | UnorderedListStart, |
98 | | UnorderedListEnd, |
99 | | PreformattedStart, |
100 | | PreformattedEnd, |
101 | | }; |
102 | | Control(Kind kind) |
103 | 25.4M | : Line("") |
104 | 25.4M | , m_kind(kind) |
105 | 25.4M | { |
106 | 25.4M | } |
107 | | virtual ~Control() override = default; |
108 | | virtual ByteString render_to_html() const override; |
109 | | |
110 | | private: |
111 | | Kind m_kind; |
112 | | }; |
113 | | |
114 | | class Heading : public Line { |
115 | | public: |
116 | | Heading(ByteString line, int level) |
117 | 11.4k | : Line(move(line)) |
118 | 11.4k | , m_level(level) |
119 | 11.4k | { |
120 | 11.4k | } |
121 | | virtual ~Heading() override = default; |
122 | | virtual ByteString render_to_html() const override; |
123 | | |
124 | | private: |
125 | | int m_level { 1 }; |
126 | | }; |
127 | | |
128 | | } |