-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdhandler.cpp
84 lines (60 loc) · 1.52 KB
/
mdhandler.cpp
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
//
// mdhandler.cpp
// markdown
//
// Created by Jackey Chen on 6/3/15.
//
//
#include "mdhandler.h"
#include <QTextEdit>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
extern "C" {
#include <mkdio.h>
}
#include <QObject>
MdHandler::MdHandler(QTextEdit *editor)
:mdEditor(editor) {
//TODO::
}
MdHandler::~MdHandler() {
//TODO::
}
void MdHandler::handleMdChanged() {
QString str = mdEditor->toPlainText();
const char *txt = str.toUtf8().constData();
int size = strlen(txt);
MMIOT *mddoc;
mddoc = mkd_string(txt, size, 0);
mkd_compile(mddoc, 0);
char *buf;
int sizeBuf;
sizeBuf = mkd_document(mddoc, &buf);
// 把内容转换到QString中
QString html = QString::fromUtf8(buf);
if (html.size() <= 0 || sizeBuf == EOF) {
html = mdEditor->toHtml();
}
// 释放内存
free(buf);
// 触发事件
emit mdCompiled(html);
}
// 编译 Markdown -> HTML
const QString MdHandler::markdown(QString mdText) {
const char *data = mdText.toUtf8().constData();
int size = strlen(data);
std::cout << "编译的内容: " << data << std::endl;
MMIOT *doc = mkd_string(data, size, 0);
mkd_compile(doc, 0);
char *buf;
int sizeBuf;
sizeBuf = mkd_document(doc, &buf);
std::cout << "编译的长度: " << sizeBuf;
QString html = QString::fromUtf8(buf);
free(buf);
std::cout << "默认HTML " << html.toUtf8().data();
return html;
}