html_blocks.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. """
  2. HTML blocks are simple blocks used to represent common HTML elements,
  3. with additional styling and attributes.
  4. """
  5. from django.utils.html import format_html
  6. from django.utils.safestring import mark_safe
  7. from django.utils.translation import ugettext_lazy as _
  8. from pygments import highlight
  9. from pygments.lexers import get_all_lexers, get_lexer_by_name
  10. from pygments.formatters import HtmlFormatter
  11. from wagtail.contrib.table_block.blocks import TableBlock as WagtailTableBlock
  12. from wagtail.core import blocks
  13. from wagtail.documents.blocks import DocumentChooserBlock
  14. from wagtail.images.blocks import ImageChooserBlock
  15. from .base_blocks import BaseBlock, BaseLinkBlock, ButtonMixin, CoderedAdvTrackingSettings, LinkStructValue
  16. class ButtonBlock(ButtonMixin, BaseLinkBlock):
  17. """
  18. A link styled as a button.
  19. """
  20. class Meta:
  21. template = 'coderedcms/blocks/button_block.html'
  22. icon = 'fa-hand-pointer-o'
  23. label = _('Button Link')
  24. value_class = LinkStructValue
  25. class CodeBlock(BaseBlock):
  26. """
  27. Source code with syntax highlighting in a <pre> tag.
  28. """
  29. LANGUAGE_CHOICES = []
  30. for lex in get_all_lexers():
  31. LANGUAGE_CHOICES.append((lex[1][0], lex[0]))
  32. language = blocks.ChoiceBlock(
  33. required=False,
  34. choices=LANGUAGE_CHOICES,
  35. label=_('Syntax highlighting'),
  36. )
  37. title = blocks.CharBlock(
  38. required=False,
  39. max_length=255,
  40. label=_('Title'),
  41. )
  42. code = blocks.TextBlock(
  43. classname='monospace',
  44. rows=8,
  45. label=('Code'),
  46. help_text=_('Code is rendered in a <pre> tag.'),
  47. )
  48. def get_context(self, value, parent_context=None):
  49. ctx = super(CodeBlock, self).get_context(value, parent_context)
  50. if value['language']:
  51. src = value['code'].strip('\n')
  52. lexer = get_lexer_by_name(value['language'])
  53. code_html = mark_safe(highlight(src, lexer, HtmlFormatter()))
  54. else:
  55. code_html = format_html('<pre>{}</pre>', value['code'])
  56. ctx.update({
  57. 'code_html': code_html,
  58. })
  59. return ctx
  60. class Meta:
  61. template = 'coderedcms/blocks/code_block.html'
  62. icon = 'fa-file-code-o'
  63. label = _('Formatted Code')
  64. class DownloadBlock(ButtonMixin, BaseBlock):
  65. """
  66. Link to a file that can be downloaded.
  67. """
  68. automatic_download = blocks.BooleanBlock(
  69. required=False,
  70. label=_('Auto download'),
  71. )
  72. downloadable_file = DocumentChooserBlock(
  73. required=False,
  74. label=_('Document link'),
  75. )
  76. advsettings_class = CoderedAdvTrackingSettings
  77. class Meta:
  78. template = 'coderedcms/blocks/download_block.html'
  79. icon = 'download'
  80. label = _('Download')
  81. class EmbedGoogleMapBlock(BaseBlock):
  82. """
  83. An embedded Google map in an <iframe>.
  84. """
  85. search = blocks.CharBlock(
  86. required=False,
  87. max_length=255,
  88. label=_('Search query'),
  89. help_text=_('Address or search term used to find your location on the map.'),
  90. )
  91. place_id = blocks.CharBlock(
  92. required=False,
  93. max_length=255,
  94. label=_('Google place ID'),
  95. help_text=_('Requires API key to use place ID.')
  96. )
  97. map_zoom_level = blocks.IntegerBlock(
  98. required=False,
  99. default=14,
  100. label=_('Map zoom level'),
  101. help_text=_('Requires API key to use zoom. 1: World, 5: Landmass/continent, 10: City, 15: Streets, 20: Buildings')
  102. )
  103. class Meta:
  104. template = 'coderedcms/blocks/google_map.html'
  105. icon = 'fa-map'
  106. label = _('Google Map')
  107. class EmbedVideoBlock(BaseBlock):
  108. """
  109. An embedded video on the page in an <iframe>. Currently supports youtube and vimeo.
  110. """
  111. url = blocks.URLBlock(
  112. required=True,
  113. label=_('URL'),
  114. help_text=_('Link to a YouTube or Vimeo video.'),
  115. )
  116. class Meta:
  117. template = 'coderedcms/blocks/embed_video_block.html'
  118. icon = 'media'
  119. label = _('Embed Video')
  120. class H1Block(BaseBlock):
  121. """
  122. An <h1> heading.
  123. """
  124. text = blocks.CharBlock(
  125. max_length=255,
  126. label=_('Text'),
  127. )
  128. class Meta:
  129. template = 'coderedcms/blocks/h1_block.html'
  130. icon = 'fa-header'
  131. label = _('Heading 1')
  132. class H2Block(BaseBlock):
  133. """
  134. An <h2> heading.
  135. """
  136. text = blocks.CharBlock(
  137. max_length=255,
  138. label=_('Text'),
  139. )
  140. class Meta:
  141. template = 'coderedcms/blocks/h2_block.html'
  142. icon = 'fa-header'
  143. label = _('Heading 2')
  144. class H3Block(BaseBlock):
  145. """
  146. An <h3> heading.
  147. """
  148. text = blocks.CharBlock(
  149. max_length=255,
  150. label=_('Text'),
  151. )
  152. class Meta:
  153. template = 'coderedcms/blocks/h3_block.html'
  154. icon = 'fa-header'
  155. label = _('Heading 3')
  156. class TableBlock(BaseBlock):
  157. table = WagtailTableBlock()
  158. class Meta:
  159. template = 'coderedcms/blocks/table_block.html'
  160. icon = 'fa-table'
  161. label = 'Table'
  162. class ImageBlock(BaseBlock):
  163. """
  164. An <img>, by default styled responsively to fill its container.
  165. """
  166. image = ImageChooserBlock(
  167. label=_('Image'),
  168. )
  169. class Meta:
  170. template = 'coderedcms/blocks/image_block.html'
  171. icon = 'image'
  172. label = _('Image')
  173. class ImageLinkBlock(BaseLinkBlock):
  174. """
  175. An <a> with an image inside it, instead of text.
  176. """
  177. image = ImageChooserBlock(
  178. label=_('Image'),
  179. )
  180. alt_text = blocks.CharBlock(
  181. max_length=255,
  182. required=True,
  183. help_text=_('Alternate text to show if the image doesn’t load'),
  184. )
  185. class Meta:
  186. template = 'coderedcms/blocks/image_link_block.html'
  187. icon = 'image'
  188. label = _('Image Link')
  189. value_class = LinkStructValue
  190. class QuoteBlock(BaseBlock):
  191. """
  192. A <blockquote>.
  193. """
  194. text = blocks.TextBlock(
  195. required=True,
  196. rows=4,
  197. label=_('Quote Text'),
  198. )
  199. author = blocks.CharBlock(
  200. required=False,
  201. max_length=255,
  202. label=_('Author'),
  203. )
  204. class Meta:
  205. template = 'coderedcms/blocks/quote_block.html'
  206. icon = 'openquote'
  207. label = _('Quote')