我刚刚开始知道W ..如何为Wagtail配置所有内置draftail
格式。
我需要添加code
和qoutes。
答案 0 :(得分:0)
我认为如果你将StreamField
用于此目的会更好,我已经用这种方式完成了它(wagtail 1.13.1):
<强> blocks.py 强>
from django.utils.safestring import mark_safe
from django.utils.html import format_html
from pygments import highlight
from pygments.formatters import get_formatter_by_name
from pygments.lexers import get_lexer_by_name
from wagtail.wagtailcore import blocks
class CodeBlock(blocks.StructBlock):
"""
Code Highlighting Block
"""
LANGUAGE_CHOICES = (
('python', 'Python'),
('bash', 'Bash/Shell'),
('html', 'HTML'),
('css', 'CSS'),
('scss', 'SCSS'),
('json', 'JSON'),
('js', 'javaScript'),
('mysql', 'mysql'),
('jinja', 'jinja'),
('go', 'go')
)
STYLE_CHOICES = (
('default', 'default'),
('monokai', 'monokai'),
)
language = blocks.ChoiceBlock(choices=LANGUAGE_CHOICES)
style = blocks.ChoiceBlock(choices=STYLE_CHOICES, default='default')
code = blocks.TextBlock()
class Meta:
icon = 'code'
def render(self, value, context=None):
src = value['code'].strip('\n')
lang = value['language']
lexer = get_lexer_by_name(lang)
css_classes = ['code', value['style']]
formatter = get_formatter_by_name(
'html',
linenos='inline',
cssclass=' '.join(css_classes),
noclasses=False,
)
return mark_safe(highlight(src, lexer, formatter))
class BlockQuoteBlock(blocks.TextBlock):
def render_basic(self, value, context=None):
if value:
return format_html(
'<blockquote>{0}</blockquote>', mark_safe(value))
else:
return ''
class Meta:
icon = "openquote"
<强> models.py 强>
from wagtail.wagtailcore.fields import StreamField
from .blocks import CodeBlock, BlockQuoteBlock
...
class BlogPage(Page):
date = models.DateField("Post date")
intro = RichTextField(blank=True)
body = StreamField([
('heading', blocks.CharBlock(classname="full title")),
('paragraph', blocks.RichTextBlock()),
('code', CodeBlock()),
('blockquote', BlockQuoteBlock())
])
由于this issue,我使用了自定义BlockQuoteBlock
。另请查看StreamField
:http://docs.wagtail.io/en/v2.0/topics/streamfield.html#