Skip to content

Commit

Permalink
Add Author field
Browse files Browse the repository at this point in the history
  • Loading branch information
cashpw committed Oct 11, 2020
1 parent 74f92c0 commit 4a8bedc
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 20 deletions.
32 changes: 25 additions & 7 deletions designer/import_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>&amp;Author</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>
Expand All @@ -49,10 +66,10 @@
</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 @@ -215,6 +232,7 @@
</widget>
<tabstops>
<tabstop>titleBox</tabstop>
<tabstop>authorBox</tabstop>
<tabstop>tagsBox</tabstop>
<tabstop>contextLinesSpin</tabstop>
<tabstop>reciteLinesSpin</tabstop>
Expand Down
7 changes: 6 additions & 1 deletion docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

LPCG 1.3.1
==========

* Added Author field.

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

Expand Down Expand Up @@ -93,4 +98,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 @@ -56,6 +56,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.mw.addonManager.getConfig(__name__))
Expand All @@ -65,7 +66,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
37 changes: 32 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,36 @@ def upgrade_onethreeoh(mod):
)


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

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()
)


def upgrade_unversioned_to_onethreeone(mod):
"Upgrade LPCG model from 1.3.0 to version 1.3.1."
upgrade_none_to_onethreeoh(mod)
upgrade_onethreeoh_to_onethreeone(mod)


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 +218,7 @@ class LpcgOneTemplate(TemplateData):
"""
back = """
<div class="title">{{Title}} {{Sequence}}</div>
{{#Author}}<div class="author">{{Author}}</div>{{/Author}}
<br>
Expand All @@ -203,7 +229,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 +260,7 @@ class LpcgOneTemplate(TemplateData):
filter: invert(85%);
}
.title {
.title, .author {
text-align: center;
font-size: small;
}
Expand All @@ -245,9 +271,10 @@ class LpcgOneTemplate(TemplateData):
"""
sort_field = "Sequence"
is_cloze = False
version = "1.3.0"
version = "1.3.1"
upgrades = (
("none", "1.3.0", upgrade_onethreeoh),
("none", "1.3.0", upgrade_none_to_onethreeoh),
("1.3.0", "1.3.1", upgrade_onethreeoh_to_onethreeone),
)


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 4a8bedc

Please sign in to comment.