QString OtamaConverter::convertToErb(const QString &html, const QString &otm, int trimMode)
{
    if (replaceMarker.isEmpty()) {
        // Sets a replace-marker
        QSettings devSetting(devIni, QSettings::IniFormat);
        replaceMarker = devSetting.value("Otama.ReplaceMarker", "%%").toString();
    }

    // Parses HTML and Otama files
    THtmlParser htmlParser((THtmlParser::TrimMode)trimMode);
    htmlParser.parse(html);

    OtmParser otmParser(replaceMarker);
    otmParser.parse(otm);

    // Inserts include-header
    QStringList inc = otmParser.includeStrings();
    for (QListIterator<QString> it(inc); it.hasNext(); ) {
        THtmlElement &e = htmlParser.insertNewElement(0, 0);
        e.text  = LEFT_DELIM.trimmed();
        e.text += it.next();
        e.text += RIGHT_DELIM;
    }

    // Inserts init-code
    QString init = otmParser.getInitSrcCode();
    if (!init.isEmpty()) {
        THtmlElement &e = htmlParser.at(0);
        e.text = LEFT_DELIM + init + RIGHT_DELIM + e.text;
    }

    for (int i = htmlParser.elementCount() - 1; i > 0; --i) {
        THtmlElement &e = htmlParser.at(i);
        QString label = e.attribute(TF_ATTRIBUTE_NAME);

        if (e.hasAttribute(TF_ATTRIBUTE_NAME)) {
            e.removeAttribute(TF_ATTRIBUTE_NAME);
        }

        if (label == DUMMY_LABEL) {
            htmlParser.removeElementTree(i, true);
            continue;
        }

        if (label == DUMMYTAG_LABEL) {
            htmlParser.removeTag(i);
            continue;
        }

        QString val;
        OtmParser::EchoOption ech;
        bool scriptArea = htmlParser.parentExists(i, "script");

        // Content assignment
        val = otmParser.getSrcCode(label, OtmParser::ContentAssignment, &ech); // ~ operator
        if (!val.isEmpty()) {
            htmlParser.removeChildElements(i);
            e.text = generateErbPhrase(val, ech);
        } else {
            QStringList vals = otmParser.getWrapSrcCode(label, OtmParser::ContentAssignment);
            if (!vals.isEmpty()) {
                // Adds block codes
                QString bak = e.text;
                e.text  = LEFT_DELIM;
                e.text += vals[0].trimmed();
                e.text += (scriptArea ? RIGHT_DELIM_NO_TRIM : RIGHT_DELIM);
                e.text += bak;
                QString s = vals.value(1).trimmed();
                if (!s.isEmpty()) {
                    THtmlElement &child = htmlParser.appendNewElement(i);
                    child.text  = LEFT_DELIM;
                    child.text += s;
                    child.text += (scriptArea ? RIGHT_DELIM_NO_TRIM : RIGHT_DELIM);
                }
            }
        }

        // Tag merging
        val = otmParser.getSrcCode(label, OtmParser::TagMerging, &ech); // |== operator
        if (!val.isEmpty()) {
            val.remove(QRegExp(";+$"));

            QString attr;
            attr  = LEFT_DELIM;
            attr += QLatin1String("do { THtmlParser ___pr = THtmlParser::mergeElements(tr(\"");
            attr += ErbConverter::escapeNewline(e.toString());
            attr += QLatin1String("\"), (");

            if (ech == OtmParser::ExportVarEcho) {  // if |==$ then
                attr += QLatin1String("T_VARIANT(");
                attr += val;
                attr += QLatin1String(")");
            } else {
                attr += val;
            }
            attr += QLatin1String(")); ");
            attr += QLatin1String("echo(___pr.at(1).attributesString()); ");
            attr += (scriptArea ? RIGHT_DELIM_NO_TRIM : RIGHT_DELIM);
            e.attributes.clear();
            e.attributes << qMakePair(attr, QString());
            e.text  = LEFT_DELIM;
            e.text += QLatin1String("eh(___pr.at(1).text); ");
            e.text += QLatin1String("echo(___pr.childElementsToString(1)); } while (0);");
            e.text += (scriptArea ? RIGHT_DELIM_NO_TRIM : RIGHT_DELIM);
        }

        // Sets attributes
        val = otmParser.getSrcCode(label, OtmParser::AttributeSet, &ech); // + operator
        if (!val.isEmpty()) {
            QString s = generateErbPhrase(val, ech);
            e.setAttribute(s, QString());
        }

        // Replaces the element
        val = otmParser.getSrcCode(label, OtmParser::TagReplacement, &ech); // : operator
        if (!val.isEmpty()) {
            // Sets the variable
            htmlParser.removeElementTree(i);
            e.text = generateErbPhrase(val, ech);
        } else {
            QStringList vals = otmParser.getWrapSrcCode(label, OtmParser::TagReplacement);
            if (!vals.isEmpty()) {
                // Adds block codes
                int idx = htmlParser.at(e.parent).children.indexOf(i);
                THtmlElement &he1 = htmlParser.insertNewElement(e.parent, idx);
                he1.text  = LEFT_DELIM;
                he1.text += vals[0].trimmed();
                he1.text += (scriptArea ? RIGHT_DELIM_NO_TRIM : RIGHT_DELIM);

                QString s = vals.value(1).trimmed();
                if (!s.isEmpty()) {
                    THtmlElement &he2 = htmlParser.insertNewElement(e.parent, idx + 2);
                    he2.text  = LEFT_DELIM;
                    he2.text += s;
                    he2.text += (scriptArea ? RIGHT_DELIM_NO_TRIM : RIGHT_DELIM);
                }
            }
        }
    }

    return htmlParser.toString();
}
Exemple #2
0
int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    int res = 0;
    QString appIni;
    QMap<QString, QString> args = convertArgs(QCoreApplication::arguments());

    if (!args.value("-f").isEmpty()) {
        appIni = args.value("-f");
        devIni = QFileInfo(appIni).dir().path() + QDir::separator() + "development.ini";
    } else {
        QString dir = QLatin1String("..") + QDir::separator() + QLatin1String("..") + QDir::separator() + "config" +  QDir::separator();
        appIni = dir + "application.ini";
        devIni = dir + "development.ini";
    }

    if (!QFile::exists(appIni)) {
        usage();
        return 1;
    }

    QSettings appSetting(appIni, QSettings::IniFormat);
    QSettings devSetting(devIni, QSettings::IniFormat);

    // Default codec
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QString codecName = appSetting.value("InternalEncoding").toString();
    if (!codecName.isEmpty()) {
        QTextCodec *c = QTextCodec::codecForName(codecName.toLatin1().constData());
        if (c) {
            codec = c;
        }
    }
    QTextCodec::setCodecForLocale(codec);

    defaultTrimMode = devSetting.value("Erb.DefaultTrimMode", "1").toInt();
    printf("Erb.DefaultTrimMode: %d\n", defaultTrimMode);

    QDir viewDir(".");
    if (!args.value("-v").isEmpty()) {
        viewDir.setPath(args.value("-v"));
    }
    if (!viewDir.exists()) {
        usage();
        return 1;
    }

    QDir outputDir(DEFAULT_OUTPUT_DIR);
    if (!args.value("-d").isEmpty()) {
        outputDir.setPath(args.value("-d"));
    }

    if (outputDir.exists()) {
        if (outputDir.path() != ".") {
            printf("  exists   %s\n", qPrintable(outputDir.path()));
        }
    } else {
        if (outputDir.mkpath(".")) {
            printf("  created  %s\n", qPrintable(outputDir.path()));
        } else {
            usage();
            return 1;
        }
    }

    bool createProFile = (args.contains("-p") || !args.contains("-P"));
    ViewConverter conv(viewDir, outputDir, createProFile);
    QString templateSystem = devSetting.value("TemplateSystem").toString();
    if (templateSystem.isEmpty()) {
        templateSystem = appSetting.value("TemplateSystem", "Erb").toString();
    }

    res = conv.convertView(templateSystem);
    return res;
}