Commit 6b0ddf4c authored by Alexander A. Maly's avatar Alexander A. Maly

Astyled DocBookViewer

parent 51f174af
Pipeline #2579 failed with stages
in 2 minutes and 4 seconds
......@@ -6,7 +6,6 @@
#include <kumir2/editorinterface.h>
#include <kumir2-libs/widgets/iconprovider.h>
#include <QUrl>
#include <QMenu>
#include <QScrollBar>
#include <QApplication>
......
......@@ -3,7 +3,6 @@
#include "contentrenderer.h"
#include <QtGlobal>
#include <QTextBrowser>
class QMenu;
......
// Self includes
#include "docbookfactory.h"
#include "docbookmodel.h"
#include "document.h"
// Qt includes
#include <QtCore>
#include <QtXml>
#include <QApplication>
#include <QPalette>
#include <QDir>
#include <QDebug>
namespace DocBookViewer {
namespace DocBookViewer
{
DocBookFactory* DocBookFactory::self()
DocBookFactory *DocBookFactory::self()
{
static DocBookFactory * instance = new DocBookFactory();
return instance;
static DocBookFactory *instance = new DocBookFactory();
return instance;
}
DocBookFactory::DocBookFactory()
: reader_(new QXmlSimpleReader)
, doc_(0)
, root_(0)
: reader_(new QXmlSimpleReader)
, doc_(0)
, root_(0)
{
reader_->setContentHandler(this);
reader_->setErrorHandler(this);
reader_->setContentHandler(this);
reader_->setErrorHandler(this);
}
Document DocBookFactory::parseDocument(const QMap<ModelType,QString> roleValues, const QUrl &url, QString *error) const
Document DocBookFactory::parseDocument(const QMap<ModelType, QString> roleValues, const QUrl &url, QString *error) const
{
// TODO network url loading
const QString fileName = url.toLocalFile();
ModelPtr content;
QFile file(fileName);
if (file.open(QIODevice::ReadOnly)) {
content = parseDocument(roleValues, &file, url, error);
file.close();
}
return Document(url, content);
// TODO network url loading
const QString fileName = url.toLocalFile();
ModelPtr content;
QFile file(fileName);
if (file.open(QIODevice::ReadOnly)) {
content = parseDocument(roleValues, &file, url, error);
file.close();
}
return Document(url, content);
}
Document DocBookFactory::createNamedSet(const QString &name, const QList<Document> documents) const
{
ModelPtr namedSetRoot(new DocBookModel(ModelPtr(), Set));
namedSetRoot->title_ = name;
Q_FOREACH(const Document & doc, documents) {
namedSetRoot->children_.append(doc.root_);
doc.root_->parent_ = namedSetRoot;
}
return Document(QUrl(), namedSetRoot);
ModelPtr namedSetRoot(new DocBookModel(ModelPtr(), Set));
namedSetRoot->title_ = name;
Q_FOREACH (const Document &doc, documents) {
namedSetRoot->children_.append(doc.root_);
doc.root_->parent_ = namedSetRoot;
}
return Document(QUrl(), namedSetRoot);
}
void DocBookFactory::setConfigurationName(const QString &configurationName)
{
configurationName_ = configurationName;
configurationName_ = configurationName;
}
ModelPtr DocBookFactory::parseDocument(
const QMap<ModelType,QString> & roles,
QIODevice *stream,
const QUrl & url,
QString *error
) const
const QMap<ModelType, QString> &roles,
QIODevice *stream,
const QUrl &url,
QString *error
) const
{
roles_ = roles;
url_ = url;
QXmlInputSource source(stream);
if (reader_->parse(source)) {
if (error)
error->clear();
filterByOs(doc_);
filterByConfiguration(doc_);
filterByRoles(roles, doc_);
return doc_;
}
else {
if (doc_)
doc_.clear();
const QString errorText = reader_->errorHandler()->errorString();
if (error) {
*error = errorText;
}
return ModelPtr();
}
roles_ = roles;
url_ = url;
QXmlInputSource source(stream);
if (reader_->parse(source)) {
if (error) {
error->clear();
}
filterByOs(doc_);
filterByConfiguration(doc_);
filterByRoles(roles, doc_);
return doc_;
} else {
if (doc_) {
doc_.clear();
}
const QString errorText = reader_->errorHandler()->errorString();
if (error) {
*error = errorText;
}
return ModelPtr();
}
}
void DocBookFactory::filterByOs(ModelPtr root) const
{
if (!root)
return;
QString pattern;
if (!root) {
return;
}
QString pattern;
#ifdef Q_WS_MAC
pattern = "mac";
pattern = "mac";
#endif
#ifdef Q_WS_X11
pattern = "x11";
pattern = "x11";
#endif
#ifdef Q_WS_WIN
pattern = "win";
pattern = "win";
#endif
QList<ModelPtr> newList;
for (ModelIterator it = root->children_.begin();
it!=root->children_.end();
it++)
{
ModelPtr child = *it;
bool toDelete = false;
if (child->os_.length() > 0) {
const QString os = child->os_.toLower().trimmed();
toDelete = os.indexOf(pattern) == -1;
if (os.startsWith("!") || os.startsWith("not")) {
toDelete = !toDelete;
}
}
if (!toDelete) {
newList.push_back(child);
filterByOs(child);
}
}
root->children_ = newList;
QList<ModelPtr> newList;
for (ModelIterator it = root->children_.begin();
it != root->children_.end();
it++) {
ModelPtr child = *it;
bool toDelete = false;
if (child->os_.length() > 0) {
const QString os = child->os_.toLower().trimmed();
toDelete = os.indexOf(pattern) == -1;
if (os.startsWith("!") || os.startsWith("not")) {
toDelete = !toDelete;
}
}
if (!toDelete) {
newList.push_back(child);
filterByOs(child);
}
}
root->children_ = newList;
}
void DocBookFactory::filterByConfiguration(ModelPtr root) const
{
if (!root)
return;
QString confName;
if (configurationName_.isEmpty()) {
static const QString applicationLanucher = QDir::fromNativeSeparators(qApp->arguments().at(0));
confName =
applicationLanucher.startsWith(qApp->applicationDirPath())
? applicationLanucher.mid(qApp->applicationDirPath().length() + 1)
: applicationLanucher;
#ifdef Q_OS_WIN32
if (confName.endsWith(".exe")) {
confName.remove(confName.length()-4, 4);
}
#endif
confName.remove("kumir2-");
}
else {
confName = configurationName_;
}
QList<ModelPtr> newList;
for (ModelIterator it = root->children_.begin();
it!=root->children_.end();
it++)
{
ModelPtr child = *it;
bool toDelete = false;
if (child->configuration_.length()) {
const QString & childConf = child->configuration_;
toDelete = childConf.toLower() != confName;
}
if (!toDelete) {
newList.push_back(child);
filterByConfiguration(child);
}
}
root->children_ = newList;
if (!root) {
return;
}
QString confName;
if (configurationName_.isEmpty()) {
static const QString applicationLanucher = QDir::fromNativeSeparators(qApp->arguments().at(0));
confName =
applicationLanucher.startsWith(qApp->applicationDirPath())
? applicationLanucher.mid(qApp->applicationDirPath().length() + 1)
: applicationLanucher;
#ifdef Q_OS_WIN32
if (confName.endsWith(".exe")) {
confName.remove(confName.length() - 4, 4);
}
#endif
confName.remove("kumir2-");
} else {
confName = configurationName_;
}
QList<ModelPtr> newList;
for (ModelIterator it = root->children_.begin();
it != root->children_.end();
it++) {
ModelPtr child = *it;
bool toDelete = false;
if (child->configuration_.length()) {
const QString &childConf = child->configuration_;
toDelete = childConf.toLower() != confName;
}
if (!toDelete) {
newList.push_back(child);
filterByConfiguration(child);
}
}
root->children_ = newList;
}
void DocBookFactory::filterByRoles(const QMap<ModelType, QString> &roles, ModelPtr root) const
{
if (!root)
return;
QList<ModelPtr> newList;
for (ModelIterator it = root->children_.begin();
it!=root->children_.end();
it++)
{
ModelPtr child = *it;
bool toDelete = false;
if (child->role_.length() > 0 && roles.contains(child->modelType_)) {
const QString & matchRole = roles[child->modelType_];
const QString & childRole = child->role_;
toDelete = childRole.toLower() != matchRole;
}
if (!toDelete) {
newList.push_back(child);
filterByRoles(roles, child);
}
}
root->children_ = newList;
if (!root) {
return;
}
QList<ModelPtr> newList;
for (ModelIterator it = root->children_.begin();
it != root->children_.end();
it++) {
ModelPtr child = *it;
bool toDelete = false;
if (child->role_.length() > 0 && roles.contains(child->modelType_)) {
const QString &matchRole = roles[child->modelType_];
const QString &childRole = child->role_;
toDelete = childRole.toLower() != matchRole;
}
if (!toDelete) {
newList.push_back(child);
filterByRoles(roles, child);
}
}
root->children_ = newList;
}
bool DocBookFactory::startDocument()
{
doc_.clear();
root_.clear();
return true;
doc_.clear();
root_.clear();
return true;
}
bool DocBookFactory::startElement(
const QString &namespaceURI,
const QString &localName,
const QString &qName,
const QXmlAttributes &atts)
const QString &namespaceURI,
const QString &localName,
const QString &qName,
const QXmlAttributes &atts)
{
const QString element = localName.toLower();
static const QRegExp XInclude("http://www.w3.org/\\d+/XInclude");
static const QRegExp MathML("http://www.w3.org/\\d+/Math/MathML");
DocBookModel * model = 0;
if (element == "article") {
model = new DocBookModel(root_, Article);
}
else if (element == "book") {
model = new DocBookModel(root_, Book);
}
else if (element == "set") {
model = new DocBookModel(root_, Set);
}
else if (element == "abstract") {
model = new DocBookModel(root_, Abstract);
}
else if (element == "preface") {
model = new DocBookModel(root_, Preface);
}
else if (element == "chapter") {
model = new DocBookModel(root_, Chapter);
}
else if (element == "section") {
model = new DocBookModel(root_, Section);
}
else if (element == "para") {
model = new DocBookModel(root_, Para);
}
else if (element == "programlisting") {
model = new DocBookModel(root_, ProgramListing);
}
else if (element == "code") {
model = new DocBookModel(root_, Code);
}
else if (element == "example") {
model = new DocBookModel(root_, Example);
}
else if (element == "orderedlist") {
model = new DocBookModel(root_, OrderedList);
}
else if (element == "itemizedlist") {
model = new DocBookModel(root_, ItemizedList);
}
else if (element == "listitem") {
model = new DocBookModel(root_, ListItem);
}
else if (element == "emphasis") {
model = new DocBookModel(root_, Emphasis);
model->role_ = atts.value("role");
}
else if (element == "keycombo") {
model = new DocBookModel(root_, KeyCombo);
}
else if (element == "keysym") {
model = new DocBookModel(root_, KeySym);
}
else if (element == "table") {
model = new DocBookModel(root_, Table);
}
else if (element == "informaltable") {
model = new DocBookModel(root_, InformalTable);
}
else if (element == "thead") {
model = new DocBookModel(root_, THead);
}
else if (element == "tbody") {
model = new DocBookModel(root_, TBody);
}
else if (element == "row") {
model = new DocBookModel(root_, Row);
}
else if (element == "entry") {
model = new DocBookModel(root_, Entry);
}
else if (element == "mediaobject") {
model = new DocBookModel(root_, MediaObject);
}
else if (element == "caption") {
model = new DocBookModel(root_, Caption);
}
else if (element == "inlinemediaobject") {
model = new DocBookModel(root_, InlineMediaObject);
}
else if (element == "imageobject") {
model = new DocBookModel(root_, ImageObject);
}
else if (element == "imagedata") {
model = new DocBookModel(root_, ImageData);
}
else if (element == "subscript") {
model = new DocBookModel(root_, Subscript);
}
else if (element == "superscript") {
model = new DocBookModel(root_, Superscript);
}
else if (element == "funcsynopsis") {
model = new DocBookModel(root_, FuncSynopsys);
}
else if (element == "funcsynopsisinfo") {
model = new DocBookModel(root_, FuncSynopsysInfo);
}
else if (element == "funcprototype") {
model = new DocBookModel(root_, FuncPrototype);
}
else if (element == "funcdef") {
model = new DocBookModel(root_, FuncDef);
}
else if (element == "function") {
model = new DocBookModel(root_, Function);
}
else if (element == "paramdef") {
model = new DocBookModel(root_, ParamDef);
}
else if (element == "parameter") {
model = new DocBookModel(root_, Parameter);
}
else if (element == "type") {
model = new DocBookModel(root_, Type);
}
else if (element == "package") {
model = new DocBookModel(root_, Package);
}
else if (element == "guimenu") {
model = new DocBookModel(root_, GuiMenu);
}
else if (element == "guimenuitem") {
model = new DocBookModel(root_, GuiMenuItem);
}
else if (element == "guibutton") {
model = new DocBookModel(root_, GuiButton);
}
else if (element == "sectioninfo") {
model = new DocBookModel(root_, SectionInfo);
}
else if (element == "keywordset") {
model = new DocBookModel(root_, KeywordSet);
}
else if (element == "keyword") {
model = new DocBookModel(root_, Keyword);
}
else if (element == "xref") {
model = new DocBookModel(root_, Xref);
model->xrefLinkEnd_ = atts.value("linkend");
model->xrefEndTerm_ = atts.value("endterm");
}
else if (element == "title" || element == "subtitle" || element == "titleabbrev") {
buffer_.clear();
}
else if (element == "include" && XInclude.indexIn(namespaceURI)!=-1) {
const QString href = atts.value("href");
if (href.length() > 0) {
const QUrl hrefUrl = url_.resolved(href);
// TODO network url support
const QString fileName = hrefUrl.toLocalFile();
QFile file(fileName);
if (file.open(QIODevice::ReadOnly)) {
DocBookFactory* innerFactory = new DocBookFactory();
innerFactory->configurationName_ = configurationName_;
QString localError;
ModelPtr include =
innerFactory->parseDocument(roles_, &file, hrefUrl, &localError);
if (include) {
if (root_) {
include->parent_ = root_;
root_->children_.append(include);
}
else {
root_ = include;
}
}
file.close();
delete innerFactory;
}
}
}
else if (element == "math" && MathML.indexIn(namespaceURI) != -1) {
model = new DocBookModel(root_, MathML_Math);
}
else if (element == "mrow" && MathML.indexIn(namespaceURI) != -1) {
model = new DocBookModel(root_, MathML_MRow);
}
else if (element == "msqrt" && MathML.indexIn(namespaceURI) != -1) {
model = new DocBookModel(root_, MathML_MSqrt);
}
else if (element == "mfrac" && MathML.indexIn(namespaceURI) != -1) {
model = new DocBookModel(root_, MathML_MFrac);
}
else if (element == "mi" && MathML.indexIn(namespaceURI) != -1) {
model = new DocBookModel(root_, MathML_MI);
}
else if (element == "mn" && MathML.indexIn(namespaceURI) != -1) {
model = new DocBookModel(root_, MathML_MN);
}
else if (element == "mo" && MathML.indexIn(namespaceURI) != -1) {
model = new DocBookModel(root_, MathML_MO);
}
else if (element == "mtext" && MathML.indexIn(namespaceURI) != -1) {
model = new DocBookModel(root_, MathML_MText);
}
else if (element == "msup" && MathML.indexIn(namespaceURI) != -1) {
model = new DocBookModel(root_, MathML_MSup);
}
else {
model = new DocBookModel(root_, Unknown);
buffer_.clear();
}
if (model) {
if (root_ && buffer_.length() > 0) {
DocBookModel* text = new DocBookModel(root_, Text);
text->text_ = buffer_;