def update_cms_page(self, title=None, content='', parent_title=None, draft=False): if not title: title = 'Untitled' from mezzanine.pages.models import Page, RichTextPage page = get_cms_page_from_title(title) in_menus = [2,3] # create the page from mezzanine.core.models import CONTENT_STATUS_PUBLISHED, CONTENT_STATUS_DRAFT if not page: parent_page = get_cms_page_from_title(parent_title) page = RichTextPage(title=title, content_model='richtextpage', parent=parent_page, content=content, status=CONTENT_STATUS_PUBLISHED) page.save() page.status = CONTENT_STATUS_DRAFT if draft else CONTENT_STATUS_PUBLISHED page.in_menus = in_menus else: # Can't find an easy way to edit page.richtextpage.content, so let's write directly to the DB! from django.db import connection cursor = connection.cursor() cursor.execute('''update pages_richtextpage set content = %s where page_ptr_id = %s''', [content, page.id]) page.save() return page
def preprocess_markdown(md, request): ret = md # As opposed to github MD, python MD will parse the content of ` and ``` # E.g. ```\n#test\n``` => <h1>test</h1> # So we convert ` and ``` to 4 space indent to produce the intended effect lines = ret.split('\n') in_code = False new_lines = [] for line in lines: code_swicth = line.rstrip() in ['`', '```'] if code_swicth: in_code = not in_code continue if in_code: line = ' ' + line new_lines.append(line) ret = '\n'.join(new_lines) # ~~test~~ => -test- ret = re.sub(ur'(?musi)~~(.*?)~~', ur'<del>\1</del>', ret) # convert link to another .md file # e.g. [See the other test document](test2.md) # => [See the other test document](test2) # link_prefix = '' # if request: # request_path = request.META['PATH_INFO'] # if request_path[-1] == '/': # link_prefix = '../' # # # we preserve the fragment (#...) # ret = re.sub(ur'\]\(([^)]+).md/?(#?[^)]*)\)', ur'](%s\1\2)' % # link_prefix, ret) pattern = re.compile(ur'\]\(([^)]+).md\b') pos = 1 while True: m = pattern.search(ret, pos) if not m: break replacement = '' from digipal import utils end_slug = re.sub(ur'^/.*/', '', m.group(1)) page = utils.get_cms_page_from_title(end_slug) # print end_slug if page: replacement = '](/%s/' % page.slug.strip('/') # print replacement if replacement: ret = ret[:m.start(0)] + replacement + ret[m.end(0):] pos = m.start(0) + len(replacement) + 1 return ret