diff options
author | Mavridis Philippe <[email protected]> | 2022-02-05 17:44:26 +0000 |
---|---|---|
committer | Slávek Banko <[email protected]> | 2022-04-17 18:35:01 +0200 |
commit | 95279fbf6dfeb43d80590740a9259d7caa614177 (patch) | |
tree | 8c3ff4de77102f1e55357dd81d650e9f22be69a0 /tdemarkdown/markdown_part.cpp | |
parent | a291f3a0a3fab073c009f77a36745c5c7bd48e9b (diff) | |
download | tdelibs-95279fbf6dfeb43d80590740a9259d7caa614177.tar.gz tdelibs-95279fbf6dfeb43d80590740a9259d7caa614177.zip |
Add tdemarkdown part - embeddable lightweight markdown viewing component.
TDEMarkdown is based on the md4c library and using TDEHTML for rendering
its output. For enhanced safety, on HTML widget is turned off everything
we don't need for viewing. It integrates nicely into Konqueror and
supports both Commonmark and GitHub markdown syntaxes.
Signed-off-by: Mavridis Philippe <[email protected]>
Prepare to merge tdemarkdown into tdelibs.
Signed-off-by: Slávek Banko <[email protected]>
Diffstat (limited to 'tdemarkdown/markdown_part.cpp')
-rw-r--r-- | tdemarkdown/markdown_part.cpp | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/tdemarkdown/markdown_part.cpp b/tdemarkdown/markdown_part.cpp new file mode 100644 index 000000000..d4b7fb810 --- /dev/null +++ b/tdemarkdown/markdown_part.cpp @@ -0,0 +1,133 @@ +/*************************************************************************** + * Markdown Viewer part * + * Copyright (c) 2022 Mavridis Philippe <[email protected]> * + * * + * 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", "libtdemarkdown/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"), "[email protected]"); + 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()) + { + begin(u); + TQString parsed(parse((MD_CHAR*) data.data())); + write(parsed); + end(); + } + } + + emit started(0L); + return true; +} + +TQString& MarkdownPart::parse(MD_CHAR* document) +{ + m_buffer = "<!DOCTYPE html>\n"; + m_buffer += "<html>\n"; + m_buffer += " <head>\n"; + m_buffer += " <meta charset='utf-8'>\n"; + m_buffer += " <title>TODO</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 + { + 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" |