-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExport_html_glossary.py
154 lines (127 loc) · 3.7 KB
/
Export_html_glossary.py
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
# export_html_glossary Anki add-on
# based on export_to_html Anki add-on
# Author Peter Borkuti
# Exports a desk to a two-column html file. The first column is the answer, the second
# column is the question.
# It is good for creating a glossary based on an Anki deck.
# The output html file must be placed in the Anki's user media folder.
# If the style must be changed, create a custom.css file in the media folder.
from anki.lang import _
from anki.hooks import addHook
from anki.exporting import TextCardExporter
from aqt.utils import getText
from aqt.utils import showInfo
from anki.cards import Card
from anki.utils import ids2str
import re
import sys,os
import time
import random
import string
class MyTextCardExporter(TextCardExporter):
key = _("Export deck as Html Glossary")
ext = ".htm"
hideTags = True
htmlBefore="""
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<style type="text/css">
.Question{
float: right;
height: auto;
width: 40%;
}
.Answer{
float: right;
height: auto;
width: 40%;
}
.Answer,.Question{
overflow: auto;
padding:4px;
position: relative;
visibility: visible;
height: auto;
}
.Card {
min-width:25px;
text-align: center;
clear:both;
height: auto;
width: 100%;
overflow: visible;
border-top-width: thin;
border-right-width: thin;
border-bottom-width: thin;
border-left-width: thin;
border-top-style: solid;
border-right-style: solid;
border-bottom-style: solid;
border-left-style: solid;
display: block;
}
.Card:after {
content: '.';
display: block;
clear: both;
visibility: hidden;
height: 0;
line-height: 0;
}
img {
max-height: 400px;
max-width: 400px;
display: compact;
margin: 0px;
padding: 1px;
top:0;
left:0;
}
</style>
<link type="text/css" rel="stylesheet" href="custom.css"/>
</head>
<body>
"""
htmlAfter="""
</body>
</html>
"""
def __init__(self, col):
TextCardExporter.__init__(self, col)
def escapeText(self, text):
"Escape newlines, tabs and CSS and change id to class"
text = text.replace("\n", "")
text = text.replace("\t", "")
text = re.sub("(?i)<style>.*?</style>", "", text)
text = text.replace("<hr id=answer>", '<hr class="answer">')
return text
def doExport(self, file):
ids = self.cardIds()
def esc(s):
# strip off the repeated question in answer if exists
s = re.sub('(?si)^.*<hr id=answer>\n*', "", s)
s = re.sub("(?si)<style.*?>.*?</style>", "", s)
return convertSound(self.escapeText(randomizeId(s)))
def convertSound(s):
return re.sub(r'\[sound:(.*)]', r'<audio controls src="\1"></audio>', s, 0, re.IGNORECASE)
def getRandomId(group):
# https://pythontips.com/2013/07/28/generating-a-random-string/
return ' id="' + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(32)])+'"'
def randomizeId(s):
return re.sub(r' +id *= *[\'"]*([^ \'">]+)[\'"]*', getRandomId, s, 0, re.IGNORECASE)
out = ""
for cid in ids:
c = self.col.getCard(cid)
out += '<div class="Card">\n'
out += '<div class="Question">\n' + esc(c.q()) + "\n</div>\n"
out += '<div class="Answer">\n' + esc(c.a()) + "\n</div>\n"
out += "</div><!-- Card -->\n\n"
out= self.htmlBefore + out + self.htmlAfter
file.write(out.encode("utf-8"))
def addMyExporter(exps):
def theid(obj):
return ("%s (*%s)" % (obj.key, obj.ext), obj)
exps.append(theid(MyTextCardExporter))
addHook("exportersList", addMyExporter)