changeset 65:defd29b9f25a

Converter: Unescape IMG and Latex references
author Andre Heinecke <andre.heinecke@intevation.de>
date Fri, 26 Aug 2016 15:26:17 +0200
parents 94cac17fe5be
children fe55e1e5bbf2
files src/constants.h src/converter.cpp
diffstat 2 files changed, 32 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/constants.h	Fri Aug 26 12:25:03 2016 +0200
+++ b/src/constants.h	Fri Aug 26 15:26:17 2016 +0200
@@ -75,6 +75,15 @@
  * @brief The pattern used to match a free text answer. */
 #define FREETXT_PATTERN "\"([^\"]*)\"?", QRegularExpression::MultilineOption
 
+/**
+ * @brief the pattern to unsecape images. */
+#define IMAGE_PATTERN "##(.*)##", QRegularExpression::MultilineOption
+
+
+/**
+ * @brief the pattern to unsecape latex. */
+#define LATEX_PATTERN "\\$\\$(.*)\\$\\$", QRegularExpression::MultilineOption
+
 #define TITLE_ROW_HEIGHT 30
 
 #define CHOICE_ROW_HEIGHT 30
--- a/src/converter.cpp	Fri Aug 26 12:25:03 2016 +0200
+++ b/src/converter.cpp	Fri Aug 26 15:26:17 2016 +0200
@@ -136,6 +136,24 @@
     return;
 }
 
+static void unescapeRegex(QString &str, const QRegularExpression &exp)
+{
+    QRegularExpressionMatch match = exp.match(str);
+    while (match.hasMatch()) {
+        str.replace(match.capturedStart(), match.capturedLength(), match.captured(1));
+        match = exp.match(str);
+    }
+}
+
+static void unescapeString(QString &str)
+{
+    static const QRegularExpression imgEx(IMAGE_PATTERN);
+    static const QRegularExpression texEx(LATEX_PATTERN);
+
+    unescapeRegex(str, imgEx);
+    unescapeRegex(str, texEx);
+}
+
 void Converter::convertToXSLX(QTextStream& instream, QList<QFile *>outputs)
 {
     Document xlsx;
@@ -198,7 +216,8 @@
             return;
         }
         foundSomething = true;
-        const QString question = match.captured(1).trimmed();
+        QString question = match.captured(1).trimmed();
+        unescapeString(question);
         const QString answerLine = match.captured(2).trimmed();
         xlsx.write(row, 2, QString(" "), mQuestionFmt);
         xlsx.write(row, 3, QString(" "), mQuestionFmt);
@@ -224,6 +243,7 @@
                 if (choiceName.startsWith("=")) {
                     choiceName = " " + choiceName;
                 }
+                unescapeString(choiceName);
                 xlsx.write(row, 1, choiceName, mChoiceTextFmt);
                 html << mChoiceTextStyle.arg(choiceName.toHtmlEscaped());
                 qDebug() << "Captured for choice: " << choiceMatch.captured(0);
@@ -261,9 +281,10 @@
             bool additionalFound = false;
             while (choiceMatch.hasMatch() && choiceMatch.capturedStart() <= cursor + 1) {
                 additionalFound = true;
-                const QString choice = choiceMatch.captured(1);
+                QString choice = choiceMatch.captured(1);
                 cursor = choiceMatch.capturedEnd();
                 /* Alternative answer that is just a list of strings */
+                unescapeString(choice);
                 qDebug() << "Caputured unfilled choice: " << choice;
                 html << mChoiceTextStyle.arg(choice.toHtmlEscaped());
                 makeBar(html, 0, doc);
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)