How to export/import text programatically

I’m trying to create a pipeline that allows me to leverage pandoc to create stylised programming cards. The way it works is that you give pandoc a markdown file with

```some language

```

and it returns a HTML representation back which will allow Anki style plugins to create more aesthetically pleasing programming cards.

I imagine this pipeline to look something like this.

  1. I create card text
    What does this function do?
    ```

function testFnc() {//… return something}

```
2. I hit convert and it exports the text out to a script that processes it and replaces the portions of backtick-language with pandoc generated HTML and then returns the output back to card.

These are the things I think I’m looking for info wise.

  1. A high level overview of the process to export text from Anki to a shell which can then process the text using pandoc, before Anki imports the response.
  2. Some information regarding important locations and files in the Anki ecosystem.
  3. Any further reading you can recommend to solve this particular task.

I understand that Anki is written in Python, which I have some experience in so I should be able to rustle up something.

I’m reading through an article called how to include external files in your template js css guide
as well as a julien sobczak article which is probably quite out of date now, but I wanted to post this in case, there is a gap in documented knowledge, that you think I should be aware of.

From your example, it looks like you want monospace (i.e. the typical font for code) and syntax highlighting.

You can get the first with the built-in HTML Editor (Ctrl-Shift-X while focused on a field in a card): wrap what you want to be monospace in <code>...</code>.

You can probably get the second with a syntax-highlighting add-on. I haven’t tried any, but search the existing options ( https:// ankiweb .net/shared/addons ) and see if one does what you like.

If none of that suffices, you could make an add-on (which would be in Python). There’s plenty more info at addon-docs .ankiweb.net and lurking in source code of existing add-ons. Some ideas to get you started:

  • “Hit convert” could be “click an item in the top menu” (you can add entries to the menus from add-ons)
  • “Export the text out to a script” could be “extract the text of a specific field in the selected note” (you can access the content of notes from add-ons)
  • “Process it and replace portions” could be “run Python code under Anki that uses outside libraries to process strings”
  • “Returns the output back to card” could be “set the content of the selected note’s field to the new string”
1 Like

You might want to have a look at the AnkiConnect add-on.

1 Like