louietan / anki-editor

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

anki-editor-push-notes ignores source block headers on execution #28

Open omidmnz opened 6 years ago

omidmnz commented 6 years ago

I use Noweb reference syntax to embed some code in all my source blocks, which needs :noweb yes in the source blocks' headers. The code works perfectly fine when I execute the block myself, but anki-editor-push-notes executes the blocks without the header, which leads to wrong outputs. I tried :cache yes to avoid the problem, but anki-editor-push-notes seems to ignore that too. Here is a minimal example:

* Noweb Test                                                       :noexport:
  #+name: enable_noweb
  #+begin_src python
    noweb = True
  #+end_src

* Noweb Test
  :PROPERTIES:
  :ANKI_NOTE_TYPE: Basic
  :ANKI_DECK: OrgTestDeck
  :ANKI_NOTE_ID: 1536670156877
  :END:

** Front
   Noweb Enabled?

** Back
  #+begin_src python :exports results :results output :noweb yes
    noweb = False
    <<enable_noweb>>
    print("Noweb {0}.".format("enabled" if noweb else "not enabled"))
  #+end_src

  #+RESULTS:
  : Noweb enabled.

The result of manual execution is Noweb enabled. as expected, but the pushed card reads Noweb not enabled.. I am using Emacs 26.1, with Org Mode 9.1.14, and the latest anki-editor on Melpa.

louietan commented 6 years ago

This "unexpected behavior" involves the way anki-editor works, that when it is building the content of a field, it does the exporting on the region of subtree of that field entry, this way Org has no idea where that noweb block is defined, but when you evaluate the source block, Org will search backward the buffer for the definition of the noweb block.

omidmnz commented 6 years ago

Thanks for clarification. Should I consider this as a "Won't Fix" tag?

marzipankaiser commented 4 years ago

This also prevents globally set headers (#+PROPERTY: header-args ..., see https://orgmode.org/manual/Property-Syntax.html ) from working properly. Interestingly, interactively calling anki-editor-export-subtree-to-html (which is defined but never used in anki-editor.el) with the point inside the respective body returns the correct result for me (i.e. respects the global header args) - is there a reason to prefer using org-export-string-as in anki-editor--build-fields over using this (or the method used there)?