Repository navigation
Expand file tree
/
Copy pathrich_html.py
More file actions
180 lines (156 loc) · 8.04 KB
/
Copy pathrich_html.py
File metadata and controls
180 lines (156 loc) · 8.04 KB
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
# WriterAgent - AI Writing Assistant for LibreOffice
# Copyright (c) 2024 John Balis
# Copyright (c) 2026 KeithCu (modifications and relicensing)
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
"""Import HTML into a Calc cell via a hidden Writer document and transferable paste."""
from __future__ import annotations
import logging
from typing import Any
from plugin.doc.compound_undo import UndoStepOrRollback
from plugin.framework.errors import ToolExecutionError, best_effort, reraise_if_disposed
from plugin.framework.i18n import _
from plugin.framework.scratch_writer import (
clear_writer_body,
is_scratch_document,
load_scratch_document,
)
from plugin.framework.uno_context import get_desktop, get_service_manager, uno_same
from plugin.calc.bridge import CalcBridge
from plugin.calc.calc_utils import FORMULA
import plugin.writer.html_import as html_import
from plugin.writer.html_import_styles import normalize_character_references
log = logging.getLogger(__name__)
# Kept Writers are never closed, because closing after a paste can hang on Windows:
# close() can return and desktop.getComponents() then blocks while Calc holds the paste,
# so deferred close is not an option.
# They are reused via this list because a named frame target cannot find them:
# LoadEnv::impl_loadContent (framework/source/loadenv/loadenv.cxx) creates an unnamed frame.
# A list is used because SwHTMLParser::Show (sw/source/filter/html/swhtml.cxx) can
# dispatch events and start a nested paste. Main thread only.
# _GUARD_TEXT holds one character in the first paragraph that is never pasted; everything
# after it is deleted on reuse and joins into that clean guard paragraph, whereas
# setString("") keeps the first pasted paragraph's attributes.
_GUARD_TEXT = "x"
_PARAGRAPH_BREAK = 0 # com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK
_idle_writers: list[Any] = []
def _take_hidden_writer(uno_ctx: Any) -> Any:
while _idle_writers:
doc = _idle_writers.pop()
if (
is_scratch_document(doc)
and clear_writer_body(doc, keep_chars=len(_GUARD_TEXT))
and doc.getText().getString() == _GUARD_TEXT
):
return doc
log.debug("insert_cell_html_rich: dropping an unusable hidden Writer")
desktop = get_desktop(uno_ctx)
doc = (
load_scratch_document(desktop, "private:factory/swriter", normal_view=True)
if desktop is not None
else None
)
if doc is None:
raise ToolExecutionError("Could not create temporary Writer document")
if not clear_writer_body(doc):
# The paste needs select and getTransferable, which only a normal Writer view has
# (SwPagePreview has neither). Closing a normal view clears Writer's active-view
# pointer (sw/source/uibase/uiview/view.cxx ~SwViewGlueDocShell), which Manage Changes
# dereferences (sw/source/uibase/misc/redlndlg.cxx SwRedlineAcceptDlg), so a Writer
# that cannot be emptied stays open, hidden and registered as scratch.
raise ToolExecutionError("Could not create temporary Writer document")
doc.getText().setString(_GUARD_TEXT)
return doc
def _release_clipboard_if_holds(uno_ctx: Any, transferable: Any) -> None:
"""If SystemClipboard still owns *transferable*, replace it after the paste.
SwXTextView.getTransferable() builds a SwTransferable bound to that Writer
shell (PrepareForCopy). The clipboard must not keep this Writer's
SwTransferable as owner after the paste (on Windows the OLE clipboard can
retain that owner after insertTransferable). Touch the clipboard only when
it holds this object so a user copy is left alone.
"""
def _release() -> None:
# Do not import chatbot for the clipboard; LibrePy ships this module.
smgr: Any = get_service_manager(uno_ctx)
clip = smgr.createInstanceWithContext("com.sun.star.datatransfer.clipboard.SystemClipboard", uno_ctx)
contents = clip.getContents()
if contents is not None and uno_same(contents, transferable):
# None, None drops the previous owner (lostOwnership) without a new OLE source.
clip.setContents(None, None)
best_effort(_release, "release clipboard after rich HTML paste")
def insert_cell_html_rich(doc: Any, uno_ctx: Any, cell_address: str, html: str) -> str:
"""Replace one cell's content with rich text parsed from *html*.
*cell_address* may include a sheet prefix (resolved via CalcBridge).
*uno_ctx* is the UNO component context (e.g. ``ToolContext.ctx``).
Imports HTML using the same StarWriter HTML filter as Writer, then pastes
into the target cell. Images and embedded objects are not supported.
Returns the cell's prior content (formula text when it held a formula,
otherwise its display string; ``""`` when empty).
"""
if not (html or "").strip():
raise ToolExecutionError("HTML content is empty")
# Cell addresses use the bridge so sheet prefixes resolve (same as other cell tools).
cell = CalcBridge(doc).get_cell_by_address(cell_address.strip())
replaced = cell.getFormula() if cell.getType() == FORMULA else cell.getString()
content = normalize_character_references(html)
prepared = html_import.ensure_html_linebreaks(content)
step = "start"
temp_doc = None
transferable: Any = None
try:
step = "load hidden Writer"
temp_doc = _take_hidden_writer(uno_ctx)
text = temp_doc.getText()
cursor = text.createTextCursor()
cursor.gotoEnd(False)
text.insertControlCharacter(cursor, _PARAGRAPH_BREAK, False)
step = "insert HTML into hidden Writer"
html_import.insert_html_fragment_at_cursor(cursor, prepared, wrap=False, advance=True)
# Hidden Writer docs must not use getViewCursor() — it can crash the
# process (no real view). Select after the guard paragraph with a text
# cursor and XSelectionSupplier.select, then getTransferable().
w_ctrl = temp_doc.getCurrentController()
sel = text.createTextCursor()
sel.gotoStart(False)
sel.goRight(len(_GUARD_TEXT) + 1, False)
sel.gotoEnd(True)
w_ctrl.select(sel)
step = "get transferable"
# A normal Writer view (SwXTextView) is an XTransferableSupplier.
transferable = w_ctrl.getTransferable()
# Do not read SystemClipboard before the paste. getContents() attaches
# the Windows OLE clipboard thread and a later desktop enum can block.
# Release-if-ours below is the only clipboard touch.
# Group the clear and the paste into one Ctrl+Z step; if the paste raises,
# UndoStepOrRollback undoes the clear so the cell keeps its previous text.
with UndoStepOrRollback(doc, _("WriterAgent: Insert HTML")):
cell.getText().setString("")
c_ctrl = doc.getCurrentController()
step = "select cell"
c_ctrl.select(cell)
step = "insert transferable"
# The Calc view (ScTabViewObj) is an XTransferableSupplier.
c_ctrl.insertTransferable(transferable)
except ToolExecutionError:
raise
except Exception as e:
reraise_if_disposed(e)
log.debug("insert_cell_html_rich failed", exc_info=True)
raise ToolExecutionError(f"Failed to insert HTML into cell at {step!r}: {e}") from e
finally:
if transferable is not None:
_release_clipboard_if_holds(uno_ctx, transferable)
if temp_doc is not None:
_idle_writers.append(temp_doc)
return replaced