RkBlog

Carbon - export your code as pretty images

2023-09-18

carbon is a simple tool that allows you to export a code snippet as a pretty image - either PNG or SVG.

You may have seen such exported images on various social media as it's quite handy for code infographics/presentations. Sadly as it's an image you can't select the code so its uses are limited.

Code exported with Carbon
Code exported with Carbon

For webpages you should be using Pygments or frontend tools like Prism or Shiki.

class Entries(LanguagePage):
    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.filter(category__slug=self.kwargs['category']).exclude(hidden=True)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        try:
            category = Article.objects.get(slug=self.kwargs['category'])
        except Article.DoesNotExist:
            raise http.Http404
        else:
            context['category'] = category
        return context

Code highlighted with Prism

Comment article