def render_markup(self): if settings.BLOG_MARKUP_DEFAULT == 'wysiwyg': self.markup = "none" self.body_html = self.body self.excerpt_html = self.excerpt else: self.body_html = mark_safe(formatter(self.body, filter_name=self.markup)) self.excerpt_html = mark_safe(formatter(self.excerpt, filter_name=self.markup))
def save(self, *args, **kwargs): self.abstract_markup = mark_safe( formatter(self.abstract, filter_name=self.markup)) self.body_markup = mark_safe( formatter(self.body, filter_name=self.markup)) if self.markup == "restructuredtext": self.abstract_markup = unescape_inline(self.abstract_markup) self.body_markup = unescape_inline(self.body_markup) super(Story, self).save(*args, **kwargs)
def save(self, *args, **kwargs): # Inlines must be rendered before markup in order to properly preserve # whitespace self.body_rendered = inlines(self.body) self.tease_rendered = inlines(self.tease) # Render the markup and save it in the body_rendered field. self.body_rendered = mark_safe(formatter(self.body_rendered, filter_name=self.markup)) self.tease_rendered = mark_safe(formatter(self.tease_rendered, filter_name=self.markup)) # Call the real save. super(Post, self).save(*args, **kwargs)
def save(self, *args, **kwargs): self.abstract_markup = mark_safe( formatter(self.abstract, filter_name=self.markup)) self.body_markup = mark_safe( formatter(self.body, filter_name=self.markup)) if self.markup == "restructuredtext": self.abstract_markup = unescape_inline(self.abstract_markup) self.body_markup = unescape_inline(self.body_markup) super(Story, self).save(*args, **kwargs) if self.status == PUBLIC: try: ping_google() except: pass
def save_information(request): """Funcion para manejar los envios de wiki de cada uno sd elos videos y paginas y colleciones""" try: type_d = request.REQUEST['type'] except KeyError: type_d = None try: search = request.REQUEST['data_information'] except KeyError: pass else: if type_d == "collection": page_d = Collection.objects.get(pk=request.REQUEST['id']) page_d.info = search page_d.save() elif type_d == "page": page_d = Page.objects.get(pk=request.REQUEST['id']) page_d.info = search page_d.save() return HttpResponse(formatter(search, filter_name='creole'), "text/plain")
def render_markup_comment(value): """ Renders a comment using a markup language specified in the first line of the comment. Template Syntax:: {{ comment.comment|render_markup_comment }} The first line of the comment field must start with the name of the markup language. A comment like:: comment = r'''#!markdown\n\rAn [example](http://url.com/ "Title")''' Would be rendered as a markdown text, producing the output:: <p><a href="http://url.com/" title="Title">example</a></p> """ lines = value.splitlines() rawstr = r"""^#!(?P<markup_filter>\w+)$""" match_obj = re.search(rawstr, lines[0]) if match_obj: markup_filter = match_obj.group('markup_filter') try: return mark_safe(formatter("\n".join(lines[1:]), filter_name=markup_filter)) except ValueError, e: output = "<p>Warning: %s</p>" % e return output + value
def html( self, ID=None ): # If an ID is provided, log it to record what resource had the warnings if self.initial is None and self.added is None: return (None) initial_html = self.initial and self.initial[:1] == '<' if initial_html: formatin = '%%INITIAL%%{0}'.format(self.added) else: formatin = '{0}{1}'.format(self.initial or '', self.added) formatout = formatter(formatin, filter_name='restructuredtext', settings_overrides=self.markup_settings) warnings = self.markup_stream.getvalue() if warnings: logger = logging.getLogger('DaemonLog') if ID: logger.warning('markup warnings for ID: {}'.format(ID)) for line in warnings.splitlines(): logger.warning('markup: {}'.format(line)) if initial_html: output = formatout.replace('%%INITIAL%%', self.initial, 1) else: output = formatout return (output)
def save(self): if self.front_title.strip() == "": self.front_title = self.title if self.published_date is None: self.published_date = datetime.datetime.now() self.body_html = formatter(self.body, filter_name=self.markup) super(Project, self).save()
def get_teaser(self): if self.description: teaser = formatter(self.description, filter_name=self.markup) else: teaser = self.rendered_content return truncate_html_words(teaser, 50)
def do_render_markup(self): """Turns any markup into HTML""" original = self.rendered_content if self.markup == MARKUP_MARKDOWN: self.rendered_content = \ markup.formatter(self.content, filter_name='markdown') elif self.markup == MARKUP_REST: self.rendered_content = \ markup.formatter(self.content, filter_name='restructuredtext') elif self.markup == MARKUP_TEXTILE: self.rendered_content = \ markup.formatter(self.content, filter_name='textile') else: self.rendered_content = self.content return (self.rendered_content != original)
def test_register_filter(self): """ Register the filter, and its wildly available. """ formatter.register('uppercase', UppercaseMarkupFilter) # It's ready to be called result = formatter('This is some text', filter_name='uppercase') self.assertEqual(result, 'THIS IS SOME TEXT')
def save(self, *args, **kwargs): # Inlines must be rendered before markup in order to properly preserve # whitespace self.body_rendered = inlines(self.body) self.tease_rendered = inlines(self.tease) # Render the markup and save it in the body_rendered field. self.body_rendered = mark_safe(formatter(self.body_rendered, filter_name=self.markup)) self.tease_rendered = mark_safe(formatter(self.tease_rendered, filter_name=self.markup)) # Run the body and tease through Smartypants, if enabled. if settings.BLOG_SMARTYPANTS: self.body_rendered = mark_safe(formatter(self.body_rendered, filter_name='smartypants')) self.tease_rendered = mark_safe(formatter(self.tease_rendered, filter_name='smartypants')) # Call the real save. super(Post, self).save(*args, **kwargs)
def to_representation(self, instance): data = super(PostSerializer, self).to_representation(instance) data.update({ 'content': formatter(instance.content, filter_name='linebreaks'), 'created_by': instance.created_by.username, }) return data
def save(self, *args, **kwargs): self.site_id = settings.SITE_ID self.abstract_markup = mark_safe( formatter(self.abstract, filter_name=self.markup)) self.body_markup = mark_safe( formatter(self.body, filter_name=self.markup)) if self.markup == "restructuredtext": self.abstract_markup = unescape_inline(self.abstract_markup) self.body_markup = unescape_inline(self.body_markup) super(Story, self).save(*args, **kwargs) if self.status == 3: # public blog_config = Config.get_current() ping_google = getattr(blog_config, "ping_google", False) if ping_google: try: ping_google() except: pass
def test_update_filter(self): """ You can update an existing filter, but keep the name. """ formatter.update('uppercase', LowercaseMarkupFilter) # Despite its key name is still 'uppercase' we actually call the # LowercaseFilter. result = formatter('This Is Some Text', filter_name='uppercase') self.assertEqual(result, 'this is some text')
def get_teaser(self, truncate=30): if self.description: teaser = formatter(self.description, filter_name=self.markup) else: teaser = self.rendered_content if truncate: return truncate_html_words(teaser, truncate) else: return teaser
def body_rendered(self): cache_key = "blog.post.pk:{0}.body.updated:{1}".format( self.pk, self.updated_at.isoformat() ) return cache.get_or_set_callback( cache_key, lambda: formatter(self.body, self.markup), timeout=5 * 3600, )
def tease_rendered(self): cache_key = "blog.post.pk:{0}.tease.updated:{1}".format( self.pk, self.updated_at.isoformat() ) return cache.get_or_set_callback( cache_key, lambda: formatter((self.tease or truncatewords_html(self.body, 150)), self.markup), timeout=5 * 3600, )
def get_context_data(self, **kwargs): context = super(Markdown, self).get_context_data(**kwargs) try: web_content = WebContent.objects.get(slug=kwargs['slug']) except WebContent.DoesNotExist: raise Http404("WebContent does not exist") context['markdown_content'] = formatter(web_content.markdown_content, filter_name='markdown', safe_mode=False) return context
def retrieve(self, request, pk=None): #pylint: disable=unused-argument queryset = self.queryset obj = get_object_or_404(queryset, pk=pk) serializer = self.get_serializer_class() data = dict() data.update(serializer(obj).data) if data['file_type'] == 1: data.update({ 'markdown': formatter(data['content'], filter_name='markdown') }) return Response(data)
def test_fallback_filter(self): """ You can call the formatter without a `filter_name` as long as a `MARKUP_FILTER_FALLBACK` setting is set. """ self.assertRaises(ValueError, formatter, 'This is some text', filter_name=None) formatter.register('uppercase', UppercaseMarkupFilter) with self.settings(MARKUP_FILTER_FALLBACK='uppercase'): result = formatter('This is some text', filter_name=None) self.assertEqual(result, 'THIS IS SOME TEXT')
def collect(self, method_type, name, method, topic, options): prop = getattr(self, method_type) prop.append({ 'name': name, 'method': method, 'topic': topic, 'options': options, 'docstring': method.__doc__, 'docstring_html': mark_safe( method.__doc__ and formatter(method.__doc__, filter_name='restructuredtext' or None)), 'signature': inspect.signature(method) })
def save(self, *args, **kwargs): body_markup = mark_safe(formatter(self.body, filter_name=self.markup)) self.body_markup = body_markup super(Post, self).save(*args, **kwargs) blog_settings = Settings.get_current() if blog_settings is None: return elif blog_settings.ping_google: try: ping_google() except: pass
def test_fallback_filter(self): """ You can call the formatter without a `filter_name` as long as a `MARKUP_FILTER_FALLBACK` setting is set. """ self.assertRaises( ValueError, formatter, 'This is some text', filter_name=None ) formatter.register('uppercase', UppercaseMarkupFilter) with self.settings(MARKUP_FILTER_FALLBACK='uppercase'): result = formatter('This is some text', filter_name=None) self.assertEqual(result, 'THIS IS SOME TEXT')
def html( self, ID=None ): # If an ID is provided, log it to record what resource had the warnings if self.value is None: return (None) output = formatter(self.value, filter_name='restructuredtext', settings_overrides=self.markup_settings) warnings = self.markup_stream.getvalue() if warnings: logger = logging.getLogger('DaemonLog') if ID: logger.warning('markup warnings for ID: {}'.format(ID)) for line in warnings.splitlines(): logger.warning('markup: {}'.format(line)) return (output)
def save(self, *args, **kwargs): blog_settings = Settings.get_current() if blog_settings.active_editor > 1: self.markup = "none" self.body_markup = self.body else: self.body_markup = mark_safe(formatter(self.body, filter_name=self.markup)) super(Post, self).save(*args, **kwargs) ping_google = getattr(blog_settings,"ping_google", False) if ping_google: try: ping_google() except: pass
def post(self, request, slug_feed, slug_post=None): feed = Feed.objects.get(slug=slug_feed) post = Post.objects.get(slug=slug_post, feed=feed) if request.POST.get('comment-input'): content = request.POST.get('comment-input') content = formatter(content, filter_name='markdown') Comment.objects.create(user=request.user, content=content, post=post) return render( request, 'dashboard/post.html', { 'post': post, 'state': post.userpost_set.get(user=request.user).state, 'root_comment_ids': get_comment_root_ids(post) })
def save(self): """ Apply a markup format to body and store the output in body_html. Customize the markup filter settings in your settings.py NADB_MARKUP_FILTER = 'codehilite' MARKUP_SETTINGS = { 'markdown': { 'safe_mode': True, 'extensions': ['codehilite'] } } """ filter_name = getattr(settings, 'NADB_MARKUP_FILTER', 'markdown') self.body_html = formatter(self.body, filter_name=filter_name) super(Post, self).save()
def preview_post(request, course_short_title): try: content = request.POST.get('content', '') if len(content) > 0: return JsonResponse({ 'success': True, 'content': formatter(content, filter_name='markdown_giffer'), }) else: return JsonResponse({ 'error': True, 'message': 'No content provided!' }) except ValueError: return JsonResponse({ 'error': True, 'message': 'Invalid post ID provided!' })
def render_column(self, row, column): # we want to render 'translation' as a custom column, because 'translation' is defined as a Textfield in Image model, # but here we only want to check the status of translating process. # so, if 'translation' is empty, i.e. no one enters any information in 'translation', we display 'waiting'; # otherwise, we display 'processing'. if column == 'text': return format_html("<p>{}</p>".format(formatter(row.text, filter_name='markdown'))) if column == 'date': return format_html("<p>{}</p>".format(row.date,)) if column == 'firstname': return format_html("<p>{}</p>".format(row.firstname,)) if column == 'lastname': return format_html("<p>{}</p>".format(row.lastname,)) if column == 'thirdname': return format_html("<p>{}</p>".format(row.thirdname,)) if column == 'nickname': return format_html("<p>{}</p>".format(row.nickname,)) if column == 'gender': return format_html("<p>{}</p>".format(row.gender,)) else: return super(DiariesJson, self).render_column(row, column)
def post(self, request, comment_id, action="reply"): comment = Comment.objects.get(id=comment_id) if request.POST.get('comment-input'): content = request.POST.get('comment-input') content = formatter(content, filter_name='markdown') if action == "edit" and content: comment.content = content comment.save() elif action == "reply" and request.POST.get('post-id') and content: post_id = request.POST.get('post-id') Comment.objects.create(parent_id=comment_id, user=request.user, content=content, post_id=post_id) return render(request, 'dashboard/comment.html', { 'comment': comment, 'action': action })
def save(self, *args, **kwargs): body_markup = mark_safe(formatter(self.body, filter_name=self.markup)) self.body_markup = body_markup super(Post, self).save(*args, **kwargs)
def apply_markup(text, filter_name): return mark_safe(formatter(text, filter_name))
def item_description(self, item): return strip_tags(formatter(item.content, filter_name='markdown'))
def rendered_content(self): return formatter(self.content, filter_name=self.markup)
def get_resources_docstrings(resources): resources_docstrings = {} for key, value in resources.items(): resources_docstrings[key] = formatter(value.__doc__.replace('<','<').replace('>','>'), filter_name='linebreaks') if value.__doc__ else "No docstring" return resources_docstrings
def save(self): if self.published_date is None: self.published_date = datetime.datetime.now() self.body_html = formatter(self.body, filter_name=self.markup) super(Post, self).save()
def get_message_as_markdown(self): return mark_safe(formatter(self.message, filter_name='markdown'))
def process(self, content): return mark_safe(formatter(content, filter_name=self.markup))
'restructuredtext': 'reStructuredText', 'markdown': 'Markdown', 'textile': 'Textile', } if appsettings.FLUENT_MARKUP_USE_DJANGO_MARKUP: # With django-markup installed, it can be used instead of out default filters. # Since most django-markup filters are meant for simple text enhancements, # only use the filters which are really full text markup languages. # NOTE: the enhanced markdown above will also be replaced. Use the MARKUP_SETTINGS setting to configure django-markup instead. from django_markup.markup import formatter for filter_name, FilterClass in formatter.filter_list.items(): real_filters = list(SUPPORTED_LANGUAGES.keys()) + ['creole'] if filter_name in real_filters: _languageNames[filter_name] = FilterClass.title SUPPORTED_LANGUAGES[filter_name] = lambda text: mark_safe(formatter(text, filter_name)) # Format as choices LANGUAGE_CHOICES = [(n, _languageNames.get(n, n.capitalize())) for n in list(SUPPORTED_LANGUAGES.keys())] LANGUAGE_CHOICES = sorted(LANGUAGE_CHOICES) LANGUAGE_NAMES = dict(LANGUAGE_CHOICES) def render_text(text, language=None): """ Render the text, reuses the template filters provided by Django. """ # Get the filter text_filter = SUPPORTED_LANGUAGES.get(language, None) if not text_filter: raise ImproperlyConfigured("markup filter does not exist: {0}. Valid options are: {1}".format(
def save(self, *args, **kwargs): self.abstract_markup = mark_safe( formatter(self.abstract, filter_name=self.markup)) self.body_markup = mark_safe( formatter(self.body, filter_name=self.markup)) super(Story, self).save(*args, **kwargs)
def save(self, *args, **kwargs): self.body_markup = mark_safe(formatter(self.body, filter_name=self.markup)) super(Quote, self).save(*args, **kwargs)
'markdown': 'Markdown', 'textile': 'Textile', } if appsettings.FLUENT_MARKUP_USE_DJANGO_MARKUP: # With django-markup installed, it can be used instead of out default filters. # Since most django-markup filters are meant for simple text enhancements, # only use the filters which are really full text markup languages. # NOTE: the enhanced markdown above will also be replaced. Use the MARKUP_SETTINGS setting to configure django-markup instead. from django_markup.markup import formatter for filter_name, FilterClass in formatter.filter_list.items(): real_filters = list(SUPPORTED_LANGUAGES.keys()) + ['creole'] if filter_name in real_filters: _languageNames[filter_name] = FilterClass.title SUPPORTED_LANGUAGES[filter_name] = lambda text: mark_safe( formatter(text, filter_name)) # Format as choices LANGUAGE_CHOICES = [(n, _languageNames.get(n, n.capitalize())) for n in list(SUPPORTED_LANGUAGES.keys())] LANGUAGE_NAMES = dict(LANGUAGE_CHOICES) def render_text(text, language=None): """ Render the text, reuses the template filters provided by Django. """ # Get the filter text_filter = SUPPORTED_LANGUAGES.get(language, None) if not text_filter: raise ImproperlyConfigured(
def save(self, *args, **kwargs): self.body_markup = mark_safe( formatter(self.body, filter_name=self.markup)) super(Quote, self).save(*args, **kwargs)