Django 1.8:如何在django模板中为语法突出显示特殊代码块? [英] Django 1.8: How can I make special code blocks for syntax highlighting in django templates?

查看:164
本文介绍了Django 1.8:如何在django模板中为语法突出显示特殊代码块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个django自定义模板过滤器,使特殊的代码块看起来像以下...



Python代码 / p>

  {%highlight python%} 

import random

#生成一个

i = random.randrand(10,50)
打印'你的号码是',i

{%endhighlight%

Ruby代码



$ $ $ $ $ $







$ b {%endhighlight%}

R代码

  {%highlight r%} 

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

#分类树
摘要(ct< - rpart(Credit〜CreditAmount + Age + CreditHistory + Employment,data = credit ))

{%endhighlight%}


我想制作根据正确的编程语言突出显示的代码块。像Ruby那样,Ruby代码被相应地突出显示,这与R和Python不同,它们彼此不同。有没有人在这里创建类似的东西?

解决方案

Python-Markdown附带了 CodeHilite Extension ,用于突出显示代码块。不需要使用模板(实际上,Markdown文件通常不会通过模板传递,尽管如果您想为其编写自定义代码是可能的 - 请参阅这里)。



只需启用扩展并定义第一行代码块的语言(如所述在文档中):

  ::: python 
import random

#生成一个

i = random.randrange(10,50)
打印'您的号码是',i

如果您还启用围栏代码块,您还可以定义它们上的语言(这不需要缩进代码块):

  

``

CodeHilite在引擎盖下使用 Pygments 突出显示代码,所以自动支持Pygments支持的任何语言



当然,为了使其工作,您需要启用扩展。没有任何有关Django如何使用Markdown的信息,我只能提供几个指针。



如果您直接从Python代码调用Markdown库,那么只需包括调用 markdown.markdown 中的扩展名:

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

否则,您可能会发现 django_markdown 库是有帮助的。它有 MARKDOWN_EXTENSIONS 设置您需要在Django设置文件中设置:

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

最后,您将需要CSS来告诉浏览器如何设置突出显示的代码。 Pygments项目提供了一些默认的 CSS样式,您可能会发现它们是一个有用的起点(这些CSS风格已经整齐地被打包 richeland )。您将需要将CSS添加到您网站使用的CSS(具体细节取决于您的Django站点配置方式,因此被排除在此答案之外)。


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?

解决方案

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).

这篇关于Django 1.8:如何在django模板中为语法突出显示特殊代码块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆