Пример #1
0
void tdRenderer::parseMarkdown(int at, int removed, int added)
{
    QTextCursor cursor = m_editor->textCursor();
    cursor.beginEditBlock();

    int steps = m_editor->document()->availableUndoSteps();
    bool undoStepsChanged = (steps != m_undoSteps);
    m_undoSteps = steps;

    if (undoStepsChanged && !m_isUndoRedo)
        m_undoStack->push(new tdRendererCursorCommand(this, at, removed, added));
    m_isUndoRedo = false;

    int start;
    int end;
    if (m_sizes.isEmpty()) {
        start = end = 0;
    } else {
        int i = 0, n = 0;
        while (i < m_fframe)
            n += m_sizes.at(i++);
        start = n;
        while (i <= m_lframe)
            n += m_sizes.at(i++);
        end = n;
    }

    int diff = added - removed;
    m_count += diff;
    if (m_sizes.isEmpty()) end = m_count;
    else end += diff;

    cursor.setPosition(start);
    cursor.setPosition(qMin(end, m_count), QTextCursor::KeepAnchor);

    int c = m_fframe;
    int klass = 0;
    QWebElementCollection collection;
    while (c++ <= m_lframe && !m_sizes.isEmpty()) {
        m_sizes.takeAt(m_fframe);
        klass = m_indices.takeAt(m_fframe);
        collection.append(m_body.findAll(".__" % QString::number(klass) % "__"));
    }
    QList<QWebElement> list = collection.toList();
    QWebElement element;

    if (klass) {
        QString k = "__" % QString::number(klass) % "__";
        element = list.last();
        while (element.parent().hasClass(k))
            element = element.parent();
        list.removeAll(element);
        element.setOuterXml("<div class=\"__tmp__\"></div>");

        QList<QWebElement>::iterator i = list.begin();
        for (; i != list.end(); ++i)
            i->takeFromDocument();
    } else {
        m_body.prependInside("<div class=\"__tmp__\"></div>");
    }

    render(cursor.selection().toPlainText().toAscii());

    cursor.endEditBlock();
    updateFrameInterval();
    emit parsingDone();
}
Пример #2
0
/*
  This will go through and modify some of the ENML tags and turn
  them into HTML tags.  Things like en-media & en-crypt have no
  HTML values, so we turn them into HTML.
  */
void NoteFormatter::modifyTags(QWebPage &doc) {
    tempFiles.clear();

    // Modify en-media tags
    QLOG_TRACE() << "Searching for all en-media tags;";
    QWebElementCollection anchors = doc.mainFrame()->findAllElements("en-media");
    QLOG_TRACE() << "Search complete: " << anchors.toList().size();
    foreach (QWebElement enmedia, anchors) {
        if (enmedia.hasAttribute("type")) {
            QString attr = enmedia.attribute("type");
            QString hash = enmedia.attribute("hash");
            QStringList type = attr.split("/");
            if (type.size() >= 2) {
                QString appl = type[1];
                QLOG_TRACE() << "En-Media tag type: " << type[0];
                if (type[0] == "image")
                    modifyImageTags(enmedia, hash);
                else
                    modifyApplicationTags(enmedia, hash, appl);
                QLOG_TRACE() << "Type modified";
            }
        }
    }

    // Modify todo tags
    anchors = doc.mainFrame()->findAllElements("en-todo");
    qint32 enTodoCount = anchors.count();
    for (qint32 i=enTodoCount-1; i>=0; i--) {
            QWebElement enmedia = anchors.at(i);
            modifyTodoTags(enmedia);
    }

    anchors = doc.mainFrame()->findAllElements("en-crypt");
    qint32 enCryptLen = anchors.count();
    for (qint32 i=enCryptLen-1; i>=0; i--) {
        QWebElement enmedia = anchors.at(i);
        QString hint = enmedia.attribute("hint");
        QString cipher = enmedia.attribute("cipher", "RC2");
        QString length = enmedia.attribute("length","64");

        enmedia.setAttribute("contentEditable","false");
        enmedia.setAttribute("src", QString("file://")+global.fileManager.getImageDirPath("encrypt.png"));
        enmedia.setAttribute("en-tag","en-crypt");
        enmedia.setAttribute("cipher", cipher);
        enmedia.setAttribute("length", length);
        enmedia.setAttribute("hint", hint);
        enmedia.setAttribute("alt", enmedia.toInnerXml());
        global.cryptCounter++;
        enmedia.setAttribute("id", "crypt"+QString().number(global.cryptCounter));
        QString encryptedText = enmedia.toInnerXml();

        // If the encryption string contains crlf at the end, remove them because they mess up the javascript.
        if (encryptedText.endsWith("\n"))
                encryptedText.truncate(encryptedText.length()-1);
        if (encryptedText.endsWith("\r"))
                encryptedText.truncate(encryptedText.length()-1);

        // Add the commands
        hint = hint.replace("'","&apos;");
        enmedia.setAttribute("onClick", "window.browserWindow.decryptText('crypt"+
                             QString().number(global.cryptCounter)+
                             "', '"+encryptedText+"', '"+
                             hint +"', '" +
                             cipher+ "', " +
                             length +
                             ");");
        enmedia.setAttribute("onMouseOver", "style.cursor='hand'");
        enmedia.setInnerXml("");
        QString k = enmedia.toOuterXml();
        k.replace("<en-crypt", "<img");
        k.replace("img>", "<en-crypt");
        enmedia.setOuterXml(k);
    }


    // Modify link tags
    anchors = doc.mainFrame()->findAllElements("a");
    enCryptLen = anchors.count();
    for (qint32 i=0; i<anchors.count(); i++) {
        QWebElement element = anchors.at(i);
        if (!element.attribute("href").toLower().startsWith("latex://"))
            element.setAttribute("title", element.attribute("href"));
        else {
            element.setAttribute("title", element.attribute("title").toLower().replace("http://latex.codecogs.com/gif.latex?",""));
        }
    }

}