1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/*
* XML formatter plugin for Juffed editor
*
* Portions Copyright (C) 2010 Petr Vanek <petr@scribus.info>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; only version 2 of
* the License is valid for this program.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "xmlformat.h"
#include <QMessageBox>
#include <QDomDocument>
#include "EditorSettings.h"
XmlformatPlugin::XmlformatPlugin() : QObject(), JuffPlugin()
{
actDoc = new QAction(QIcon(":xmlwrap"), tr("Format XML Document"), this);
connect(actDoc, SIGNAL(triggered()), this, SLOT(format()));
}
void XmlformatPlugin::init() {
}
XmlformatPlugin::~XmlformatPlugin() {
}
QString XmlformatPlugin::name() const {
return "XML Formatter";
}
QString XmlformatPlugin::title() const {
return tr("XML Formatter");
}
QString XmlformatPlugin::targetEngine() const {
return "all";
}
QString XmlformatPlugin::description() const {
return "Plugin that allows to format XML document - idenation, verification...";
}
QToolBar* XmlformatPlugin::toolBar() const
{
QToolBar * bar = new QToolBar(tr("XML Formatter"));
bar->addAction(actDoc);
bar->setObjectName("XMLFormatterToolBar");
return bar;
}
Juff::ActionList XmlformatPlugin::mainMenuActions(Juff::MenuID id) const
{
Juff::ActionList list;
if ( Juff::MenuTools == id )
{
list << actDoc;
}
return list;
}
void XmlformatPlugin::format()
{
Juff::Document* doc = api()->currentDocument();
// api()->currentDocument() never returns NULL, it returns
// NullDoc if there is no docs.
if ( doc->isNull() )
return;
// if there is a selection - try to format a selection.
// In the other case format all document
if (doc->hasSelectedText())
{
formatSelection(doc);
}
else
{
formatDocument(doc);
}
}
void XmlformatPlugin::formatDocument(Juff::Document *doc)
{
QString content;
QDomDocument dom;
QString errmsg;
int errline, errcolumn;
if (! doc->getText(content))
return;
if (dom.setContent(content, false, &errmsg, &errline, &errcolumn))
{
QString newContent = dom.toString(EditorSettings::get(EditorSettings::TabWidth));
if (EditorSettings::get(EditorSettings::UseTabs))
newContent = changeSpacesToTabs(newContent);
doc->setText(newContent);
}
else
{
QMessageBox::information(0, tr("XML format error"),
tr("Cannot format XML due error (line: %1, column: %2)").arg(errline).arg(errcolumn)
+ "<br/>"
+ errmsg);
}
}
void XmlformatPlugin::formatSelection(Juff::Document *doc)
{
QString content;
QDomDocument dom;
QString errmsg;
int errline, errcolumn;
if (! doc->getSelectedText(content))
return;
int line, column, tmp1, tmp2;
doc->getSelection(line, column, tmp1, tmp2);
if (dom.setContent(content, false, &errmsg, &errline, &errcolumn))
{
QString newContent = dom.toString(EditorSettings::get(EditorSettings::TabWidth));
if (EditorSettings::get(EditorSettings::UseTabs))
newContent = changeSpacesToTabs(newContent);
doc->replaceSelectedText(newContent);
}
else
{
QMessageBox::information(0, tr("XML format error"),
tr("Cannot format XML due error (line: %1, column: %2)").arg(errline+line).arg(errline==1 ? errcolumn+column : errcolumn)
+ "<br/>"
+ errmsg);
}
}
QString XmlformatPlugin::changeSpacesToTabs(const QString &text)
{
/* hack and slash implementation of <TAB> leading lines.
* Qt itself do not handle <TAB>s in QDomDocument::toString internally
* so output is always <SPACE> indented.
* Here, if is Juffed setup to use <TAB> for indentation, are leading
* spaces converted to tabs.
* Currently it's implemented line by line searching for 1st non-whitespace
* character in the line. Then are spaces replaced by tabs. One tab per "indent"
* spaces.
* Finally the new QString is returned.
*/
QString tab('\t');
QStringList ret;
int indent = EditorSettings::get(EditorSettings::TabWidth);
foreach (QString line, text.split('\n'))
{
int ix = line.indexOf(QRegExp("\\S"));
if (ix < 1)
ret << line;
else
{
line = line.replace(0, ix, tab.repeated(ix / indent));
ret << line;
}
}
return ret.join("\n");
}
#include "xmlformat.moc"
#if QT_VERSION < 0x050000
Q_EXPORT_PLUGIN2(xmlformat, XmlformatPlugin)
#endif
|