使用 Markdown 和 Markdown 进行语法高亮显示 Django 中的 Pygments
我一直在尝试使用 Markdown (2.0.1) 和 Markdown (2.0.1) 在我的简单 Django (1.1) 测试应用程序中进行语法突出显示。 皮格兹 (1.0)。 这个想法是从用户输入生成 Markdown 格式的 HTML 并将两者存储在数据库中,这样我就不必在获取期间将 Markdown 转换为 html。
到目前为止,我的降价处理工作正常,但我似乎无法使语法突出显示工作。 我的 models.py 看起来像这样:
from django.db import models
from django.contrib import admin
from markdown import markdown
class BlogPost( models.Model ):
title = models.CharField( max_length = 150 )
body = models.TextField()
body_html = models.TextField(editable=False, blank=True, null=True)
timestamp = models.DateTimeField()
def save(self):
self.body_html = markdown(self.body, ['codehilite'])
super( BlogPost, self).save()
class Meta:
ordering = ( '-timestamp', )
class BlogPostAdmin( admin.ModelAdmin ):
list_display = ( 'title', 'timestamp' )
admin.site.register(BlogPost, BlogPostAdmin)
到目前为止,仅测试 markdown 语法是有效的,但是如果我尝试类似以下的操作,我在输出或输出源中没有看到任何语法突出显示:
:::python
from foo import bar
foobar = bar('foo')
我希望看到至少一组输出源中的代码元素。
I've been trying to get syntax highlighting working in my simple Django (1.1) test app using Markdown (2.0.1) & Pygments (1.0). The idea is to generate HTML from the users input which is in markdown format and store both in the DB so I don't have to do the markdown to html translation during the fetch.
So far I have the markdown processing working but I cannot seem to get syntax highlighting working. My models.py looks like this:
from django.db import models
from django.contrib import admin
from markdown import markdown
class BlogPost( models.Model ):
title = models.CharField( max_length = 150 )
body = models.TextField()
body_html = models.TextField(editable=False, blank=True, null=True)
timestamp = models.DateTimeField()
def save(self):
self.body_html = markdown(self.body, ['codehilite'])
super( BlogPost, self).save()
class Meta:
ordering = ( '-timestamp', )
class BlogPostAdmin( admin.ModelAdmin ):
list_display = ( 'title', 'timestamp' )
admin.site.register(BlogPost, BlogPostAdmin)
So far testing just markdown syntax works but if I try something like the following I don't seen any syntax highlighting in the output or the output source:
:::python
from foo import bar
foobar = bar('foo')
I'd expect to see at least a set of code elements in the output source.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
修复! 代码应该缩进四个空格而不是三个!
在提出问题之前,我进行了多次编辑来测试这一点,但 Firefox 似乎将该页面缓存为测试帖子。 由于我一直使用 Windows 键盘快捷键来强制页面重新加载,而不是 Mac 键盘快捷键,哦!
当我出于对四个空格缩进的沮丧而制作了一个新的测试帖子,然后检查页面源代码时,我发现它正在工作。
Fixed it! The code should have been indented four spaces not three!
I made multiple edits to test that out before asking the question but it would seem Firefox cached the page as using as a test post. As I had been using the windows keyboard shortcut to force a page reload not the mac keyboard shortcut, d'oh!
I spotted it was working when I made a new test post out of frustration with four space indenting and then inspected the page source.
最好以 Markdown 格式存储在数据库中,然后在显示时将其转换为您想要的演示格式(HTML)。 这样您就可以像最初添加数据一样编辑数据。
在模板的顶部,您应该包括:
然后使用模板过滤器 markdown。
然后只需使用 css 来确保格式正确。
如果这里没有的话,您还需要安装 markdown 包 。
在 INSTALLED_APPS 的 settings.py 中,您应该包含 'django.contrib.markup'
了解更多信息请参阅此页。
至于为什么看不到格式,请检查标记的源并确保其正常工作。 即确保它正确标记。 然后确保您拥有所需的样式表。
Markdown 格式是标记之前的格式。
您还可以使用 JQuery 向标记的元素添加类,这样您就可以设置 Markdown 文本的样式,而不会影响页面的其余部分。
It's better to store it in the database in markdown format, and then convert it to the presentation format you'd like (HTML) at display time. That way you can edit your data the same way you added it in the first place.
At the top of your template you should include:
Then use the template filter markdown.
Then just use css to make sure you have the proper formatting.
You also need to install the markdown package if you don't have it here.
And in your settings.py in your INSTALLED_APPS you should include 'django.contrib.markup'
For more information see this page.
As for why you don't see formatting, check the marked up source and make sure it is working correctly. i.e. make sure it is marking up properly. Then make sure you have the needed stylesheets.
Markdown format is the format before it is marked up.
You can also use JQuery to add a class to the marked up elements, so you can style the markdown text without affecting the rest of the page.