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
|
/***************************************************************************
* Markdown Viewer part *
* Copyright (c) 2022 Mavridis Philippe <mavridisf@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include <tqbuffer.h>
#include <tqfile.h>
#include <tdeparts/genericfactory.h>
#include <kstandarddirs.h>
#include <tdehtmlview.h>
/* MD4C-HTML */
#include <md4c-html.h>
#include "markdown_part.h"
typedef KParts::GenericFactory<MarkdownPart> Factory;
K_EXPORT_COMPONENT_FACTORY(libtdemarkdown, Factory)
MarkdownPart::MarkdownPart(TQWidget* parentWidget, const char* widgetName,
TQObject* parent, const char* name, const TQStringList& args)
: TDEHTMLPart(parentWidget, name = "TDEMarkdown")
{
setInstance(Factory::instance());
/* Features */
setJScriptEnabled(false);
setJavaEnabled(false);
setMetaRefreshEnabled(false);
setPluginsEnabled(false);
setAutoloadImages(true);
setXMLFile( locate("data", "tdemarkdown/markdown_part.rc") );
}
MarkdownPart::~MarkdownPart()
{
}
TDEAboutData* MarkdownPart::createAboutData()
{
TDEAboutData* aboutData = new TDEAboutData(
"tdemarkdown", I18N_NOOP("TDE Markdown Viewer"), "1.0",
I18N_NOOP("TDEMarkdown is an embeddable viewer for Markdown documents."),
TDEAboutData::License_GPL_V2, "© 2022 Mavridis Philippe"
);
aboutData->addAuthor("Mavridis Philippe (blu.256)", I18N_NOOP("Developer"), "mavridisf@gmail.com");
return aboutData;
}
bool MarkdownPart::openURL(const KURL& u)
{
if(u.isLocalFile())
{
TQFile local(u.path());
if(!local.open(IO_ReadOnly))
{
return false;
}
TQByteArray data = local.readAll();
local.close();
if(!data.isNull())
{
if (data[data.size()-1] != '\0')
{
data.resize(data.size()+1);
data[data.size()-1] = '\0';
}
begin(u);
TQString parsed(parse((MD_CHAR*) data.data(), u.fileName().utf8().data()));
write(parsed);
end();
}
}
emit started(0L);
return true;
}
TQString& MarkdownPart::parse(MD_CHAR* document, MD_CHAR* title)
{
m_buffer = "<!DOCTYPE html>\n";
m_buffer += "<html>\n";
m_buffer += " <head>\n";
m_buffer += " <meta charset='utf-8'>\n";
m_buffer += " <title>" + (title ? title : i18n("Markdown document")) + "</title>\n";
m_buffer += " </head>\n";
m_buffer += " <body>\n";
TQByteArray data;
int success = md_html(document,
MD_SIZE(strlen(document)),
&MarkdownPart::processHTML,
&data,
MD_DIALECT_GITHUB | MD_FLAG_PERMISSIVEURLAUTOLINKS | MD_FLAG_PERMISSIVEEMAILAUTOLINKS | MD_FLAG_PERMISSIVEWWWAUTOLINKS
| MD_FLAG_LATEXMATHSPANS | MD_FLAG_PERMISSIVEATXHEADERS | MD_FLAG_UNDERLINE | MD_FLAG_TASKLISTS,
0);
if (success == -1)
{
m_buffer += TQString("<b>%1</b>").arg(i18n("Error: malformed document."));
}
else
{
if (data[data.size()-1] != '\0')
{
data.resize(data.size()+1);
data[data.size()-1] = '\0';
}
m_buffer += TQString::fromLocal8Bit(data);
}
m_buffer += " </body>\n";
m_buffer += "</html>\n";
return m_buffer;
}
void MarkdownPart::processHTML(const MD_CHAR* data, MD_SIZE data_size, void* user_data)
{
TQByteArray *ud = static_cast<TQByteArray*>(user_data);
TQBuffer buff(*ud);
if (data_size > 0)
{
buff.open(IO_WriteOnly | IO_Append);
buff.writeBlock(data, (int)data_size);
buff.close();
}
}
#include "markdown_part.moc"
|