[Ktutorial-commits] SF.net SVN: ktutorial:[298] trunk/ktutorial/ktutorial-editor
Status: Alpha
Brought to you by:
danxuliu
From: <dan...@us...> - 2011-04-15 16:29:34
|
Revision: 298 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=298&view=rev Author: danxuliu Date: 2011-04-15 16:29:27 +0000 (Fri, 15 Apr 2011) Log Message: ----------- Fix always exporting step text as rich text, even when there were no rich text or semantic markup tags. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/serialization/JavascriptExporter.cpp trunk/ktutorial/ktutorial-editor/src/serialization/JavascriptExporter.h trunk/ktutorial/ktutorial-editor/tests/unit/serialization/JavascriptExporterTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/serialization/JavascriptExporter.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/serialization/JavascriptExporter.cpp 2011-04-08 15:14:19 UTC (rev 297) +++ trunk/ktutorial/ktutorial-editor/src/serialization/JavascriptExporter.cpp 2011-04-15 16:29:27 UTC (rev 298) @@ -19,7 +19,9 @@ #include "JavascriptExporter.h" #include <QRegExp> +#include <QSet> #include <QStringList> +#include <QTextDocument> #include "../data/Reaction.h" #include "../data/Step.h" @@ -137,9 +139,13 @@ if (step->text().isEmpty()) { out() << "//Error: Step without text!\n"; - } else { + } else if (Qt::mightBeRichText(step->text()) || + mightContainSemanticMarkup(step->text())) { out() << stepVariable << ".setText(t.i18nc(\"@info\", \"" << escape(step->text()) << "\"));\n"; + } else { + out() << stepVariable << ".setText(t.i18nc(\"@info/plain\", \"" + << escape(step->text()) << "\"));\n"; } mOut << '\n'; @@ -485,3 +491,26 @@ return upperCamelCase; } + +bool JavascriptExporter::mightContainSemanticMarkup(const QString& text) const { + QSet<QString> semanticMarkupTags; + semanticMarkupTags << "application" << "bcode" << "command" << "email" + << "emphasis" << "envar" << "filename" << "icode" + << "interface" << "link" << "message" << "nl" + << "numid" << "placeholder" << "resource" << "shortcut"; + semanticMarkupTags << "para" << "title" << "subtitle" << "list" << "item"; + semanticMarkupTags << "note" << "warning"; + + QRegExp tagRegExp(QString::fromLatin1("<\\s*(\\w+)[^>]*>")); + + int index = text.indexOf(tagRegExp); + while (index >= 0) { + QString tagName = tagRegExp.capturedTexts().at(1).toLower(); + if (semanticMarkupTags.contains(tagName)) { + return true; + } + index = text.indexOf(tagRegExp, index + tagRegExp.matchedLength()); + } + + return false; +} Modified: trunk/ktutorial/ktutorial-editor/src/serialization/JavascriptExporter.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/serialization/JavascriptExporter.h 2011-04-08 15:14:19 UTC (rev 297) +++ trunk/ktutorial/ktutorial-editor/src/serialization/JavascriptExporter.h 2011-04-15 16:29:27 UTC (rev 298) @@ -130,7 +130,9 @@ /** * Writes the full code (step creation, setup, tear down and adding to the * tutorial) of a step. - * If the step id isn't set, an error message is written instead. + * If the step id or name aren't set, an error message is written instead. + * When the text seems to contain rich text or semantic markup, it is + * formatted as rich text. Otherwise, it is formatted as plain text. * * @param step The step to write its code. */ @@ -312,6 +314,18 @@ */ QString toUpperCamelCase(QString text) const; + /** + * Returns true if the string text is likely to contain semantic markup. + * It just checks whether there is something that looks like a KUIT tag in + * the text. Although the result may be correct for common cases, there is + * no guarantee. + * + * @param text The string to check. + * @return True if the string text is likely to contain semantic markup, + * false otherwise. + */ + bool mightContainSemanticMarkup(const QString& text) const; + }; #endif Modified: trunk/ktutorial/ktutorial-editor/tests/unit/serialization/JavascriptExporterTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/serialization/JavascriptExporterTest.cpp 2011-04-08 15:14:19 UTC (rev 297) +++ trunk/ktutorial/ktutorial-editor/tests/unit/serialization/JavascriptExporterTest.cpp 2011-04-15 16:29:27 UTC (rev 298) @@ -65,6 +65,8 @@ void testStep(); void testStepWithEscapeSequences(); + void testStepWithRichText(); + void testStepWithSemanticMarkup(); void testStepSetupCode(); void testStepSetupCodeWithReactions(); void testStepTearDownCode(); @@ -239,7 +241,7 @@ TUTORIAL_EMPTY_INFORMATION_CODE "//Step The id1\n" "theId1Step = ktutorial.newStep(\"The id1\");\n" -"theId1Step.setText(t.i18nc(\"@info\", \"The text1\"));\n" +"theId1Step.setText(t.i18nc(\"@info/plain\", \"The text1\"));\n" "\n" "function theId1StepSetup(step) {\n" " waitForTheSignalNameByTheEmitterName = \ @@ -262,7 +264,7 @@ "\n" "//Step The id2\n" "theId2Step = ktutorial.newStep(\"The id2\");\n" -"theId2Step.setText(t.i18nc(\"@info\", \"The text2\"));\n" +"theId2Step.setText(t.i18nc(\"@info/plain\", \"The text2\"));\n" "\n" "function theId2StepSetup(step) {\n" " waitForTheSignalNameByTheEmitterName = \ @@ -301,7 +303,7 @@ TUTORIAL_EMPTY_INFORMATION_CODE "//Step The id\n" "theIdStep = ktutorial.newStep(\"The id\");\n" -"theIdStep.setText(t.i18nc(\"@info\", \"The text\"));\n" +"theIdStep.setText(t.i18nc(\"@info/plain\", \"The text\"));\n" "\n" "tutorial.addStep(theIdStep);\n" "\n"; @@ -323,7 +325,7 @@ TUTORIAL_EMPTY_INFORMATION_CODE "//Step The \"id\"\n" "theIdStep = ktutorial.newStep(\"The \\\"id\\\"\");\n" -"theIdStep.setText(t.i18nc(\"@info\", \ +"theIdStep.setText(t.i18nc(\"@info/plain\", \ \"The\\ttext\\nwith \\\"escape\\\" sequences\"));\n" "\n" "tutorial.addStep(theIdStep);\n" @@ -332,6 +334,51 @@ QCOMPARE(exportedTutorial, expected); } +void JavascriptExporterTest::testStepWithRichText() { + Tutorial tutorial; + Step* step = new Step(); + step->setId("The id"); + step->setText("The <i>rich</i> text"); + tutorial.addStep(step); + + JavascriptExporter exporter; + QString exportedTutorial = exporter.exportTutorial(&tutorial); + + QString expected = +TUTORIAL_EMPTY_INFORMATION_CODE +"//Step The id\n" +"theIdStep = ktutorial.newStep(\"The id\");\n" +"theIdStep.setText(t.i18nc(\"@info\", \"The <i>rich</i> text\"));\n" +"\n" +"tutorial.addStep(theIdStep);\n" +"\n"; + + QCOMPARE(exportedTutorial, expected); +} + +void JavascriptExporterTest::testStepWithSemanticMarkup() { + Tutorial tutorial; + Step* step = new Step(); + step->setId("The id"); + step->setText("The text with <emphasis>semantic markup</emphasis>"); + tutorial.addStep(step); + + JavascriptExporter exporter; + QString exportedTutorial = exporter.exportTutorial(&tutorial); + + QString expected = +TUTORIAL_EMPTY_INFORMATION_CODE +"//Step The id\n" +"theIdStep = ktutorial.newStep(\"The id\");\n" +"theIdStep.setText(t.i18nc(\"@info\", \ +\"The text with <emphasis>semantic markup</emphasis>\"));\n" +"\n" +"tutorial.addStep(theIdStep);\n" +"\n"; + + QCOMPARE(exportedTutorial, expected); +} + void JavascriptExporterTest::testStepSetupCode() { Tutorial tutorial; Step* step = new Step(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |