Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to extra field in Cloze note type #47

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions anki-plugin/src/trmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ class TiddlyRememberClozeTemplate(TemplateData):
{{cloze:Text}}
"""
back = """
{{cloze:Text}}

{{cloze:Text}}<br>
{{Extra}}
<div class="note-id">
{{#Permalink}}
[<a href="{{text:Permalink}}">{{Wiki}}/{{Reference}}</a> {{ID}}]
Expand All @@ -336,8 +336,8 @@ class TiddlyRememberClozeTemplate(TemplateData):
</div>
"""

name = "TiddlyRemember Cloze v1"
fields = ("Text", ID_FIELD_NAME, "Wiki", "Reference", "Permalink")
name = "TiddlyRemember Cloze v2"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I mentioned, I don't want this to be a separate note type. I'll dive into implementing upgrades over the next couple of days and get back to you.

fields = ("Text", "Extra", ID_FIELD_NAME, "Wiki", "Reference", "Permalink")
templates = (TiddlyRememberClozeTemplate,)
styling = """
.card {
Expand Down
15 changes: 11 additions & 4 deletions anki-plugin/src/twnote.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,14 @@ class ClozeNote(TwNote):
model = TiddlyRememberCloze

def __init__(self, id_: Twid, wiki_name: str, tidref: str, text: str,
target_tags: Set[str], target_deck: Optional[str]) -> None:
extra: str, target_tags: Set[str], target_deck: Optional[str]) -> None:
super().__init__(id_, wiki_name, tidref, target_tags, target_deck)
self.text = text
self.extra = extra

def __repr__(self):
return (f"ClozeNote(id_={self.id_!r}, tidref={self.tidref!r}, "
f"text={self.text!r}, target_tags={self.target_tags!r}, "
f"text={self.text!r}, extra={self.extra!r}, target_tags={self.target_tags!r}, "
f"target_deck={self.target_deck!r})")

@classmethod
Expand All @@ -320,12 +321,13 @@ def parse_html(cls, soup: BeautifulSoup, wiki_name: str,
pairs = soup.find_all(class_="remembercz")
for pair in pairs:
text = clean_field_html(pair.find("span", class_="cloze-text"))
extra = clean_field_html(pair.find("span", class_="cloze-extra"))
id_raw = pair.find("div", class_="rid").get_text()
id_ = id_raw.strip().lstrip('[').rstrip(']')
tidref = select_tidref(pair.find("div", class_="tr-reference"),
tiddler_name)
parsed_text = ankify_clozes(text, wiki_name, tidref)
notes.add(cls(id_, wiki_name, tidref, parsed_text, tags, deck))
notes.add(cls(id_, wiki_name, tidref, parsed_text, extra, tags, deck))

return notes

Expand All @@ -334,10 +336,15 @@ def wants_soup(cls, soup: BeautifulSoup) -> bool:
return bool(soup.find(class_="remembercz"))

def _fields_equal(self, anki_note: Note) -> bool:
return self.text == anki_note['Text'] and self._base_equal(anki_note)
return (
self.text == anki_note['Text']
and self.extra == anki_note['Extra']
and self._base_equal(anki_note)
)

def _update_fields(self, anki_note: Note) -> None:
anki_note['Text'] = self.text
anki_note['Extra'] = self.extra
self._base_update(anki_note)


Expand Down
88 changes: 49 additions & 39 deletions tw-plugin/macros/remember.tid
Original file line number Diff line number Diff line change
Expand Up @@ -47,47 +47,57 @@ type: text/vnd.tiddlywiki
\end

\define remembercz(id, text, mode: "block", reference: "")
<$list filter="[[$mode$]match[inline]]" variable=_>
<$macrocall $name=twRememberClozeInline id=<<__id__>> text=<<__text__>> reference=<<__reference__>>/>
</$list>
<$list filter="[[$mode$]!match[inline]]" variable=_>
<$macrocall $name=twRememberClozeBlock id=<<__id__>> text=<<__text__>> reference=<<__reference__>>/>
</$list>
<$macrocall $name=remembercze id=<<__id__>> text=<<__text__>> extra="" mode=<<__mode__>> reference=<<__reference__>>/>
Copy link
Owner

@sobjornstad sobjornstad Sep 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I was expecting to do this differently...rather than creating an entirely new macro, just put the new field at the end of the argument list. Like

\define remembercz(id, text, mode: "block", reference: "", extra:"")

Then it would be backwards compatible and we wouldn't have to add in an extra stub and change the macro button to insert something new. It's still easy to use without specifying a mode or reference...

<<remembercz "20200101120000000" "This is my {test} cloze." extra:"Extra content goes here.">>

Unless you see some reason that doesn't work?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did on this way just to avoid to put the "extra:", but I think it is only trivial detail. Your proposal looks cleaner.

\end

\define twRememberClozeBlock(id, text, reference)
<div class="remembercz">
<span class="cloze-identifier">cloze: </span>
<span class="cloze-text">$text$</span>
<div class="tr-selfidentification">
<$set name="selfid" filter="""[enlist[$reference$]]""" value="""[<$link to="$reference$">$reference$</$link>: $id$]""" emptyValue="[$id$]">
<<selfid>>
</$set>
</div>
<div class="rid">
[$id$]
</div>
<div class="tr-reference">
<$text text=<<__reference__>>/>
</div>
</div>
\define remembercze(id, text, extra: "",mode: "block", reference: "")
<$list filter="[[$mode$]match[inline]]" variable=_>
<$macrocall $name=twRememberClozeInline id=<<__id__>> text=<<__text__>> reference=<<__reference__>>/>
</$list>
<$list filter="[[$mode$]!match[inline]]" variable=_>
<$macrocall $name=twRememberClozeBlock id=<<__id__>> text=<<__text__>> extra=<<__extra__>> reference=<<__reference__>>/>
</$list>
\end

\define twRememberClozeBlock(id, text, extra, reference)
<div class="remembercz">
<span class="cloze-identifier">cloze: </span>
<span class="cloze-text">$text$</span>
<$list filter="[[$extra$]!match[]]" variable=_>
<br>
<span class="cloze-identifier">extra: </span>
</$list>
<span class="cloze-extra">$extra$</span>
<div class="tr-selfidentification">
<$set name="selfid" filter="""[enlist[$reference$]]""" value="""[<$link to="$reference$">$reference$</$link>: $id$]""" emptyValue="[$id$]">
<<selfid>>
</$set>
</div>
<div class="rid">
[$id$]
</div>
<div class="tr-reference">
<$text text=<<__reference__>>/>
</div>
</div>
\end

\define twRememberClozeInline(id, text, reference)
<span class="remembercz">
<span class="cloze-identifier">{cloze: </span>
<span class="cloze-text">$text$</span>
<span class="cloze-identifier">}</span>
<div class="tr-selfidentification">
<$set name="selfid" filter="""[enlist[$reference$]]""" value="""[<$link to="$reference$">$reference$</$link>: $id$]""" emptyValue="[$id$]">
<<selfid>>
</$set>
</div>
<div class="rid">
[$id$]
</div>
<div class="tr-reference">
<$text text=<<__reference__>>/>
</div>
</span>
\end
<span class="remembercz">
<span class="cloze-identifier">{cloze: </span>
<span class="cloze-text">$text$</span>
<span class="cloze-extra"/>
<span class="cloze-identifier">}</span>
<div class="tr-selfidentification">
<$set name="selfid" filter="""[enlist[$reference$]]""" value="""[<$link to="$reference$">$reference$</$link>: $id$]""" emptyValue="[$id$]">
<<selfid>>
</$set>
</div>
<div class="rid">
[$id$]
</div>
<div class="tr-reference">
<$text text=<<__reference__>>/>
</div>
</span>
\end
2 changes: 1 addition & 1 deletion tw-plugin/stylesheets/remember.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ div.remembertwo div.tr-reference, .remembercz div.tr-reference {

div.remembertwo div.rid, .remembercz div.rid {
display: none;
}
}