From cc44058968102429f65a50646f452a334e85597e Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Thu, 1 Feb 2018 09:35:13 +0100 Subject: shiboken/QtDocGenerator: Copy images Previously, the doc generator would rewrite the URLs to point to QTDIR/doc/src/images where the images were located in Qt 4. Add a function to copy the images from the webxml/images directory to a matching directory under rst where they can be picked up by sphinx. Task-number: PYSIDE-363 Change-Id: I1da83a7717dd61a9c0b80a7cc18444e00a1f4c1b Reviewed-by: Cristian Maureira-Fredes Reviewed-by: Venugopal Shivashankar Reviewed-by: Alexandru Croitor --- .../shiboken2/generator/qtdoc/qtdocgenerator.cpp | 68 +++++++++++++++++++--- sources/shiboken2/generator/qtdoc/qtdocgenerator.h | 2 + 2 files changed, 63 insertions(+), 7 deletions(-) (limited to 'sources/shiboken2/generator') diff --git a/sources/shiboken2/generator/qtdoc/qtdocgenerator.cpp b/sources/shiboken2/generator/qtdoc/qtdocgenerator.cpp index 1310f5ac0..f8db29f95 100644 --- a/sources/shiboken2/generator/qtdoc/qtdocgenerator.cpp +++ b/sources/shiboken2/generator/qtdoc/qtdocgenerator.cpp @@ -694,21 +694,75 @@ void QtXmlToSphinx::handleLinkTag(QXmlStreamReader& reader) } } +// Copy images that are placed in a subdirectory "images" under the webxml files +// by qdoc to a matching subdirectory under the "rst/PySide2/" directory +static bool copyImage(const QString &href, const QString &docDataDir, + const QString &context, const QString &outputDir, + QString *errorMessage) +{ + const QChar slash = QLatin1Char('/'); + const int lastSlash = href.lastIndexOf(slash); + const QString imagePath = lastSlash != -1 ? href.left(lastSlash) : QString(); + const QString imageFileName = lastSlash != -1 ? href.right(href.size() - lastSlash - 1) : href; + QFileInfo imageSource(docDataDir + slash + href); + if (!imageSource.exists()) { + QTextStream(errorMessage) << "Image " << href << " does not exist in " + << QDir::toNativeSeparators(docDataDir); + return false; + } + // Determine directory from context, "Pyside2.QtGui.QPainter" ->"Pyside2/QtGui". + // FIXME: Not perfect yet, should have knowledge about namespaces (DataVis3D) or + // nested classes "Pyside2.QtGui.QTouchEvent.QTouchPoint". + QString relativeTargetDir = context; + const int lastDot = relativeTargetDir.lastIndexOf(QLatin1Char('.')); + if (lastDot != -1) + relativeTargetDir.truncate(lastDot); + relativeTargetDir.replace(QLatin1Char('.'), slash); + if (!imagePath.isEmpty()) + relativeTargetDir += slash + imagePath; + + const QString targetDir = outputDir + slash + relativeTargetDir; + const QString targetFileName = targetDir + slash + imageFileName; + if (QFileInfo::exists(targetFileName)) + return true; + if (!QFileInfo::exists(targetDir)) { + const QDir outDir(outputDir); + if (!outDir.mkpath(relativeTargetDir)) { + QTextStream(errorMessage) << "Cannot create " << QDir::toNativeSeparators(relativeTargetDir) + << " under " << QDir::toNativeSeparators(outputDir); + return false; + } + } + + QFile source(imageSource.absoluteFilePath()); + if (!source.copy(targetFileName)) { + QTextStream(errorMessage) << "Cannot copy " << QDir::toNativeSeparators(source.fileName()) + << " to " << QDir::toNativeSeparators(targetFileName) << ": " + << source.errorString(); + return false; + } + qCDebug(lcShiboken()).noquote().nospace() << __FUNCTION__ << " href=\"" + << href << "\", context=\"" << context << "\", docDataDir=\"" + << docDataDir << "\", outputDir=\"" << outputDir << "\", copied \"" + << source.fileName() << "\"->\"" << targetFileName << '"'; + return true; +} + void QtXmlToSphinx::handleImageTag(QXmlStreamReader& reader) { QXmlStreamReader::TokenType token = reader.tokenType(); if (token == QXmlStreamReader::StartElement) { QString href = reader.attributes().value(QLatin1String("href")).toString(); - QString packageName = m_generator->packageName(); - packageName.replace(QLatin1Char('.'), QLatin1Char('/')); - QDir dir(m_generator->outputDirectory() + QLatin1Char('/') + packageName); - QString imgPath = dir.relativeFilePath(m_generator->libSourceDir() + QLatin1String("/doc/src/")) - + QLatin1Char('/') + href; + QString errorMessage; + if (!copyImage(href,m_generator->docDataDir(), m_context, + m_generator->outputDirectory(), &errorMessage)) { + qCWarning(lcShiboken, "%s", qPrintable(errorMessage)); + } if (reader.name() == QLatin1String("image")) - m_output << INDENT << ".. image:: " << imgPath << endl << endl; + m_output << INDENT << ".. image:: " << href << endl << endl; else - m_output << ".. image:: " << imgPath << ' '; + m_output << ".. image:: " << href << ' '; } } diff --git a/sources/shiboken2/generator/qtdoc/qtdocgenerator.h b/sources/shiboken2/generator/qtdoc/qtdocgenerator.h index 6ba9e7907..7b3be2ebc 100644 --- a/sources/shiboken2/generator/qtdoc/qtdocgenerator.h +++ b/sources/shiboken2/generator/qtdoc/qtdocgenerator.h @@ -179,6 +179,8 @@ public: return m_libSourceDir; } + QString docDataDir() const { return m_docDataDir; } + bool doSetup(const QMap& args); const char* name() const -- cgit v1.2.3