/src/serenity/Userland/Libraries/LibGemini/Line.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020-2022, the SerenityOS developers. |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <AK/StringBuilder.h> |
8 | | #include <LibGemini/Document.h> |
9 | | |
10 | | namespace Gemini { |
11 | | |
12 | | ByteString Text::render_to_html() const |
13 | 0 | { |
14 | 0 | StringBuilder builder; |
15 | 0 | builder.append(escape_html_entities(m_text)); |
16 | 0 | builder.append("<br>\n"sv); |
17 | 0 | return builder.to_byte_string(); |
18 | 0 | } |
19 | | |
20 | | ByteString Heading::render_to_html() const |
21 | 0 | { |
22 | 0 | return ByteString::formatted("<h{}>{}</h{}>", m_level, escape_html_entities(m_text.substring_view(m_level, m_text.length() - m_level)), m_level); |
23 | 0 | } |
24 | | |
25 | | ByteString UnorderedList::render_to_html() const |
26 | 0 | { |
27 | | // 1.3.5.4.2 "Advanced clients can take the space of the bullet symbol into account" |
28 | | // FIXME: The spec is unclear about what the space means, or where it goes |
29 | | // somehow figure this out |
30 | 0 | StringBuilder builder; |
31 | 0 | builder.append("<li>"sv); |
32 | 0 | builder.append(escape_html_entities(m_text.substring_view(1, m_text.length() - 1))); |
33 | 0 | builder.append("</li>"sv); |
34 | 0 | return builder.to_byte_string(); |
35 | 0 | } |
36 | | |
37 | | ByteString Control::render_to_html() const |
38 | 0 | { |
39 | 0 | switch (m_kind) { |
40 | 0 | case Kind::PreformattedEnd: |
41 | 0 | return "</pre>"; |
42 | 0 | case Kind::PreformattedStart: |
43 | 0 | return "<pre>"; |
44 | 0 | case Kind::UnorderedListStart: |
45 | 0 | return "<ul>"; |
46 | 0 | case Kind::UnorderedListEnd: |
47 | 0 | return "</ul>"; |
48 | 0 | default: |
49 | 0 | dbgln("Unknown control kind _{}_", (int)m_kind); |
50 | 0 | VERIFY_NOT_REACHED(); |
51 | 0 | return ""; |
52 | 0 | } |
53 | 0 | } |
54 | | |
55 | | Link::Link(ByteString text, Document const& document) |
56 | 9.99k | : Line(move(text)) |
57 | 9.99k | { |
58 | 9.99k | size_t index = 2; |
59 | 11.9k | while (index < m_text.length() && (m_text[index] == ' ' || m_text[index] == '\t')) |
60 | 1.95k | ++index; |
61 | 9.99k | auto url_string = m_text.substring_view(index, m_text.length() - index); |
62 | 9.99k | auto space_offset = url_string.find_any_of(" \t"sv); |
63 | 9.99k | ByteString url = url_string; |
64 | 9.99k | if (space_offset.has_value()) { |
65 | 1.25k | url = url_string.substring_view(0, space_offset.value()); |
66 | 1.25k | auto offset = space_offset.value(); |
67 | 3.53k | while (offset < url_string.length() && (url_string[offset] == ' ' || url_string[offset] == '\t')) |
68 | 2.27k | ++offset; |
69 | 1.25k | m_name = url_string.substring_view(offset, url_string.length() - offset); |
70 | 1.25k | } |
71 | 9.99k | m_url = document.url().complete_url(url); |
72 | 9.99k | if (m_name.is_empty()) |
73 | 9.05k | m_name = m_url.to_byte_string(); |
74 | 9.99k | } |
75 | | |
76 | | ByteString Link::render_to_html() const |
77 | 0 | { |
78 | 0 | StringBuilder builder; |
79 | 0 | builder.append("<a href=\""sv); |
80 | 0 | builder.append(escape_html_entities(m_url.to_byte_string())); |
81 | 0 | builder.append("\">"sv); |
82 | 0 | builder.append(escape_html_entities(m_name)); |
83 | 0 | builder.append("</a><br>\n"sv); |
84 | 0 | return builder.to_byte_string(); |
85 | 0 | } |
86 | | |
87 | | ByteString Preformatted::render_to_html() const |
88 | 0 | { |
89 | 0 | StringBuilder builder; |
90 | 0 | builder.append(escape_html_entities(m_text)); |
91 | 0 | builder.append('\n'); |
92 | |
|
93 | 0 | return builder.to_byte_string(); |
94 | 0 | } |
95 | | |
96 | | } |