louietan / anki-editor

Emacs minor mode for making Anki cards with Org
700 stars 87 forks source link

Fails to parse card if first word is in bold letters #6

Closed torfjelde closed 6 years ago

torfjelde commented 6 years ago

First of, this package is great! Before I've been using a small personal script which copies a region, wraps latex and maths, thus I could just paste into a card in Anki. What you've got going her, is waaay better. Thank you!

When attempting to submit this card, the Back is left blank.

If I then add some random text in front of *Perturbation Theory*, e.g. abc *Perturbation Theory*, it works.

*** Front
What's the purpose of *Perturbation Theory* ?

*** Back
*Perturbation Theory* provides a method for finding /approx./ energy eigenvalues and eigenstates for a system whose Hamiltonian is of the form
\begin{equation*}
  \hat{H} = \hat{H}_0 + \hat{H}'
\end{equation*}
where $\hat{H}_0$ is the Hamiltonian of an /exactly/ solvable system, for which we know the eigenvalues, $E_n^{(0)}$, and eigenstates, $\ket{n^{(0)}}$, and $\hat{H}'$ is /small, time-independent perturbation/.

I'll be happy to help out if you need help!

torfjelde commented 6 years ago

Did some debugging and it seems like it's an issue with how org-element extracts the contents after (org-element-parse-buffer) is called in anki-editor--process-note-heading. *Perturbation Theory* is treated as a headline for some reason.

louietan commented 6 years ago

I would like to know which version of Emacs and Org-mode you're using? I tried your example, it worked fine. I don't know if there once was a bug in org-element as you described, but how could it be rendered as blank if was treated as a headline ? Since I can't reproduce your problem, could you paste and elaborate the snippet which you think buggy here?

louietan commented 6 years ago

I need more clues, so please

  1. Put the point at the Back heading
  2. Execute command anki-editor-export-heading-contents-to-html
  3. Paste the result here
torfjelde commented 6 years ago

Emacs 25.1 and org-20180115.

Using anki-editor-export-heading-contents-to-html (when point is at the Back heading) it works fine:

<p>
<b>Perturbation Theory</b> provides a method for finding <i>approx.</i> energy eigenvalues and eigenstates for a system whose Hamiltonian is of the form
</p>

<p>
[latex]\begin{equation*}
  \hat{H} = \hat{H}_0 + \hat{H}'
\end{equation*}
[/latex]
where [$]\hat{H}_0[/$] is the Hamiltonian of an <i>exactly</i> solvable system, for which we know the eigenvalues, [$]E_n^{(0)}[/$], and eigenstates, [$]\ket{n^{(0)}}[/$], and [$]\hat{H}'[/$] is <i>small, time-independent perturbation</i>.
</p>

But Back is still empty if I submit to Anki.

louietan commented 6 years ago

I tried you example myself, Anki reported an error saying something like ! Undefined control sequence.<recently read> \ket. I don't use latex very often, but \ket seems to belong to the package physics, so you may have to check if you have added \usepackage{physics} to the latex preamble of your note type. (you can do this from menu bar "Tools -> Manage Note Types -> Options")

torfjelde commented 6 years ago

It's not that no; I'm already including \usepackage{physics} and it works fine if I just don't start the paragraph with *Perturbation Theory*.

As I noted in my earlier comments, it seems to "disappear" when you car the results from (org-element-parse-buffer) in anki-editor--process-note-heading. For some reason it's not included under the heading in this case, and ends up being dropped.

louietan commented 6 years ago

Oh well, I can reproduce this now. Yep, *Perturbation Theory* seems to be interpreted as headline by org-element, I'm still investigating.

louietan commented 6 years ago

Seems I have to enable Org major mode in the temp buffer to make org-element functions correctly.

louietan commented 6 years ago

org-element uses buffer-local variable outline-regexp to test headline, the default value of which is "[*\^L]+", which matches any text that starts with stars, that's why bold being interpreted as headline. When org gets enabled, it overwrites this variable with org-outline-regexp, whose value is "\\*+ " that's stars followed by a space. Also the doc string of org-element-parse-buffer says it assumes that current major mode is org-mode, so we have to enable Org-mode before using org-element.

971181d fixes this.