def onPrint(): path = os.path.join( QStandardPaths.writableLocation(QStandardPaths.DesktopLocation), "print.html") ids = sortFieldOrderCids(mw.col.decks.selected()) def esc(s): # strip off the repeated question in answer if exists #s = re.sub("(?si)^.*<hr id=answer>\n*", "", s) # remove type answer s = re.sub("\[\[type:[^]]+\]\]", "", s) return s buf = open(path, "w", encoding="utf8") buf.write("<html><head>" '<meta charset="utf-8">') buf.write(mw.baseHTML()) buf.write("""<style> img { max-width: 100%; } body { display: flex; flex-flow: row wrap; } body > div { page-break-after: auto; border: 1px solid #ccc; padding: 1em; flex-grow: 1; flex-basis: """ + str(config["card_width"]) + """; flex-shrink: """ + str(config["flexbox_shrink"]) + """; } </style></head><body>""") mw.progress.start(immediate=True) for j, cid in enumerate(ids): c = mw.col.getCard(cid) css = c.css() qatxt = c._getQA(True, False)['a'] qatxt = mungeQA(mw.col, qatxt) cont = u'<div><div class="card">{}</div>{}</div>'.format( esc(qatxt), css) buf.write(cont) if j % 50 == 0: mw.progress.update("Cards exported: %d" % (j + 1)) buf.write("""<script> document.querySelectorAll("body > div").forEach(function(e) { var card = e.querySelector("div"); var css = e.querySelector("style"); e.removeChild(card); e.removeChild(css); var shadow = e.attachShadow({mode: 'open'}); shadow.appendChild(card); shadow.appendChild(css); }); </script></body></html>""") mw.progress.finish() buf.close() openLink(QUrl.fromLocalFile(path))
def stdHtml(self, body, css=[], js=["jquery.js"], head=""): if isWin: widgetspec = "button { font-size: 12px; font-family:'Segoe UI'; }" fontspec = 'font-size:12px;font-family:"Segoe UI";' elif isMac: family="Helvetica" fontspec = 'font-size:15px;font-family:"%s";'% \ family widgetspec = """ button { font-size: 13px; -webkit-appearance: none; background: #fff; border: 1px solid #ccc; border-radius:5px; font-family: Helvetica }""" else: palette = self.style().standardPalette() family = self.font().family() color_hl = palette.color(QPalette.Highlight).name() color_hl_txt = palette.color(QPalette.HighlightedText).name() color_btn = palette.color(QPalette.Button).name() fontspec = 'font-size:14px;font-family:"%s";'% family widgetspec = """ /* Buttons */ button{ font-size:14px; -webkit-appearance:none; outline:0; background-color: %(color_btn)s; border:1px solid rgba(0,0,0,.2); border-radius:2px; height:24px; font-family:"%(family)s"; } button:focus{ border-color: %(color_hl)s } button:hover{ background-color:#fff } button:active, button:active:hover { background-color: %(color_hl)s; color: %(color_hl_txt)s;} /* Input field focus outline */ textarea:focus, input:focus, input[type]:focus, .uneditable-input:focus, div[contenteditable="true"]:focus { outline: 0 none; border-color: %(color_hl)s; }""" % {"family": family, "color_btn": color_btn, "color_hl": color_hl, "color_hl_txt": color_hl_txt} csstxt = "\n".join([self.bundledCSS("webview.css")]+ [self.bundledCSS(fname) for fname in css]) jstxt = "\n".join([self.bundledScript("webview.js")]+ [self.bundledScript(fname) for fname in js]) from aqt import mw head = mw.baseHTML() + head + csstxt + jstxt html = """ <!doctype html> <html><head> <title>{}</title> <style> body {{ zoom: {}; {} }} {} </style> {} </head> <body>{}</body> </html>""".format(self.title, self.zoomFactor(), fontspec, widgetspec, head, body) #print(html) self.setHtml(html)
def stdHtml( self, body: str, css: Optional[list[str]] = None, js: Optional[list[str]] = None, head: str = "", context: Optional[Any] = None, default_css: bool = True, ) -> None: css = (["css/webview.css"] if default_css else []) + ([] if css is None else css) web_content = WebContent( body=body, head=head, js=["js/webview.js"] + (["js/vendor/jquery.min.js"] if js is None else js), css=css, ) gui_hooks.webview_will_set_content(web_content, context) csstxt = "" if "css/webview.css" in css: # we want our dynamic styling to override the defaults in # css/webview.css, but come before user-provided stylesheets so that # they can override us if necessary web_content.css.remove("css/webview.css") csstxt = self.bundledCSS("css/webview.css") csstxt += f"<style>{self.standard_css()}</style>" csstxt += "\n".join( self.bundledCSS(fname) for fname in web_content.css) jstxt = "\n".join( self.bundledScript(fname) for fname in web_content.js) from aqt import mw head = mw.baseHTML() + csstxt + jstxt + web_content.head body_class = theme_manager.body_class() if theme_manager.night_mode: doc_class = "night-mode" else: doc_class = "" html = f""" <!doctype html> <html class="{doc_class}"> <head> <title>{self.title}</title> {head} </head> <body class="{body_class}">{web_content.body}</body> </html>""" # print(html) self.setHtml(html)
def onPrint(): path = os.path.join( QStandardPaths.writableLocation(QStandardPaths.DesktopLocation), "print.html" ) ids = sortFieldOrderCids(mw.col.decks.selected()) def esc(s): # strip off the repeated question in answer if exists # s = re.sub("(?si)^.*<hr id=answer>\n*", "", s) # remove type answer s = re.sub(r"\[\[type:[^]]+\]\]", "", s) return s buf = open(path, "w", encoding="utf8") buf.write( "<html><head>" + '<meta charset="utf-8">' + mw.baseHTML() + "</head><body>" ) buf.write( """<style> img { max-width: 100%; } tr { page-break-inside:avoid; page-break-after:auto } td { page-break-after:auto; } td { border: 1px solid #ccc; padding: 1em; } .playImage { display: none; } </style><table cellspacing=10 width=100%>""" ) first = True mw.progress.start(immediate=True) for j, cid in enumerate(ids): if j % config["cardsPerRow"] == 0: if not first: buf.write("</tr>") else: first = False buf.write("<tr>") c = mw.col.getCard(cid) qatxt = c.render_output(True, False).answer_text qatxt = mw.prepare_card_text_for_display(qatxt) cont = u'<td width="{1}%"><center>{0}</center></td>'.format( esc(qatxt), 100 / config["cardsPerRow"] ) buf.write(cont) if j % 50 == 0: mw.progress.update("Cards exported: %d" % (j + 1)) buf.write("</tr>") buf.write("</table></body></html>") mw.progress.finish() buf.close() openLink(QUrl.fromLocalFile(path))
def onPrint(): path = os.path.join(QStandardPaths.writableLocation( QStandardPaths.DesktopLocation), "print.html") ids = sortFieldOrderCids(mw.col.decks.selected()) def esc(s): # strip off the repeated question in answer if exists #s = re.sub("(?si)^.*<hr id=answer>\n*", "", s) # remove type answer s = re.sub(r"\[\[type:[^]]+\]\]", "", s) return s buf = open(path, "w", encoding="utf8") buf.write("<html><head>" + '<meta charset="utf-8">' + mw.baseHTML() + "</head><body>") buf.write("""<style> img { max-width: 100%; } tr { page-break-inside:avoid; page-break-after:auto } td { page-break-after:auto; } td { border: 1px solid #ccc; padding: 1em; } </style><table cellspacing=10 width=100%>""") first = True mw.progress.start(immediate=True) for j, cid in enumerate(ids): if j % config['cardsPerRow'] == 0: if not first: buf.write("</tr>") else: first = False buf.write("<tr>") c = mw.col.getCard(cid) qatxt = c._getQA(True, False)['a'] qatxt = mungeQA(mw.col, qatxt) cont = u'<td width="{1}%"><center>{0}</center></td>'.format( esc(qatxt), 100/config['cardsPerRow']) buf.write(cont) if j % 50 == 0: mw.progress.update("Cards exported: %d" % (j+1)) buf.write("</tr>") buf.write("</table></body></html>") mw.progress.finish() buf.close() openLink(QUrl.fromLocalFile(path))
def stdHtml(self, body, css=[], js=["jquery.js"], head=""): if isWin: buttonspec = "button { font-size: 12px; font-family:'Segoe UI'; }" fontspec = 'font-size:12px;font-family:"Segoe UI";' elif isMac: family = "Helvetica" fontspec = 'font-size:15px;font-family:"%s";'% \ family buttonspec = """ button { font-size: 13px; -webkit-appearance: none; background: #fff; border: 1px solid #ccc; border-radius:5px; font-family: Helvetica }""" else: buttonspec = "" family = self.font().family() fontspec = 'font-size:14px;font-family:"%s";'%\ family csstxt = "\n".join([self.bundledCSS("webview.css")] + [self.bundledCSS(fname) for fname in css]) jstxt = "\n".join([self.bundledScript("webview.js")] + [self.bundledScript(fname) for fname in js]) from aqt import mw head = mw.baseHTML() + head + csstxt + jstxt html = """ <!doctype html> <html><head> <title>{}</title> <style> body {{ zoom: {}; {} }} {} </style> {} </head> <body>{}</body> </html>""".format(self.title, self.zoomFactor(), fontspec, buttonspec, head, body) #print(html) self.setHtml(html)
def stdHtml(self, body, css=[], js=["jquery.js"], head=""): if isWin: buttonspec = "button { font-size: 12px; font-family:'Segoe UI'; }" fontspec = 'font-size:12px;font-family:"Segoe UI";' elif isMac: family="Helvetica" fontspec = 'font-size:15px;font-family:"%s";'% \ family buttonspec = """ button { font-size: 13px; -webkit-appearance: none; background: #fff; border: 1px solid #ccc; border-radius:5px; font-family: Helvetica }""" else: buttonspec = "" family = self.font().family() fontspec = 'font-size:14px;font-family:%s;'%\ family csstxt = "\n".join([self.bundledCSS("webview.css")]+ [self.bundledCSS(fname) for fname in css]) jstxt = "\n".join([self.bundledScript("webview.js")]+ [self.bundledScript(fname) for fname in js]) from aqt import mw head = mw.baseHTML() + head + csstxt + jstxt html=f""" <!doctype html> <html><head> <title>{self.title}</title> <style> body {{ zoom: {self.zoomFactor()}; {fontspec} }} {buttonspec} </style> {head} </head> <body>{body}</body> </html>""" #print(html) self.setHtml(html)
def stdHtml( self, body: str, css: Optional[List[str]] = None, js: Optional[List[str]] = None, head: str = "", context: Optional[Any] = None, ): web_content = WebContent( body=body, head=head, js=["webview.js"] + (["jquery.js"] if js is None else js), css=["webview.css"] + ([] if css is None else css), ) gui_hooks.webview_will_set_content(web_content, context) palette = self.style().standardPalette() color_hl = palette.color(QPalette.Highlight).name() if isWin: # T: include a font for your language on Windows, eg: "Segoe UI", "MS Mincho" family = _('"Segoe UI"') widgetspec = "button { font-family:%s; }" % family widgetspec += "\n:focus { outline: 1px solid %s; }" % color_hl fontspec = "font-size:12px;font-family:%s;" % family elif isMac: family = "Helvetica" fontspec = 'font-size:15px;font-family:"%s";' % family widgetspec = """ button { -webkit-appearance: none; background: #fff; border: 1px solid #ccc; border-radius:5px; font-family: Helvetica }""" else: family = self.font().family() color_hl_txt = palette.color(QPalette.HighlightedText).name() color_btn = palette.color(QPalette.Button).name() fontspec = 'font-size:14px;font-family:"%s";' % family widgetspec = """ /* Buttons */ button{ background-color: %(color_btn)s; font-family:"%(family)s"; } button:focus{ border-color: %(color_hl)s } button:active, button:active:hover { background-color: %(color_hl)s; color: %(color_hl_txt)s;} /* Input field focus outline */ textarea:focus, input:focus, input[type]:focus, .uneditable-input:focus, div[contenteditable="true"]:focus { outline: 0 none; border-color: %(color_hl)s; }""" % { "family": family, "color_btn": color_btn, "color_hl": color_hl, "color_hl_txt": color_hl_txt, } csstxt = "\n".join(self.bundledCSS(fname) for fname in web_content.css) jstxt = "\n".join( self.bundledScript(fname) for fname in web_content.js) from aqt import mw head = mw.baseHTML() + csstxt + jstxt + web_content.head body_class = theme_manager.body_class() html = """ <!doctype html> <html><head> <title>{}</title> <style> body {{ zoom: {}; background: {}; {} }} {} </style> {} </head> <body class="{}">{}</body> </html>""".format( self.title, self.zoomFactor(), self._getWindowColor().name(), fontspec, widgetspec, head, body_class, web_content.body, ) # print(html) self.setHtml(html)
def getBase(col): from aqt import mw return mw.baseHTML()
def stdHtml_old(self, body, css=None, js=None, head=""): if css is None: css = [] if js is None: js = ["jquery.js"] palette = self.style().standardPalette() color_hl = palette.color(QPalette.Highlight).name() family = config["Interface Font"] font_size = config["Font Size"] if isWin: #T: include a font for your language on Windows, eg: "Segoe UI", "MS Mincho" # family = _('"Segoe UI"') widgetspec = "button { font-size: 12px; font-family:%s; }" % family widgetspec += "\n:focus { outline: 1px solid %s; }" % color_hl fontspec = 'font-size:{}px; font-family:"{}";'.format(font_size, family) elif isMac: # family="Helvetica" fontspec = 'font-size:{}px; font-family:"{}";'.format(font_size, family) widgetspec = """ button { font-size: 13px; -webkit-appearance: none; background: #fff; border: 1px solid #ccc; border-radius:5px; font-family: %s }""" % family else: # family = self.font().family() color_hl_txt = palette.color(QPalette.HighlightedText).name() color_btn = palette.color(QPalette.Button).name() fontspec = 'font-size:{}px; font-family:"{}";'.format(font_size, family) widgetspec = """ /* Buttons */ button{ font-size:14px; -webkit-appearance:none; outline:0; background-color: %(color_btn)s; border:1px solid rgba(0,0,0,.2); border-radius:2px; height:24px; font-family:"%(family)s"; } button:focus{ border-color: %(color_hl)s } button:hover{ background-color:#fff } button:active, button:active:hover { background-color: %(color_hl)s; color: %(color_hl_txt)s;} /* Input field focus outline */ textarea:focus, input:focus, input[type]:focus, .uneditable-input:focus, div[contenteditable="true"]:focus { outline: 0 none; border-color: %(color_hl)s; }""" % {"family": family, "color_btn": color_btn, "color_hl": color_hl, "color_hl_txt": color_hl_txt} csstxt = "\n".join([self.bundledCSS("webview.css")]+ [self.bundledCSS(fname) for fname in css]) jstxt = "\n".join([self.bundledScript("webview.js")]+ [self.bundledScript(fname) for fname in js]) from aqt import mw head = mw.baseHTML() + head + csstxt + jstxt html = """ <!doctype html> <html><head> <title>{}</title> <style> body {{ zoom: {}; background: {}; {} }} {} </style> {} </head> <body>{}</body> </html>""".format(self.title, self.zoomFactor(), self._getWindowColor().name(), fontspec, widgetspec, head, body) #print(html) self.setHtml(html)
def onPrint(cids=None): path = os.path.join(mw.pm.profileFolder(), "flashcards.html") if cids: ids = cids else: ids = sortFieldOrderCids(mw.col.decks.selected()) def esc(s): # strip off the repeated question in answer if exists #s = re.sub("(?si)^.*<hr id=answer>\n*", "", s) # remove type answer s = re.sub("\[\[type:[^]]+\]\]", "", s) return s def prefixed_path(path): if isWin: prefix = "file:///" else: prefix = "file://" return prefix + path buf = open(path, "w") buf.write("<html>" + mw.baseHTML()) buf.write("<meta charset=\'utf-8\'><head>") buf.write("""<style> img {{ max-width: 100%; }} td {{ border: 1px solid #ccc; padding: 0; width: {width:.0f}%; }} table {{ table-layout: fixed; border-spacing: 0; page-break-after: always; width: 100%; height: 100%; }} .a td {{ border: none; }} tr {{ height: {height:.0f}%; }} @page {{ size: landscape; }} </style></head><body>""".format(width = 100 / CARDS_PER_ROW, \ height = 100 / ROWS_PER_TABLE)) ans = [] que = [] processed_notes = [] mw.progress.start(immediate=True) for j, cid in enumerate(ids): c = mw.col.getCard(cid) if c.note().id not in processed_notes: q = esc(c._getQA(True, False)['a']).split('<hr id=answer>')[0] a = esc(c._getQA(True, False)['a']).split('<hr id=answer>')[1] que.append(q) ans.append(a) processed_notes.append(c.note().id) if (len(que) % CARDS_PER_TABLE != 0): for i in range(len(que) % CARDS_PER_TABLE): que.append("") ans.append("") for i in range((len(que) // CARDS_PER_TABLE)): theTable = ["<table>", "<table class=\'a\'>"] for j in range(ROWS_PER_TABLE): theTable[0] += "<tr>" theTable[1] += "<tr>" for k in range(CARDS_PER_ROW): theTable[0] += "<td><center>"+que[CARDS_PER_TABLE * i + \ CARDS_PER_ROW * j + k] + "</td></center>" theTable[1] += "<td><center>"+ans[CARDS_PER_TABLE * i + \ CARDS_PER_TABLE - CARDS_PER_ROW - \ CARDS_PER_ROW * j + k] + "</td></center>" theTable[0] += "</tr>" theTable[1] += "</tr>" theTable[0] += "</table>" theTable[1] += "</table>" buf.write(theTable[0]) buf.write(theTable[1]) buf.write("</body></html>") mw.progress.finish() buf.close() tooltip(_("Loading..."), period=1000) QDesktopServices.openUrl(QUrl.fromUserInput(prefixed_path(path)))