|
@@ -3,12 +3,7 @@ HTML blocks are simple blocks used to represent common HTML elements,
|
|
|
with additional styling and attributes.
|
|
|
"""
|
|
|
|
|
|
-from django.utils.html import format_html
|
|
|
-from django.utils.safestring import mark_safe
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
-from pygments import highlight
|
|
|
-from pygments.lexers import get_all_lexers, get_lexer_by_name
|
|
|
-from pygments.formatters import HtmlFormatter
|
|
|
from wagtail.contrib.table_block.blocks import TableBlock as WagtailTableBlock
|
|
|
from wagtail.core import blocks
|
|
|
from wagtail.documents.blocks import DocumentChooserBlock
|
|
@@ -29,54 +24,6 @@ class ButtonBlock(ButtonMixin, BaseLinkBlock):
|
|
|
value_class = LinkStructValue
|
|
|
|
|
|
|
|
|
-class CodeBlock(BaseBlock):
|
|
|
- """
|
|
|
- Source code with syntax highlighting in a <pre> tag.
|
|
|
- """
|
|
|
- LANGUAGE_CHOICES = []
|
|
|
-
|
|
|
- for lex in get_all_lexers():
|
|
|
- LANGUAGE_CHOICES.append((lex[1][0], lex[0]))
|
|
|
-
|
|
|
- language = blocks.ChoiceBlock(
|
|
|
- required=False,
|
|
|
- choices=LANGUAGE_CHOICES,
|
|
|
- label=_('Syntax highlighting'),
|
|
|
- )
|
|
|
- title = blocks.CharBlock(
|
|
|
- required=False,
|
|
|
- max_length=255,
|
|
|
- label=_('Title'),
|
|
|
- )
|
|
|
- code = blocks.TextBlock(
|
|
|
- classname='monospace',
|
|
|
- rows=8,
|
|
|
- label=('Code'),
|
|
|
- help_text=_('Code is rendered in a <pre> tag.'),
|
|
|
- )
|
|
|
-
|
|
|
- def get_context(self, value, parent_context=None):
|
|
|
- ctx = super(CodeBlock, self).get_context(value, parent_context)
|
|
|
-
|
|
|
- if value['language']:
|
|
|
- src = value['code'].strip('\n')
|
|
|
- lexer = get_lexer_by_name(value['language'])
|
|
|
- code_html = mark_safe(highlight(src, lexer, HtmlFormatter()))
|
|
|
- else:
|
|
|
- code_html = format_html('<pre>{}</pre>', value['code'])
|
|
|
-
|
|
|
- ctx.update({
|
|
|
- 'code_html': code_html,
|
|
|
- })
|
|
|
-
|
|
|
- return ctx
|
|
|
-
|
|
|
- class Meta:
|
|
|
- template = 'coderedcms/blocks/code_block.html'
|
|
|
- icon = 'fa-file-code-o'
|
|
|
- label = _('Formatted Code')
|
|
|
-
|
|
|
-
|
|
|
class DownloadBlock(ButtonMixin, BaseBlock):
|
|
|
"""
|
|
|
Link to a file that can be downloaded.
|