Closed ghost closed 4 years ago
When Anki encodes rxn0
, it stores the LaTeX source as the following in the .apkg
file:
[latex] \schemestart \chemfig{*6(--(<OH)-(<:Br)---)} \arrow{->[?]} \chemfig{*6(--(<[:30]{O}?)(<:H)-?[,{>},](<:H)---)} \schemestop[/latex]
As you can see, <
and >
are HTML-encoded as <
and >
. This seems strange because this code does not represent HTML, but that is apparently what Anki does.
Meanwhile, genanki does not do any such encoding; the data in the .apkg
is
[latex]
\schemestart
\chemfig{*6(--(<OH)-(<:Br)---)}
\arrow{->[?]}
\chemfig{*6(--(<[:30]{O}?)(<:H)-?[,{>},](<:H)---)}
\schemestop[/latex]
(it also keeps newlines).
So, it looks like genanki needs to do some encoding of <
and >
symbols inside field data. I will look further into this and create a test / patch.
Anki HTML-escapes <
, >
, and &
in the user input, because it allows the user to input arbitrary HTML for the field data (using a rich text editor).
We actually cannot automatically HTML-escape these characters, because there may be genanki
users that want to include HTML in field data.
Instead, what we can do here is (1) add documentation stating that field data must be HTML and literal <>&
must be escaped using the Python html.escape
function (even for LaTeX), and (2) add a warning that triggers if a field contains <...>
but it doesn't look like a HTML tag (the ...
doesn't match the regex ^/?[a-z]+( |/?$)
).
I will implement both of these changes.
After https://github.com/kerrickstaley/genanki/commit/104ea564217ecdaaccf6c127b9b2b81f66bb8d2f, when I run your code, I get the following warnings:
/home/kerrick/src/genanki/genanki/note.py:143: UserWarning: Field contained the following invalid HTML tags. Make sure you are calling html.escape() if your field data isn't already HTML-encoded: <OH)-(<:Br)---)}
\arrow{-> <[:30]{O}?)(<:H)-?[,{>
warnings.warn("Field contained the following invalid HTML tags. Make sure you are calling html.escape() if"
/home/kerrick/src/genanki/genanki/note.py:143: UserWarning: Field contained the following invalid HTML tags. Make sure you are calling html.escape() if your field data isn't already HTML-encoded: <[:30]{O}?)(<:H)-?[,{>
warnings.warn("Field contained the following invalid HTML tags. Make sure you are calling html.escape() if"
I think this is adequate to help the user diagnose/resolve the sorts of issue you mentioned. Thanks for the detailed bug report!
Released in 0.8.1.
On Arch Linux, Python 3.7.2
If I create a apkg file with this script:
results in an anki popup window that says:
which looks fine.
And I set the appropriate latex header and enable dvisvgm:
However, when studying or browsing, for some reason the latex output of rxn0 and rxn1 gets messed up:
rxn0
/tmp/anki_temp/tmp.tex
:when it should be:
/tmp/anki_temp/tmp.tex
:rxn1
/tmp/anki_temp/tmp.tex
:instead of
/tmp/anki_temp/tmp.tex
:So basically,
becomes
and
becomes
What's stranger is that I tested the python script several times and one time rxn1 did not have the issue. rxn2 has never been affected by this quirk. For some reason, chemfigs of epoxides get messed up by anki when importing a apkg generated by genanki.