Django 1.8: How can I make special code blocks for

2019-06-10 07:46发布

问题:

I want to make a django custom template filter to make special code blocks that look like the following...

Python code

{% highlight python %}

    import random

    # Generate a random integer in the range 10 to 49.

    i = random.randrange(10,50)
    print 'Your number is', i

{% endhighlight %}

Ruby code

{% highlight ruby %}

    for i in (1..4)
        print i," "
    end

{% endhighlight %}

R code

{% highlight r %}

    require(rpart)
    load("C:/Users/Jaysp_000/Downloads/credit.rdata")

    # Classification Tree
    summary(ct <- rpart(Credit ~ CreditAmount + Age + CreditHistory + Employment, data=credit))

{% endhighlight %}

Does anyone know how I can do create one like this? I wanted to make code blocks that are highlighted according to the right programming language. Like, Ruby code is highlighted accordingly, which is different from R and Python, which are different from one another. Has anyone here created something similar?

回答1:

Python-Markdown ships with the CodeHilite Extension for highlighting code blocks. No need to use "templates" (in fact Markdown files would not generally be passed through templates, although it is possible if you wanted to write the custom code for it -- see an explanation here).

Simply enable the extension and define the language of the code block on the first line (as explained in the documentation):

    :::python
    import random

    # Generate a random integer in the range 10 to 49.

    i = random.randrange(10,50)
    print 'Your number is', i

If you also enable Fenced Code Blocks, you can also define the language on them (which eliminates the need to indent the code blocks):

```ruby
for i in (1..4)
    print i," "
end
```

Under the hood, CodeHilite uses Pygments to highlight the code, so any language supported by Pygments is automatically supported.

Of course, for this to work you need to enable the extensions. Without any information about how you are using Markdown from Django, I can only provide a few pointers.

If you are calling the Markdown library directly from Python code, then simply include the extensions in the call to markdown.markdown:

body = markdown.markdown(source, extensions=['markdown.extensions.codehilite', 'markdown.extensions.fenced_code'])

Otherwise, you may find the django_markdown library to be helpful. It has a setting for MARKDOWN_EXTENSIONS which you would need to set in your Django settings file:

MARKDOWN_EXTENSIONS = ['markdown.extensions.codehilite', 'markdown.extensions.fenced_code']

Finally, you will need the CSS to tell the browser how to style the highlighted code. The Pygments project provides some default CSS styles which you may find to be a useful starting point (those CSS styles have been neatly packaged up by richeland). You will need to add that CSS to the CSS used by your site (the specifics of which depend on how your Django site is configured and therefore is excluded from this answer).



回答2:

You can write a custom template tag that parses until an end tag and does something with the contents.