def prepare(self): # Set the template and title for the page content, if they are not set (but don't save them) self.title = self.title or self.page.title self.template = self.template or self.page.template self.slug = self.slug or self.page.slug if not self.description: self.description = '' if not self.keywords: self.keywords = '' if not self.page_topic: self.page_topic = '' # Convert the content to HTML if self.content_type == 'html': pass # Nothing to do elif self.content_type == 'markdown': self.content = markdown(self.content) elif self.content_type == 'textile': self.content = textile(self.content) elif self.content_type == 'rst': self.content = rst(self.content) else: self.content = mark_safe(linebreaks(escape(self.content))) return self
def test_add_comment1(self): ''' Tests adding a comment in the agora ''' # get activity - its empty data = self.getAndParse('action/agora/1/') agoras = data['objects'] self.assertEqual(len(agoras), 0) # add a comment as anonymous - fails, forbidden orig_data = dict(comment='blah blah blah blah.') data = self.post('agora/1/add_comment/', orig_data, code=HTTP_FORBIDDEN, content_type='application/json') # still no activity data = self.getAndParse('action/agora/1/') agoras = data['objects'] self.assertEqual(len(agoras), 0) # add a comment as a logged in user that is a member of the agora self.login('david', 'david') data = self.postAndParse('agora/1/add_comment/', orig_data, code=HTTP_OK, content_type='application/json') # now the comment is there data = self.getAndParse('action/agora/1/') objects = data['objects'] self.assertEqual(len(objects), 1) self.assertEqual(objects[0]['actor']['content_type'], 'user') self.assertEqual(objects[0]['actor']['username'], 'david') self.assertEqual(objects[0]['action_object']['content_type'], 'comment') self.assertEqual(objects[0]['action_object']['comment'], textile(orig_data['comment']))
def readme(repository): """ Return a rendered version of the readme for the given repository """ if not repository.readme or not repository.readme.strip(): return 'No readme :(' readme = None try: if repository.readme_type == 'markdown': readme = markup.markdown(repository.readme) elif repository.readme_type == 'textile': readme = markup.textile(repository.readme) elif repository.readme_type == 'rest': readme = markup.restructuredtext(repository.readme) except: pass if not readme: readme = '<pre>%s</pre>' % urlize(repository.readme) try: result = mark_safe(clean_html(readme)) except: result = 'Unreadble readme :(' return result
def _output(self): """It is this method that is responsible for rendering the object in HTML. """ if self.text is None or self.text == "": return u"" if self.text_format == '\E': return linebreaks(urlize(escape(self.text))) elif self.text_format == '\T': try: return textile(self.text) except: return mark_safe(_('There is an error in the Textile.')) elif self.text_format == '\M': try: return markdown(self.text, MARKDOWN_EXTENSIONS) except: return mark_safe(_('There is an error in the Markdown.')) elif self.text_format == '\R': try: return restructuredtext(self.text) except: return mark_safe( _('There is an error in the reStructuredText.')) elif self.text_format == '\H' or self.text_format == '\W': return mark_safe(self.text) else: return mark_safe(escape(self.text))
def markup(value, mtype=1): try: from django.utils.html import escape if mtype == MARKDOWN[0]: try: import markdown2 except ImportError: try: from django.contrib.markup.templatetags.markup import markdown except ImportError: return sanitize_html(force_unicode(value)) return mark_safe(sanitize_html(markdown(force_unicode(value)))) else: safe_mode = False return mark_safe(sanitize_html(markdown2.markdown(force_unicode(value), safe_mode=safe_mode))) elif mtype == TEXTILE[0]: from django.contrib.markup.templatetags.markup import textile return textile(force_unicode(value)) ## elif mtype == REST[0]: ## from django.contrib.markup.templatetags.markup import restructuredtext ## return restructuredtext(value) elif mtype == HTML[0]: return mark_safe(sanitize_html(force_unicode(value))) elif mtype == PLAINTEXT[0]: return escape(force_unicode(value)) else: return markup(value, DEFAULT_MARKUP[0]) except ImportError: # Not marking safe, in case tag fails and users input malicious code. return force_unicode(value)
def test_list_comments(self): ''' Tests adding a comment in the agora and listing it ''' # list comments - its empty data = self.getAndParse('agora/1/comments/') comments = data['objects'] self.assertEqual(len(comments), 0) # add a comment as a logged in user that is a member of the agora self.login('david', 'david') orig_data = dict(comment='blah blah blah blah.') data = self.postAndParse('agora/1/add_comment/', orig_data, code=HTTP_OK, content_type='application/json') # now the comment is there data = self.getAndParse('agora/1/comments/') objects = data['objects'] self.assertEqual(len(objects), 1) self.assertEqual(objects[0]['actor']['content_type'], 'user') self.assertEqual(objects[0]['actor']['username'], 'david') self.assertEqual(objects[0]['action_object']['content_type'], 'comment') self.assertEqual(objects[0]['action_object']['comment'], textile(orig_data['comment']))
def auto_transform_markup(comment): """ Given a comment (``ThreadedComment`` or ``FreeThreadedComment``), this tag looks up the markup type of the comment and formats the output accordingly. It can also output the formatted content to a context variable, if a context name is specified. """ try: from django.utils.html import escape from threadedcomments.models import MARKDOWN, TEXTILE, REST, PLAINTEXT if comment.markup == MARKDOWN: from django.contrib.markup.templatetags.markup import markdown return markdown(comment.comment) elif comment.markup == TEXTILE: from django.contrib.markup.templatetags.markup import textile return textile(comment.comment) elif comment.markup == REST: from django.contrib.markup.templatetags.markup import restructuredtext return restructuredtext(comment.comment) # elif comment.markup == HTML: # return mark_safe(force_unicode(comment.comment)) elif comment.markup == PLAINTEXT: return escape(comment.comment) except ImportError: # Not marking safe, in case tag fails and users input malicious code. return force_unicode(comment.comment)
def prepare(self): # Set the template and title for the page content, if they are not set (but don't save them) self.title = self.title or self.page.title self.template = self.template or self.page.template self.slug = self.slug or self.page.slug if not self.description: self.description = '' if not self.keywords: self.keywords = '' if not self.page_topic: self.page_topic = '' # Convert the content to HTML if self.content_type == 'html': pass # Nothing to do elif self.content_type == 'markdown': self.content = markdown(self.content) elif self.content_type == 'textile': self.content = textile(self.content) elif self.content_type == 'rst': self.content = rst(self.content) else: self.content = mark_safe(linebreaks(escape(self.content))) self.toc = mark_safe(self.toc) return self
def _output(self): """It is this method that is responsible for rendering the object in HTML. """ if self.text is None or self.text == "": return u"" if self.text_format == '\E': return linebreaks(urlize(escape(self.text))) elif self.text_format == '\T': try: return textile(self.text) except: return mark_safe(_('There is an error in the Textile.')) elif self.text_format == '\M': try: return markdown(self.text, MARKDOWN_EXTENSIONS) except: return mark_safe(_('There is an error in the Markdown.')) elif self.text_format == '\R': try: return restructuredtext(self.text) except: return mark_safe(_('There is an error in the reStructuredText.')) elif self.text_format == '\H' or self.text_format == '\W': return mark_safe(self.text) else: return mark_safe(escape(self.text))
def get_html(self): from django.contrib.markup.templatetags import markup key = self._cache_key() val = cache.get(key) if val is None: val = markup.textile(self.content) cache.set(key, val, 3600) return val
def doc_content(value): """docstring for markup""" content = value.text m = value.get('markup', None) if m == 'textile': return textile(content) else: return mark_safe(content)
def view_page(request, page_name,project_id): project = get_object_or_404(Project,pk=project_id) try: page = project.wiki.get(title=page_name) text = textile(page.text) except WikiPage.DoesNotExist: pass return direct_to_template(request, 'wiki/main.html', locals())
def save(self, *args, **kwargs): "On saving fragment, save textile formatted html into html property if use_textile is True" if self.use_textile: self.html = textile(self.content) else: self.html = self.content super(Fragment, self).save(*args, **kwargs)
def html_content(self): """Return the content correctly formatted""" if MARKUP_LANGUAGE == 'markdown': return markdown(self.content, MARKDOWN_EXTENSIONS) elif MARKUP_LANGUAGE == 'textile': return textile(self.content) elif MARKUP_LANGUAGE == 'restructuredtext': return restructuredtext(self.content) return self.content
def test_add_comment2(self): ''' Tests adding a comment in the agora ''' # no activity data = self.getAndParse('action/agora/1/') objects = data['objects'] self.assertEqual(len(objects), 0) # set comment policy to only members self.login('david', 'david') orig_data = {'pretty_name': "updated name", 'short_description': "new desc", 'is_vote_secret': False, 'biography': "bio", 'membership_policy': 'ANYONE_CAN_JOIN', 'comments_policy': 'ONLY_MEMBERS_CAN_COMMENT'} data = self.put('agora/1/', data=orig_data, code=HTTP_ACCEPTED, content_type='application/json') # add a comment as a non member - fails self.login('user1', '123') orig_data = dict(comment='blah blah blah blah.') data = self.post('agora/1/add_comment/', orig_data, code=HTTP_FORBIDDEN, content_type='application/json') # still no activity data = self.getAndParse('action/agora/1/') objects = data['objects'] self.assertEqual(len(objects), 0) # user1 joins the agora orig_data = dict(action="join") data = self.post('agora/1/action/', data=orig_data, code=HTTP_OK, content_type='application/json') # this generates "joined" and "started following" actions data = self.getAndParse('action/agora/1/') objects = data['objects'] self.assertEqual(len(objects), 2) # add a comment as a member - succeeds orig_data = dict(comment='blah blah blah blah 2 yeahh pirata.') data = self.post('agora/1/add_comment/', orig_data, code=HTTP_OK, content_type='application/json') # now the comment is there data = self.getAndParse('action/agora/1/') objects = data['objects'] self.assertEqual(len(objects), 3) self.assertEqual(objects[0]['actor']['content_type'], 'user') self.assertEqual(objects[0]['actor']['username'], 'user1') self.assertEqual(objects[0]['action_object']['content_type'], 'comment') self.assertEqual(objects[0]['action_object']['comment'].strip(), textile(orig_data['comment']).strip())
def test_add_comment2(self): ''' Tests adding a comment in the agora ''' # no activity data = self.getAndParse('action/agora/1/') objects = data['objects'] self.assertEqual(len(objects), 0) # set comment policy to only members self.login('david', 'david') orig_data = {'pretty_name': "updated name", 'short_description': "new desc", 'is_vote_secret': False, 'biography': "bio", 'membership_policy': 'ANYONE_CAN_JOIN', 'comments_policy': 'ONLY_MEMBERS_CAN_COMMENT'} data = self.put('agora/1/', data=orig_data, code=HTTP_ACCEPTED, content_type='application/json') # add a comment as a non member - fails self.login('user1', '123') orig_data = dict(comment='blah blah blah blah.') data = self.post('agora/1/add_comment/', orig_data, code=HTTP_FORBIDDEN, content_type='application/json') # still no activity data = self.getAndParse('action/agora/1/') objects = data['objects'] self.assertEqual(len(objects), 0) # user1 joins the agora orig_data = dict(action="join") data = self.post('agora/1/action/', data=orig_data, code=HTTP_OK, content_type='application/json') # this generates "joined" and "started following" actions data = self.getAndParse('action/agora/1/') objects = data['objects'] self.assertEqual(len(objects), 2) # add a comment as a member - succeeds orig_data = dict(comment='blah blah blah blah 2 yeahh pirata.') data = self.post('agora/1/add_comment/', orig_data, code=HTTP_OK, content_type='application/json') # now the comment is there data = self.getAndParse('action/agora/1/') objects = data['objects'] self.assertEqual(len(objects), 3) self.assertEqual(objects[0]['actor']['content_type'], 'user') self.assertEqual(objects[0]['actor']['username'], 'user1') self.assertEqual(objects[0]['action_object']['content_type'], 'comment') self.assertEqual(objects[0]['action_object']['comment'], textile(orig_data['comment']))
def html_content(self): """Return the content correctly formatted""" if MARKUP_LANGUAGE == 'markdown': return markdown(self.content, MARKDOWN_EXTENSIONS) elif MARKUP_LANGUAGE == 'textile': return textile(self.content) elif MARKUP_LANGUAGE == 'restructuredtext': return restructuredtext(self.content) # elif not '</p>' in self.content: # return linebreaks(self.content) return self.content
def html_content(self): """Return the Entry.content attribute formatted in HTML""" if MARKUP_LANGUAGE == 'markdown': return markdown(self.content, MARKDOWN_EXTENSIONS) elif MARKUP_LANGUAGE == 'textile': return textile(self.content) elif MARKUP_LANGUAGE == 'restructuredtext': return restructuredtext(self.content) elif not '</p>' in self.content: return linebreaks(self.content) return self.content
def process_markup(text, markup): if markup == 'textile': return textile(text) elif markup == 'html': return mark_safe(text) elif markup == 'markdown': return markdown(text) elif markup == 'rst': return restructuredtext(text) else: return mark_safe('<pre>' + text + '</pre>')
def html_content(self): """Return the content correctly formatted""" if MARKUP_LANGUAGE == "markdown": return markdown(self.content, MARKDOWN_EXTENSIONS) elif MARKUP_LANGUAGE == "textile": return textile(self.content) elif MARKUP_LANGUAGE == "restructuredtext": return restructuredtext(self.content) elif not "</p>" in self.content: return linebreaks(self.content) return self.content
def essay(req, title): if req.method == 'GET': essay = get_object_or_404(models.Essay, slug=title) elif req.method == 'POST': from django.contrib.markup.templatetags import markup if not req.user.is_staff: raise PermissionDenied() essay = dict([ (k,req.POST.get(k, '')) for k in [ f.name for f in models.Essay._meta.fields]]) essay['html'] = markup.textile(essay['content']) return render_to_response('mapstory/essay.html', RequestContext(req, { 'essay' : essay }))
def markup(text, format): if format == 'html': return mark_safe(text) elif format == 'rest': return markup_filters.restructuredtext(text) elif format == 'mdown': return markup_filters.markdown(text) elif format == 'txtile': return markup_filters.textile(text) elif settings.DEBUG: raise Exception, "Invalid 'markup' format: %s" % format return ''
def essay(req, title): if req.method == 'GET': essay = get_object_or_404(models.Essay, slug=title) elif req.method == 'POST': from django.contrib.markup.templatetags import markup if not req.user.is_staff: raise PermissionDenied() essay = dict([(k, req.POST.get(k, '')) for k in [f.name for f in models.Essay._meta.fields]]) essay['html'] = markup.textile(essay['content']) return render_to_response('mapstory/essay.html', RequestContext(req, {'essay': essay}))
def markup(txt, markupname=0): if markupname is Page.TEXTILE: from django.contrib.markup.templatetags.markup import textile return textile(txt) elif markupname is Page.MARKDOWN: from django.contrib.markup.templatetags.markup import markdown return markdown(txt) elif markupname is Page.RESTRUCTUREDTEXT: from django.contrib.markup.templatetags.markup import restructuredtext return restructuredtext(txt) else: return txt
def get_markuped_body(self): if not self.syntax: return self.body syntax_name = self.syntax.name if syntax_name == "textile": return textile(self.body) elif syntax_name == "markdown": return markdown(self.body) elif syntax_name == "restructuredtext": return restructuredtext(self.body) else: return self.body
def render(self, context): if self.slug[0] in ('\'', '"') and self.slug[0] == self.slug[-1]: blurb_slug = self.slug[1:-1] else: try: blurb_slug = template.Variable(self.slug).resolve(context) except template.VariableDoesNotExist: return u'' try: blurb = Blurb.objects.get(is_active=True, slug=blurb_slug) except Blurb.DoesNotExist: return u'' return markup.textile(blurb.content)
def textile(text, **kwargs): """ Applies Textile conversion to a string, and returns the HTML. This is simply a pass-through to the ``textile`` template filter included in ``django.contrib.markup``, which works around issues PyTextile has with Unicode strings. If you're not using Django but want to use Textile with ``MarkupFormatter``, you'll need to supply your own Textile filter. """ from django.contrib.markup.templatetags.markup import textile return textile(text)
def html_content(self): """ Returns the "content" field formatted in HTML. """ MARKUP_LANGUAGE = self.get_content_markup_display() if MARKUP_LANGUAGE == 'markdown': return markdown(self.content, MARKDOWN_EXTENSIONS) elif MARKUP_LANGUAGE == 'textile': return textile(self.content) elif MARKUP_LANGUAGE == 'restructuredtext': return restructuredtext(self.content) elif not '</p>' in self.content: return linebreaks(self.content) return self.content
def do_render_markup(self): """Turns any markup into HTML""" original = self.rendered_content if self.markup == MARKUP_MARKDOWN: self.rendered_content = markup.markdown(self.content) elif self.markup == MARKUP_REST: self.rendered_content = markup.restructuredtext(self.content) elif self.markup == MARKUP_TEXTILE: self.rendered_content = markup.textile(self.content) else: self.rendered_content = self.content return (self.rendered_content != original)
def save(self): self.html = textile(self.content) super(Article, self).save() if not DEBUG: try: ping_google('/sitemap.xml') except Exception: pass try: technorati = xmlrpclib.Server(TECHNORATI_PING_SERVER) reply = technorati.weblogUpdates.ping(TECHNORATI_SITE_NAME,TECHNORATI_URL) except Exception: pass
def do_render_markup(self): """Turns any markup into HTML""" original = self._rendered if self.markup == MARKUP_MARKDOWN: rendered = markup.markdown(self._source) elif self.markup == MARKUP_REST: rendered = markup.restructuredtext(self._source) elif self.markup == MARKUP_TEXTILE: rendered = markup.textile(self._source) else: rendered = self._source setattr(self, self._markup.rendered_field, rendered) return (rendered != original)
def user_filter(string, args=None): # Apply oEmbed oembed_kwargs = {} oembed_kwargs["max_width"] = 320 oembed_kwargs["max_height"] = 240 string = oembed_replace(string, **oembed_kwargs) # Apply Urlize string = urlize(string, nofollow=True, autoescape=True) # Apply Textile string = textile(string) return string
def markuping(markup, value): """ Transform plain text markup syntaxes to HTML with filters in django.contrib.markup.templatetags. """ from django.contrib.markup.templatetags.markup \ import textile, markdown, restructuredtext if markup == 'markdown': return markdown(value) elif markup == 'rest': return restructuredtext(value) elif markup == 'textile': return textile(value) else: return value # raw
def save(self, *args, **kwargs): """ Renders the article using the appropriate markup language. """ if self.markup == MARKUP_MARKDOWN: self.rendered_content = markup.markdown(self.content) elif self.markup == MARKUP_REST: self.rendered_content = markup.restructuredtext(self.content) elif self.markup == MARKUP_TEXTILE: self.rendered_content = markup.textile(self.content) else: self.rendered_content = self.content # if the author wishes to have an "AddThis" button on this article, # make sure we have a username to go along with it. if self.use_addthis_button and self.addthis_use_author and not self.addthis_username: self.addthis_username = self.author.username # make sure the slug is always unique for the year this article was posted if not self.id: # make sure we have a slug first if not len(self.slug.strip()): self.slug = slugify(self.title) self.slug = self.get_unique_slug(self.slug) super(Article, self).save(*args, **kwargs) requires_save = False # if we don't have keywords, use the tags if len(self.keywords.strip()) == 0: self.keywords = ', '.join([t.name for t in self.tags.all()]) requires_save = True # if we don't have a description, use the teaser if len(self.description.strip()) == 0: self.description = self.teaser requires_save = True # we have to have an object before we can create relationships like this if not len(self.sites.all()): self.sites = [Site.objects.get_current()] requires_save = True if requires_save: super(Article, self).save(*args, **kwargs)
def readme(repository): """ Return a rendered version of the readme for the given repository """ if not repository.readme or not repository.readme.strip(): return 'No readme :(' if repository.readme_type == 'markdown': readme = markup.markdown(repository.readme) elif repository.readme_type == 'textile': readme = markup.textile(repository.readme) elif repository.readme_type == 'rest': readme = markup.restructuredtext(repository.readme) else: readme = '<pre>%s</pre>' % urlize(repository.readme) return mark_safe(clean_html(readme))
def to_html(subject, markup_style): """Convert ``subject`` to html from markup defined in ``markup_style``. This relies on the Django markup filters to render html. This isn't really ideal, but good enough for now. """ if markup_style == TEXTILE: return markup.textile(subject) elif markup_style == MARKDOWN: return markup.markdown(subject) elif markup_style == RESTRUCTUREDTEXT: return markup.restructuredtext(subject) elif markup_style == PLAIN_TEXT: from django.utils.html import linebreaks return linebreaks(subject, False) else: return subject
def markuping(markup, value): """ Transform plain text markup syntaxes to HTML with filters in django.contrib.markup.templatetags. *Required arguments:* * ``markup``: 'markdown', 'rest' or 'texttile'. For any other string value is returned without modifications. * ``value``: plain text input """ if markup == 'markdown': from markdown import markdown return markdown(value) elif markup == 'rest': from django.contrib.markup.templatetags.markup import restructuredtext return restructuredtext(value) elif markup == 'textile': from django.contrib.markup.templatetags.markup import textile return textile(value) else: return value # raw
def dehydrate_comment(self, bundle): return textile(bundle.obj.comment)