58 8
Sign in to rate

Syntax Highlighting for Code

0.82MB. Updated 2018-10-07. Supports both Anki 2.0.x and 2.1.x.

Description

Allows you to insert syntax-highlighted code snippets into your notes. SCREENSHOT RELEASE LOG LATEST CHANGES AND NEWS 2018-08-16 v2.0.0 This is the first public release of my fork of Tiago Barroso's Syntax Highlighting for Code add-on. I would like to extend my heartfelt gratitude to all of what he has done for the Anki community over the years. This update is meant to carry on his legacy by adding Anki 2.1 support, implementing some long-requested features, and refactoring major parts of the codebase. All of this was made possible through the generous support of a fellow Anki user who would like to remain anonymous. An overview of the most important changes in this release follows below: USAGE
  1. Open the Add Note window in Anki.
  2. Compose your code snippet in your favorite text editor.
  3. Copy the code to the clipboard (e.g. Ctrl+C)
  4. Move the cursor to to the field you want to insert your code snippet into
  5. In the top right corner of the editing window there should be a new Thunderbolt icon with a dropdown.
  6. Choose the language your snippet is written in, and click the Thunderbolt / use it's associated hotkey (default: Alt+S).
  7. Anki will copy your syntax highlighted snippet to the field
Alternatively, you can compose your code directly in Anki, highlight it, and then click the lightning button. But generally it is much more convenient to use a dedicated code editor with monospaced fonts and proper syntax highlighting. The add-on will automatically remember the last programming language you chose, even after restarting Anki. CONFIGURATION Basic Currently there are four configuration options, available from Anki's main screen through ToolsSyntax Highlighting Options: Please note that changes in the configuration will only affect new notes. Advanced Syntax Highlighting also comes with a number of advanced options. These can be edited by either using Anki 2.1's inbuilt add-on configuration screen (ToolsAdd-ons → select Syntax Highlighting → click on Config) or by manually editing the corresponding config keys in syntax_highlighting/meta.json in Anki's add-on folder (Anki 2.0) [the config.json file contains the default values and should not be modified.] The following options may be customized: These advanced settings do not sync and require a restart to apply. SUPPORT Please do not report issues or bugs in the review section below. I can only reply to your reviews in a limited fashion, so this is not a good way to strike up a dialog and track issues down. Instead, please report all issues you encounter either by creating a bug report on GitHub, or by posting a new thread on the Anki add-on support forums. Please make sure to include the name of the affected add-on in your report title when you do so. CREDITS AND LICENSE Copyright © 2012-2015 Tiago Barroso Copyright © 2015 Tim Rae Copyright © 2018 Aristotelis P. Syntax Highlighting is based on Syntax Highlighting for Code by Tiago Barroso. All credit for the original idea and implementation goes to him. A major thanks is also due to Tim Rae, who extended the original add-on with CSS support. The present fork and update to Anki 2.1 was made possible through the generous support of a fellow Anki user who would like to remain anonymous. Licensed under the GNU AGPLv3. The code for this add-on is available on GitHub. For more information on the licensing terms and other software shipped with this package please check out the README. MORE RESOURCES A lot of my add-ons were commissioned by other Anki users. Please feel free to reach out to me if you would like to hire my services for any Anki-related development work (writing an add-on for you, converting existing ones to Anki 2.1, implementing a specific feature): ankiglutanimate [αt] gmail .com. Want to stay up-to-date with my latest add-on releases and updates? Feel free to follow me on Twitter: @Glutanimate New to Anki? Make sure to check out my YouTube channel where I post weekly tutorials on Anki add-ons and related topics: / Glutanimate ============================================ SUPPORT THIS ADD-ON Writing, supporting, and maintaining Anki add-ons like these takes a lot of time and effort. If Syntax Highlighting has been a valuable asset in your studies, please consider using one of the buttons below to support my efforts by buying me a coffee, sandwich, meal, or anything else you'd like: New: I also have a Patreon now! If you would like to support my work while also receiving some exclusive add-ons and other goodies, please do check it out!: Each and every contribution is greatly appreciated and will help me maintain and improve Syntax Highlighting as time goes by!

Download

As add-ons are programs downloaded from the internet, they are potentially malicious. You should only download add-ons you trust.

To download this add-on, please copy and paste the following code into the desktop program:

1463041493

If you were linked to this page from the internet, please open Anki on your computer, go to the Tools menu and then Add-ons>Browse & Install to paste in the code.

Other Anki 2.0.x Add-Ons Other Anki 2.1.x Add-Ons Ask a Question

Reviews

on 1558704418
Very useful
on 1558106922
Works pretty well, thanks!
on 1557464296
Worked flawlessly with Python code
on 1557267502
Works well.
on 1556585473
Excelent addon for develops
on 1555491241
Thanks for the super useful addon. I really appreciate it.
on 1555469886
Easy to use, and does exactly what I wanted it to do.
on 1555229530
Excellent, C support is great.
I can fianlly make readable code card.
Top!
on 1552583862
Looks great and I appreciate the huge list of language options!
on 1547608273
beautiful !
thank you
on 1546381988
Everything works fine (Anki 2.1.x).

Except not compatible with Night Mode (some text is white on white in night mode, like line numbers).
on 1546216628
Exactly what I needed.
on 1546189993
good idea
on 1545231392
Simple and does what it means to do - thanks!
on 1544797248
gets the job done!
on 1544013475
এটা দারুন, প্রোগ্রামারদের জন্যে কাজের জিনিস। Its really usefull.
on 1543396135
Cool
on 1543010018
This is amazing...love how you can choose between which languages you want to highlight.
on 1542205496
Works well
on 1541337997
Provides good syntax highlighting for code within cards
on 1541258915
Excellent stuff!
Thank you.
on 1541130027
simple and perfect
on 1540446972
Once I realised Alt-S is the way to apply the styling (not simply selecting your language in the drop down) and the centering can be removed in config this got the thumbs up!
on 1539094116
Great !
on 1538930093
Great!! Thanks for updating the Plugin so it works with Liquid Syntax. Once I updated the plugin and did another copy and paste it worked. No missing lines of text. I guess Liquid can be awkward because of it's particular syntax. I've removed the negative vote and criticism and gave it a thumbs up. I will be happy to make a small donation sometime in the future. It's a great plugin for those learning to code like me.

If I discover any other minor issues how do I contact you.. Via this method or do you have a website/email?
Comment from author
Happy to hear that things are working fine now. Thanks for the rating! GitHub would be best, as replying here is rather cumbersome: https://github.com/glutanimate/syntax-highlighting/issues
on 1538862923
Unconditionally loved it.
on 1538619940
awesome!
on 1538503385
Love this add-on. Very useful and tons of languages supported. Thanks for the awesome work!!
on 1537926338
This is great add-on. All you have to do is highlight your code, and press the lightning button to format it and add syntax highlighting. I made some flashcards for ES6+ features/syntax and my code snippets are now beautiful.
Personally, I disabled line numbers, disabled centering, and used CSS classes (copy and paste the CSS in the link and add it to your note template).
on 1527638400
Fix this problem:
https://gist.github.com/ferstar/61dbec4e74bcc725172ec46e546c65e1
Comment from author
Hi there! Thanks for taking the time to link to the hotfix. For what it's worth, with the latest release of the add-on this is no longer required as all issues previously reported in the review section should be fixed.
on 1523318400
Works great after applying ferstar's fix.
on 1523232000
Работает после замены файла code_highlight_addon.py скачанного с https://gist.github.com/ferstar/61dbec4e74bcc725172ec46e546c65e1
on 1514073600
worked after I added this code to line 125 of code_highlight_addon/code_highlight_addon.py
mw.col.conf.setdefault('syntax_highlighting_conf', default_conf)
on 1511481600
Working after applying ferstar's fix. Thanks!
on 1501027200
great addon, little problem but easy to fix

here is my solution:

vist https://gist.github.com/ferstar/61dbec4e74bcc725172ec46e546c65e1

download it as "code_highlight_addon.py"

replace the stock one

enjoy!
on 1499472000
Doesn't work anymore!

This addon was a great one,but it doesn't work anymore. Shows error when Anki starts! :(
on 1489708800
I applied two tips from the comments below and it works

1. Remove all add-ons
2. Add this add-on FIRST 162313389
This add-on can be referenced here: https://ankiweb.net/shared/info/162313389
3. Then I add this add-on: 1463041493
4. Select Tools/Add-ons/Open Add-ons Folder from the Anki menu and close Anki
5. go inside "code_highlight_addon" folder
6. edit code_highlight_addon.py in text editor

In code_highlight_addon.py, add right after the line (approx line 238):
def highlight_code(self):

this line:

mw.col.conf['syntax_highlighting_conf'] = default_conf

so it seems like:
def highlight_code(self):
mw.col.conf['syntax_highlighting_conf'] = default_conf

save, restart anki, enjoy
on 1487462400
Was good, but not any more

This was a useful add on until it stopped working. There are work around, but they seem to create more issues than they fix.
Comment from author
Hi there! Please feel free to give the latest version of Syntax Highlighting a try. I've recently taken over maintenance of the add-on and most if not all issues reported in the review section should be fixed now.
on 1487376000
works like a charm after changing one line of code

Follow the "Fixing KeyError" review, simply add one line to its source python code. All work now for both ubuntu and mac.
on 1474675200
Fixing KeyError

In code_highlight_addon.py, simply add this line right after
def highlight_code(self):
mw.col.conf['syntax_highlighting_conf'] = default_conf

This will still allow you to save your line number and centering options.

If you get a pygments error, follow the instructions of the previous reviews (either install the supplementary buttons addon first, or edit code_highlight_addon.py with the pygments path)

4 stars because it requires manual fixing.
on 1474588800
Good,but not perfect

It depend on another plugin,so it's not perfect.but good enough,
on 1472169600
The add on don't work

An error occurred in an add-on.
Please post on the add-on forum:
https://anki.tenderapp.com/discussions/add-ons

Traceback (most recent call last):
File "C:\Users\rafael\Documents\Anki\addons\ankihabitica.py", line 621, in score_backlog
ah.habitica.grab_scorecounter('Anki Points')
File "C:\Users\rafael\Documents\Anki\addons\AnkiHabitica\habitica_class.py", line 332, in grab_scorecounter
if self.missing[habit]:
KeyError: 'Anki Points'
on 1466380800
Getting it to work

If you are are having trouble getting this code to work, here is what I did:

1. Removed all add-ons
2. Added this add-on FIRST 162313389
This add-on can be referenced here: https://ankiweb.net/shared/info/162313389
3. Then I added this add-on: 1463041493
on 1465603200
Help

I fixed the pygments error mentioned below, but still have this one...

An error occurred in an add-on.
Please post on the add-on forum:
https://anki.tenderapp.com/discussions/add-ons

Traceback (most recent call last):
File "/home/<user>/Documents/Anki/addons/code_highlight_addon/code_highlight_addon.py", line 125, in setupUi
if mw.col.conf['syntax_highlighting_conf']['linenos']:
KeyError: 'syntax_highlighting_conf'
Comment from author
Hi there! Thanks for the issue report. I know it's been a while, but please feel free to give the latest version of Syntax Highlighting a try. I've recently taken over maintenance of the add-on and most if not all issues reported in the review section should be fixed now.
on 1461974400
Not working as others mentioned

Gives the following error message when started

An error occurred in an add-on.
Please post on the add-on forum:
https://anki.tenderapp.com/discussions/add-ons

Traceback (most recent call last):
File "C:\cygwin\home\dae\win\build\pyi.win32\anki\outPYZ1.pyz/aqt.addons", line 41, in loadAddons
File "c:\pyi\iu.py", line 436, in importHook
File "c:\pyi\iu.py", line 521, in doimport
File "C:\Users\prave_000\Documents\Anki\addons\Syntax Highlighting for Code.py", line 2, in <module>
import code_highlight_addon.code_highlight_addon
File "c:\pyi\iu.py", line 436, in importHook
File "c:\pyi\iu.py", line 521, in doimport
File "C:\Users\prave_000\Documents\Anki\addons\code_highlight_addon\code_highlight_addon.py", line 221, in <module>
from pygments import highlight
File "c:\pyi\iu.py", line 455, in importHook
ImportError: No module named pygments
Comment from author
Hi there! Thanks for the issue report. I know it's been a while, but please feel free to give the latest version of Syntax Highlighting a try. I've recently taken over maintenance of the add-on and most if not all issues reported in the review section should be fixed now.
on 1459296000
Great add-on when it worked.

ANKI Version 2.0.33
Qt 4.8.4 PyQt 4.10

ERROR:
Traceback (most recent call last):
File "C:\cygwin\home\dae\win\build\pyi.win32\anki\outPYZ1.pyz/aqt.addons", line 41, in loadAddons
File "c:\pyi\iu.py", line 436, in importHook
File "c:\pyi\iu.py", line 521, in doimport
File "C:\Users\jrodarte\Documents\Anki\addons\Syntax Highlighting for Code.py", line 2, in <module>
import code_highlight_addon.code_highlight_addon
File "c:\pyi\iu.py", line 436, in importHook
File "c:\pyi\iu.py", line 521, in doimport
File "C:\Users\jrodarte\Documents\Anki\addons\code_highlight_addon\code_highlight_addon.py", line 221, in <module>
from pygments import highlight
File "c:\pyi\iu.py", line 455, in importHook
ImportError: No module named pygments
Comment from author
Hi there! Thanks for the issue report. I know it's been a while, but please feel free to give the latest version of Syntax Highlighting a try. I've recently taken over maintenance of the add-on and most if not all issues reported in the review section should be fixed now.
on 1453680000
Alternative

You can use the Power format pack addon as an alternative, since it also allows you to highlight code using pygments.
on 1453334400
Good News

I'm running Anki 2.0.20 on Ubuntu 14.04. I added this add-on to my installation and it's working perfectly, as advertised.
Here's a screenshot of it in action (the snippet is Python):
http://s21.postimg.org/xwy2yzo8n/Screenshot_from_2016_01_21_13_08_42.png
on 1452643200
Bad News

I used this addon for about a year and loved it. Then it broke.

I (and several other users) posted the errors. No help. No responses at all.

Then the addon disappeared from the Anki repository for about a month. No explanation. Nothing. Then it suddenly reappeared. It still doesn't work.

Don't waste your time.

For what it's worth, I write about one product review every ten years. That should tell you how annoyed I am right now.

UPDATE: 1/12/16 -- Used the advice of other reviewers, edited code_highlight_addon.py per the instructions. No help at all. Still doesn't work.

Using Linux Mint 17.2 with Cinnamon desktop.
Comment from author
Hi there! I know it's been a while, but please feel free to give the latest version of Syntax Highlighting a try. I've recently taken over maintenance of the add-on and most if not all issues reported in the review section should be fixed now.
on 1451433600
This was an amazing, extremely helpful plugin until it stopped working. I'm getting this error on my Mac, El Capitan:

An error occurred in an add-on.
Please post on the add-on forum:
https://anki.tenderapp.com/discussions/add-ons

Traceback (most recent call last):
File "aqt/addons.pyc", line 41, in loadAddons
File "/Users/trevormiller/Documents/Anki/addons/Syntax Highlighting for Code.py", line 2, in <module>
File "/Users/trevormiller/Documents/Anki/addons/code_highlight_addon/code_highlight_addon.py", line 221, in <module>
ImportError: No module named pygments
on 1450828800
It used to work perfectly, but I'm having minor issues on Ubuntu 14.04

So as one of the other users mentioned, the
center = False

tweak prevented the error from being reported, but the behavior was the opposite as expected. Setting it to
center = True

doesn't center it so it's good to go again.

Thank you for your plugin!
on 1449964800
Two review answers fixed the problem

I also had the two mentioned problems but just fixed them like the people suggested. It works, thanks to author and the users. :)
on 1448150400
Fixes

If you get the pygments error, do what the review posted at 2015-11-15 says to do.

If you get the Keyerror: center error, then open code_highlight_addon.py in a text editor, and edit this line:

center = mw.col.conf['syntax_highlighting_conf']['center']

To this:

center = False

You can also set this to True if you prefer to have the code centered in your anki cards.
on 1447804800
I have used it before it is broken...

This is error in my Mac:

Traceback (most recent call last):
File "Documents/Anki/addons/code_highlight_addon/code_highlight_addon.py", line 244, in highlight_code
center = mw.col.conf['syntax_highlighting_conf']['center']
KeyError: 'center'
on 1447545600
Here's a Fix

This is a great plugin, but currently broken for some folks.

To fix it:

1. Remove the add-on and restart Anki (if it's installed)
2. Install the add-on, but don't quit Anki yet
3. From menu, choose Tools -> Add-ons -> Open Add-ons Folder, then in that folder open up the "code_highlight_addon" folder and then open code_highlight_addon.py in a text editor. There's a line in there that says this:

sys.path.insert(0, os.path.join(addons_folder(), "code_highlight_addon/libs/pygments"))

add this line underneath it (with matching indendation):

sys.path.insert(0, os.path.join(addons_folder(), "code_highlight_addon/libs"))

4. Save the file and close it
5. Restart Anki

This worked for me on a Mac, hopefully it will fix the plugin for you until the author can donate some more time to fixing it :).
on 1447459200
Looks good, but doesn't seem to work anymore

As of Nov 2015, it doesn't look like this works.

I get loading errors concerning 'pygments'. I tried with Python 3.5 and 2.7, and I tried forcing pygments to version 1.6, as one of the other reviewers suggested, but it makes no difference. Won't load.
Comment from author
Hi there! I know it's been a while, but please feel free to give the latest version of Syntax Highlighting a try. I've recently taken over maintenance of the add-on and most if not all issues reported in the review section should be fixed now.
on 1447027200
Great plugin

Make sure to keep pygments at version 1.6, other wise you might get some errors concerning bytes

https://stackoverflow.com/questions/27405405/i-get-an-importerror-cannot-import-name-bytes-with-pygments