1/*
2 SPDX-FileCopyrightText: 2009 Milian Wolff <mail@milianw.de>
3 SPDX-FileCopyrightText: 2002 John Firebaugh <jfirebaugh@kde.org>
4 SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org>
5 SPDX-FileCopyrightText: 2001 Joseph Wenninger <jowenn@kde.org>
6 SPDX-FileCopyrightText: 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de>
7
8 SPDX-License-Identifier: LGPL-2.0-or-later
9*/
10
11#include "exporter.h"
12#include "abstractexporter.h"
13#include "htmlexporter.h"
14
15#include <ktexteditor/document.h>
16#include <ktexteditor/view.h>
17
18#include <KLocalizedString>
19
20#include <QApplication>
21#include <QClipboard>
22#include <QFileDialog>
23#include <QMimeData>
24
25void KateExporter::exportToClipboard()
26{
27 if (!m_view->selection()) {
28 return;
29 }
30
31 QMimeData *data = new QMimeData();
32
33 QString s;
34 QTextStream output(&s, QIODevice::WriteOnly);
35 exportData(useSelction: true, output);
36
37 data->setHtml(s);
38 data->setText(s);
39
40 QApplication::clipboard()->setMimeData(data);
41}
42
43void KateExporter::exportToFile(const QString &file)
44{
45 QFile savefile(file);
46 if (!savefile.open(flags: QIODevice::WriteOnly | QIODevice::Truncate)) {
47 return;
48 }
49
50 QTextStream outputStream(&savefile);
51 exportData(useSelction: false, output&: outputStream);
52}
53
54void KateExporter::exportData(const bool useSelection, QTextStream &output)
55{
56 const KTextEditor::Range range = useSelection ? m_view->selectionRange() : m_view->document()->documentRange();
57 const bool blockwise = useSelection ? m_view->blockSelection() : false;
58
59 if ((blockwise || range.onSingleLine()) && (range.start().column() > range.end().column())) {
60 return;
61 }
62
63 /// TODO: add more exporters
64 std::unique_ptr<AbstractExporter> exporter = std::make_unique<HTMLExporter>(args&: m_view, args&: output, args: !useSelection);
65
66 const KTextEditor::Attribute::Ptr noAttrib(nullptr);
67
68 for (int i = range.start().line(); (i <= range.end().line()) && (i < m_view->document()->lines()); ++i) {
69 const QString &line = m_view->document()->line(line: i);
70
71 const QList<KTextEditor::AttributeBlock> attribs = m_view->lineAttributes(line: i);
72
73 int lineStart = 0;
74 int remainingChars = line.length();
75 if (blockwise || range.onSingleLine()) {
76 lineStart = range.start().column();
77 remainingChars = range.columnWidth();
78 } else if (i == range.start().line()) {
79 lineStart = range.start().column();
80 } else if (i == range.end().line()) {
81 remainingChars = range.end().column();
82 }
83
84 int handledUntil = lineStart;
85
86 for (const KTextEditor::AttributeBlock &block : attribs) {
87 // honor (block-) selections
88 if (block.start + block.length <= lineStart) {
89 continue;
90 } else if (block.start >= lineStart + remainingChars) {
91 break;
92 }
93 int start = qMax(a: block.start, b: lineStart);
94 if (start > handledUntil) {
95 exporter->exportText(text: line.mid(position: handledUntil, n: start - handledUntil), attrib: noAttrib);
96 }
97 int length = qMin(a: block.length, b: remainingChars);
98 exporter->exportText(text: line.mid(position: start, n: length), attrib: block.attribute);
99 handledUntil = start + length;
100 }
101
102 if (handledUntil < lineStart + remainingChars) {
103 exporter->exportText(text: line.mid(position: handledUntil, n: remainingChars), attrib: noAttrib);
104 }
105
106 exporter->closeLine(lastLine: i == range.end().line());
107 }
108
109 output.flush();
110}
111

source code of ktexteditor/src/export/exporter.cpp