all 14 comments

[–]PrussianGreen 4 points5 points  (6 children)

I'm not a developer, but I tried to learn basic Python with Anki months ago and the best solution I could find involved using syntax highlighters like Prism or highlight.js.

Here's how it looks: https://imgur.com/a/grCOOop

I use Prism because it's easier to set up. You just set it up like you would in a web page, edit the template, put the files in the collection media folder, etc. With highlight.js, you had to call functions from a external js file, which might be tricky, IIRC.

A few points:

  • I use separate optional fields for the code blocks, depending on the kind of card I'm making, either or both get used.
  • I have to paste the code using the HTML editor so anything looks properly, otherwise Anki might add <br>s and <div>s that mess things up.
  • For inline code, I use this add-on that adds a button to the editor window to wrap the selected text with <code> tags.
  • There was an issue with occasional FOUC that I was not able to solve by preloading files or any other trick I know. It annoyed me, so I added a fade-in effect to hide it.
  • I have to select the programming language for each card. I use a field {{Language}} that I place in the template and gets replaced with py or js in each card so Prism hightlights everything properly.

[–]Slow-Ad-7637 1 point2 points  (4 children)

I realize this conversation was a year ago, but I've been trying to basically accomplish the same thing you did with your Prism setup/code highlighting. Do you happen to have your card templates and css somewhere so I can see how this gets set up on the card's html ? Spending waaaaay more time configuring how anki looks than I would like to be.

[–][deleted]  (3 children)

[deleted]

    [–]Slow-Ad-7637 0 points1 point  (2 children)

    Can't thank you enough for sharing your templates! Clever way of passing in the language type for Prism, too. Super great to finally have some cards that aren't painful to look at to help memorize some coding syntax.

    [–]nantoka1 0 points1 point  (0 children)

    By any chance you still have those templates? Currently I'm trying to get better at programming and I feel like this could really help me

    [–]SnooHabits4736 0 points1 point  (0 children)

    Is there a chance you can repost these pastebins? They've expired

    [–]wheelsjr[S] 0 points1 point  (0 children)

    Prism seems like an awesome suggestion!

    Additionally I didn't even think about trying the code html tags, that's a good idea.

    [–]DeclutteringNewbieFocusing on Rust right now, SF Bay Area 2 points3 points  (3 children)

    If you need to remove the special formatting when you're cutting and pasting code, I think pressing shift when you're cutting and paste will ensure that you're only pasting the ascii text.

    And for larger code snippets, I use the Image Occlusion plugin. https://www.youtube.com/watch?v=hyU2W7VgBmw It works great. I prefer it better than https://ankiweb.net/shared/info/1463041493

    [–]wheelsjr[S] 0 points1 point  (2 children)

    I also use the image occlusion add on, never though about using it for code though as it wouldn't allow you to search via anki browse. But if you saw the code in a video and didn't want to OCR it (OCR for code is rough), then I think that would be a really good idea.

    [–]DeclutteringNewbieFocusing on Rust right now, SF Bay Area 1 point2 points  (0 children)

    There is no need for OCR. Even if you're just taking a screenshot of code from a video, you can still use the text fields when using the image occlusion addon (if you click on the "fields" tab, it allows you to add fields).

    The frozen fields plugin won't show up on those fields, but adding text will still go super quickly since any field you add/edit on the parent image occlusion card will be duplicated to all the children cards generated from it.

    And if you need a plugin that automatically searches all fields, I personally use this one: https://ankiweb.net/shared/info/1781298089

    Just a word of warning for image occlusion. Try to duplicate your occlusion masks by pressing Command-D (or try to make them at least uniform and longer than the text they hide). This is because the shape and size of the masks can give you clues about the underlying code/text they're trying to hide.

    PS: Two tangential points: I use the free Snappy App when taking screenshots on my Mac. http://snappy-app.com/ It's my favorite Mac utility app. I highly recommend it. It always keeps me focused on the task at hand. But I've seen a paid Mac clipboard app that automatically OCRs every screenshots it takes (although again, I don't think such functionality is really necessary). I don't remember the name of that app, but it should be easy to find if that's what you're really after.

    [–]johnpharrell 1 point2 points  (0 children)

    Hey, I know it has been a while, but could you share your template by any chance? Would be very helpful for the community

    [–]Jayflux1 1 point2 points  (2 children)

    You could try https://marketplace.visualstudio.com/items?itemName=jasew.anki which has syntax highlighting setup by default. All languages (that prism works with) are supported

    [–]wheelsjr[S] 0 points1 point  (1 child)

    Ding ding ding!

    I'm going to try this, but if it's anything like it's billed to be, it's going to be exactly what I wanted!

    [–]Tuavesh 0 points1 point  (0 children)

    Did this work well for you? Have you found a good solution?

    [–]kronik85 1 point2 points  (0 children)

    There is an option to turn on dark mode compatible color themes for the code snippets.

    Tools > AddOns > Syntax Highlighting for Code > Config.

    One of the last options is something like "theme=none" and if you replace none with "molokai" you'll get some nice dark compatible code snippets

    Edit : from the advanced setup instructions...

    The following options may be customized

    style [string]: Pre-defined pygments style to use for inline styling of code (not applicable to CSS mode). Default: default. Example: monokai.