据说mistune比markdown好用,于是兴冲冲的安装了。
另外还有Pygments
好吧,费了半天劲才能正常的显示。
还是英文说明文档的问题,看的太费劲了。
我先测试测试Python语法会不会高亮
1 | #highlights.py |
好吧,Python还不高亮,关于Markdown标记的语法还是有问题的,或者说是后台pygments处理的时候有问题。
好像后台pygments不会把1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
但是我如果用`html`标记,就可以正常解析。
另外的难点还是陌生的语言的支持吧,比如`.ini`文件和`.conf`文件语言的识别。
虽然`Pygments`支持的语言有很种,但是就是可能默认的语言处理功能有点弱,就是上面那几行代码。
后台预览markdown的功能太差了,看来要换了,后台和前台根本就不一致。换掉`Markdownx`。
----------
## 今天又来更新
> 我错了,昨天怎么弄也不行的代码高亮,今天莫名其妙的就好了。
只需要把代码部分前面加上````lang`,这里的`lang`可用的字段,貌似可以从`Pygments`其官网找到,链接如下:<http://pygments.org/docs/lexers/>,就是其`Short Names`字段的值。
首先当然是安装`mistune`和`pygments`,**如果项目在虚拟环境,就进虚拟环境安装**。
```python
pip install mistune
pip install pygments
然后新建highlights.py文件,文件代码见上面。
再然后,在view.py
文件中加入如下代码:
1 | #view.py |
然后我放了一段js代码在三个反引号中间,类似这样:
1 | $(document).ready(function(){ |
或者放一段html代码在三个反引号中间,类似这样:
1 | ... |
或者一段css代码放到三个反引号中间,类似这样:
1 | .codehilite.hll { background-color: #ffffcc } |
反引号就是主键盘
数字1左边
,Tab键
上面的那个键,直接按下去就是反引号。
其实这个时候html代码已经翻译完成了,但是代码并不会高亮显示。这个时候需要引入css文件。
PS: 因为教程的原因,下载了highlight的好几个css文件,每个文件里面的样式都是以
codehilite
开头的,所以上面的highlights.py
中的HtmlFormatter
加入了参数cssclass='codehilite'
。
只要把css文件放入static目录,并正确引用就可以了。css代码类似于上面我贴的那一段,就是指定了一些文字的颜色。