mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -04:00
filter implicit html on field save; set color with wrap()
- using setFormat() to set the colour results in the text being marked as implicitly coloured, so we wrap() instead
This commit is contained in:
parent
cd1c958230
commit
770036020a
1 changed files with 24 additions and 21 deletions
|
@ -246,6 +246,24 @@ document.body.onmouseup = function () {
|
||||||
</body></html>
|
</body></html>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def _filterHTML(html):
|
||||||
|
doc = BeautifulSoup(html)
|
||||||
|
# filter out implicit formatting from webkit
|
||||||
|
for tag in doc("span", "Apple-style-span"):
|
||||||
|
tag.replaceWithChildren()
|
||||||
|
for tag in doc("font", "Apple-style-span"):
|
||||||
|
tag.replaceWithChildren()
|
||||||
|
# turn file:/// links into relative ones
|
||||||
|
for tag in doc("img"):
|
||||||
|
if tag['src'].lower().startswith("file://"):
|
||||||
|
tag['src'] = os.path.basename(tag['src'])
|
||||||
|
# strip superfluous elements
|
||||||
|
for elem in "html", "head", "body", "meta":
|
||||||
|
for tag in doc(elem):
|
||||||
|
tag.replaceWithChildren()
|
||||||
|
html = unicode(doc)
|
||||||
|
return html
|
||||||
|
|
||||||
# caller is responsible for resetting note on reset
|
# caller is responsible for resetting note on reset
|
||||||
class Editor(object):
|
class Editor(object):
|
||||||
def __init__(self, mw, widget, parentWindow, addMode=False):
|
def __init__(self, mw, widget, parentWindow, addMode=False):
|
||||||
|
@ -450,7 +468,7 @@ class Editor(object):
|
||||||
def mungeHTML(self, txt):
|
def mungeHTML(self, txt):
|
||||||
if txt == "<br>":
|
if txt == "<br>":
|
||||||
txt = ""
|
txt = ""
|
||||||
return txt
|
return _filterHTML(txt)
|
||||||
|
|
||||||
# Setting/unsetting the current note
|
# Setting/unsetting the current note
|
||||||
######################################################################
|
######################################################################
|
||||||
|
@ -767,7 +785,8 @@ class Editor(object):
|
||||||
recent = self.mw.pm.profile['recentColours']
|
recent = self.mw.pm.profile['recentColours']
|
||||||
recent.remove(colour)
|
recent.remove(colour)
|
||||||
recent.append(colour)
|
recent.append(colour)
|
||||||
self._eval("restoreSel(); setFormat('forecolor', '%s')" % colour)
|
self._eval("restoreSel(); wrap('<font color=\"%s\">', '</font>');"%
|
||||||
|
colour)
|
||||||
self.colourDiag.close()
|
self.colourDiag.close()
|
||||||
self.colourChanged()
|
self.colourChanged()
|
||||||
|
|
||||||
|
@ -934,7 +953,7 @@ class EditorWebView(AnkiWebView):
|
||||||
if evt.source():
|
if evt.source():
|
||||||
if oldmime.hasHtml():
|
if oldmime.hasHtml():
|
||||||
mime = QMimeData()
|
mime = QMimeData()
|
||||||
mime.setHtml(self._filteredHTML(oldmime.html()))
|
mime.setHtml(_filterHTML(oldmime.html()))
|
||||||
else:
|
else:
|
||||||
# old qt on linux won't give us html when dragging an image;
|
# old qt on linux won't give us html when dragging an image;
|
||||||
# in that case just do the default action (which is to ignore
|
# in that case just do the default action (which is to ignore
|
||||||
|
@ -955,7 +974,7 @@ class EditorWebView(AnkiWebView):
|
||||||
if keep:
|
if keep:
|
||||||
new = QMimeData()
|
new = QMimeData()
|
||||||
if mime.hasHtml():
|
if mime.hasHtml():
|
||||||
new.setHtml(self._filteredHTML(mime.html()))
|
new.setHtml(_filterHTML(mime.html()))
|
||||||
else:
|
else:
|
||||||
new.setText(mime.text())
|
new.setText(mime.text())
|
||||||
mime = new
|
mime = new
|
||||||
|
@ -1035,7 +1054,7 @@ class EditorWebView(AnkiWebView):
|
||||||
if self.strip:
|
if self.strip:
|
||||||
html = stripHTML(html)
|
html = stripHTML(html)
|
||||||
else:
|
else:
|
||||||
html = self._filteredHTML(html)
|
html = _filterHTML(html)
|
||||||
mime = QMimeData()
|
mime = QMimeData()
|
||||||
mime.setHtml(html)
|
mime.setHtml(html)
|
||||||
return mime
|
return mime
|
||||||
|
@ -1070,19 +1089,3 @@ class EditorWebView(AnkiWebView):
|
||||||
file.write(filecontents)
|
file.write(filecontents)
|
||||||
file.close()
|
file.close()
|
||||||
return self.editor._addMedia(path)
|
return self.editor._addMedia(path)
|
||||||
|
|
||||||
def _filteredHTML(self, html):
|
|
||||||
doc = BeautifulSoup(html)
|
|
||||||
# filter out implicit formatting from webkit
|
|
||||||
for tag in doc("span", "Apple-style-span"):
|
|
||||||
tag.replaceWithChildren()
|
|
||||||
# turn file:/// links into relative ones
|
|
||||||
for tag in doc("img"):
|
|
||||||
if tag['src'].lower().startswith("file://"):
|
|
||||||
tag['src'] = os.path.basename(tag['src'])
|
|
||||||
# strip superfluous elements
|
|
||||||
for elem in "html", "head", "body", "meta":
|
|
||||||
for tag in doc(elem):
|
|
||||||
tag.replaceWithChildren()
|
|
||||||
html = unicode(doc)
|
|
||||||
return html
|
|
||||||
|
|
Loading…
Reference in a new issue