def render(self, name, value, attrs=None, choices=()): mailing_lists = dict((ml.id, ml) for ml in MailingList.objects.all()) if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<ol>'] # Normalize to strings str_values = set([force_unicode(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = forms.CheckboxInput(final_attrs, check_test=lambda value: value in str_values) ml = mailing_lists[option_value] output.append(u'<li>%s <div class="label"><label%s>%s</label> <p>%s</p></div></li>' % ( cb.render(name, force_unicode(option_value)), label_for, conditional_escape(typogrify(ml.name)), conditional_escape(typogrify(ml.description)), )) output.append(u'</ol>') return mark_safe(u'\n'.join(output))
def _process_markup(self): """ Returns the entry with content elements processed by markup and typogrify. """ self.summary_processed = typogrify(formatter(self.summary)) self.body_processed = typogrify(formatter(self.body)) self.footnotes_processed = typogrify(formatter(self.footnotes)) return self
def save(self, force_insert=False, force_update=False): if self.summary: self.summary_html = formatter(self.summary, filter_name=self.format) if typogrify: self.summary_html = typogrify(self.summary_html) self.body_html = formatter(self.body, filter_name=self.format) if typogrify: self.body_html = typogrify(self.body_html) super(Entry, self).save(force_insert, force_update)
def render(self): try: renderer = getattr(self, 'get_%s_render' % self.render_method)() except AttributeError: raise RenderException(u"Unknown render method: '%s'" % self.render_method) return unicode(typogrify(more_fix(renderer(self.content))))
def render(self): try: renderer = getattr(self, 'get_%s_render' % self.render_method)() return unicode(typogrify(more_fix(renderer(self.content)))) except AttributeError: import traceback traceback.print_exc() raise RenderException(u"Unknown render method: '%s'" % self.render_method)
def _process_markup(self): self.html_teaser = typogrify(formatter(self.teaser)) self.html_summary = typogrify(formatter(self.summary)) self.html_body = typogrify(formatter(self.body)) self.html_pull_quote = typogrify(formatter(self.pull_quote)) self.html_lead_caption = typogrify(formatter(self.lead_caption)) self.html_sidebar_caption = typogrify(formatter(self.sidebar_caption)) self.html_inline_caption = typogrify(formatter(self.inline_caption)) return self
def pre_save(self, model_instance, add): value = getattr(model_instance, self.attname) # apply markdown html = markdown(value) # apply "typogrify" filter html = typogrify(html) setattr(model_instance, self._html_field, html) return value
def _render_markup(self, value): """ Picks the right markup filter, applies it, applies typogrify, and also processes inlines, if necessary.""" from django.contrib.markup.templatetags.markup import markdown, textile, restructuredtext import markdown from typogrify.templatetags.typogrify import typogrify from savoy.contrib.inlines.templatetags.inlines import inlines if self.markup_language: if self.markup_language == 'markdown': value = markdown.markdown(value, safe_mode=False) elif self.markup_language == 'textile': value = textile(value) elif self.markup_language == 'restructured_text': value = restructured_text(value) value = typogrify(value) if self.process_inlines: value = inlines(value) return value
def filter(content, block): return typogrify(content)
def save(self): if self.description: self.description_html = typogrify(formatter(self.description)) super(Category, self).save()
def _process_markup(self): self.html_image_caption = typogrify(formatter(self.image_caption)) return self
def typygmentdown(text, **kwargs): """ Given a string of text using Markdown syntax, applies the following transformations: 1. Searches out and temporarily removes any raw ``<code>`` elements in the text. 2. Applies Markdown and typogrify to the remaining text. 3. Applies Pygments highlighting to the contents of the removed ``<code>`` elements. 4. Re-inserts the ``<code>`` elements and returns the result. The Pygments lexer to be used for highlighting is determined by the ``class`` attribute of each ``<code>`` element found; if none is present, it will attempt to guess the correct lexer before falling back on plain text. The following keyword arguments are understood and passed to markdown if found: * ``extensions`` Markdown's ``safe_mode`` argument is *not* passed on, because it would cause the temporary ``<code>`` elements in the text to be escaped. The following keyword arguments are understood and passed to Pygments if found: * ``linenos`` The removal, separate highlighting and re-insertion of the ``<code>`` elements is necessary because Markdown and SmartyPants do not reliably avoid formatting text inside these elements; removing them before applying Markdown and typogrify means they are in no danger of having extraneous HTML or fancy punctuation inserted by mistake. Original implementation by user 'blinks' as snippet #119 at djangosnippets: http://www.djangosnippets.org/snippets/119/. This version makes the following changes: * The name of the function is now ``typygmentdown``. * The argument signature has changed to work better with the ``template_utils`` formatter. * The ``extensions`` and ``linenos`` arguments are looked for and passed to Markdown and Pygments, respectively. * The function is registered with the ``template_utils`` formatter. """ #soup = BeautifulSoup(unicode(text)) #code_blocks = soup.findAll(u'code') #for block in code_blocks: # block.replaceWith(u'<code class="removed"></code>') text2 = unicode(text) code_blocks = [] converted = [] start = 0 end = len(text2) # loop over every tag reg = re.compile(u'<code.*?code>', re.X | re.DOTALL) for m in reg.finditer(text2): # ignore comments #if not re.match(r'^!--(.*)--',tag): block = m.group(0) start_block = m.start() end_block = m.end() code_blocks.append(block) if start < start_block: converted.append(text2[start:(start_block-1)]) converted.append(u'<code class="removed"></code>') start = end_block + 1 if start < end: converted.append(text2[start:end]) htmlized = typogrify(markdown(u''.join(converted), extensions=kwargs.get('extensions', []))) ##typogrify() soup = ICantBelieveItsBeautifulSoup(htmlized) empty_code_blocks, index = soup.findAll('code', 'removed'), 0 formatter = HtmlFormatter(cssclass='typygmentdown', linenos=kwargs.get('linenos', False)) for block in code_blocks: #import pdb #pdb.set_trace() m = re.search(u"class=\"(?P<class_name>\w+)\">(?P<block_content>.*)</code>", block, re.DOTALL) block_content = u'' if m: block_group = m.groupdict() block_content = block_group['block_content'] language = block_group['class_name'] else: language = 'text' try: lexer = get_lexer_by_name(language, stripnl=True, encoding='UTF-8') except ValueError, e: try: lexer = guess_lexer(block.renderContents()) except ValueError, e: lexer = get_lexer_by_name('text', stripnl=True, encoding='UTF-8')
def _process_markup(self): self.html_description = typogrify(formatter(self.description)) self.html_commentary = typogrify(formatter(self.commentary)) return self
def _process_markup(self): self.html_text = typogrify(formatter(self.text))