+
+MarkdownEntry::MarkdownEntry(Worksheet* worksheet) : TextEntry(worksheet), dirty(false), evalJustNow(false)
+{
+ m_textItem->installEventFilter(this);
+}
+
+MarkdownEntry::~MarkdownEntry()
+{
+}
+
+void MarkdownEntry::setContent(const QString& content)
+{
+ dirty = true;
+ plain = content;
+ TextEntry::setContent(content);
+}
+
+void MarkdownEntry::setContent(const QDomElement& content, const KZip& file)
+{
+ Q_UNUSED(file);
+
+ dirty = true;
+ plain = content.text();
+ m_textItem->setPlainText(plain);
+ qDebug() << plain;
+}
+
+QDomElement MarkdownEntry::toXml(QDomDocument& doc, KZip* archive)
+{
+ Q_UNUSED(archive);
+
+ qDebug() << plain;
+ QDomElement el = doc.createElement(QLatin1String("Markdown"));
+ QDomText text=doc.createTextNode(plain);
+ el.appendChild(text);
+ return el;
+}
+
+bool MarkdownEntry::evaluate(EvaluationOption evalOp)
+{
+#ifdef discount_FOUND
+ if(m_textItem->hasFocus()) // text in the entry may be edited
+ plain = m_textItem->toPlainText();
+
+ /*
+ QString tempDir = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
+ QTemporaryFile* markdownQFile = new QTemporaryFile(tempDir + QLatin1String("/cantor_md-XXXXXX.md"));
+ markdownQFile->open(QIODevice::ReadOnly);
+ FILE* mdFile = fdopen(markdownQFile->handle(), "rb");
+ if(!mdFile)
+ {
+ qDebug()<<"Failed to open the markdown temporary file";
+ return TextEntry::evaluate(evalOp);
+ }
+ */
+
+ // convert markdown to html
+ QByteArray mdCharArray = plain.toUtf8();
+ MMIOT* mdHandle = mkd_string(mdCharArray.data(), mdCharArray.size()+1, 0); // get the size of the string in byte
+ if(!mkd_compile(mdHandle, MKD_NOSUPERSCRIPT | MKD_FENCEDCODE | MKD_GITHUBTAGS))
+ {
+ qDebug()<<"Failed to compile the markdown document";
+ return TextEntry::evaluate(evalOp);
+ }
+ char *htmlDocument;
+ int htmlSize = mkd_document(mdHandle, &htmlDocument);
+ html = QString::fromUtf8(htmlDocument, htmlSize);
+
+ m_textItem->setHtml(html);
+ dirty = false;
+ evalJustNow = true;
+#endif
+ return TextEntry::evaluate(evalOp);
+}
+
+bool MarkdownEntry::eventFilter(QObject* object, QEvent* event)
+{
+ if(object == m_textItem)
+ {
+ if(event->type() == QEvent::FocusIn)
+ {
+ QString plainHtml = QLatin1String("") + plain + QLatin1String("
"); // clear the style, such as font
+ plainHtml.replace(QLatin1String("\n"), QLatin1String("
"));
+ m_textItem->setHtml(plainHtml);
+ }
+ else if(event->type() == QEvent::FocusOut)
+ {
+ if(evalJustNow)
+ {
+ evalJustNow = false;
+ return false;
+ }
+
+ if(!dirty && plain.compare(m_textItem->toPlainText()) == 0)
+ {
+ m_textItem->setHtml(html);
+ TextEntry::evaluate();
+ }
+ else
+ {
+ dirty = true;
+ plain = m_textItem->toPlainText();
+ }
+ }
+ }
+ return false;
+}
diff --git a/src/textentry.h b/src/textentry.h
--- a/src/textentry.h
+++ b/src/textentry.h
@@ -78,12 +78,10 @@
protected:
bool wantToEvaluate() Q_DECL_OVERRIDE;
-
- private:
QTextCursor findLatexCode(const QTextCursor& cursor = QTextCursor()) const;
QString showLatexCode(QTextCursor& cursor);
- private:
+ protected:
WorksheetTextItem* m_textItem;
};
diff --git a/src/worksheet.h b/src/worksheet.h
--- a/src/worksheet.h
+++ b/src/worksheet.h
@@ -116,17 +116,20 @@
WorksheetEntry* appendCommandEntry();
void appendCommandEntry(const QString& text);
WorksheetEntry* appendTextEntry();
+ WorksheetEntry* appendMarkdownEntry();
WorksheetEntry* appendImageEntry();
WorksheetEntry* appendPageBreakEntry();
WorksheetEntry* appendLatexEntry();
WorksheetEntry* insertCommandEntry(WorksheetEntry* current = nullptr);
void insertCommandEntry(const QString& text);
WorksheetEntry* insertTextEntry(WorksheetEntry* current = nullptr);
+ WorksheetEntry* insertMarkdownEntry(WorksheetEntry* current = nullptr);
WorksheetEntry* insertImageEntry(WorksheetEntry* current = nullptr);
WorksheetEntry* insertPageBreakEntry(WorksheetEntry* current = nullptr);
WorksheetEntry* insertLatexEntry(WorksheetEntry* current = nullptr);
WorksheetEntry* insertCommandEntryBefore(WorksheetEntry* current = nullptr);
WorksheetEntry* insertTextEntryBefore(WorksheetEntry* current = nullptr);
+ WorksheetEntry* insertMarkdownEntryBefore(WorksheetEntry* current = nullptr);
WorksheetEntry* insertImageEntryBefore(WorksheetEntry* current = nullptr);
WorksheetEntry* insertPageBreakEntryBefore(WorksheetEntry* current = nullptr);
WorksheetEntry* insertLatexEntryBefore(WorksheetEntry* current = nullptr);
diff --git a/src/worksheet.cpp b/src/worksheet.cpp
--- a/src/worksheet.cpp
+++ b/src/worksheet.cpp
@@ -37,6 +37,7 @@
#include "settings.h"
#include "commandentry.h"
#include "textentry.h"
+#include "markdownentry.h"
#include "latexentry.h"
#include "imageentry.h"
#include "pagebreakentry.h"
@@ -544,6 +545,10 @@
return appendEntry(TextEntry::Type);
}
+WorksheetEntry* Worksheet::appendMarkdownEntry()
+{
+ return appendEntry(MarkdownEntry::Type);
+}
WorksheetEntry* Worksheet::appendPageBreakEntry()
{
@@ -612,6 +617,11 @@
return insertEntry(TextEntry::Type, current);
}
+WorksheetEntry* Worksheet::insertMarkdownEntry(WorksheetEntry* current)
+{
+ return insertEntry(MarkdownEntry::Type, current);
+}
+
WorksheetEntry* Worksheet::insertCommandEntry(WorksheetEntry* current)
{
return insertEntry(CommandEntry::Type, current);
@@ -675,6 +685,11 @@
return insertEntryBefore(TextEntry::Type, current);
}
+WorksheetEntry* Worksheet::insertMarkdownEntryBefore(WorksheetEntry* current)
+{
+ return insertEntryBefore(MarkdownEntry::Type, current);
+}
+
WorksheetEntry* Worksheet::insertCommandEntryBefore(WorksheetEntry* current)
{
return insertEntryBefore(CommandEntry::Type, current);
@@ -1073,6 +1088,10 @@
{
entry = appendTextEntry();
entry->setContent(expressionChild, file);
+ } else if (tag == QLatin1String("Markdown"))
+ {
+ entry = appendMarkdownEntry();
+ entry->setContent(expressionChild, file);
} else if (tag == QLatin1String("Latex"))
{
entry = appendLatexEntry();
@@ -1170,12 +1189,14 @@
insert->addAction(i18n("Command Entry"), entry, SLOT(insertCommandEntry()));
insert->addAction(i18n("Text Entry"), entry, SLOT(insertTextEntry()));
+ insert->addAction(i18n("Markdown Entry"), entry, SLOT(insertMarkdownEntry()));
insert->addAction(i18n("LaTeX Entry"), entry, SLOT(insertLatexEntry()));
insert->addAction(i18n("Image"), entry, SLOT(insertImageEntry()));
insert->addAction(i18n("Page Break"), entry, SLOT(insertPageBreakEntry()));
insertBefore->addAction(i18n("Command Entry"), entry, SLOT(insertCommandEntryBefore()));
insertBefore->addAction(i18n("Text Entry"), entry, SLOT(insertTextEntryBefore()));
+ insertBefore->addAction(i18n("Markdown Entry"), entry, SLOT(insertMarkdownEntryBefore()));
insertBefore->addAction(i18n("LaTeX Entry"), entry, SLOT(insertLatexEntryBefore()));
insertBefore->addAction(i18n("Image"), entry, SLOT(insertImageEntryBefore()));
insertBefore->addAction(i18n("Page Break"), entry, SLOT(insertPageBreakEntryBefore()));
@@ -1187,6 +1208,7 @@
} else {
menu->addAction(i18n("Insert Command Entry"), this, SLOT(appendCommandEntry()));
menu->addAction(i18n("Insert Text Entry"), this, SLOT(appendTextEntry()));
+ menu->addAction(i18n("Insert Markdown Entry"), this, SLOT(appendMarkdownEntry()));
menu->addAction(i18n("Insert LaTeX Entry"), this, SLOT(appendLatexEntry()));
menu->addAction(i18n("Insert Image"), this, SLOT(appendImageEntry()));
menu->addAction(i18n("Insert Page Break"), this, SLOT(appendPageBreakEntry()));
diff --git a/src/worksheetentry.h b/src/worksheetentry.h
--- a/src/worksheetentry.h
+++ b/src/worksheetentry.h
@@ -29,6 +29,7 @@
#include "worksheetcursor.h"
class TextEntry;
+class MarkdownEntry;
class CommandEntry;
class ImageEntry;
class PageBreakEntry;
@@ -112,11 +113,13 @@
void insertCommandEntry();
void insertTextEntry();
+ void insertMarkdownEntry();
void insertLatexEntry();
void insertImageEntry();
void insertPageBreakEntry();
void insertCommandEntryBefore();
void insertTextEntryBefore();
+ void insertMarkdownEntryBefore();
void insertLatexEntryBefore();
void insertImageEntryBefore();
void insertPageBreakEntryBefore();
diff --git a/src/worksheetentry.cpp b/src/worksheetentry.cpp
--- a/src/worksheetentry.cpp
+++ b/src/worksheetentry.cpp
@@ -21,6 +21,7 @@
#include "worksheetentry.h"
#include "commandentry.h"
#include "textentry.h"
+#include "markdownentry.h"
#include "latexentry.h"
#include "imageentry.h"
#include "pagebreakentry.h"
@@ -88,6 +89,8 @@
{
case TextEntry::Type:
return new TextEntry(worksheet);
+ case MarkdownEntry::Type:
+ return new MarkdownEntry(worksheet);
case CommandEntry::Type:
return new CommandEntry(worksheet);
case ImageEntry::Type:
@@ -111,6 +114,11 @@
worksheet()->insertTextEntry(this);
}
+void WorksheetEntry::insertMarkdownEntry()
+{
+ worksheet()->insertMarkdownEntry(this);
+}
+
void WorksheetEntry::insertLatexEntry()
{
worksheet()->insertLatexEntry(this);
@@ -136,6 +144,11 @@
worksheet()->insertTextEntryBefore(this);
}
+void WorksheetEntry::insertMarkdownEntryBefore()
+{
+ worksheet()->insertMarkdownEntryBefore(this);
+}
+
void WorksheetEntry::insertLatexEntryBefore()
{
worksheet()->insertLatexEntryBefore(this);