From 95279fbf6dfeb43d80590740a9259d7caa614177 Mon Sep 17 00:00:00 2001 From: Mavridis Philippe Date: Sat, 5 Feb 2022 17:44:26 +0000 Subject: Add tdemarkdown part - embeddable lightweight markdown viewing component. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Prepare to merge tdemarkdown into tdelibs. Signed-off-by: Slávek Banko --- tdemarkdown/markdown_part.cpp | 133 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 tdemarkdown/markdown_part.cpp (limited to 'tdemarkdown/markdown_part.cpp') 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 * + * * + * 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 +#include + +#include +#include + +#include + +/* MD4C-HTML */ +#include + +#include "markdown_part.h" + + +typedef KParts::GenericFactory 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"), "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()) + { + 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 = "\n"; + m_buffer += "\n"; + m_buffer += " \n"; + m_buffer += " \n"; + m_buffer += " TODO\n"; + m_buffer += " \n"; + m_buffer += " \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("%1").arg(i18n("Error: malformed document.")); + } + else + { + m_buffer += TQString::fromLocal8Bit(data); + } + + m_buffer += " \n"; + m_buffer += "\n"; + return m_buffer; +} + +void MarkdownPart::processHTML(const MD_CHAR* data, MD_SIZE data_size, void* user_data) +{ + TQByteArray *ud = static_cast(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" -- cgit v1.2.1