1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
#include "gophermaprenderer.hpp"
#include "renderhelpers.hpp"
#include <cassert>
#include <QTextList>
#include <QTextBlock>
#include <QList>
#include <QStringList>
#include <QTextImageFormat>
#include <QDebug>
#include <QImage>
#include "kristall.hpp"
std::unique_ptr<QTextDocument> GophermapRenderer::render(const QByteArray &input, const QUrl &root_url, const DocumentStyle &themed_style)
{
QTextCharFormat standard;
standard.setFont(themed_style.preformatted_font);
standard.setForeground(themed_style.preformatted_color);
QTextCharFormat standard_link;
standard_link.setFont(themed_style.preformatted_font);
standard_link.setForeground(QBrush(themed_style.internal_link_color));
QTextCharFormat external_link;
external_link.setFont(themed_style.standard_font);
external_link.setForeground(QBrush(themed_style.external_link_color));
bool emit_text_only = (kristall::globals().options.gophermap_display == GenericSettings::PlainText);
std::unique_ptr<QTextDocument> result = std::make_unique<QTextDocument>();
renderhelpers::setPageMargins(result.get(), themed_style.margin_h, themed_style.margin_v);
if(not emit_text_only)
{
QString icon_prefix;
if(themed_style.background_color.valueF() < 0.65)
icon_prefix = ":/icons/dark/gopher/";
else
icon_prefix = ":/icons/light/gopher/";
qDebug() << "lightness" << themed_style.background_color.lightnessF();
qDebug() << "value" << themed_style.background_color.valueF();
result->addResource(QTextDocument::ImageResource, QUrl("gopher/binary"), QVariant::fromValue(QImage(icon_prefix + "binary.svg")));
result->addResource(QTextDocument::ImageResource, QUrl("gopher/directory"), QVariant::fromValue(QImage(icon_prefix + "directory.svg")));
result->addResource(QTextDocument::ImageResource, QUrl("gopher/dns"), QVariant::fromValue(QImage(icon_prefix + "dns.svg")));
result->addResource(QTextDocument::ImageResource, QUrl("gopher/error"), QVariant::fromValue(QImage(icon_prefix + "error.svg")));
result->addResource(QTextDocument::ImageResource, QUrl("gopher/gif"), QVariant::fromValue(QImage(icon_prefix + "gif.svg")));
result->addResource(QTextDocument::ImageResource, QUrl("gopher/html"), QVariant::fromValue(QImage(icon_prefix + "html.svg")));
result->addResource(QTextDocument::ImageResource, QUrl("gopher/image"), QVariant::fromValue(QImage(icon_prefix + "image.svg")));
result->addResource(QTextDocument::ImageResource, QUrl("gopher/mirror"), QVariant::fromValue(QImage(icon_prefix + "mirror.svg")));
result->addResource(QTextDocument::ImageResource, QUrl("gopher/search"), QVariant::fromValue(QImage(icon_prefix + "search.svg")));
result->addResource(QTextDocument::ImageResource, QUrl("gopher/sound"), QVariant::fromValue(QImage(icon_prefix + "sound.svg")));
result->addResource(QTextDocument::ImageResource, QUrl("gopher/telnet"), QVariant::fromValue(QImage(icon_prefix + "telnet.svg")));
result->addResource(QTextDocument::ImageResource, QUrl("gopher/text"), QVariant::fromValue(QImage(icon_prefix + "text.svg")));
}
QTextCursor cursor{result.get()};
QTextBlockFormat non_list_format = cursor.blockFormat();
QTextCharFormat text_fmt = standard;
char last_type = '1';
QList<QByteArray> lines = input.split('\n');
for (auto const &line : lines)
{
if (line.length() < 2) // skip lines without
continue;
if (line[line.size() - 1] != '\r')
continue;
auto items = line.mid(1, line.length() - 2).split('\t');
if (items.size() < 2) // invalid
continue;
QString icon;
QString scheme = "gopher";
auto type = line.at(0);
switch (type)
{
case '0': // Text File
icon = "text";
break;
case '1': // Gopher submenu or link to another gopher server
icon = "directory";
break;
case '2': // CCSO Nameserver
icon = "dns";
break;
case '3': // Error code returned by a Gopher server to indicate failure
icon = "error";
break;
case '4': // BinHex-encoded file (primarily for Macintosh computers)
icon = "binary";
break;
case '5': // DOS file
icon = "binary";
break;
case '6': // uuencoded file
icon = "binary";
break;
case '7': // Gopher full-text search
icon = "search";
break;
case '8': // Telnet
icon = "telnet";
scheme = "telnet";
break;
case '9': // Binary file
icon = "binary";
break;
case '+': // Mirror or alternate server (for load balancing or in case of primary server downtime)
icon = "mirror";
break;
case 'g': // GIF file
icon = "gif";
break;
case 'I': // Image file
icon = "image";
break;
case 'T': // Telnet 3270
icon = "telnet";
scheme = "telnet";
break;
//Non-Canonical Types
case 'h': // HTML file
icon = "html";
break;
case 'i': // Informational message
icon = "informational";
break;
case 's': // Sound file
icon = "sound";
break;
default: // unknown
continue;
}
if(type == '+') {
type = last_type;
} else {
last_type = type;
}
QString title = items.at(0);
if (type == 'i')
{
const QString escapeRenderInput = title + "\n";
renderhelpers::renderEscapeCodes(escapeRenderInput.toUtf8(), text_fmt, standard, cursor);
}
else
{
QString dst_url;
switch (items.size())
{
case 0:
assert(false);
case 1:
assert(false);
case 2:
dst_url = root_url.resolved(QUrl(items.at(1))).toString();
break;
case 3:
dst_url = scheme + "://" + items.at(2) + "/" + QString(type) + items.at(1);
break;
default:
dst_url = scheme + "://" + items.at(2) + ":" + items.at(3) + "/" + QString(type) + items.at(1);
break;
}
if (not QUrl(dst_url).isValid())
{
// invlaid URL generated
qDebug() << line << dst_url;
}
if(emit_text_only)
{
cursor.insertText("[" + icon + "] ", standard);
}
else
{
QTextImageFormat icon_fmt;
icon_fmt.setFont(themed_style.preformatted_font);
icon_fmt.setName(QString("gopher/%1").arg(icon));
icon_fmt.setVerticalAlignment(QTextImageFormat::AlignTop);
cursor.insertImage(icon_fmt);
cursor.insertText(" ");
}
QTextCharFormat fmt = standard_link;
fmt.setAnchor(true);
fmt.setAnchorHref(dst_url);
cursor.insertText(title + "\n", fmt);
}
}
return result;
}
|