def buildCardsAndDeck(path: Path): global AnkiNotes, AnkiModels, Anki_Collections, totalCardCount, FAILED_DECKS conn = sqlite3.connect(path.joinpath("collection.anki2").as_posix()) cursor = conn.cursor() cursor.execute( "SELECT * FROM cards ORDER BY factor ASC") # min ease would at rows[0] and max index would be at rows[-1] rows = cursor.fetchall() with IncrementalBar("\tBuilding Cards and deck", max=len(rows)) as bar: for row in rows: cid, nid, did, ordi, mod, usn, crtype, queue, due, ivl, factor, reps, lapses, left, odue, odid, flags, data = row reqNote = AnkiNotes[str(nid)] genCard = None if reqNote.model.type == 0: reqTemplate = getTemplateofOrd(reqNote.model.tmpls, int(ordi)) questionTg = "<style> " + buildCssForOrd(reqNote.model.css, ordi) \ + "</style><section class='card' style=\" height:100%; width:100%; margin:0; \">" \ + mustache.render(reqTemplate.qfmt, buildStubbleDict(reqNote)) + "</section>" answerTag = "<style> " + buildCssForOrd(reqNote.model.css, ordi) \ + "</style><section class='card' style=\" height:100%; width:100%; margin:0; \">" \ + mustache.render(reqTemplate.afmt, buildStubbleDict(reqNote)) + "</section>" questionTg = premailer.transform(questionTg) answerTag = premailer.transform(answerTag) genCard = Card(cid, questionTg, answerTag) elif reqNote.model.type == 1: reqTemplate = getTemplateofOrd(reqNote.model.tmpls, 0) mustache.filters["cloze"] = lambda txt: Formatters.cloze_q_filter(txt, str(int(ordi) + 1)) css = reqNote.model.css css = buildCssForOrd(css, ordi) if css else "" questionTg = "<style> " + css + " </style><section class='card' style=\" height:100%; width:100%; margin:0; \">" \ + mustache.render(reqTemplate.qfmt, buildStubbleDict(reqNote)) + "</section>" mustache.filters["cloze"] = lambda txt: Formatters.cloze_a_filter(txt, str(int(ordi) + 1)) answerTag = "<section class='card' style=\" height:100%; width:100%; margin:0; \">" \ + mustache.render(reqTemplate.afmt, buildStubbleDict(reqNote)) + "</section>" questionTg = premailer.transform(questionTg) answerTag = premailer.transform(answerTag) genCard = Card(cid, questionTg, answerTag) if genCard is not None: reqDeck = getDeckFromID(Anki_Collections, str(did)) if reqDeck is not None: reqDeck.cards.append(genCard) else: if did not in FAILED_DECKS: FAILED_DECKS.append(did) else: if did not in FAILED_DECKS: FAILED_DECKS.append(did) totalCardCount += 1 bar.next() bar.finish()
def SuperMemoElement(card: Card) -> None: global doc, tag, text, get_id, IMAGES_TEMP, DEFAULT_SIDE, SIDES IMAGES_TEMP = () QContent_Sounds = () QContent_Videos = () AContent_Sounds = () AContent_Videos = () if "[sound:" in str(card.q): g = re.search(r"(?:\[sound:)([^]]+)(?:\])", str(card.q)) if g is not None: for p in g.groups(): m = Path("{}/{}".format("out/out_files/elements", p)) if m.exists(): if any([ext in m.suffix for ext in ["mp3", "ogg", "wav"]]) \ or "audio" in magic.from_file(m.as_posix(), mime=True): QContent_Sounds = QContent_Sounds + (p, ) if any([ext in m.suffix for ext in ["mp4", "wmv", "mkv"]]) \ or "video" in magic.from_file(m.as_posix(), mime=True): QContent_Videos = QContent_Videos + (p, ) if "[sound:" in str(card.a): g = re.search(r"(?:\[sound:)([^]]+)(?:\])", str(card.a)) if g is not None: for p in g.groups(): m = Path("{}/{}".format("out/out_files/elements", p)) if m.exists(): if any([ext in m.suffix for ext in ["mp3", "ogg", "wav"]]) \ or "audio" in magic.from_file(m.as_posix(), mime=True): AContent_Sounds = AContent_Sounds + (p, ) if any([ext in m.suffix for ext in ["mp4", "wmv", "mkv"]]) \ or "video" in magic.from_file(m.as_posix(), mime=True): AContent_Videos = AContent_Videos + (p, ) card.q = Formatters.reSound.sub("", card.q) card.a = Formatters.reSound.sub("", card.a) enforceSectionJS = """<script>document.createElement("section");</script>""" liftIERestriction = """<meta http-equiv="X-UA-Compatible" content="IE=10">""" forcedCss = """<style>img{max-width:50%;}</style>""" with tag('SuperMemoElement'): with tag('ID'): text(get_id()) with tag('Type'): text('Item') with tag('Content'): with tag('Question'): a = wrapHtmlIn(card.q, 'head', 'body') res = cleanHtml(a, imgcmp=IMAGES_AS_COMPONENT) if IMAGES_AS_COMPONENT: IMAGES_TEMP = IMAGES_TEMP + res["imgs"] if MAINTAIN_STYLING: a = insertHtmlAt(res["soup"], enforceSectionJS, 'head', 0) a = insertHtmlAt(a, liftIERestriction, 'head', 0) else: a = res["soup"] if not IMAGES_AS_COMPONENT and len(IMAGES_TEMP) != 0: a = insertHtmlAt(a, forcedCss, 'head', 0) a = strip_control_characters(a) a = a.encode("ascii", "xmlcharrefreplace").decode("utf-8") text(a) for s in QContent_Videos: with tag('Video'): with tag('URL'): text( os.path.expandvars(r'%LocalAppData%') + "\\temp\\smmedia\\{}".format(s)) with tag('Name'): text(s) if DEFAULT_SIDE != SIDES[2] and \ DEFAULT_SIDE != SIDES[0]: with tag("Question"): text("F") with tag("Answer"): text("T") else: with tag("Question"): text("T") with tag("Answer"): text("F") for s in QContent_Sounds: with tag('Sound'): with tag('URL'): text( os.path.expandvars(r'%LocalAppData%') + "\\temp\\smmedia\\{}".format(s)) with tag('Name'): text(s) with tag('Text'): text("") if DEFAULT_SIDE != SIDES[2] and \ DEFAULT_SIDE != SIDES[0]: with tag("Question"): text("F") with tag("Answer"): text("T") else: with tag("Question"): text("T") with tag("Answer"): text("F") # html = Soup(a,'html.parser') # m=[p['href'] for p in html.find_all('a') ] # urls.append(m[0]) if len(m) else "" with tag('Answer'): res = cleanHtml(card.a, imgcmp=IMAGES_AS_COMPONENT) if IMAGES_AS_COMPONENT: IMAGES_TEMP = IMAGES_TEMP + res["imgs"] if MAINTAIN_STYLING: a = insertHtmlAt(res["soup"], enforceSectionJS, 'head', 0) a = insertHtmlAt(a, liftIERestriction, 'head', 0) else: a = res["soup"] if not IMAGES_AS_COMPONENT and len(IMAGES_TEMP) != 0: a = insertHtmlAt(a, forcedCss, 'head', 0) a = strip_control_characters(a) a = a.encode("ascii", "xmlcharrefreplace").decode("utf-8") text(a) for s in AContent_Videos: with tag('Video'): with tag('URL'): text( os.path.expandvars(r'%LocalAppData%') + "\\temp\\smmedia\\{}".format(s)) with tag('Name'): text(s) if DEFAULT_SIDE != SIDES[2] and \ DEFAULT_SIDE != SIDES[1]: with tag("Question"): text("T") with tag("Answer"): text("F") else: with tag("Question"): text("F") with tag("Answer"): text("T") for s in AContent_Sounds: with tag('Sound'): with tag('URL'): text( os.path.expandvars(r'%LocalAppData%') + "\\temp\\smmedia\\{}".format(s)) with tag('Name'): text(s) with tag('Text'): text("") if DEFAULT_SIDE != SIDES[2] and \ DEFAULT_SIDE != SIDES[1]: with tag("Question"): text("T") with tag("Answer"): text("F") else: with tag("Question"): text("F") with tag("Answer"): text("T") for img in IMAGES_TEMP: with tag('Image'): with tag('URL'): text( os.path.expandvars(r'%LocalAppData%') + "\\temp\\smmedia\\{}".format(img)) with tag('Name'): text(img) if DEFAULT_SIDE == SIDES[1]: with tag("Question"): text("F") with tag("Answer"): text("T") elif DEFAULT_SIDE == SIDES[0]: with tag("Question"): text("T") with tag("Answer"): text("F") if False and cardHasData(card): with tag("LearningData"): with tag("Interval"): text("1") with tag("Repetitions"): text("1") with tag("Lapses"): text("0") with tag("LastRepetition"): text(datetime.date("").strftime("%d.%m.%Y")) with tag("AFactor"): text("3.92") with tag("UFactor"): text("3")