Skip to content

Commit 2fdcd63

Browse files
committed
Add Author field
1 parent 74f92c0 commit 2fdcd63

File tree

6 files changed

+80
-20
lines changed

6 files changed

+80
-20
lines changed

designer/import_dialog.ui

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<item row="0" column="0" colspan="2">
2323
<widget class="QWidget" name="deckChooser" native="true"/>
2424
</item>
25+
2526
<item row="2" column="0">
2627
<widget class="QLabel" name="label">
2728
<property name="text">
@@ -40,6 +41,23 @@
4041
</widget>
4142
</item>
4243
<item row="4" column="0">
44+
<widget class="QLabel" name="label">
45+
<property name="text">
46+
<string>&amp;Author</string>
47+
</property>
48+
<property name="buddy">
49+
<cstring>authorBox</cstring>
50+
</property>
51+
</widget>
52+
</item>
53+
<item row="4" column="1">
54+
<widget class="QLineEdit" name="authorBox">
55+
<property name="text">
56+
<string/>
57+
</property>
58+
</widget>
59+
</item>
60+
<item row="6" column="0">
4361
<widget class="QLabel" name="label_3">
4462
<property name="text">
4563
<string>Ta&amp;gs</string>
@@ -49,10 +67,10 @@
4967
</property>
5068
</widget>
5169
</item>
52-
<item row="4" column="1">
70+
<item row="6" column="1">
5371
<widget class="QLineEdit" name="tagsBox"/>
5472
</item>
55-
<item row="6" column="0">
73+
<item row="8" column="0">
5674
<widget class="QLabel" name="label_2">
5775
<property name="text">
5876
<string>Lines of Conte&amp;xt</string>
@@ -62,7 +80,7 @@
6280
</property>
6381
</widget>
6482
</item>
65-
<item row="6" column="1">
83+
<item row="8" column="1">
6684
<widget class="QSpinBox" name="contextLinesSpin">
6785
<property name="minimumSize">
6886
<size>
@@ -84,7 +102,7 @@
84102
</property>
85103
</widget>
86104
</item>
87-
<item row="7" column="0">
105+
<item row="9" column="0">
88106
<widget class="QLabel" name="label_5">
89107
<property name="text">
90108
<string>Lines to &amp;Recite</string>
@@ -94,7 +112,7 @@
94112
</property>
95113
</widget>
96114
</item>
97-
<item row="7" column="1">
115+
<item row="9" column="1">
98116
<widget class="QSpinBox" name="reciteLinesSpin">
99117
<property name="toolTip">
100118
<string>Number of occluded lines shown on the answer side of each card.</string>
@@ -107,7 +125,7 @@
107125
</property>
108126
</widget>
109127
</item>
110-
<item row="8" column="0">
128+
<item row="10" column="0">
111129
<widget class="QLabel" name="label_4">
112130
<property name="text">
113131
<string>Lines in Gro&amp;ups of</string>
@@ -117,7 +135,7 @@
117135
</property>
118136
</widget>
119137
</item>
120-
<item row="8" column="1">
138+
<item row="10" column="1">
121139
<widget class="QSpinBox" name="groupLinesSpin">
122140
<property name="minimum">
123141
<number>1</number>
@@ -215,6 +233,7 @@
215233
</widget>
216234
<tabstops>
217235
<tabstop>titleBox</tabstop>
236+
<tabstop>authorBox</tabstop>
218237
<tabstop>tagsBox</tabstop>
219238
<tabstop>contextLinesSpin</tabstop>
220239
<tabstop>reciteLinesSpin</tabstop>

docs/changes.rst

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

5+
LPCG 1.3.1
6+
==========
7+
8+
* Added Author field.
9+
510
LPCG 1.3.0
611
==========
712

@@ -93,4 +98,4 @@ LPCG 0.9.1
9398
LPCG 0.9.0
9499
==========
95100

96-
* First public release.
101+
* 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
@@ -56,6 +56,7 @@ def accept(self):
5656
'"Open File" button to import a text file.')
5757
return
5858

59+
author = self.form.authorBox.text().strip()
5960
tags = self.mw.col.tags.split(self.form.tagsBox.text())
6061
text = cleanse_text(self.form.textBox.toPlainText().strip(),
6162
self.mw.addonManager.getConfig(__name__))
@@ -65,7 +66,7 @@ def accept(self):
6566
did = self.deckChooser.selectedId()
6667

6768
try:
68-
notes_generated = add_notes(self.mw.col, Note, title, tags, text, did,
69+
notes_generated = add_notes(self.mw.col, Note, title, author, tags, text, did,
6970
context_lines, group_lines, recite_lines)
7071
except KeyError as e:
7172
showWarning(

src/models.py

Lines changed: 34 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,36 @@ def upgrade_onethreeoh(mod):
175175
)
176176

177177

178+
def upgrade_onethreeoh_to_onethreeone(mod):
179+
"Upgrade LPCG model from 1.3.0 to version 1.3.1."
180+
mm = aqt.mw.col.models
181+
mm.addField(mod, mm.newField("Author"))
182+
183+
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+
196+
def upgrade_unversioned_to_onethreeone(mod):
197+
"Upgrade LPCG model from 1.3.0 to version 1.3.1."
198+
upgrade_none_to_onethreeoh(mod)
199+
upgrade_onethreeoh_to_onethreeone(mod)
200+
201+
178202
class LpcgOne(ModelData):
179203
class LpcgOneTemplate(TemplateData):
180204
name = "LPCG1"
181205
front = """
182206
<div class="title">{{Title}} {{Sequence}}</div>
207+
{{#Author}}<div class="author">{{Author}}</div>{{/Author}}
183208
184209
<br>
185210
@@ -193,6 +218,7 @@ class LpcgOneTemplate(TemplateData):
193218
"""
194219
back = """
195220
<div class="title">{{Title}} {{Sequence}}</div>
221+
{{#Author}}<div class="author">{{Author}}</div>{{/Author}}
196222
197223
<br>
198224
@@ -203,7 +229,7 @@ class LpcgOneTemplate(TemplateData):
203229
"""
204230

205231
name = "LPCG 1.0"
206-
fields = ("Line", "Context", "Title", "Sequence", "Prompt")
232+
fields = ("Line", "Context", "Title", "Author", "Sequence", "Prompt")
207233
templates = (LpcgOneTemplate,)
208234
styling = """
209235
.card {
@@ -234,7 +260,7 @@ class LpcgOneTemplate(TemplateData):
234260
filter: invert(85%);
235261
}
236262
237-
.title {
263+
.title, .author {
238264
text-align: center;
239265
font-size: small;
240266
}
@@ -245,9 +271,10 @@ class LpcgOneTemplate(TemplateData):
245271
"""
246272
sort_field = "Sequence"
247273
is_cloze = False
248-
version = "1.3.0"
274+
version = "1.3.1"
249275
upgrades = (
250-
("none", "1.3.0", upgrade_onethreeoh),
276+
("none", "1.3.0", upgrade_none_to_onethreeoh),
277+
("1.3.0", "1.3.1", upgrade_onethreeoh_to_onethreeone),
251278
)
252279

253280

@@ -285,3 +312,5 @@ def ensure_note_type() -> None:
285312
"Your LPCG model is out of date, but I couldn't find a valid upgrade path. " \
286313
"You are likely to encounter issues. " \
287314
"Please contact the developer for assistance resolving this problem."
315+
316+

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)