def render(self, context):
     md = get_markdown()
     html = md.convert(self.value.resolve(context))
     # We use context.dicts[0] instead of context in order to access the
     # variables in any template blocks
     # See http://od-eon.com/blogs/liviu/scope-variables-template-blocks/
     context.dicts[0][self.var_name] = html
     if hasattr(md, "Meta"):
         context.dicts[0][self.meta_name] = md.Meta
     else:
         context.dicts[0][self.meta_name] = {}
     return ''
Esempio n. 2
0
def page_edit(request, slug):
    """
    Displays the edit form for :model:`aawiki.Page`

    **methods**
    ``GET``
        Either the edit form, OR provides Markdown source via AJAX call
    ``POST``
        Receives/commits edits on POST (either via form or AJAX)

    **parameters**
    ``section``
        Optional. Limits the scope of edition to the given section.

    **template**
        :template:`aawiki/page_edit.html`
    """
    context = {}
    name = dewikify(slug)

    section = int(request.REQUEST.get('section', 0))
    is_ajax = request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'

    try:
        page = Page.objects.get(name=name)
    except Page.DoesNotExist:
        page = None

    # Gets the edit form
    if request.method == "GET":
        if page:
            # Gets the whole content or just a section
            if section:
                sections = sectionalize(page.content)
                sectiondict = sections[section]
                context['content'] = sectiondict['header'] + sectiondict['body']
                context['section'] = section
            else:
                context['content'] = page.content
            # Returns plain content in case of ajax editing 
            if is_ajax:
                return HttpResponse(context['content'])
            else:
                context['page'] = page  # So templates nows about what page we are editing
                context['form'] = PageEditForm(initial={"content": context['content']})
        else:
            context['content'] = ''
            context['name'] = name  # So templates nows about what page we are editing
            rendered = render_to_string("aawiki/partials/initial_page_content.md", context)
            context['form'] = PageEditForm(initial={"content": rendered, "message": '/* Created a new page "%s" */' % name, })
        
        return render_to_response("aawiki/page_edit.html", context, \
                context_instance=RequestContext(request))

    elif request.method == "POST":
        content = request.POST.get('content', '')
        content = convert_line_endings(content, 0)  # Normalizes EOL
        content = content.strip() + "\n\n" # Normalize whitespace around the markdown

        is_cancelled = request.POST.get('cancel', None)
        if is_cancelled:
            url = reverse('aa-page-detail', kwargs={'slug': slug})
            return redirect(url)

        form = PageEditForm(request.POST)

        if form.is_valid():  # Processes the content of the form
            # Retrieves and cleans the form values
            content = form.cleaned_data["content"]
            content = convert_line_endings(content, 0)  # Normalizes EOL
            content = content.strip() + "\n\n" # Normalize whitespace around the markdown
            message = form.cleaned_data["message"] or "<no messages>"
            is_minor = form.cleaned_data["is_minor"]
            if request.user.is_authenticated():
                author = "%s <%s@%s>" % (request.user.username, request.user.username, 
                                         request.META['REMOTE_ADDR'])
            else:
                author = "Anonymous <anonymous@%s>" % request.META['REMOTE_ADDR']

            if page:
                old_content = page.content
                if section:  # section edit
                    keep_header = bool(request.REQUEST.get('keep_header'))
                    #print(keep_header)
                    if section == -1:
                        page.content = page.content.rstrip() + "\n\n" + content
                    else:
                        page.content = sectionalize_replace(page.content, section, content)
                    if page.content != old_content:
                        page.commit(message=message, author=author, is_minor=is_minor)
                else:
                    if content == "delete":
                        page.delete()
                    else:
                        page.content = content
                        if page.content != old_content:
                            page.commit(message=message, author=author, is_minor=is_minor)
            else:
                if content == "delete":
                    pass
                else:
                    page = Page(content=content, name=name)
                    page.commit(message=message, author=author, is_minor=is_minor)

            if is_ajax:
                # FIXME: apply typogrify filters here too!
                md = get_markdown()
                rendered = md.convert(content)
                return HttpResponse(rendered)

        else:  # Returns the invalid form for correction
            # TODO: factorize this chunk
            context['page'] = page  # So templates nows about what page we are editing
            context['name'] = name  # So templates nows about what page we are editing
            context['form'] = form
            return render_to_response("aawiki/page_edit.html", context, \
                    context_instance=RequestContext(request))

        url = reverse('aa-page-detail', kwargs={'slug': slug})
        return redirect(url)
def aasimplemarkdown (value):
    """ 
    markdown with aa extensions
    """
    md = get_markdown(simple=True)
    return md.convert(value)
def aamarkdown (value):
    """ 
    markdown with aa extensions
    """
    md = get_markdown()
    return md.convert(value)