Skip to content

Commit

Permalink
add Author field
Browse files Browse the repository at this point in the history
Closes #20 and PR #21.
  • Loading branch information
cashpw authored and sobjornstad committed Jun 6, 2021
1 parent 87b875f commit 888d72d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 22 deletions.
36 changes: 27 additions & 9 deletions designer/import_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,36 @@
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>&amp;Author (optional)</string>
</property>
<property name="buddy">
<cstring>authorBox</cstring>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLineEdit" name="authorBox">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Ta&amp;gs</string>
<string>Ta&amp;gs (optional)</string>
</property>
<property name="buddy">
<cstring>tagsBox</cstring>
</property>
</widget>
</item>
<item row="4" column="1">
<item row="6" column="1">
<widget class="QLineEdit" name="tagsBox"/>
</item>
<item row="6" column="0">
<item row="8" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Lines of Conte&amp;xt</string>
Expand All @@ -62,7 +79,7 @@
</property>
</widget>
</item>
<item row="6" column="1">
<item row="8" column="1">
<widget class="QSpinBox" name="contextLinesSpin">
<property name="minimumSize">
<size>
Expand All @@ -84,7 +101,7 @@
</property>
</widget>
</item>
<item row="7" column="0">
<item row="9" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Lines to &amp;Recite</string>
Expand All @@ -94,7 +111,7 @@
</property>
</widget>
</item>
<item row="7" column="1">
<item row="9" column="1">
<widget class="QSpinBox" name="reciteLinesSpin">
<property name="toolTip">
<string>Number of occluded lines shown on the answer side of each card.</string>
Expand All @@ -107,7 +124,7 @@
</property>
</widget>
</item>
<item row="8" column="0">
<item row="10" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Lines in Gro&amp;ups of</string>
Expand All @@ -117,7 +134,7 @@
</property>
</widget>
</item>
<item row="8" column="1">
<item row="10" column="1">
<widget class="QSpinBox" name="groupLinesSpin">
<property name="minimum">
<number>1</number>
Expand Down Expand Up @@ -186,7 +203,7 @@
<string>Generate notes from the text in the poem editor.</string>
</property>
<property name="text">
<string>&amp;Add notes</string>
<string>A&amp;dd notes</string>
</property>
<property name="autoDefault">
<bool>false</bool>
Expand Down Expand Up @@ -215,6 +232,7 @@
</widget>
<tabstops>
<tabstop>titleBox</tabstop>
<tabstop>authorBox</tabstop>
<tabstop>tagsBox</tabstop>
<tabstop>contextLinesSpin</tabstop>
<tabstop>reciteLinesSpin</tabstop>
Expand Down
20 changes: 19 additions & 1 deletion docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@
Changelog
=========

LPCG 1.4.0
==========

Released on November 15, 2020.

* Add an *Author* field to the note type,
which can optionally be set when adding poems
(thanks to @cashweaver).
The author displays just underneath the title and index when reviewing,
if present.
* The Add Notes access key has been changed from :kbd:`a` to :kbd:`d`
to resolve a conflict with :guilabel:`Author`
(all the letters in *Author* were already taken!).
* Allow quotation marks to be used in the title of a poem,
now that Anki is able to escape quotation marks in searches.
* Add screenshots to the README.


LPCG 1.3.0
==========

Expand Down Expand Up @@ -93,4 +111,4 @@ LPCG 0.9.1
LPCG 0.9.0
==========

* First public release.
* First public release.
14 changes: 8 additions & 6 deletions src/gen_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ def __init__(self) -> None:
self.successor: Optional['PoemLine'] = None
self.seq = -1

def populate_note(self, note: 'Note', title: str, tags: List[str],
def populate_note(self, note: 'Note', title: str, author: str, tags: List[str],
context_lines: int, recite_lines: int, deck_id: int) -> None:
"""
Fill the _note_ with content testing on the current line.
"""
note.model()['did'] = deck_id # type: ignore
note.tags = tags
note['Title'] = title
note['Author'] = author
note['Sequence'] = str(self.seq)
note['Context'] = self._format_context(context_lines)
note['Line'] = self._format_text(recite_lines)
Expand Down Expand Up @@ -80,7 +81,7 @@ def _get_text(self, _lines: int) -> List[str]:
"""
raise NotImplementedError

def populate_note(self, note: 'Note', title: str, tags: List[str],
def populate_note(self, note: 'Note', title: str, author: str, tags: List[str],
context_lines: int, recite_lines: int, deck_id: int) -> None:
raise AssertionError("The Beginning node cannot be used to populate a note.")

Expand Down Expand Up @@ -243,10 +244,11 @@ def _normalize_blank_lines(text_lines):


def add_notes(col: Any, note_constructor: Callable,
title: str, tags: List[str], text: List[str], deck_id: int,
context_lines: int, group_lines: int, recite_lines: int):
title: str, author:str, tags: List[str], text: List[str],
deck_id: int, context_lines: int, group_lines: int,
recite_lines: int):
"""
Generate notes from the given title, tags, poem text, and number of
Generate notes from the given title, author, tags, poem text, and number of
lines of context. Return the number of notes added.
Return the number of notes added.
Expand All @@ -258,7 +260,7 @@ def add_notes(col: Any, note_constructor: Callable,
added = 0
for line in _poemlines_from_textlines(text, group_lines):
n = note_constructor(col, col.models.byName("LPCG 1.0"))
line.populate_note(n, title, tags, context_lines, recite_lines, deck_id)
line.populate_note(n, title, author, tags, context_lines, recite_lines, deck_id)
col.addNote(n)
added += 1
return added
3 changes: 2 additions & 1 deletion src/lpcg_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def accept(self):
'"Open File" button to import a text file.')
return

author = self.form.authorBox.text().strip()
tags = self.mw.col.tags.split(self.form.tagsBox.text())
text = cleanse_text(self.form.textBox.toPlainText().strip(), self.addonConfig)
context_lines = self.form.contextLinesSpin.value()
Expand All @@ -71,7 +72,7 @@ def accept(self):
did = self.deckChooser.selectedId()

try:
notes_generated = add_notes(self.mw.col, Note, title, tags, text, did,
notes_generated = add_notes(self.mw.col, Note, title, author, tags, text, did,
context_lines, group_lines, recite_lines)
except KeyError as e:
showWarning(
Expand Down
31 changes: 26 additions & 5 deletions src/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def is_at_version(cls, current_version: str) -> bool:
return current_version == cls.version


def upgrade_onethreeoh(mod):
def upgrade_none_to_onethreeoh(mod):
"Upgrade LPCG model from unversioned to version 1.3.0."
mm = aqt.mw.col.models
field = mm.newField("Prompt")
Expand Down Expand Up @@ -175,11 +175,30 @@ def upgrade_onethreeoh(mod):
)


def upgrade_onethreeoh_to_onefouroh(mod):
"Upgrade LPCG model from 1.3.0 to version 1.4.0."
mm = aqt.mw.col.models
mm.addField(mod, mm.newField("Author"))

mod['css'] = mod['css'].replace('.title {', '.title, .author {')

assert len(mod['tmpls']) == 1, "LPCG note type has extra templates!"
for side in ['qfmt', 'afmt']:
mod['tmpls'][0][side] = mod['tmpls'][0][side].replace(
'<div class="title">{{Title}} {{Sequence}}</div>',
dedent('''
<div class="title">{{Title}} {{Sequence}}</div>
{{#Author}}<div class="author">{{Author}}</div>{{/Author}}
''').strip()
)


class LpcgOne(ModelData):
class LpcgOneTemplate(TemplateData):
name = "LPCG1"
front = """
<div class="title">{{Title}} {{Sequence}}</div>
{{#Author}}<div class="author">{{Author}}</div>{{/Author}}
<br>
Expand All @@ -193,6 +212,7 @@ class LpcgOneTemplate(TemplateData):
"""
back = """
<div class="title">{{Title}} {{Sequence}}</div>
{{#Author}}<div class="author">{{Author}}</div>{{/Author}}
<br>
Expand All @@ -203,7 +223,7 @@ class LpcgOneTemplate(TemplateData):
"""

name = "LPCG 1.0"
fields = ("Line", "Context", "Title", "Sequence", "Prompt")
fields = ("Line", "Context", "Title", "Author", "Sequence", "Prompt")
templates = (LpcgOneTemplate,)
styling = """
.card {
Expand Down Expand Up @@ -234,7 +254,7 @@ class LpcgOneTemplate(TemplateData):
filter: invert(85%);
}
.title {
.title, .author {
text-align: center;
font-size: small;
}
Expand All @@ -245,9 +265,10 @@ class LpcgOneTemplate(TemplateData):
"""
sort_field = "Sequence"
is_cloze = False
version = "1.3.0"
version = "1.4.0"
upgrades = (
("none", "1.3.0", upgrade_onethreeoh),
("none", "1.3.0", upgrade_none_to_onethreeoh),
("1.3.0", "1.4.0", upgrade_onethreeoh_to_onefouroh),
)


Expand Down
4 changes: 4 additions & 0 deletions test/test_gen_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def mock_note():
col = MockCollection()
note_constructor = MockNote
title = "'Tis Winter"
author = "Samuel Longfellow"
tags = ["poem", "test"]
deck_id = 1
context_lines = 2
Expand All @@ -185,13 +186,15 @@ def test_render_default_settings(mock_note):
assert len(col.notes) == 16

assert col.notes[0]['Title'] == mock_note['title']
assert col.notes[0]['Author'] == mock_note['author']
assert col.notes[0].tags == mock_note['tags']
assert col.notes[0]['Sequence'] == "1"
assert col.notes[0]['Context'] == "<p>[Beginning]</p>"
assert col.notes[0]['Line'] == "<p>'Tis winter now; the fallen snow</p>"
assert 'Prompt' not in col.notes[0]

assert col.notes[3]['Title'] == mock_note['title']
assert col.notes[3]['Author'] == mock_note['author']
assert col.notes[3].tags == mock_note['tags']
assert col.notes[3]['Sequence'] == "4"
assert col.notes[3]['Context'] == (
Expand Down Expand Up @@ -242,6 +245,7 @@ def test_render_groups_of_three(mock_note):
assert len(col.notes) == 6

assert col.notes[0]['Title'] == mock_note['title']
assert col.notes[0]['Author'] == mock_note['author']
assert col.notes[0].tags == mock_note['tags']
assert col.notes[0]['Sequence'] == "1"
assert col.notes[0]['Context'] == "<p>[Beginning]</p>"
Expand Down

0 comments on commit 888d72d

Please sign in to comment.