int main(int argc, char *argv[]) { QString error; QString arg; QString collectionFile; QString configFile; QString basePath; bool showHelp = false; bool showVersion = false; // don't require a window manager even though we're a QGuiApplication qputenv("QT_QPA_PLATFORM", QByteArrayLiteral("minimal")); QGuiApplication app(argc, argv); #ifndef Q_OS_WIN32 QTranslator translator; QTranslator qtTranslator; QTranslator qt_helpTranslator; QString sysLocale = QLocale::system().name(); QString resourceDir = QLibraryInfo::location(QLibraryInfo::TranslationsPath); if (translator.load(QLatin1String("assistant_") + sysLocale, resourceDir) && qtTranslator.load(QLatin1String("qt_") + sysLocale, resourceDir) && qt_helpTranslator.load(QLatin1String("qt_help_") + sysLocale, resourceDir)) { app.installTranslator(&translator); app.installTranslator(&qtTranslator); app.installTranslator(&qt_helpTranslator); } #endif // Q_OS_WIN32 for (int i=1; i<argc; ++i) { arg = QString::fromLocal8Bit(argv[i]); if (arg == QLatin1String("-o")) { if (++i < argc) { QFileInfo fi(QString::fromLocal8Bit(argv[i])); collectionFile = fi.absoluteFilePath(); } else { error = QCG::tr("Missing output file name."); } } else if (arg == QLatin1String("-h")) { showHelp = true; } else if (arg == QLatin1String("-v")) { showVersion = true; } else { QFileInfo fi(arg); configFile = fi.absoluteFilePath(); basePath = fi.absolutePath(); } } if (showVersion) { fputs(qPrintable(QCG::tr("Qt Collection Generator version 1.0 (Qt %1)\n") .arg(QT_VERSION_STR)), stdout); return 0; } if (configFile.isEmpty() && !showHelp) error = QCG::tr("Missing collection config file."); QString help = QCG::tr("\nUsage:\n\n" "qcollectiongenerator <collection-config-file> [options]\n\n" " -o <collection-file> Generates a collection file\n" " called <collection-file>. If\n" " this option is not specified\n" " a default name will be used.\n" " -v Displays the version of\n" " qcollectiongenerator.\n\n"); if (showHelp) { fputs(qPrintable(help), stdout); return 0; } else if (!error.isEmpty()) { fprintf(stderr, "%s\n\n%s", qPrintable(error), qPrintable(help)); return -1; } QFile file(configFile); if (!file.open(QIODevice::ReadOnly)) { fputs(qPrintable(QCG::tr("Could not open %1.\n").arg(configFile)), stderr); return -1; } if (collectionFile.isEmpty()) { QFileInfo fi(configFile); collectionFile = basePath + QDir::separator() + fi.baseName() + QLatin1String(".qhc"); } fputs(qPrintable(QCG::tr("Reading collection config file...\n")), stdout); CollectionConfigReader config; config.readData(file.readAll()); if (config.hasError()) { fputs(qPrintable(QCG::tr("Collection config file error: %1\n") .arg(config.errorString())), stderr); return -1; } QMap<QString, QString>::const_iterator it = config.filesToGenerate().constBegin(); while (it != config.filesToGenerate().constEnd()) { fputs(qPrintable(QCG::tr("Generating help for %1...\n").arg(it.key())), stdout); QHelpProjectData helpData; if (!helpData.readData(absoluteFileName(basePath, it.key()))) { fprintf(stderr, "%s\n", qPrintable(helpData.errorMessage())); return -1; } HelpGenerator helpGenerator; if (!helpGenerator.generate(&helpData, absoluteFileName(basePath, it.value()))) { fprintf(stderr, "%s\n", qPrintable(helpGenerator.error())); return -1; } ++it; } fputs(qPrintable(QCG::tr("Creating collection file...\n")), stdout); QFileInfo colFi(collectionFile); if (colFi.exists()) { if (!colFi.dir().remove(colFi.fileName())) { fputs(qPrintable(QCG::tr("The file %1 cannot be overwritten.\n") .arg(collectionFile)), stderr); return -1; } } QHelpEngineCore helpEngine(collectionFile); if (!helpEngine.setupData()) { fprintf(stderr, "%s\n", qPrintable(helpEngine.error())); return -1; } foreach (const QString &file, config.filesToRegister()) { if (!helpEngine.registerDocumentation(absoluteFileName(basePath, file))) { fprintf(stderr, "%s\n", qPrintable(helpEngine.error())); return -1; } } if (!config.filesToRegister().isEmpty()) CollectionConfiguration::updateLastRegisterTime(helpEngine); if (!config.title().isEmpty()) CollectionConfiguration::setWindowTitle(helpEngine, config.title()); if (!config.homePage().isEmpty()) { CollectionConfiguration::setDefaultHomePage(helpEngine, config.homePage()); } if (!config.startPage().isEmpty()) { CollectionConfiguration::setLastShownPages(helpEngine, QStringList(config.startPage())); } if (!config.currentFilter().isEmpty()) { helpEngine.setCurrentFilter(config.currentFilter()); } if (!config.cacheDirectory().isEmpty()) { CollectionConfiguration::setCacheDir(helpEngine, config.cacheDirectory(), config.cacheDirRelativeToCollection()); } CollectionConfiguration::setFilterFunctionalityEnabled(helpEngine, config.enableFilterFunctionality()); CollectionConfiguration::setFilterToolbarVisible(helpEngine, !config.hideFilterFunctionality()); CollectionConfiguration::setDocumentationManagerEnabled(helpEngine, config.enableDocumentationManager()); CollectionConfiguration::setAddressBarEnabled(helpEngine, config.enableAddressBar()); CollectionConfiguration::setAddressBarVisible(helpEngine, !config.hideAddressBar()); CollectionConfiguration::setCreationTime(helpEngine, QDateTime::currentDateTime().toTime_t()); CollectionConfiguration::setFullTextSearchFallbackEnabled(helpEngine, config.fullTextSearchFallbackEnabled()); if (!config.applicationIcon().isEmpty()) { QFile icon(absoluteFileName(basePath, config.applicationIcon())); if (!icon.open(QIODevice::ReadOnly)) { fputs(qPrintable(QCG::tr("Cannot open %1.\n").arg(icon.fileName())), stderr); return -1; } CollectionConfiguration::setApplicationIcon(helpEngine, icon.readAll()); } if (config.aboutMenuTexts().count()) { QByteArray ba; QDataStream s(&ba, QIODevice::WriteOnly); QMap<QString, QString>::const_iterator it = config.aboutMenuTexts().constBegin(); while (it != config.aboutMenuTexts().constEnd()) { s << it.key(); s << it.value(); ++it; } CollectionConfiguration::setAboutMenuTexts(helpEngine, ba); } if (!config.aboutIcon().isEmpty()) { QFile icon(absoluteFileName(basePath, config.aboutIcon())); if (!icon.open(QIODevice::ReadOnly)) { fputs(qPrintable(QCG::tr("Cannot open %1.\n").arg(icon.fileName())), stderr); return -1; } CollectionConfiguration::setAboutIcon(helpEngine, icon.readAll()); } if (config.aboutTextFiles().count()) { QByteArray ba; QDataStream s(&ba, QIODevice::WriteOnly); QMap<QString, QString>::const_iterator it = config.aboutTextFiles().constBegin(); QMap<QString, QByteArray> imgData; QRegExp srcRegExp(QLatin1String("src=(\"(.+)\"|([^\"\\s]+)).*>")); srcRegExp.setMinimal(true); QRegExp imgRegExp(QLatin1String("(<img[^>]+>)")); imgRegExp.setMinimal(true); while (it != config.aboutTextFiles().constEnd()) { s << it.key(); QFileInfo fi(absoluteFileName(basePath, it.value())); QFile f(fi.absoluteFilePath()); if (!f.open(QIODevice::ReadOnly)) { fputs(qPrintable(QCG::tr("Cannot open %1.\n").arg(f.fileName())), stderr); return -1; } QByteArray data = f.readAll(); s << data; QString contents = QString::fromUtf8(data); int pos = 0; while ((pos = imgRegExp.indexIn(contents, pos)) != -1) { QString imgTag = imgRegExp.cap(1); pos += imgRegExp.matchedLength(); if (srcRegExp.indexIn(imgTag, 0) != -1) { QString src = srcRegExp.cap(2); if (src.isEmpty()) src = srcRegExp.cap(3); QFile img(fi.absolutePath() + QDir::separator() + src); if (img.open(QIODevice::ReadOnly)) { if (!imgData.contains(src)) imgData.insert(src, img.readAll()); } else { fputs(qPrintable(QCG::tr("Cannot open referenced image file %1.\n") .arg(img.fileName())), stderr); } } } ++it; } CollectionConfiguration::setAboutTexts(helpEngine, ba); if (imgData.count()) { QByteArray imageData; QBuffer buffer(&imageData); buffer.open(QIODevice::WriteOnly); QDataStream out(&buffer); out << imgData; CollectionConfiguration::setAboutImages(helpEngine, imageData); } } return 0; }
QT_USE_NAMESPACE int main(int argc, char *argv[]) { QString error; QString arg; QString compressedFile; QString projectFile; QString basePath; bool showHelp = false; bool showVersion = false; bool checkLinks = false; for (int i = 1; i < argc; ++i) { arg = QString::fromLocal8Bit(argv[i]); if (arg == QLatin1String("-o")) { if (++i < argc) { QFileInfo fi(QString::fromLocal8Bit(argv[i])); compressedFile = fi.absoluteFilePath(); } else { error = QCoreApplication::translate("QHelpGenerator", "Missing output file name!"); } } else if (arg == QLatin1String("-v")) { showVersion = true; } else if (arg == QLatin1String("-h")) { showHelp = true; } else if (arg == QLatin1String("-c")) { checkLinks = true; } else { QFileInfo fi(arg); projectFile = fi.absoluteFilePath(); basePath = fi.absolutePath(); } } if (showVersion) { fprintf(stdout, "Qt Help Generator version 1.0 (Qt %s)\n", QT_VERSION_STR); return 0; } if (projectFile.isEmpty() && !showHelp) error = QCoreApplication::translate("QHelpGenerator", "Missing Qt help project file!"); QString help = QCoreApplication::translate("QHelpGenerator", "\nUsage:\n\n" "qhelpgenerator <help-project-file> [options]\n\n" " -o <compressed-file> Generates a Qt compressed help\n" " file called <compressed-file>.\n" " If this option is not specified\n" " a default name will be used.\n" " -c Checks whether all links in HTML files\n" " point to files in this help project.\n" " -v Displays the version of \n" " qhelpgenerator.\n\n"); if (showHelp) { fprintf(stdout, "%s", qPrintable(help)); return 0; }else if (!error.isEmpty()) { fprintf(stderr, "%s\n\n%s", qPrintable(error), qPrintable(help)); return -1; } QFile file(projectFile); if (!file.open(QIODevice::ReadOnly)) { fprintf(stderr, "Could not open %s!\n", qPrintable(projectFile)); return -1; } if (compressedFile.isEmpty()) { if (!checkLinks) { QFileInfo fi(projectFile); compressedFile = basePath + QDir::separator() + fi.baseName() + QLatin1String(".qch"); } } else { // check if the output dir exists -- create if it doesn't QFileInfo fi(compressedFile); QDir parentDir = fi.dir(); if (!parentDir.exists()) { if (!parentDir.mkpath(QLatin1String("."))) { fprintf(stderr, "Could not create output directory: %s\n", qPrintable(parentDir.path())); } } } QHelpProjectData *helpData = new QHelpProjectData(); if (!helpData->readData(projectFile)) { fprintf(stderr, "%s\n", qPrintable(helpData->errorMessage())); return -1; } QCoreApplication app(argc, argv); HelpGenerator generator; bool success = true; if (checkLinks) success = generator.checkLinks(*helpData); if (success && !compressedFile.isEmpty()) success = generator.generate(helpData, compressedFile); delete helpData; if (!success) { fprintf(stderr, "%s\n", qPrintable(generator.error())); return -1; } return 0; }
int main(int argc, char *argv[]) { QString error; QString arg; QString compressedFile; QString projectFile; QString basePath; bool showHelp = false; bool showVersion = false; bool checkLinks = false; // don't require a window manager even though we're a QGuiApplication qputenv("QT_QPA_PLATFORM", QByteArrayLiteral("minimal")); QGuiApplication app(argc, argv); #ifndef Q_OS_WIN32 QTranslator translator; QTranslator qtTranslator; QTranslator qt_helpTranslator; QString sysLocale = QLocale::system().name(); QString resourceDir = QLibraryInfo::location(QLibraryInfo::TranslationsPath); if (translator.load(QLatin1String("assistant_") + sysLocale, resourceDir) && qtTranslator.load(QLatin1String("qt_") + sysLocale, resourceDir) && qt_helpTranslator.load(QLatin1String("qt_help_") + sysLocale, resourceDir)) { app.installTranslator(&translator); app.installTranslator(&qtTranslator); app.installTranslator(&qt_helpTranslator); } #endif // Q_OS_WIN32 for (int i = 1; i < argc; ++i) { arg = QString::fromLocal8Bit(argv[i]); if (arg == QLatin1String("-o")) { if (++i < argc) { QFileInfo fi(QString::fromLocal8Bit(argv[i])); compressedFile = fi.absoluteFilePath(); } else { error = QHG::tr("Missing output file name."); } } else if (arg == QLatin1String("-v")) { showVersion = true; } else if (arg == QLatin1String("-h")) { showHelp = true; } else if (arg == QLatin1String("-c")) { checkLinks = true; } else { QFileInfo fi(arg); projectFile = fi.absoluteFilePath(); basePath = fi.absolutePath(); } } if (showVersion) { fputs(qPrintable(QHG::tr("Qt Help Generator version 1.0 (Qt %1)\n") .arg(QT_VERSION_STR)), stdout); return 0; } if (projectFile.isEmpty() && !showHelp) error = QHG::tr("Missing Qt help project file."); QString help = QHG::tr("\nUsage:\n\n" "qhelpgenerator <help-project-file> [options]\n\n" " -o <compressed-file> Generates a Qt compressed help\n" " file called <compressed-file>.\n" " If this option is not specified\n" " a default name will be used.\n" " -c Checks whether all links in HTML files\n" " point to files in this help project.\n" " -v Displays the version of \n" " qhelpgenerator.\n\n"); if (showHelp) { fputs(qPrintable(help), stdout); return 0; }else if (!error.isEmpty()) { fprintf(stderr, "%s\n\n%s", qPrintable(error), qPrintable(help)); return -1; } QFile file(projectFile); if (!file.open(QIODevice::ReadOnly)) { fputs(qPrintable(QHG::tr("Could not open %1.\n").arg(projectFile)), stderr); return -1; } if (compressedFile.isEmpty()) { if (!checkLinks) { QFileInfo fi(projectFile); compressedFile = basePath + QDir::separator() + fi.baseName() + QLatin1String(".qch"); } } else { // check if the output dir exists -- create if it doesn't QFileInfo fi(compressedFile); QDir parentDir = fi.dir(); if (!parentDir.exists()) { if (!parentDir.mkpath(QLatin1String("."))) { fputs(qPrintable(QHG::tr("Could not create output directory: %1\n") .arg(parentDir.path())), stderr); } } } QHelpProjectData *helpData = new QHelpProjectData(); if (!helpData->readData(projectFile)) { fprintf(stderr, "%s\n", qPrintable(helpData->errorMessage())); return -1; } HelpGenerator generator; bool success = true; if (checkLinks) success = generator.checkLinks(*helpData); if (success && !compressedFile.isEmpty()) success = generator.generate(helpData, compressedFile); delete helpData; if (!success) { fprintf(stderr, "%s\n", qPrintable(generator.error())); return -1; } return 0; }
int main(int argc, char *argv[]) { QString error; QString arg; QString collectionFile; QString configFile; QString basePath; bool showHelp = false; bool showVersion = false; for (int i=1; i<argc; ++i) { arg = QString::fromLocal8Bit(argv[i]); if (arg == QLatin1String("-o")) { if (++i < argc) { QFileInfo fi(QString::fromLocal8Bit(argv[i])); collectionFile = fi.absoluteFilePath(); } else { error = QObject::tr("Missing output file name!"); } } else if (arg == QLatin1String("-h")) { showHelp = true; } else if (arg == QLatin1String("-v")) { showVersion = true; } else { QFileInfo fi(arg); configFile = fi.absoluteFilePath(); basePath = fi.absolutePath(); } } if (showVersion) { fprintf(stdout, "Qt Collection Generator version 1.0 (Qt %s)\n", QT_VERSION_STR); return 0; } if (configFile.isEmpty() && !showHelp) error = QObject::tr("Missing collection config file!"); QString help = QObject::tr("\nUsage:\n\n" "qcollectiongenerator <collection-config-file> [options]\n\n" " -o <collection-file> Generates a collection file\n" " called <collection-file>. If\n" " this option is not specified\n" " a default name will be used.\n" " -v Displays the version of\n" " qcollectiongenerator.\n\n"); if (showHelp) { fprintf(stdout, "%s", qPrintable(help)); return 0; }else if (!error.isEmpty()) { fprintf(stderr, "%s\n\n%s", qPrintable(error), qPrintable(help)); return -1; } QFile file(configFile); if (!file.open(QIODevice::ReadOnly)) { fprintf(stderr, "Could not open %s!\n", qPrintable(configFile)); return -1; } if (collectionFile.isEmpty()) { QFileInfo fi(configFile); collectionFile = basePath + QDir::separator() + fi.baseName() + QLatin1String(".qhc"); } QCoreApplication app(argc, argv); fprintf(stdout, "Reading collection config file...\n"); CollectionConfigReader config; config.readData(file.readAll()); if (config.hasError()) { fprintf(stderr, "Collection config file error: %s\n", qPrintable(config.errorString())); return -1; } QMap<QString, QString>::const_iterator it = config.filesToGenerate().constBegin(); while (it != config.filesToGenerate().constEnd()) { fprintf(stdout, "Generating help for %s...\n", qPrintable(it.key())); QHelpProjectData helpData; if (!helpData.readData(basePath + QDir::separator() + it.key())) { fprintf(stderr, "%s\n", qPrintable(helpData.errorMessage())); return -1; } HelpGenerator helpGenerator; if (!helpGenerator.generate(&helpData, basePath + QDir::separator() + it.value())) { fprintf(stderr, "%s\n", qPrintable(helpGenerator.error())); return -1; } ++it; } fprintf(stdout, "Creating collection file...\n"); QFileInfo colFi(collectionFile); if (colFi.exists()) { if (!colFi.dir().remove(colFi.fileName())) { fprintf(stderr, "The file %s cannot be overwritten!\n", qPrintable(collectionFile)); return -1; } } QHelpEngineCore helpEngine(collectionFile); if (!helpEngine.setupData()) { fprintf(stderr, "%s\n", qPrintable(helpEngine.error())); return -1; } foreach (const QString &file, config.filesToRegister()) { if (!helpEngine.registerDocumentation(basePath + QDir::separator() + file)) { fprintf(stderr, "%s\n", qPrintable(helpEngine.error())); return -1; } } if (!config.title().isEmpty()) helpEngine.setCustomValue(QLatin1String("WindowTitle"), config.title()); if (!config.homePage().isEmpty()) { helpEngine.setCustomValue(QLatin1String("defaultHomepage"), config.homePage()); } if (!config.startPage().isEmpty()) helpEngine.setCustomValue(QLatin1String("LastShownPages"), config.startPage()); if (!config.currentFilter().isEmpty()) helpEngine.setCustomValue(QLatin1String("CurrentFilter"), config.currentFilter()); if (!config.cacheDirectory().isEmpty()) helpEngine.setCustomValue(QLatin1String("CacheDirectory"), config.cacheDirectory()); helpEngine.setCustomValue(QLatin1String("EnableFilterFunctionality"), config.enableFilterFunctionality()); helpEngine.setCustomValue(QLatin1String("HideFilterFunctionality"), config.hideFilterFunctionality()); helpEngine.setCustomValue(QLatin1String("EnableDocumentationManager"), config.enableDocumentationManager()); helpEngine.setCustomValue(QLatin1String("EnableAddressBar"), config.enableAddressBar()); helpEngine.setCustomValue(QLatin1String("HideAddressBar"), config.hideAddressBar()); helpEngine.setCustomValue(QLatin1String("CreationTime"), QDateTime::currentDateTime().toTime_t()); if (!config.applicationIcon().isEmpty()) { QFile icon(basePath + QDir::separator() + config.applicationIcon()); if (!icon.open(QIODevice::ReadOnly)) { fprintf(stderr, "Cannot open %s!\n", qPrintable(icon.fileName())); return -1; } helpEngine.setCustomValue(QLatin1String("ApplicationIcon"), icon.readAll()); } if (config.aboutMenuTexts().count()) { QByteArray ba; QDataStream s(&ba, QIODevice::WriteOnly); QMap<QString, QString>::const_iterator it = config.aboutMenuTexts().constBegin(); while (it != config.aboutMenuTexts().constEnd()) { s << it.key(); s << it.value(); ++it; } helpEngine.setCustomValue(QLatin1String("AboutMenuTexts"), ba); } if (!config.aboutIcon().isEmpty()) { QFile icon(basePath + QDir::separator() + config.aboutIcon()); if (!icon.open(QIODevice::ReadOnly)) { fprintf(stderr, "Cannot open %s!\n", qPrintable(icon.fileName())); return -1; } helpEngine.setCustomValue(QLatin1String("AboutIcon"), icon.readAll()); } if (config.aboutTextFiles().count()) { QByteArray ba; QDataStream s(&ba, QIODevice::WriteOnly); QMap<QString, QString>::const_iterator it = config.aboutTextFiles().constBegin(); QMap<QString, QByteArray> imgData; QRegExp srcRegExp(QLatin1String("src=(\"(.+)\"|([^\"\\s]+)).*>")); srcRegExp.setMinimal(true); QRegExp imgRegExp(QLatin1String("(<img[^>]+>)")); imgRegExp.setMinimal(true); while (it != config.aboutTextFiles().constEnd()) { s << it.key(); QFileInfo fi(basePath + QDir::separator() + it.value()); QFile f(fi.absoluteFilePath()); if (!f.open(QIODevice::ReadOnly)) { fprintf(stderr, "Cannot open %s!\n", qPrintable(f.fileName())); return -1; } QByteArray data = f.readAll(); s << data; QString contents = QString::fromUtf8(data); int pos = 0; while ((pos = imgRegExp.indexIn(contents, pos)) != -1) { QString imgTag = imgRegExp.cap(1); pos += imgRegExp.matchedLength(); if (srcRegExp.indexIn(imgTag, 0) != -1) { QString src = srcRegExp.cap(2); if (src.isEmpty()) src = srcRegExp.cap(3); QFile img(fi.absolutePath() + QDir::separator() + src); if (img.open(QIODevice::ReadOnly)) { if (!imgData.contains(src)) imgData.insert(src, img.readAll()); } else { fprintf(stderr, "Cannot open referenced image file %s!\n", qPrintable(img.fileName())); } } } ++it; } helpEngine.setCustomValue(QLatin1String("AboutTexts"), ba); if (imgData.count()) { QByteArray imageData; QBuffer buffer(&imageData); buffer.open(QIODevice::WriteOnly); QDataStream out(&buffer); out << imgData; helpEngine.setCustomValue(QLatin1String("AboutImages"), imageData); } } return 0; }