Solved How to turn REStructeredText into html?
-
Hi, I am working on a REST(REStructeredText) and maybe Markdown editor in Qt. I need to transfer a QString that has REST formatting. into html so I can display it on a QWebEngineView(that may not be the right class but, this post is about transferring REST to html)
- I would prefer it woudn't include Javascript but am willing to do it with
- Using KDE Neon
- Installed Qt from repos
- Using Qt Creator 5.11.2
[VRonin] Changed title as REST usually refer to restful, not to a markup language
-
Have you considered using KDE's components like KTextEdit? It's used in many KDE apps (Kate, KDevelop etc.) and can already support a lot of different markups.
Regarding your question - I think the only way to transform the text into HTML is to write a transcriber which would translate RST / MD into HTML.
-
Is there a way I could transport the
KTextEdit
to another distro that isnt't KDE?
Because I am making this for someone else who runs a machine with Bodhi. -
@Nite-Coder
Hi , its just a Widget. If /when you can get it to compile,
you can use it on any platform where you app can run.
The only real issue can be it depends on other stuff from KDE, which you also need then.
Its a Qt widget so not tied to KDE as windows manager. -
Right! Forgot I am a year into programming and therefore forget critical details such as that! But by any chance do you know what things it depends on?
-
@Nite-Coder
Hi
Seems pretty clean, from first look
https://api.kde.org/frameworks/ktextwidgets/html/classKTextEdit.html -
@mrjj said in How to turn REStructeredText into html?:
Seems pretty clean
It's not a walk in the park but every module is built with CMake so once you compile 1 the rest should be easy
-
@VRonin
Ah, thx. i looked for the Tier label but i looked wrong place.
Its tier 3? it seems so thats some mean park agreed. -
So I have been working on a parser but I have run into some issues!
I have a parser and it crashes every time I use it!It runs fine until I select the reStructeredText option and type something
The error when I debugged was SIGABRT
When it fails it just says it exited with code one.
I am pretty certain it is the parserHere is my code:
main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication app(argc, argv); QCoreApplication::setApplicationName("SHtmlPlay"); QCoreApplication::setApplicationVersion(QT_VERSION_STR); QCoreApplication::setOrganizationName("Sash"); MainWindow w; w.show(); return app.exec(); }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QSplitter> #include <QTextEdit> #include <QWebEngineView> #include <QApplication> #include <QGuiApplication> #include <QCoreApplication> #include <QSettings> #include <QScreen> #include <QComboBox> #include <QToolBar> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); protected: QSplitter* pEditorLayout; QWebEngineView* pEditorOutput; QTextEdit* pEditor; QString currentFile; QComboBox* pReadTypeSelection; QToolBar* pToolBar; QString reStructeredTexttoHTML(QString input); void writeSettings(); void readSettings(); protected slots: void textChanged(); }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { readSettings(); pEditorLayout = new QSplitter(Qt::Horizontal); pEditor = new QTextEdit; pEditorOutput = new QWebEngineView; pToolBar = new QToolBar; pReadTypeSelection = new QComboBox; pReadTypeSelection->addItem("HTML"); pReadTypeSelection->addItem("reStructeredText"); pToolBar->addWidget(pReadTypeSelection); addToolBar(pToolBar); pEditorLayout->addWidget(pEditor); pEditorLayout->addWidget(pEditorOutput); pEditorLayout->setStretchFactor(1,1); setCentralWidget(pEditorLayout); connect(pEditor, &QTextEdit::textChanged, this, &MainWindow::textChanged); connect(pReadTypeSelection, &QComboBox::currentTextChanged, this, &MainWindow::textChanged); } MainWindow::~MainWindow() { writeSettings(); delete pEditorLayout; delete pEditor; delete pEditorOutput; } //This is my parser! Not mush currently but something to parse for italics and bold QString MainWindow::reStructeredTexttoHTML(QString input) { std::string str = input.toStdString(); if(str == "") return ""; while(true) { if(str.find(std::string("**")) > 0) { if(str.find(std::string("**"), str.find(std::string("**"))) > 0) { std::string bold = str.substr( str.find(std::string("**"))+2, str.find(std::string("**"), str.find(std::string("**"))-2 )); bold = "<b>" + bold + "</b>"; int pos = str.find(std::string("**")); str.erase(str.find(std::string("**")), str.find(std::string("**"), str.find(std::string("**")))); bold = reStructeredTexttoHTML( QString::fromStdString( bold ) ).toStdString(); str.insert(pos, bold); } else if(str.find("*") > 0) { std::string italics = str.substr( str.find(std::string("**"))+2, str.find(std::string("**"), str.find(std::string("**")))-2 ); italics = "<i>" + italics + "</i>"; int pos = str.find(std::string("**")); str.erase(str.find(std::string("**")), str.find(std::string("**"), str.find(std::string("**")))); italics = reStructeredTexttoHTML( QString::fromStdString( italics ) ).toStdString(); str.insert(pos, italics); } else { break; } } } return QString::fromStdString(str); } void MainWindow::textChanged() { if(pReadTypeSelection->currentText() == "HTML") { pEditorOutput->setHtml(pEditor->toPlainText()); } else if(pReadTypeSelection->currentText() == "reStructeredText") { pEditorOutput->setHtml(reStructeredTexttoHTML(pEditor->toPlainText())); } } void MainWindow::readSettings() { QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName()); const QByteArray geometry = settings.value("geometry", QByteArray()).toByteArray(); if (geometry.isEmpty()) { const QRect availableGeometry = QGuiApplication::screens().first()->availableGeometry(); resize(availableGeometry.width() / 3, availableGeometry.height() / 2); move((availableGeometry.width() - width()) / 2, (availableGeometry.height() - height()) / 2); } else { restoreGeometry(geometry); } } void MainWindow::writeSettings() { QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName()); settings.setValue("geometry", saveGeometry()); }
Thanks for any help in advance
-
Hi,
What does the stack trace of your crash tell you ?
-
Thanks for all the help!
-
What was the problem ?