正面演示一下 代码展示方法(代码片段取自 Pagedown https://github.com/ujifgc/pagedown )
怎么样,代码好看吧,
class PagedownWidget(forms.Textarea):
def __init__(self, attrs = None):
super(PagedownWidget, self).__init__(attrs)
class Media:
css = {
'all': ('pagedown/demo/browser/demo.css',)
}
js = ('%s/pagedown/Markdown.Converter.js' % settings.STATIC_URL.rstrip('/'),
'%s/pagedown/Markdown.Sanitizer.js' % settings.STATIC_URL.rstrip('/'),
'%s/pagedown/Markdown.Editor.js' % settings.STATIC_URL.rstrip('/'),)
def render(self, name, value, attrs = None):
panel = "wmd-panel"
input = "wmd-input"
if self.attrs.get('id', "") == "reply_content":
panel = "wmd-panel-reply"
input = "wmd-input-reply"
if value is None:
value = ''
if 'class' in self.attrs:
self.attrs['class'] += (" " + input)
else:
self.attrs['class'] = input
final_attrs = self.build_attrs(attrs, name = name)
html = """
<div class="wmd-wrapper">
<div class="%(panel)s">
<div id="%(id)s_wmd_button_bar"></div>
<textarea%(attrs)s>%(body)s</textarea>
</div>
<div id="%(id)s_wmd_preview" class="wmd-panel wmd-preview"></div>
</div>
<script type="text/javascript">
(function () {
var converter = Markdown.getSanitizingConverter();
selectors = {
input : "%(id)s",
button : "%(id)s_wmd_button_bar",
preview : "%(id)s_wmd_preview",
}
var editor = new Markdown.Editor(converter, "", selectors);
editor.run();
})();
</script>
""" % {
'panel': panel,
'attrs': flatatt(final_attrs),
'body': conditional_escape(force_unicode(value)),
'id': final_attrs['id'],
}
return mark_safe(html)
class AdminPagedownWidget(admin_widgets.AdminTextareaWidget, PagedownWidget):
class Media:
css = {
'all': ('admin/css/pagedown.css',)
}