Syntax Highlighting (Enhanced FORK) [Official thread]

This thread is NOT about the popular add-on Syntax Highlighting for Code

If you have a problem with the add-on Syntax Highlighting (Enhanced FORK) post in this thread.

This way I get notified. If you make a new thread I will likely miss it. If you post your comment, question or bug report on ankiweb as a review I won’t get notified. You really should post in this thread.

2 Likes

Hello, this add-on is great. Thanks! The links to github are broken though - did the repo move?

I removed it because I often forgot to update it so that it was older than the ankiweb releases which would cause confusion/extra work for anyone forking it.

1 Like

Where can I find your repository? I have an issue:

An add-on you installed failed to load. If problems persist, please go to the Tools>Add-ons menu, and disable or delete the add-on.

When loading ‘⁨Syntax Highlighting FORK night mode fix gui config etc⁩’:
⁨Traceback (most recent call last):
File “/usr/lib/python3.10/site-packages/pkg_resources/init.py”, line 3030, in _dep_map
return self.__dep_map
File “/usr/lib/python3.10/site-packages/pkg_resources/init.py”, line 2827, in getattr
raise AttributeError(attr)
AttributeError: _DistInfoDistribution__dep_map

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/usr/lib/python3.10/site-packages/pkg_resources/init.py”, line 3021, in _parsed_pkg_info
return self._pkg_info
File “/usr/lib/python3.10/site-packages/pkg_resources/init.py”, line 2827, in getattr
raise AttributeError(attr)
AttributeError: _pkg_info

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/usr/lib/python3.10/site-packages/aqt/addons.py”, line 239, in loadAddons
import(addon.dir_name)
File “/home/grim/.local/share/Anki2/addons21/1972239816/init.py”, line 3, in
from . import syntax_highlighting
File “/home/grim/.local/share/Anki2/addons21/1972239816/syntax_highlighting.py”, line 19, in
from .config import anki_point_version
File “/home/grim/.local/share/Anki2/addons21/1972239816/config.py”, line 62, in
for lex in get_all_lexers():
File “/home/grim/.local/share/Anki2/addons21/1972239816/libs/pygments/lexers/init.py”, line 56, in get_all_lexers
for lexer in find_plugin_lexers():
File “/home/grim/.local/share/Anki2/addons21/1972239816/libs/pygments/plugin.py”, line 54, in find_plugin_lexers
yield entrypoint.load()
File “/usr/lib/python3.10/site-packages/pkg_resources/init.py”, line 2463, in load
self.require(*args, **kwargs)
File “/usr/lib/python3.10/site-packages/pkg_resources/init.py”, line 2486, in require
items = working_set.resolve(reqs, env, installer, extras=self.extras)
File “/usr/lib/python3.10/site-packages/pkg_resources/init.py”, line 785, in resolve
new_requirements = dist.requires(req.extras)[::-1]
File “/usr/lib/python3.10/site-packages/pkg_resources/init.py”, line 2748, in requires
dm = self._dep_map
File “/usr/lib/python3.10/site-packages/pkg_resources/init.py”, line 3032, in _dep_map
self.__dep_map = self._compute_dependencies()
File “/usr/lib/python3.10/site-packages/pkg_resources/init.py”, line 3041, in _compute_dependencies
for req in self._parsed_pkg_info.get_all(‘Requires-Dist’) or []:
File “/usr/lib/python3.10/site-packages/pkg_resources/init.py”, line 3023, in _parsed_pkg_info
metadata = self.get_metadata(self.PKG_INFO)
File “/usr/lib/python3.10/site-packages/pkg_resources/init.py”, line 1412, in get_metadata
value = self._get(path)
File “/usr/lib/python3.10/site-packages/pkg_resources/init.py”, line 1616, in _get
with open(path, ‘rb’) as stream:
FileNotFoundError: [Errno 2] No such file or directory: ‘/home/grim/.local/share/Anki2/addons21/1972239816/libs/Pygments-2.10.1.dist-info/METADATA’

@GrimPixel: I don’t have this repo online for two reasons: When I had repos online I often forgot to push the latest changes so that the few people who were interested got old code. Also, the way how I group my changes into commits and the commit messages I write could be improved so that knowing my commit history has only limited use. When I’m back at my main computer I can re-upload my old repo.

I haven’t seen this bug before and my last pygments update in this add-on was two months ago.

maybe it’s about your install?

Do you also have this problem if you use the packaged anki builds from https://apps.ankiweb.net/

for some reason the add-on appears to execute code outside of its folder from your general python system installation in /usr/lib/python3.10/ . My coding and linux skills are limited and there’s a good chance that you’re more qualified in tracking down the reason for this.

when it comes to bundling pygments: I just download the latest release from Releases · pygments/pygments · GitHub and put the code in the subfolder in my add-on.
the way I load the pygments library at the top of my file syntax_highlighting.py is the same that’s used in the original add-on from 2018 which re-used this approach from the old version for anki 2.0 which dates to 2015 or earlier.

I installed it from Arch User Repository. It’s called anki-git r9068.8306bc1e2-1

Anyway, I guess I am not going to spend much time on it in the near future because I am new to Python.

@GrimPixel: Could you do me a favor and try the original syntax highlighting add-on from Syntax Highlighting for Code - AnkiWeb and tell me whether you get a similar error when you just use the original version?

I can’t find that dropdown menu in the original one.

I had an idea that might have caused the problem with my version and just uploaded a slightly changed version of my add-on. Could you reinstall or update my version and restart anki and try again.

This would help me. Thanks.

The same error occurred again.

@GrimPixel: Thanks for testing. At the moment I have no more idea about this.

For some reason unknown to me, on desktop (macOS) Solarized light is used as expected. On mobile (iOS) the code snippets are displayed in Solarized dark. Any ideas?

@ferophila: Thanks for the report. But it’ll be some time until I get my hands at a recent ios device. I’m afraid that maybe a general night mode class “inverts” the solarized formatting on ios (= in the apple webview). There’s a good chance that you are in a better position than me to fix this …

Hi! how can I add syntax highlighting to specific words in the middle of a sentence like the red words in the image below?

@Hossein : I think Custom Styles (font color, background colour, classes) - AnkiWeb might be more useful here

Currently, I use a combination of Just mono (for shape) and Quick Colour Changing (custom shortcuts for colors).

How to add space before syntax highlighting box, like the image below; I don’t mean addimng space inside the box! I want to move the box to the right.

@ijgnd

@Hossein: check the html source code of the highllighted code. it should be in a table with the class highlighttable. then google how to properly add some space to the left of an element. I also have to lookup a lot of this stuff.

It’s probably some css like:

.highlighttable {
    margin-left: 10px;
}

this must be in the styling section so that it’s applied during reviews.

for the editor: you need an extra add-on. for newer anki versions it’s “css injector”, CSS Injector - Change default editor styles - AnkiWeb, for older anki versions it’s “customize stylesheet”. these add-ons tell you which files you need to edit.

1 Like

Hey there! @ijgnd
I’ve been trying to use this add-on to studying programming and found myself struggling with type-in cloze contrast cards (How do you do this in Python and Javascript? Write in the missing code).
The objective is having two formatted consoles in the same field, as clozes only work in the first field of the card.

The main problem is having to use two different fields for the languages and finding a way to put the second one between the two snippets, with a span.
I’ve thought of modifying the add-on source code to accommodate for this need, by adding the language name before the table (thus also eliminating the need of typing in a different field the language every time).

This gives you the possibility of styling the language on top of the code snippet
This was done by adding a style-less span in:
syntax-highlighting.py, line 277;

pretty_code = "".join([f'<span class="lang">{langAlias}</span><table style="text-align: left;" class="highlighttable"><tbody><tr><td>',

I was wondering if it could be turned into an option or a default feature.
I plan on sharing my cards/methodology with other people so it would make for an harder set-up to share the mod every time, and I thought also other people could benefit from this.
Thank you for your time and help :slight_smile:

This is the result:

1 Like