Pygments won't keep whitespaces

Issue #1382 new
Rafał Olszewski
created an issue

I've searched everywhere, and couldn't find a working solution or anything near that.

This is how my code in a template looks like:

        {% pygment 'java' %}
          private void registerUserEngageCustomer() {
  Customer customer = new Customer()
          .id("idfromyourdatabase") // id of your user
          .attr("email", "myemail@example.com")
          .attr("name", "John Doe")
          .attr("loyalCustomer", true)
          .attr("age", 33)
          .withDeviceInfo(this);

  UserEngage.getInstance()
          .register(customer, new UserEngage.RegisterCallback() {
              @Override
              public void onSuccess(RegisterResponse response) {
                  Log.d(TAG, "onSuccess: " + response);
                  // user is registered and now you can send events via SDK
              }

              @Override
              public void onFailure(Throwable throwable) {
                  Log.e(TAG, "onFailure: ", throwable);
                  // try again - something went wrong
              }
          });
}
        {% endpygment %}

This is how my highlighted code looks like: Screen Shot 2017-09-28 at 11.14.33.png

CSS is fine btw.

Comments (4)

  1. Rafał Olszewski reporter

    The template tag I'm using is taken from djangoproject.com and modified HtmlFormatter like so:

    class PygmentsNode(template.Node):
        def __init__(self, lexer_name, nodelist):
            self.nodelist = nodelist
            self.lexer_name = lexer_name
    
        def render(self, context):
            content = self.nodelist.render(context)
            lexer_name = self.lexer_name.resolve(context)
            lexer = get_lexer_by_name(lexer_name)
            output = highlight(content, lexer,
                               HtmlFormatter(linenos=True, lineseparator='<br>',
                                             lineanchors='highlight',
                                             anchorlinenos=True))
            return mark_safe(output)
    
    
    @register.tag(name='pygment')
    def do_pygments(parser, token):
        """
        Template tag that uses pygments to highlight code examples.
        Example usage:
            {% pygment 'python' %}
            def view_article(request, pk):
                return render(request, 'view_article.html', {
                    'article_id': get_object_or_404(Article, pk=pk)
                })
            {% endpygment %}
        """
        tokens = token.split_contents()
        nodelist = parser.parse(('endpygment',))
        parser.delete_first_token()
        return PygmentsNode(parser.compile_filter(tokens[1]), nodelist)
    

    So now it looks like: Screen Shot 2017-09-28 at 11.58.03.png

    But still missing white spaces

  2. Log in to comment