Skip to content

Commit e5671a2

Browse files
cashpwsobjornstad
authored andcommitted
add Author field
Closes #20 and PR #21.
1 parent 59532cb commit e5671a2

File tree

6 files changed

+86
-22
lines changed

6 files changed

+86
-22
lines changed

designer/import_dialog.ui

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,36 @@
4040
</widget>
4141
</item>
4242
<item row="4" column="0">
43+
<widget class="QLabel" name="label">
44+
<property name="text">
45+
<string>&amp;Author (optional)</string>
46+
</property>
47+
<property name="buddy">
48+
<cstring>authorBox</cstring>
49+
</property>
50+
</widget>
51+
</item>
52+
<item row="4" column="1">
53+
<widget class="QLineEdit" name="authorBox">
54+
<property name="text">
55+
<string/>
56+
</property>
57+
</widget>
58+
</item>
59+
<item row="6" column="0">
4360
<widget class="QLabel" name="label_3">
4461
<property name="text">
45-
<string>Ta&amp;gs</string>
62+
<string>Ta&amp;gs (optional)</string>
4663
</property>
4764
<property name="buddy">
4865
<cstring>tagsBox</cstring>
4966
</property>
5067
</widget>
5168
</item>
52-
<item row="4" column="1">
69+
<item row="6" column="1">
5370
<widget class="QLineEdit" name="tagsBox"/>
5471
</item>
55-
<item row="6" column="0">
72+
<item row="8" column="0">
5673
<widget class="QLabel" name="label_2">
5774
<property name="text">
5875
<string>Lines of Conte&amp;xt</string>
@@ -62,7 +79,7 @@
6279
</property>
6380
</widget>
6481
</item>
65-
<item row="6" column="1">
82+
<item row="8" column="1">
6683
<widget class="QSpinBox" name="contextLinesSpin">
6784
<property name="minimumSize">
6885
<size>
@@ -84,7 +101,7 @@
84101
</property>
85102
</widget>
86103
</item>
87-
<item row="7" column="0">
104+
<item row="9" column="0">
88105
<widget class="QLabel" name="label_5">
89106
<property name="text">
90107
<string>Lines to &amp;Recite</string>
@@ -94,7 +111,7 @@
94111
</property>
95112
</widget>
96113
</item>
97-
<item row="7" column="1">
114+
<item row="9" column="1">
98115
<widget class="QSpinBox" name="reciteLinesSpin">
99116
<property name="toolTip">
100117
<string>Number of occluded lines shown on the answer side of each card.</string>
@@ -107,7 +124,7 @@
107124
</property>
108125
</widget>
109126
</item>
110-
<item row="8" column="0">
127+
<item row="10" column="0">
111128
<widget class="QLabel" name="label_4">
112129
<property name="text">
113130
<string>Lines in Gro&amp;ups of</string>
@@ -117,7 +134,7 @@
117134
</property>
118135
</widget>
119136
</item>
120-
<item row="8" column="1">
137+
<item row="10" column="1">
121138
<widget class="QSpinBox" name="groupLinesSpin">
122139
<property name="minimum">
123140
<number>1</number>
@@ -186,7 +203,7 @@
186203
<string>Generate notes from the text in the poem editor.</string>
187204
</property>
188205
<property name="text">
189-
<string>&amp;Add notes</string>
206+
<string>A&amp;dd notes</string>
190207
</property>
191208
<property name="autoDefault">
192209
<bool>false</bool>
@@ -215,6 +232,7 @@
215232
</widget>
216233
<tabstops>
217234
<tabstop>titleBox</tabstop>
235+
<tabstop>authorBox</tabstop>
218236
<tabstop>tagsBox</tabstop>
219237
<tabstop>contextLinesSpin</tabstop>
220238
<tabstop>reciteLinesSpin</tabstop>

docs/changes.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22
Changelog
33
=========
44

5+
LPCG 1.4.0
6+
==========
7+
8+
Released on November 15, 2020.
9+
10+
* Add an *Author* field to the note type,
11+
which can optionally be set when adding poems
12+
(thanks to @cashweaver).
13+
The author displays just underneath the title and index when reviewing,
14+
if present.
15+
* The Add Notes access key has been changed from :kbd:`a` to :kbd:`d`
16+
to resolve a conflict with :guilabel:`Author`
17+
(all the letters in *Author* were already taken!).
18+
* Allow quotation marks to be used in the title of a poem,
19+
now that Anki is able to escape quotation marks in searches.
20+
* Add screenshots to the README.
21+
22+
523
LPCG 1.3.0
624
==========
725

@@ -93,4 +111,4 @@ LPCG 0.9.1
93111
LPCG 0.9.0
94112
==========
95113

96-
* First public release.
114+
* First public release.

src/gen_notes.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ def __init__(self) -> None:
1212
self.successor: Optional['PoemLine'] = None
1313
self.seq = -1
1414

15-
def populate_note(self, note: 'Note', title: str, tags: List[str],
15+
def populate_note(self, note: 'Note', title: str, author: str, tags: List[str],
1616
context_lines: int, recite_lines: int, deck_id: int) -> None:
1717
"""
1818
Fill the _note_ with content testing on the current line.
1919
"""
2020
note.model()['did'] = deck_id # type: ignore
2121
note.tags = tags
2222
note['Title'] = title
23+
note['Author'] = author
2324
note['Sequence'] = str(self.seq)
2425
note['Context'] = self._format_context(context_lines)
2526
note['Line'] = self._format_text(recite_lines)
@@ -80,7 +81,7 @@ def _get_text(self, _lines: int) -> List[str]:
8081
"""
8182
raise NotImplementedError
8283

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

@@ -243,10 +244,11 @@ def _normalize_blank_lines(text_lines):
243244

244245

245246
def add_notes(col: Any, note_constructor: Callable,
246-
title: str, tags: List[str], text: List[str], deck_id: int,
247-
context_lines: int, group_lines: int, recite_lines: int):
247+
title: str, author:str, tags: List[str], text: List[str],
248+
deck_id: int, context_lines: int, group_lines: int,
249+
recite_lines: int):
248250
"""
249-
Generate notes from the given title, tags, poem text, and number of
251+
Generate notes from the given title, author, tags, poem text, and number of
250252
lines of context. Return the number of notes added.
251253
252254
Return the number of notes added.
@@ -258,7 +260,7 @@ def add_notes(col: Any, note_constructor: Callable,
258260
added = 0
259261
for line in _poemlines_from_textlines(text, group_lines):
260262
n = note_constructor(col, col.models.byName("LPCG 1.0"))
261-
line.populate_note(n, title, tags, context_lines, recite_lines, deck_id)
263+
line.populate_note(n, title, author, tags, context_lines, recite_lines, deck_id)
262264
col.addNote(n)
263265
added += 1
264266
return added

src/lpcg_dialog.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def accept(self):
6363
'"Open File" button to import a text file.')
6464
return
6565

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

7374
try:
74-
notes_generated = add_notes(self.mw.col, Note, title, tags, text, did,
75+
notes_generated = add_notes(self.mw.col, Note, title, author, tags, text, did,
7576
context_lines, group_lines, recite_lines)
7677
except KeyError as e:
7778
showWarning(

src/models.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def is_at_version(cls, current_version: str) -> bool:
144144
return current_version == cls.version
145145

146146

147-
def upgrade_onethreeoh(mod):
147+
def upgrade_none_to_onethreeoh(mod):
148148
"Upgrade LPCG model from unversioned to version 1.3.0."
149149
mm = aqt.mw.col.models
150150
field = mm.newField("Prompt")
@@ -175,11 +175,30 @@ def upgrade_onethreeoh(mod):
175175
)
176176

177177

178+
def upgrade_onethreeoh_to_onefouroh(mod):
179+
"Upgrade LPCG model from 1.3.0 to version 1.4.0."
180+
mm = aqt.mw.col.models
181+
mm.addField(mod, mm.newField("Author"))
182+
183+
mod['css'] = mod['css'].replace('.title {', '.title, .author {')
184+
185+
assert len(mod['tmpls']) == 1, "LPCG note type has extra templates!"
186+
for side in ['qfmt', 'afmt']:
187+
mod['tmpls'][0][side] = mod['tmpls'][0][side].replace(
188+
'<div class="title">{{Title}} {{Sequence}}</div>',
189+
dedent('''
190+
<div class="title">{{Title}} {{Sequence}}</div>
191+
{{#Author}}<div class="author">{{Author}}</div>{{/Author}}
192+
''').strip()
193+
)
194+
195+
178196
class LpcgOne(ModelData):
179197
class LpcgOneTemplate(TemplateData):
180198
name = "LPCG1"
181199
front = """
182200
<div class="title">{{Title}} {{Sequence}}</div>
201+
{{#Author}}<div class="author">{{Author}}</div>{{/Author}}
183202
184203
<br>
185204
@@ -193,6 +212,7 @@ class LpcgOneTemplate(TemplateData):
193212
"""
194213
back = """
195214
<div class="title">{{Title}} {{Sequence}}</div>
215+
{{#Author}}<div class="author">{{Author}}</div>{{/Author}}
196216
197217
<br>
198218
@@ -203,7 +223,7 @@ class LpcgOneTemplate(TemplateData):
203223
"""
204224

205225
name = "LPCG 1.0"
206-
fields = ("Line", "Context", "Title", "Sequence", "Prompt")
226+
fields = ("Line", "Context", "Title", "Author", "Sequence", "Prompt")
207227
templates = (LpcgOneTemplate,)
208228
styling = """
209229
.card {
@@ -234,7 +254,7 @@ class LpcgOneTemplate(TemplateData):
234254
filter: invert(85%);
235255
}
236256
237-
.title {
257+
.title, .author {
238258
text-align: center;
239259
font-size: small;
240260
}
@@ -245,9 +265,10 @@ class LpcgOneTemplate(TemplateData):
245265
"""
246266
sort_field = "Sequence"
247267
is_cloze = False
248-
version = "1.3.0"
268+
version = "1.4.0"
249269
upgrades = (
250-
("none", "1.3.0", upgrade_onethreeoh),
270+
("none", "1.3.0", upgrade_none_to_onethreeoh),
271+
("1.3.0", "1.4.0", upgrade_onethreeoh_to_onefouroh),
251272
)
252273

253274

test/test_gen_notes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ def mock_note():
168168
col = MockCollection()
169169
note_constructor = MockNote
170170
title = "'Tis Winter"
171+
author = "Samuel Longfellow"
171172
tags = ["poem", "test"]
172173
deck_id = 1
173174
context_lines = 2
@@ -185,13 +186,15 @@ def test_render_default_settings(mock_note):
185186
assert len(col.notes) == 16
186187

187188
assert col.notes[0]['Title'] == mock_note['title']
189+
assert col.notes[0]['Author'] == mock_note['author']
188190
assert col.notes[0].tags == mock_note['tags']
189191
assert col.notes[0]['Sequence'] == "1"
190192
assert col.notes[0]['Context'] == "<p>[Beginning]</p>"
191193
assert col.notes[0]['Line'] == "<p>'Tis winter now; the fallen snow</p>"
192194
assert 'Prompt' not in col.notes[0]
193195

194196
assert col.notes[3]['Title'] == mock_note['title']
197+
assert col.notes[3]['Author'] == mock_note['author']
195198
assert col.notes[3].tags == mock_note['tags']
196199
assert col.notes[3]['Sequence'] == "4"
197200
assert col.notes[3]['Context'] == (
@@ -242,6 +245,7 @@ def test_render_groups_of_three(mock_note):
242245
assert len(col.notes) == 6
243246

244247
assert col.notes[0]['Title'] == mock_note['title']
248+
assert col.notes[0]['Author'] == mock_note['author']
245249
assert col.notes[0].tags == mock_note['tags']
246250
assert col.notes[0]['Sequence'] == "1"
247251
assert col.notes[0]['Context'] == "<p>[Beginning]</p>"

0 commit comments

Comments
 (0)