From ff715f77854c84ef7bce446ca5b48190d64b45a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sat, 16 May 2015 23:04:00 +0200 Subject: NOISSUE replace derpy merkdown thing with hoedown --- application/dialogs/UpdateDialog.cpp | 201 ++++++++++------------------------- 1 file changed, 57 insertions(+), 144 deletions(-) (limited to 'application/dialogs') diff --git a/application/dialogs/UpdateDialog.cpp b/application/dialogs/UpdateDialog.cpp index 37e44b1e..d8f5d8ac 100644 --- a/application/dialogs/UpdateDialog.cpp +++ b/application/dialogs/UpdateDialog.cpp @@ -5,6 +5,9 @@ #include "MultiMC.h" #include +#include +#include + UpdateDialog::UpdateDialog(bool hasUpdate, QWidget *parent) : QDialog(parent), ui(new Ui::UpdateDialog) { MultiMCPlatform::fixWM_CLASS(this); @@ -39,166 +42,76 @@ void UpdateDialog::loadChangelog() dljob->start(); } -// TODO: this will be replaced. -QString reprocessMarkdown(QString markdown) +/** + * hoedown wrapper, because dealing with resource lifetime in C is stupid + */ +class HoeDown { - QString htmlData; - QTextStream html(&htmlData); - auto lines = markdown.split(QRegExp("[\r]?[\n]"),QString::KeepEmptyParts); - enum - { - BASE, - LIST1, - LIST2 - }state = BASE; - html << ""; - int i = 0; - auto procLine = [&](QString line) -> QString +public: + class buffer { - // [GitHub issues](https://github.com/MultiMC/MultiMC5/issues) - line.replace(QRegExp("\\[([^\\]]+)\\]\\(([^\\)]+)\\)"), "\\1"); - line.replace(QRegExp("GH-([0-9]+)"), "GH-\\1"); - line.replace(QRegExp("\\*\\*([^*]+)\\*\\*"), "\\1"); - line.replace(QRegExp("\\*([^*]+)\\*"), "\\1"); - return line; - }; - for(auto line: lines) - { - if(line.isEmpty()) + public: + buffer(size_t unit = 4096) { - // html << "
\n"; + buf = hoedown_buffer_new(unit); } - else switch (state) + ~buffer() { - case BASE: - if(line.startsWith("####")) - { - html << "

" << procLine(line.mid(4)) << "

\n"; - } - if(line.startsWith("###")) - { - html << "

" << procLine(line.mid(3)) << "

\n"; - } - if(line.startsWith("##")) - { - html << "

" << procLine(line.mid(2)) << "

\n"; - } - else if(line.startsWith("#")) - { - html << "

" << procLine(line.mid(1)) << "

\n"; - } - else if(line.startsWith("- ")) - { - state = LIST1; - html << "
    \n"; - html << "
  • " << procLine(line.mid(2)) << "
  • \n"; - } - else qCritical() << "Invalid input on line " << i << ": " << line; - break; - case LIST1: - if(line.startsWith("####")) - { - state = BASE; - html << "
\n"; - html << "

" << procLine(line.mid(4)) << "

\n"; - } - else if(line.startsWith("###")) - { - state = BASE; - html << "\n"; - html << "

" << procLine(line.mid(3)) << "

\n"; - } - if(line.startsWith("##")) - { - state = BASE; - html << "\n"; - html << "

" << procLine(line.mid(2)) << "

\n"; - } - else if(line.startsWith("#")) - { - state = BASE; - html << "\n"; - html << "

" << procLine(line.mid(1)) << "

\n"; - } - else if(line.startsWith("- ")) - { - html << "
  • " << procLine(line.mid(2)) << "
  • \n"; - } - else if(line.startsWith(" - ")) - { - state = LIST2; - html << "
      \n"; - html << "
    • " << procLine(line.mid(4)) << "
    • \n"; - } - else qCritical() << "Invalid input on line " << i << ": " << line; - break; - case LIST2: - if(line.startsWith("####")) - { - state = BASE; - html << "
    \n"; - html << "\n"; - html << "

    " << procLine(line.mid(4)) << "

    \n"; - } - else if(line.startsWith("###")) - { - state = BASE; - html << "\n"; - html << "\n"; - html << "

    " << procLine(line.mid(3)) << "

    \n"; - } - if(line.startsWith("##")) - { - state = BASE; - html << "\n"; - html << "\n"; - html << "

    " << procLine(line.mid(2)) << "

    \n"; - } - else if(line.startsWith("#")) - { - state = BASE; - html << "\n"; - html << "\n"; - html << "

    " << procLine(line.mid(1)) << "

    \n"; - } - else if(line.startsWith("- ")) - { - state = LIST1; - html << "\n"; - html << "
  • " << procLine(line.mid(2)) << "
  • \n"; - } - else if(line.startsWith(" - ")) - { - html << "
  • " << procLine(line.mid(4)) << "
  • \n"; - } - else qCritical() << "Invalid input on line " << i << ": " << line; - break; + hoedown_buffer_free(buf); } - i++; - } - if(state == LIST2) + const char * cstr() + { + return hoedown_buffer_cstr(buf); + } + void put(QByteArray input) + { + hoedown_buffer_put(buf, (uint8_t *) input.data(), input.size()); + } + const uint8_t * data() const + { + return buf->data; + } + size_t size() const + { + return buf->size; + } + hoedown_buffer * buf; + } ib, ob; + HoeDown() { - html << "\n"; - state = LIST1; + renderer = hoedown_html_renderer_new((hoedown_html_flags) 0,0); + document = hoedown_document_new(renderer, (hoedown_extensions) 0, 8); } - if(state == LIST1) + ~HoeDown() { - html << "\n"; - state = BASE; + hoedown_document_free(document); + hoedown_html_renderer_free(renderer); } - if (state != BASE) + QString process(QByteArray input) { - qCritical() << "Reprocessing markdown didn't end in a final state!"; + ib.put(input); + hoedown_document_render(document, ob.buf, ib.data(), ib.size()); + return ob.cstr(); } - html << "\n"; - qDebug() << htmlData; - return htmlData; +private: + hoedown_document * document; + hoedown_renderer * renderer; +}; + +QString reprocessMarkdown(QByteArray markdown) +{ + HoeDown hoedown; + QString output = hoedown.process(markdown); + + // HACK: easier than customizing hoedown + output.replace(QRegExp("GH-([0-9]+)"), "GH-\\1"); + qDebug() << output; + return output; } void UpdateDialog::changelogLoaded() { - auto rawMarkdown = QString::fromUtf8(changelogDownload->m_data); - auto html = reprocessMarkdown(rawMarkdown); + auto html = reprocessMarkdown(changelogDownload->m_data); ui->changelogBrowser->setHtml(html); } -- cgit v1.2.3