Move CKeditor configuration to a separate JS file

Remove unneeded try-except block
This commit is contained in:
Roland Geider 2013-04-15 21:12:29 +02:00 committed by Oskar Hahn
parent 1d1f5fa218
commit 56844d93f2
6 changed files with 86 additions and 88 deletions

View File

@ -169,37 +169,34 @@ def convert_html_to_reportlab(pdf, text):
soup = BeautifulSoup(text) soup = BeautifulSoup(text)
# read all list elements... # read all list elements...
for element in soup.find_all('li'): for element in soup.find_all('li'):
try: # ... and replace ul list elements with <para><bullet>&bull;</bullet>...<para>
# ... and replace ul list elements with <para><bullet>&bull;</bullet>...<para> if element.parent.name == "ul":
if element.parent.name == "ul": if element.ul:
if element.ul: # for nested ul lists use simple spaces (pragmatic solution)
# for nested ul lists use simple spaces (pragmatic solution) element.li.insert(0, '&nbsp;&nbsp;&nbsp;&nbsp;')
element.li.insert(0,'&nbsp;&nbsp;&nbsp;&nbsp;') element.insert_before(element.find_all('li'))
element.insert_before(element.find_all('li')) element.clear()
element.clear() else:
else: element.name = "para"
element.name = "para" bullet_tag = soup.new_tag("bullet")
bullet_tag = soup.new_tag("bullet") bullet_tag.string = "&bull;"
bullet_tag.string = "&bull;" element.insert(0, bullet_tag)
element.insert(0, bullet_tag) # ... and replace ol list elements with <para><bullet><seq id="%id"></seq>.</bullet>...</para>
# ... and replace ol list elements with <para><bullet><seq id="%id"></seq>.</bullet>...</para> if element.parent.name == "ol":
if element.parent.name == "ol": # set list id if element is the first of numbered list
# set list id if element is the first of numbered list if not element.find_previous_sibling():
if not element.find_previous_sibling(): id = random.randrange(0, 101)
id = random.randrange(0, 101) if element.ol:
if element.ol: # nested ol list
# nested ol list element.li.insert(0, '&nbsp;&nbsp;&nbsp;&nbsp;')
element.li.insert(0,'&nbsp;&nbsp;&nbsp;&nbsp;') element.insert_before(element.find_all('li'))
element.insert_before(element.find_all('li')) element.clear()
element.clear() else:
else: element.name = "para"
element.name = "para" element.insert(0, soup.new_tag("bullet"))
element.insert(0, soup.new_tag("bullet")) element.bullet.insert(0, soup.new_tag("seq"))
element.bullet.insert(0, soup.new_tag("seq")) element.bullet.seq['id'] = id
element.bullet.seq['id'] = id element.bullet.insert(1, ".")
element.bullet.insert(1, ".")
except AttributeError:
pass
# remove tags which are not supported by reportlab (replace tags with their children tags) # remove tags which are not supported by reportlab (replace tags with their children tags)
for tag in soup.find_all('ul'): for tag in soup.find_all('ul'):
tag.unwrap() tag.unwrap()

View File

@ -10,60 +10,8 @@
{% endblock %} {% endblock %}
{% block javascript %} {% block javascript %}
<script type="text/javascript" src="{% static 'javascript/ckeditor/ckeditor.js' %}"></script> <script type="text/javascript" src="{% static 'javascript/ckeditor/ckeditor.js' %}"></script>
<script type="text/javascript"> <script type="text/javascript" src="{% static 'javascript/ckeditor-config.js' %}"></script>
$(function() {
var ck_options = {
// Using a custom CSS file allows us to specifically style elements entered
// using the editor. Using the CSS prefix class .ckeditor_html prevents these
// styles from meddling with the main layout
contentsCss: "{% static 'styles/ckeditor.css' %}",
bodyClass: "ckeditor_html",
allowedContent:
'h1 h2 h3 pre blockquote b i u strike;' +
// A workaround for the problem described in http://dev.ckeditor.com/ticket/10192
// Hopefully, the problem will be solved in the final version of CKEditor 4.1
// If so, then {margin-left} can be removed
'p{margin-left};' +
'a[!href];' +
'table tr th td caption;' +
'ol ul li;' +
'span{!font-family};' +
'span{!color};',
removePlugins: "save, print, preview, pagebreak, templates, showblocks, magicline",
// Not part of the standard package of CKEditor
// http://ckeditor.com/addon/insertpre
extraPlugins: "insertpre",
toolbar_Full: [
{ name: 'document', items : [ 'Source','-','Save','DocProps','Preview','Print','-','Templates' ] },
{ name: 'clipboard', items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },
{ name: 'editing', items : [ 'Find','Replace','-','SpellChecker', 'Scayt' ] },
{ name: 'forms', items : [ 'Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField' ] },
{ name: 'basicstyles', items : [ 'Bold','Italic','Underline','Strike','Subscript','Superscript','-','RemoveFormat' ] },
{ name: 'paragraph', items : [ 'NumberedList','BulletedList','-','Pre','InsertPre','CreateDiv','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl' ] },
{ name: 'links', items : [ 'Link','Unlink','Anchor' ] },
{ name: 'styles', items : [ 'Format','FontSize' ] },
{ name: 'tools', items : [ 'Maximize', 'ShowBlocks','-','About' ] }
],
toolbar: 'Full'
};
// Override the tags 'strong' and 'em' so that reportlab can read it
CKEDITOR.config.coreStyles_bold = { element : 'b', overrides : 'strong' };
CKEDITOR.config.coreStyles_italic = { element : 'i', overrides : 'em' };
CKEDITOR.replace('id_text', ck_options);
CKEDITOR.replace('id_reason', ck_options);
});
</script>
{% endblock %} {% endblock %}
{% block title %} {% block title %}

View File

@ -0,0 +1,53 @@
/*
* Configuration file for the CKeditor
*/
$(function() {
var ck_options = {
// Using a custom CSS file allows us to specifically style elements entered
// using the editor. Using the CSS prefix class .ckeditor_html prevents these
// styles from meddling with the main layout
contentsCss: "/static/styles/ckeditor.css",
bodyClass: "ckeditor_html",
allowedContent:
'h1 h2 h3 pre blockquote b i u strike;' +
// A workaround for the problem described in http://dev.ckeditor.com/ticket/10192
// Hopefully, the problem will be solved in the final version of CKEditor 4.1
// If so, then {margin-left} can be removed
'p{margin-left};' +
'a[!href];' +
'table tr th td caption;' +
'ol ul li;' +
'span{!font-family};' +
'span{!color};',
removePlugins: "save, print, preview, pagebreak, templates, showblocks, magicline",
// Not part of the standard package of CKEditor
// http://ckeditor.com/addon/insertpre
extraPlugins: "insertpre",
toolbar_Full: [
{ name: 'document', items : [ 'Source','-','Save','DocProps','Preview','Print','-','Templates' ] },
{ name: 'clipboard', items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },
{ name: 'editing', items : [ 'Find','Replace','-','SpellChecker', 'Scayt' ] },
{ name: 'forms', items : [ 'Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField' ] },
{ name: 'basicstyles', items : [ 'Bold','Italic','Underline','Strike','Subscript','Superscript','-','RemoveFormat' ] },
{ name: 'paragraph', items : [ 'NumberedList','BulletedList','-','Pre','InsertPre','CreateDiv','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl' ] },
{ name: 'links', items : [ 'Link','Unlink','Anchor' ] },
{ name: 'styles', items : [ 'Format','FontSize' ] },
{ name: 'tools', items : [ 'Maximize', 'ShowBlocks','-','About' ] }
],
toolbar: 'Full'
};
// Override the tags 'strong' and 'em' so that reportlab can read it
CKEDITOR.config.coreStyles_bold = { element : 'b', overrides : 'strong' };
CKEDITOR.config.coreStyles_italic = { element : 'i', overrides : 'em' };
CKEDITOR.replace('id_text', ck_options);
CKEDITOR.replace('id_reason', ck_options);
});

View File

@ -83,7 +83,7 @@ class CleanHtmlFormMixin(FormMixin):
def clean(self): def clean(self):
cleaned_data = super(CleanHtmlFormMixin, self).clean() cleaned_data = super(CleanHtmlFormMixin, self).clean()
for field in self.get_clean_html_fields(): for field in self.get_clean_html_fields():
cleaned_data[field] = bleach.clean(cleaned_data[field], cleaned_data[field] = bleach.clean(cleaned_data[field],
tags=HTML_TAG_WHITELIST, tags=HTML_TAG_WHITELIST,
attributes=HTML_ATTRIBUTES_WHITELIST, attributes=HTML_ATTRIBUTES_WHITELIST,

View File

@ -71,7 +71,7 @@ stylesheet.add(ParagraphStyle(
bulletIndent=10, bulletIndent=10,
bulletFontSize=10, bulletFontSize=10,
bulletColor=colors.black, bulletColor=colors.black,
leftIndent = 30 leftIndent=30
)) ))
stylesheet.add(ParagraphStyle( stylesheet.add(ParagraphStyle(
name='InnerMonotypeParagraph', name='InnerMonotypeParagraph',

View File

@ -65,4 +65,4 @@ class CleanHtmlTest(TestCase):
self.clean_html('<pre>OK</pre>') self.clean_html('<pre>OK</pre>')
self.clean_html('<blockquote>OK</blockquote>') self.clean_html('<blockquote>OK</blockquote>')
self.clean_html('<ul><li>OK</li></ul>') self.clean_html('<ul><li>OK</li></ul>')
self.clean_html('<p style="text-decoration: underline;">OK</p>') self.clean_html('<p style="text-decoration: underline;">OK</p>')