"Copy template as markdown" button. (#3719)

* Added: Copy template info button

* Consistent with Ankidroid

* Fix: Missing newline

* Renamed variables

* ./check

* Fix: Remove ``` from templates

* Stylistic changes

* ./check
This commit is contained in:
Luc Mcgrady 2025-01-15 09:29:35 +00:00 committed by GitHub
parent 39e293b27d
commit 146a0b2dcf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 0 deletions

View file

@ -37,6 +37,7 @@ card-templates-card = Card { $val }
card-templates-card-types-for = Card Types for { $val }
card-templates-cloze = Cloze { $val }
card-templates-deck-override = Deck Override...
card-templates-copy-info = Copy Info to Clipboard
card-templates-delete-the-as-card-type-and = Delete the '{ $template }' card type, and its { $cards }?
card-templates-enter-deck-to-place-new = Enter deck to place new { $val } cards in, or leave blank:
card-templates-enter-new-card-position-1 = Enter new card position (1...{ $val }):

View file

@ -754,6 +754,31 @@ class CardLayout(QDialog):
dst["afmt"] = "{{FrontSide}}\n\n<hr id=answer>\n\n%s" % src["qfmt"]
dst["qfmt"] = m.group(2).strip()
def onCopyMarkdown(self) -> None:
template = self.current_template()
def sanitizeMarkdown(md):
return md.replace("```", "\\`\\`\\`")
markdown = (
f"## Front Template\n"
"```html\n"
f"{sanitizeMarkdown(template['qfmt'])}\n"
"```\n"
"## Back Template\n"
"```html\n"
f"{sanitizeMarkdown(template['afmt'])}\n"
"```\n"
"## Styling\n"
"```css\n"
f"{sanitizeMarkdown(self.model['css'])}\n"
"```\n"
)
clipboard = QApplication.clipboard()
assert clipboard is not None
clipboard.setText(markdown)
tooltip(tr.about_copied_to_clipboard())
def onMore(self) -> None:
m = QMenu(self)
@ -794,6 +819,10 @@ class CardLayout(QDialog):
assert a is not None
qconnect(a.triggered, self.onTargetDeck)
a = m.addAction(tr.card_templates_copy_info())
assert a is not None
qconnect(a.triggered, self.onCopyMarkdown)
a = m.addAction(tr.card_templates_browser_appearance())
assert a is not None
qconnect(a.triggered, self.onBrowserDisplay)