From 1e7728524cc5d565fc30dfe666ee2b7ce03fd011 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Thu, 19 Feb 2015 17:18:50 +0000 Subject: [PATCH] make wagtailcore/blocks.py into a submodule (because it's too big) --- .../{blocks.py => blocks/__init__.py} | 21 ++----------------- wagtail/wagtailcore/blocks/utils.py | 20 ++++++++++++++++++ 2 files changed, 22 insertions(+), 19 deletions(-) rename wagtail/wagtailcore/{blocks.py => blocks/__init__.py} (98%) create mode 100644 wagtail/wagtailcore/blocks/utils.py diff --git a/wagtail/wagtailcore/blocks.py b/wagtail/wagtailcore/blocks/__init__.py similarity index 98% rename from wagtail/wagtailcore/blocks.py rename to wagtail/wagtailcore/blocks/__init__.py index fca443eae..68d9a82fa 100644 --- a/wagtail/wagtailcore/blocks.py +++ b/wagtail/wagtailcore/blocks/__init__.py @@ -1,9 +1,8 @@ -from __future__ import unicode_literals +from __future__ import absolute_import, unicode_literals # this ensures that any render / __str__ methods returning HTML via calls to mark_safe / format_html # return a SafeText, not SafeBytes; necessary so that it doesn't get re-encoded when the template engine # calls force_text, which would cause it to lose its 'safe' flag -import re import collections from django.core.exceptions import ValidationError, ImproperlyConfigured @@ -22,24 +21,8 @@ import six from wagtail.wagtailcore.utils import escape_script from wagtail.wagtailcore.rich_text import expand_db_html -# helpers for Javascript expression formatting +from .utils import indent, js_dict -def indent(string, depth=1): - """indent all non-empty lines of string by 'depth' 4-character tabs""" - return re.sub(r'(^|\n)([^\n]+)', '\g<1>' + (' ' * depth) + '\g<2>', string) - -def js_dict(d): - """ - Return a Javascript expression string for the dict 'd'. - Keys are assumed to be strings consisting only of JS-safe characters, and will be quoted but not escaped; - values are assumed to be valid Javascript expressions and will be neither escaped nor quoted (but will be - wrapped in parentheses, in case some awkward git decides to use the comma operator...) - """ - dict_items = [ - indent("'%s': (%s)" % (k, v)) - for (k, v) in d.items() - ] - return "{\n%s\n}" % ',\n'.join(dict_items) # ========================================= # Top-level superclasses and helper objects diff --git a/wagtail/wagtailcore/blocks/utils.py b/wagtail/wagtailcore/blocks/utils.py new file mode 100644 index 000000000..0f270d4b2 --- /dev/null +++ b/wagtail/wagtailcore/blocks/utils.py @@ -0,0 +1,20 @@ +import re + +# helpers for Javascript expression formatting + +def indent(string, depth=1): + """indent all non-empty lines of string by 'depth' 4-character tabs""" + return re.sub(r'(^|\n)([^\n]+)', '\g<1>' + (' ' * depth) + '\g<2>', string) + +def js_dict(d): + """ + Return a Javascript expression string for the dict 'd'. + Keys are assumed to be strings consisting only of JS-safe characters, and will be quoted but not escaped; + values are assumed to be valid Javascript expressions and will be neither escaped nor quoted (but will be + wrapped in parentheses, in case some awkward git decides to use the comma operator...) + """ + dict_items = [ + indent("'%s': (%s)" % (k, v)) + for (k, v) in d.items() + ] + return "{\n%s\n}" % ',\n'.join(dict_items)