Skip to content

Instantly share code, notes, and snippets.

@nicpottier
Created July 29, 2010 08:05
Show Gist options
  • Save nicpottier/497582 to your computer and use it in GitHub Desktop.
Save nicpottier/497582 to your computer and use it in GitHub Desktop.
Restructured Text, Pygments, CodeMirror and Moin Moin, all playing together.

Restructured Text Moin Moin

A set of tweaks to Moin Moin to accomplish the following:

  • make Restructured Text the default markup
  • make pygments work on code blocks
  • default pygments to python highlighting
  • use codemirror as an RST editor

This is based off of Moin 1.9.3.

This was made for the RapidSMS wiki that Nyaruka is hosting. http://www.nyaruka.com/ http://docs.rapidsms.org/

Installation

To just get RST as the default and have Pygments highlighting be automatic

  • copy text_rst.py to MoinMoin/parser/text_rst.py

  • copy pygments.css to MoinMoin/web/static/htdocs

  • edit your wikiconfig.py and add the following:

    default_markup = 'rst'
    stylesheets = [('screen', '/moin_static193/pygments.css')]
    

You should now be able to add code blocks that are by default Python highlighted using the syntax:

.. sourcecode:
    def foo(bar):
      print "hello world"

To also get syntax highlighting via CodeMirror

  • copy PageEditor.py to MoinMoin/PageEditor.py
  • download and unzip CodeMirror (I used .8) from http://marijn.haverbeke.nl/codemirror/
  • copy the /js and /css directories out of CodeMirror to MoinMoin/web/static/htdocs/js and MoinMoin/web/static/htdocs/css respectively
  • copy parserst.js to MoinMoin/web/static/htdocs/js
  • copy rstcolors.css to MoinMoin/web/static/htdocs/css

You should now have a fancy code editor with syntax highlighting too.

Enjoy your Sphinx like wiki.

# -*- coding: iso-8859-1 -*-
"""
MoinMoin - PageEditor class
PageEditor is used for r/w access to a wiki page (edit, rename, delete operations).
TODO:
* See comments in Page.py, most apply here, too.
* The editor code should be modularized so we will be able to use it for any
text/* mimetype data with some special features enabled depending on the
mimetype (e.g. enable wiki markup help when editing wiki mimetype).
@copyright: 2000-2004 by Juergen Hermann <jh@web.de>,
2005-2007 by MoinMoin:ThomasWaldmann,
2007 by MoinMoin:ReimarBauer
@license: GNU GPL, see COPYING for details.
"""
import os, time, codecs, errno
from MoinMoin import caching, config, wikiutil, error
from MoinMoin.Page import Page
from MoinMoin.widget import html
from MoinMoin.widget.dialog import Status
from MoinMoin.logfile import editlog, eventlog
from MoinMoin.mail.sendmail import encodeSpamSafeEmail
from MoinMoin.support.python_compatibility import set
from MoinMoin.util import filesys, timefuncs, web
from MoinMoin.events import PageDeletedEvent, PageRenamedEvent, PageCopiedEvent, PageRevertedEvent
from MoinMoin.events import PagePreSaveEvent, Abort, send_event
import MoinMoin.events.notification as notification
# used for merging
conflict_markers = ("\n---- /!\\ '''Edit conflict - other version:''' ----\n",
"\n---- /!\\ '''Edit conflict - your version:''' ----\n",
"\n---- /!\\ '''End of edit conflict''' ----\n")
#############################################################################
### Javascript code for editor page
#############################################################################
# we avoid the "--" operator to make this XHTML happy!
_countdown_js = """
%(countdown_script)s
<script type="text/javascript">
var countdown_timeout_min = %(lock_timeout)s
var countdown_lock_expire = "%(lock_expire)s"
var countdown_lock_mins = "%(lock_mins)s"
var countdown_lock_secs = "%(lock_secs)s"
addLoadEvent(countdown)
</script>
"""
#############################################################################
### PageEditor - Edit pages
#############################################################################
class PageEditor(Page):
""" Editor for a wiki page. """
# exceptions for .saveText()
class SaveError(error.Error):
pass
class RevertError(SaveError):
pass
class AccessDenied(SaveError):
pass
class Immutable(AccessDenied):
pass
class NoAdmin(AccessDenied):
pass
class EmptyPage(SaveError):
pass
class Unchanged(SaveError):
pass
class EditConflict(SaveError):
pass
class CouldNotLock(SaveError):
pass
def __init__(self, request, page_name, **keywords):
""" Create page editor object.
@param page_name: name of the page
@param request: the request object
@keyword do_revision_backup: if 0, suppress making a page backup per revision
@keyword do_editor_backup: if 0, suppress saving of draft copies
@keyword uid_override: override user id and name (default None)
"""
Page.__init__(self, request, page_name, **keywords)
self._ = request.getText
self.do_revision_backup = keywords.get('do_revision_backup', 1)
self.do_editor_backup = keywords.get('do_editor_backup', 1)
self.uid_override = keywords.get('uid_override', None)
self.lock = PageLock(self)
def mergeEditConflict(self, origrev):
""" Try to merge current page version with new version the user tried to save
@param origrev: the original revision the user was editing
@rtype: bool
@return: merge success status
"""
from MoinMoin.util import diff3
allow_conflicts = 1
# Get current editor text
savetext = self.get_raw_body()
# The original text from the revision the user was editing
original_text = Page(self.request, self.page_name, rev=origrev).get_raw_body()
# The current revision someone else saved
saved_text = Page(self.request, self.page_name).get_raw_body()
# And try to merge all into one with edit conflict separators
verynewtext = diff3.text_merge(original_text, saved_text, savetext,
allow_conflicts, *conflict_markers)
if verynewtext:
self.set_raw_body(verynewtext)
return True
# this should never happen, except for empty pages
return False
def sendconfirmleaving(self):
""" Prevents moving away from the page without saving it. """
_ = self._
self.request.write(u'''\
<script type="text/javascript">
var flgChange = false;
function confirmleaving() {
if ( flgChange )
return "%s";
}
</script>
''' % _("Your changes are not saved!"))
def sendEditor(self, **kw):
""" Send the editor form page.
@keyword preview: if given, show this text in preview mode
@keyword staytop: don't go to #preview
@keyword comment: comment field (when preview is true)
"""
from MoinMoin import i18n
from MoinMoin.action import SpellCheck
request = self.request
form = request.form
_ = self._
raw_body = ''
msg = None
conflict_msg = None
edit_lock_message = None
preview = kw.get('preview', None)
staytop = kw.get('staytop', 0)
from MoinMoin.formatter.text_html import Formatter
request.formatter = Formatter(request, store_pagelinks=1)
# check edit permissions
if not request.user.may.write(self.page_name):
msg = _('You are not allowed to edit this page.')
elif not self.isWritable():
msg = _('Page is immutable!')
elif self.rev:
# Trying to edit an old version, this is not possible via
# the web interface, but catch it just in case...
msg = _('Cannot edit old revisions!')
else:
try:
# try to acquire edit lock
ok, edit_lock_message = self.lock.acquire()
if not ok:
# failed to get the lock
if preview is not None:
edit_lock_message = _('The lock you held timed out. Be prepared for editing conflicts!'
) + "<br>" + edit_lock_message
else:
msg = edit_lock_message
except OSError, err:
if err.errno == errno.ENAMETOOLONG:
msg = _("Page name is too long, try shorter name.")
else:
raise
# Did one of the prechecks fail?
if msg:
request.theme.add_msg(msg, "error")
self.send_page()
return
# Emit http_headers after checks (send_page)
request.disableHttpCaching(level=2)
# check if we want to load a draft
use_draft = None
if 'button_load_draft' in form:
wanted_draft_timestamp = int(form.get('draft_ts', '0'))
if wanted_draft_timestamp:
draft = self._load_draft()
if draft is not None:
draft_timestamp, draft_rev, draft_text = draft
if draft_timestamp == wanted_draft_timestamp:
use_draft = draft_text
# Check for draft / normal / preview submit
if use_draft is not None:
title = _('Draft of "%(pagename)s"')
# Propagate original revision
rev = int(form['draft_rev'])
self.set_raw_body(use_draft, modified=1)
preview = use_draft
elif preview is None:
title = _('Edit "%(pagename)s"')
else:
title = _('Preview of "%(pagename)s"')
# Propagate original revision
rev = request.rev
self.set_raw_body(preview, modified=1)
# send header stuff
lock_timeout = self.lock.timeout / 60
lock_page = wikiutil.escape(self.page_name, quote=1)
lock_expire = _("Your edit lock on %(lock_page)s has expired!") % {'lock_page': lock_page}
lock_mins = _("Your edit lock on %(lock_page)s will expire in # minutes.") % {'lock_page': lock_page}
lock_secs = _("Your edit lock on %(lock_page)s will expire in # seconds.") % {'lock_page': lock_page}
# get request parameters
try:
text_rows = int(form['rows'])
except StandardError:
text_rows = self.cfg.edit_rows
if request.user.valid:
text_rows = int(request.user.edit_rows)
if preview is not None:
# Check for editing conflicts
if not self.exists():
# page does not exist, are we creating it?
if rev:
conflict_msg = _('Someone else deleted this page while you were editing!')
elif rev != self.current_rev():
conflict_msg = _('Someone else changed this page while you were editing!')
if self.mergeEditConflict(rev):
conflict_msg = _("""Someone else saved this page while you were editing!
Please review the page and save then. Do not save this page as it is!""")
rev = self.current_rev()
if conflict_msg:
# We don't show preview when in conflict
preview = None
elif self.exists():
# revision of existing page
rev = self.current_rev()
else:
# page creation
rev = 0
# Page editing is done using user language
request.setContentLanguage(request.lang)
# Get the text body for the editor field.
# TODO: what about deleted pages? show the text of the last revision or use the template?
if preview is not None:
raw_body = self.get_raw_body()
if use_draft:
request.theme.add_msg(_("[Content loaded from draft]"), 'info')
elif self.exists():
# If the page exists, we get the text from the page.
# TODO: maybe warn if template argument was ignored because the page exists?
raw_body = self.get_raw_body()
elif 'template' in request.values:
# If the page does not exist, we try to get the content from the template parameter.
template_page = wikiutil.unquoteWikiname(request.values['template'])
template_page_escaped = wikiutil.escape(template_page)
if request.user.may.read(template_page):
raw_body = Page(request, template_page).get_raw_body()
if raw_body:
request.theme.add_msg(_("[Content of new page loaded from %s]") % (template_page_escaped, ), 'info')
else:
request.theme.add_msg(_("[Template %s not found]") % (template_page_escaped, ), 'warning')
else:
request.theme.add_msg(_("[You may not read %s]") % (template_page_escaped, ), 'error')
# Make backup on previews - but not for new empty pages
if not use_draft and preview and raw_body:
self._save_draft(raw_body, rev)
draft_message = None
loadable_draft = False
if preview is None:
draft = self._load_draft()
if draft is not None:
draft_timestamp, draft_rev, draft_text = draft
if draft_text != raw_body:
loadable_draft = True
page_rev = rev
draft_timestamp_str = request.user.getFormattedDateTime(draft_timestamp)
draft_message = _(u"'''<<BR>>Your draft based on revision %(draft_rev)d (saved %(draft_timestamp_str)s) can be loaded instead of the current revision %(page_rev)d by using the load draft button - in case you lost your last edit somehow without saving it.''' A draft gets saved for you when you do a preview, cancel an edit or unsuccessfully save.", wiki=True) % locals()
# Setup status message
status = [kw.get('msg', ''), conflict_msg, edit_lock_message, draft_message]
status = [msg for msg in status if msg]
status = ' '.join(status)
status = Status(request, content=status)
request.theme.add_msg(status, "dialog")
request.theme.send_title(
title % {'pagename': self.split_title(), },
page=self,
html_head=self.lock.locktype and (
_countdown_js % {
'countdown_script': request.theme.externalScript('countdown'),
'lock_timeout': lock_timeout,
'lock_expire': lock_expire,
'lock_mins': lock_mins,
'lock_secs': lock_secs,
}) or '',
editor_mode=1,
allow_doubleclick=1,
)
request.write(request.formatter.startContent("content"))
# Generate default content for new pages
if not raw_body:
raw_body = _('Describe %s here.') % (self.page_name, )
# send form
request.write('<form id="editor" method="post" action="%s#preview" onSubmit="flgChange = false;">' % (
request.href(self.page_name)
))
# yet another weird workaround for broken IE6 (it expands the text
# editor area to the right after you begin to type...). IE sucks...
# http://fplanque.net/2003/Articles/iecsstextarea/
request.write('<fieldset style="border:none;padding:0;">')
request.write(unicode(html.INPUT(type="hidden", name="action", value="edit")))
# Send revision of the page our edit is based on
request.write('<input type="hidden" name="rev" value="%d">' % (rev, ))
# Create and send a ticket, so we can check the POST
request.write('<input type="hidden" name="ticket" value="%s">' % wikiutil.createTicket(request))
# Save backto in a hidden input
backto = request.values.get('backto')
if backto:
request.write(unicode(html.INPUT(type="hidden", name="backto", value=backto)))
# button bar
button_spellcheck = '<input class="button" type="submit" name="button_spellcheck" value="%s" onClick="flgChange = false;">' % _('Check Spelling')
save_button_text = _('Save Changes')
cancel_button_text = _('Cancel')
if self.cfg.page_license_enabled:
request.write('<p><em>', _(
"""By hitting '''%(save_button_text)s''' you put your changes under the %(license_link)s.
If you don't want that, hit '''%(cancel_button_text)s''' to cancel your changes.""", wiki=True) % {
'save_button_text': save_button_text,
'cancel_button_text': cancel_button_text,
'license_link': wikiutil.getLocalizedPage(request, self.cfg.page_license_page).link_to(request),
}, '</em></p>')
request.write('''
<input class="button" type="submit" name="button_save" value="%s" onClick="flgChange = false;">
<input class="button" type="submit" name="button_preview" value="%s" onClick="flgChange = false;">
''' % (save_button_text, _('Preview'), ))
if not (request.cfg.editor_force and request.cfg.editor_default == 'text'):
request.write('''
<input id="switch2gui" style="display: none;" class="button" type="submit" name="button_switch" value="%s">
''' % (_('GUI Mode'), ))
if loadable_draft:
request.write('''
<input class="button" type="submit" name="button_load_draft" value="%s" onClick="flgChange = false;">
<input type="hidden" name="draft_ts" value="%d">
<input type="hidden" name="draft_rev" value="%d">
''' % (_('Load Draft'), draft_timestamp, draft_rev))
request.write('''
%s
<input class="button" type="submit" name="button_cancel" value="%s">
<input type="hidden" name="editor" value="text">
''' % (button_spellcheck, cancel_button_text, ))
# Trivial Change-checkbox to the top of the page, shows up only if user has JavaScript enabled. It's "linked" with the bottom's box (checking one checks both)
if self.cfg.mail_enabled:
request.write('''
<script type="text/javascript">
<!--
function toggle_trivial(CheckedBox)
{
TrivialBoxes = document.getElementsByName("trivial");
for (var i = 0; i < TrivialBoxes.length; i++)
TrivialBoxes[i].checked = CheckedBox.checked;
}
document.write('<input type="checkbox" name="trivial" id="chktrivialtop" value="1" %(checked)s onclick="toggle_trivial(this)">');
document.write('<label for="chktrivialtop">%(label)s</label>');
//-->
</script> ''' % {
'checked': ('', 'checked')[form.get('trivial', '0') == '1'],
'label': _("Trivial change"),
})
from MoinMoin.security.textcha import TextCha
request.write(TextCha(request).render())
# Add textarea with page text
self.sendconfirmleaving()
lang = self.pi.get('language', request.cfg.language_default)
if not text_rows:
# if no specific value is given for editor height, but 0, we
# compute the rows from the raw_body line count plus some
# extra rows for adding new text in the editor. Maybe this helps
# with the "double slider" usability issue, esp. for devices like
# the iphone where you can't operate both sliders.
current_rows = len(raw_body.split('\n'))
text_rows = max(10, int(current_rows * 1.5))
request.write(
u'''\
<div id='editor-textarea-div'><textarea id="editor-textarea" name="savetext" lang="%(lang)s" dir="%(dir)s" rows="%(rows)d" cols="80"
onChange="flgChange = true;" onKeyPress="flgChange = true;">\
%(text)s\
</textarea></div>''' % {
'lang': lang,
'dir': i18n.getDirection(lang),
'rows': text_rows,
'text': wikiutil.escape(raw_body)
})
request.write("<p>")
request.write(_("Comment:"),
' <input id="editor-comment" type="text" name="comment" value="%s" size="80" maxlength="200"'
' onChange="flgChange = true;" onKeyPress="flgChange = true;">' % (
wikiutil.escape(kw.get('comment', ''), 1), ))
request.write("</p>")
# Category selection
filterfn = self.cfg.cache.page_category_regexact.search
cat_pages = request.rootpage.getPageList(filter=filterfn)
cat_pages.sort()
cat_pages = [wikiutil.pagelinkmarkup(p) for p in cat_pages]
cat_pages.insert(0, ('', _('<No addition>')))
request.write("<p>")
request.write(_('Add to: %(category)s') % {
'category': unicode(web.makeSelection('category', cat_pages)),
})
if self.cfg.mail_enabled:
request.write('''
&nbsp;
<input type="checkbox" name="trivial" id="chktrivial" value="1" %(checked)s onclick="toggle_trivial(this)">
<label for="chktrivial">%(label)s</label>
''' % {
'checked': ('', 'checked')[form.get('trivial', '0') == '1'],
'label': _("Trivial change"),
})
request.write('''
&nbsp;
<input type="checkbox" name="rstrip" id="chkrstrip" value="1" %(checked)s>
<label for="chkrstrip">%(label)s</label>
''' % {
'checked': ('', 'checked')[form.get('rstrip', '0') == '1'],
'label': _('Remove trailing whitespace from each line')
})
request.write("</p>")
badwords_re = None
if preview is not None:
if 'button_spellcheck' in form or 'button_newwords' in form:
badwords, badwords_re, msg = SpellCheck.checkSpelling(self, request, own_form=0)
request.write("<p>%s</p>" % msg)
request.write('</fieldset>')
request.write("</form>")
# QuickHelp originally by Georg Mischler <schorsch@lightingwiki.com>
markup = self.pi['format'] or request.cfg.default_markup
try:
parser = wikiutil.searchAndImportPlugin(self.request.cfg, "parser", markup)
except wikiutil.PluginMissingError:
parser = wikiutil.searchAndImportPlugin(self.request.cfg, "parser", "plain")
quickhelp = getattr(parser, 'quickhelp', None)
if quickhelp:
request.write(request.formatter.div(1, id="editor-help"))
request.write(_(quickhelp, wiki=True))
request.write(request.formatter.div(0))
if preview is not None:
if staytop:
content_id = 'previewbelow'
else:
content_id = 'preview'
self.send_page(content_id=content_id, content_only=1, hilite_re=badwords_re)
request.write('''
<script src="/moin_static193/js/codemirror.js" type="text/javascript"></script>
<script type="text/javascript">
var editor = CodeMirror.fromTextArea('editor-textarea', {
height: "500px",
parserfile: ["parserst.js"],
tabMode: "spaces",
stylesheet: ["/moin_static193/css/rstcolors.css"],
path: "/moin_static193/js/"
});
</script>
''')
request.write(request.formatter.endContent())
request.theme.send_footer(self.page_name)
request.theme.send_closing_html()
def sendCancel(self, newtext, rev):
""" User clicked on Cancel button.
If edit locking is active, delete the current lock file.
@param newtext: the edited text (which has been cancelled)
@param rev: not used!?
"""
request = self.request
_ = self._
self._save_draft(newtext, rev) # shall we really save a draft on CANCEL?
self.lock.release()
backto = request.values.get('backto')
if backto:
pg = Page(request, backto)
request.http_redirect(pg.url(request))
else:
request.theme.add_msg(_('Edit was cancelled.'), "error")
self.send_page()
def copyPage(self, newpagename, comment=u''):
""" Copy the current version of the page (keeping the backups, logs and attachments).
@param comment: Comment given by user
@rtype: unicode
@return: success flag, error message
"""
request = self.request
_ = self._
if not newpagename:
return False, _("You can't copy to an empty pagename.")
if not self.request.user.may.write(newpagename):
return False, _('You are not allowed to copy this page!')
newpage = PageEditor(request, newpagename)
pageexists_error = _("""'''A page with the name {{{'%s'}}} already exists.'''
Try a different name.""", wiki=True) % (wikiutil.escape(newpagename), )
# Check whether a page with the new name already exists
if newpage.exists(includeDeleted=1):
return False, pageexists_error
# Get old page text
savetext = self.get_raw_body()
oldpath = self.getPagePath(check_create=0)
newpath = newpage.getPagePath(check_create=0)
# Copy page
# NOTE: might fail if another process created newpagename just
try:
filesys.copytree(oldpath, newpath)
self.error = None
savetext = u"## page was copied from %s\n%s" % (self.page_name, savetext)
Page.__init__(self, request, newpagename)
self._write_file(savetext, "SAVENEW", comment)
event = PageCopiedEvent(request, newpage, self, comment)
send_event(event)
return True, None
except OSError, err:
# Try to understand what happened. Maybe its better to check
# the error code, but I just reused the available code above...
if newpage.exists(includeDeleted=1):
return False, pageexists_error
else:
return False, _('Could not copy page because of file system error: %s.') % unicode(err)
def renamePage(self, newpagename, comment=u''):
""" Rename the current version of the page (making a backup before deletion
and keeping the backups, logs and attachments).
@param comment: Comment given by user
@rtype: unicode
@return: success flag, error message
"""
request = self.request
_ = self._
if not (request.user.may.delete(self.page_name)
and request.user.may.write(newpagename)):
msg = _('You are not allowed to rename this page!')
raise self.AccessDenied, msg
if not newpagename:
return False, _("You can't rename to an empty pagename.")
newpage = PageEditor(request, newpagename)
pageexists_error = _("""'''A page with the name {{{'%s'}}} already exists.'''
Try a different name.""", wiki=True) % (wikiutil.escape(newpagename), )
# Check whether a page with the new name already exists
if newpage.exists(includeDeleted=1):
return False, pageexists_error
# Get old page text
savetext = self.get_raw_body()
oldpath = self.getPagePath(check_create=0)
newpath = newpage.getPagePath(check_create=0)
# Rename page
# NOTE: might fail if another process created newpagename just
# NOW, while you read this comment. Rename is atomic for files -
# but for directories, rename will fail if the directory
# exists. We should have global edit-lock to avoid this.
# See http://docs.python.org/lib/os-file-dir.html
try:
os.rename(oldpath, newpath)
self.error = None
# Save page text with a comment about the old name
savetext = u"## page was renamed from %s\n%s" % (self.page_name, savetext)
newpage.saveText(savetext, 0, comment=comment, extra=self.page_name, action='SAVE/RENAME', notify=False)
# delete pagelinks
arena = newpage
key = 'pagelinks'
cache = caching.CacheEntry(request, arena, key, scope='item')
cache.remove()
# clean the cache
for formatter_name in self.cfg.caching_formats:
arena = newpage
key = formatter_name
cache = caching.CacheEntry(request, arena, key, scope='item')
cache.remove()
event = PageRenamedEvent(request, newpage, self, comment)
send_event(event)
return True, None
except OSError, err:
# Try to understand what happened. Maybe its better to check
# the error code, but I just reused the available code above...
if newpage.exists(includeDeleted=1):
return False, pageexists_error
else:
return False, _('Could not rename page because of file system error: %s.') % unicode(err)
def revertPage(self, revision, comment=u''):
""" Reverts page to the given revision
@param revision: revision to revert to
@type revision: int
"""
_ = self.request.getText
if not self.request.user.may.revert(self.page_name):
# no real message necessary, cannot happen if
# user doesn't try to exploit us
raise self.RevertError('not allowed')
elif revision is None:
# see above
raise self.RevertError('cannot revert to current rev')
else:
revstr = '%08d' % revision
pg = Page(self.request, self.page_name, rev=revision)
msg = self.saveText(pg.get_raw_body(), 0, extra=revstr, action="SAVE/REVERT", notify=False, comment=comment)
# Remove cache entry (if exists)
pg = Page(self.request, self.page_name)
key = self.request.form.get('key', 'text_html') # XXX see cleanup code in deletePage
caching.CacheEntry(self.request, pg, key, scope='item').remove()
caching.CacheEntry(self.request, pg, "pagelinks", scope='item').remove()
# Notify observers
e = PageRevertedEvent(self.request, self.page_name, revision, revstr)
send_event(e)
return msg
def deletePage(self, comment=None):
""" Delete the current version of the page (making a backup before deletion
and keeping the backups, logs and attachments).
@param comment: Comment given by user
@rtype: unicode
@return: success flag, error message
"""
request = self.request
_ = self._
success = True
if not (request.user.may.write(self.page_name)
and request.user.may.delete(self.page_name)):
msg = _('You are not allowed to delete this page!')
raise self.AccessDenied, msg
try:
msg = self.saveText(u"deleted\n", 0, comment=comment or u'', deleted=True, notify=False)
msg = msg.replace(
_("Thank you for your changes. Your attention to detail is appreciated."),
_('Page "%s" was successfully deleted!') % (wikiutil.escape(self.page_name), ))
event = PageDeletedEvent(request, self, comment)
send_event(event)
except self.SaveError, message:
# XXX do not only catch base class SaveError here, but
# also the derived classes, so we can give better err msgs
success = False
msg = "SaveError has occured in PageEditor.deletePage. We need locking there."
# delete pagelinks
arena = self
key = 'pagelinks'
cache = caching.CacheEntry(request, arena, key, scope='item')
cache.remove()
# clean the cache
for formatter_name in self.cfg.caching_formats:
arena = self
key = formatter_name
cache = caching.CacheEntry(request, arena, key, scope='item')
cache.remove()
return success, msg
def _get_local_timestamp(self):
""" Returns the string that can be used by the TIME substitution.
@return: str with a timestamp in it
"""
now = time.time()
# default: UTC
zone = "Z"
u = self.request.user
# setup the timezone
if u.valid and u.tz_offset:
tz = u.tz_offset
# round to minutes
tz -= tz % 60
minutes = tz / 60
hours = minutes / 60
minutes -= hours * 60
# construct the offset
zone = "%+0.2d%02d" % (hours, minutes)
# correct the time by the offset we've found
now += tz
return time.strftime("%Y-%m-%dT%H:%M:%S", timefuncs.tmtuple(now)) + zone
def _expand_variables(self, text):
""" Expand @VARIABLE@ in `text`and return the expanded text.
@param text: current text of wikipage
@rtype: string
@return: new text of wikipage, variables replaced
"""
# TODO: Allow addition of variables via wikiconfig or a global wiki dict.
request = self.request
now = self._get_local_timestamp()
u = request.user
obfuscated_email_address = encodeSpamSafeEmail(u.email)
signature = u.signature()
variables = {
'PAGE': self.page_name,
'TIMESTAMP': now,
'TIME': "<<DateTime(%s)>>" % now,
'DATE': "<<Date(%s)>>" % now,
'ME': u.name,
'USERNAME': signature,
'USER': "-- %s" % signature,
'SIG': "-- %s <<DateTime(%s)>>" % (signature, now),
'EMAIL': "<<MailTo(%s)>>" % (obfuscated_email_address)
}
if u.valid and u.name:
if u.email:
variables['MAILTO'] = "<<MailTo(%s)>>" % u.email
# Users can define their own variables via
# UserHomepage/MyDict, which override the default variables.
userDictPage = u.name + "/MyDict"
if userDictPage in request.dicts:
variables.update(request.dicts[userDictPage])
for name in variables:
text = text.replace('@%s@' % name, variables[name])
return text
def normalizeText(self, text, **kw):
""" Normalize text
Make sure text uses '\n' line endings, and has a trailing
newline. Strip whitespace on end of lines if needed.
You should normalize any text you enter into a page, for
example, when getting new text from the editor, or when setting
new text manually.
@param text: text to normalize (unicode)
@keyword stripspaces: if 1, strip spaces from text
@rtype: unicode
@return: normalized text
"""
if text:
lines = text.splitlines()
# Strip trailing spaces if needed
if kw.get('stripspaces', 0):
lines = [line.rstrip() for line in lines]
# Add final newline if not present, better for diffs (does
# not include former last line when just adding text to
# bottom; idea by CliffordAdams)
if not lines[-1] == u'':
# '' will make newline after join
lines.append(u'')
text = u'\n'.join(lines)
return text
def _save_draft(self, text, rev, **kw):
""" Save an editor backup to the drafts cache arena.
@param text: draft text of the page
(if None, the draft gets removed from the cache)
@param rev: the revision of the page this draft is based on
@param kw: no keyword args used currently
"""
request = self.request
if not request.user.valid or not self.do_editor_backup:
return None
arena = 'drafts'
key = request.user.id
cache = caching.CacheEntry(request, arena, key, scope='wiki', use_pickle=True)
if cache.exists():
cache_data = cache.content()
else:
cache_data = {}
pagename = self.page_name
if text is None:
try:
del cache_data[pagename]
except:
pass
else:
timestamp = int(time.time())
cache_data[pagename] = (timestamp, rev, text)
cache.update(cache_data)
def _load_draft(self):
""" Get a draft from the drafts cache arena.
@rtype: unicode
@return: draft text or None
"""
request = self.request
if not request.user.valid:
return None
arena = 'drafts'
key = request.user.id
cache = caching.CacheEntry(request, arena, key, scope='wiki', use_pickle=True)
pagename = self.page_name
try:
cache_data = cache.content()
return cache_data.get(pagename)
except caching.CacheError:
return None
def copy_underlay_page(self):
# renamed from copypage to avoid conflicts with copyPage
""" Copy a page from underlay directory to page directory """
src = self.getPagePath(use_underlay=1, check_create=0)
dst = self.getPagePath(use_underlay=0, check_create=0)
if src and dst and src != dst and os.path.exists(src):
try:
os.rmdir(dst) # simply remove empty dst dirs
# XXX in fact, we should better remove anything we regard as an
# empty page, maybe also if there is also an edit-lock or
# empty cache. revisions subdir...
except:
pass
if not os.path.exists(dst):
filesys.copytree(src, dst)
self.reset() # reinit stuff
def _write_file(self, text, action='SAVE', comment=u'', extra=u'', deleted=False):
""" Write the text to the page file (and make a backup of old page).
@param text: text to save for this page
@param deleted: if True, then don't write page content (used by deletePage)
@rtype: int
@return: mtime_usec of new page
"""
request = self.request
_ = self._
was_deprecated = self.pi.get('deprecated', False)
self.copy_underlay_page()
# remember conflict state
self.setConflict(wikiutil.containsConflictMarker(text))
# Write always on the standard directory, never change the
# underlay directory copy!
pagedir = self.getPagePath(use_underlay=0, check_create=0)
revdir = os.path.join(pagedir, 'revisions')
cfn = os.path.join(pagedir, 'current')
clfn = os.path.join(pagedir, 'current-locked')
cltfn = os.path.join(pagedir, 'current-locked.tmp')
# !!! these log objects MUST be created outside the locked area !!!
# The local log should be the standard edit log, not the
# underlay copy log!
pagelog = self.getPagePath('edit-log', use_underlay=0, isfile=1)
llog = editlog.EditLog(request, filename=pagelog,
uid_override=self.uid_override)
# Open the global log
glog = editlog.EditLog(request, uid_override=self.uid_override)
if not os.path.exists(pagedir): # new page, create and init pagedir
os.mkdir(pagedir)
if not os.path.exists(revdir):
os.mkdir(revdir)
f = file(cfn, 'w')
f.write('%08d\n' % 0)
f.close()
got_lock = False
retry = 0
try:
while not got_lock and retry < 100:
retry += 1
try:
filesys.rename(cfn, clfn)
got_lock = True
except OSError, err:
got_lock = False
if err.errno == 2: # there was no 'current' file
time.sleep(0.1)
else:
raise self.CouldNotLock, _("Page could not get locked. Unexpected error (errno=%d).") % err.errno
if not got_lock:
raise self.CouldNotLock, _("Page could not get locked. Missing 'current' file?")
# increment rev number of current(-locked) page
f = file(clfn)
revstr = f.read()
f.close()
try:
rev = int(revstr)
except ValueError, err:
raise self.SaveError, _("Unable to determine current page revision from the 'current' file. The page %s is damaged and cannot be edited right now.") % self.page_name
if not was_deprecated:
if self.do_revision_backup or rev == 0:
rev += 1
revstr = '%08d' % rev
# write the current page rev to a temporary file
try:
f = file(cltfn, 'w')
f.write(revstr+'\n')
f.close()
except IOError, err:
try:
os.remove(cltfn)
except:
pass # we don't care for errors in the os.remove
# throw a nicer exception
if err.errno == errno.ENOSPC:
raise self.SaveError, _("Cannot save page %s, no storage space left.") % self.page_name
else:
raise self.SaveError, _("An I/O error occurred while saving page %s (errno=%d)") % (self.page_name, err.errno)
# atomically put it in place (except on windows)
else:
filesys.rename(cltfn, clfn)
if not deleted:
# save to page file
pagefile = os.path.join(revdir, revstr)
f = codecs.open(pagefile, 'wb', config.charset)
# Write the file using text/* mime type
f.write(self.encodeTextMimeType(text))
f.close()
mtime_usecs = wikiutil.timestamp2version(os.path.getmtime(pagefile))
# set in-memory content
self.set_raw_body(text)
else:
mtime_usecs = wikiutil.timestamp2version(time.time())
# set in-memory content
self.set_raw_body(None)
# reset page object
self.reset()
# write the editlog entry
# for now simply make 2 logs, better would be some multilog stuff maybe
if self.do_revision_backup:
# do not globally log edits with no revision backup
# if somebody edits a deprecated page, log it in global log, but not local log
glog.add(request, mtime_usecs, rev, action, self.page_name, None, extra, comment)
if not was_deprecated and self.do_revision_backup:
# if we did not create a new revision number, do not locally log it
llog.add(request, mtime_usecs, rev, action, self.page_name, None, extra, comment)
finally:
if got_lock:
filesys.rename(clfn, cfn)
# add event log entry
elog = eventlog.EventLog(request)
elog.add(request, 'SAVEPAGE', {'pagename': self.page_name}, 1, mtime_usecs)
return mtime_usecs, rev
def saveText(self, newtext, rev, **kw):
""" Save new text for a page.
@param newtext: text to save for this page
@param rev: revision of the page
@keyword trivial: trivial edit (default: 0)
@keyword extra: extra info field (e.g. for SAVE/REVERT with revno)
@keyword comment: comment field (when preview is true)
@keyword action: action for editlog (default: SAVE)
@keyword index: needs indexing, not already handled (default: 1)
@keyword deleted: if True, then don't save page content (used by DeletePage, default: False)
@keyword notify: if False (default: True), don't send a PageChangedEvent
@rtype: unicode
@return: error msg
"""
request = self.request
_ = self._
self._save_draft(newtext, rev, **kw)
action = kw.get('action', 'SAVE')
deleted = kw.get('deleted', False)
notify = kw.get('notify', True)
#!!! need to check if we still retain the lock here
#!!! rev check is not enough since internal operations use "0"
# expand variables, unless it's a template or form page
if not wikiutil.isTemplatePage(request, self.page_name):
newtext = self._expand_variables(newtext)
msg = ""
if not request.user.may.save(self, newtext, rev, **kw):
msg = _('You are not allowed to edit this page!')
raise self.AccessDenied, msg
elif not self.isWritable():
msg = _('Page is immutable!')
raise self.Immutable, msg
elif not newtext:
msg = _('You cannot save empty pages.')
raise self.EmptyPage, msg
elif rev != 0 and rev != self.current_rev():
# check if we already saved that page
other = False
pagelog = self.getPagePath('edit-log', use_underlay=0, isfile=1)
next_line = None
for line in editlog.EditLog(request, pagelog).reverse():
if int(line.rev) == int(rev):
break
if not line.is_from_current_user(request):
other = True
next_line = line
if next_line and next_line.is_from_current_user(request):
saved_page = Page(request, self.page_name, rev=int(next_line.rev))
if newtext == saved_page.get_raw_body():
msg = _("You already saved this page!")
return msg
else:
msg = _("You already edited this page! Please do not use the back button.")
raise self.EditConflict, msg
msg = _("""Someone else saved this page while you were editing!
Please review the page and save then. Do not save this page as it is!""")
raise self.EditConflict, msg
elif newtext == self.get_raw_body():
msg = _('You did not change the page content, not saved!')
self.lock.release()
raise self.Unchanged, msg
else:
from MoinMoin.security import parseACL
# Get current ACL and compare to new ACL from newtext. If
# they are not the sames, the user must have admin
# rights. This is a good place to update acl cache - instead
# of wating for next request.
acl = self.getACL(request)
if (not request.user.may.admin(self.page_name) and
parseACL(request, newtext).acl != acl.acl and
action != "SAVE/REVERT"):
msg = _("You can't change ACLs on this page since you have no admin rights on it!")
raise self.NoAdmin, msg
presave = PagePreSaveEvent(request, self, newtext)
results = send_event(presave)
for result in results:
if isinstance(result, Abort):
# XXX: this should return a list of messages to the sorrounding context
# XXX: rather than dumbly concatenate them. Fix in the future.
msg = msg + result.reason
# save only if no error occurred (msg is empty) and no abort has been requested
if not msg:
# set success msg
msg = _("Thank you for your changes. Your attention to detail is appreciated.")
# determine action for edit log
if action == 'SAVE' and not self.exists():
action = 'SAVENEW'
comment = kw.get('comment', u'')
extra = kw.get('extra', u'')
trivial = kw.get('trivial', 0)
# write the page file
mtime_usecs, rev = self._write_file(newtext, action, comment, extra, deleted=deleted)
self._save_draft(None, None) # everything fine, kill the draft for this page
if notify:
# send notifications
from MoinMoin import events
if trivial:
e = events.TrivialPageChangedEvent(self.request, self, comment)
else:
e = events.PageChangedEvent(self.request, self, comment)
results = events.send_event(e)
recipients = set()
for result in results:
if isinstance(result, notification.Success):
recipients.update(result.recipients)
if recipients:
info = _("Notifications sent to:")
msg = msg + "<p>%s %s</p>" % (info, ", ".join(recipients))
# Update page trail with the page we just saved.
# This is needed for NewPage macro with backto because it does not
# send the page we just saved.
request.user.addTrail(self)
# remove lock (forcibly if we were allowed to break it by the UI)
# !!! this is a little fishy, since the lock owner might not notice
# we broke his lock ==> but revision checking during preview will
self.lock.release(force=not msg) # XXX does "not msg" make any sense?
return msg
class PageLock:
""" PageLock - Lock pages """
# TODO: race conditions throughout, need to lock file during queries & update
def __init__(self, pageobj):
"""
"""
self.pageobj = pageobj
self.page_name = pageobj.page_name
request = pageobj.request
self.request = request
self._ = self.request.getText
self.cfg = self.request.cfg
# current time and user for later checks
self.now = int(time.time())
self.uid = request.user.valid and request.user.id or request.remote_addr
# get details of the locking preference, i.e. warning or lock, and timeout
self.locktype = None
self.timeout = 10 * 60 # default timeout in minutes
if self.cfg.edit_locking:
lockinfo = self.cfg.edit_locking.split()
if 1 <= len(lockinfo) <= 2:
self.locktype = lockinfo[0].lower()
if len(lockinfo) > 1:
try:
self.timeout = int(lockinfo[1]) * 60
except ValueError:
pass
def acquire(self):
""" Begin an edit lock depending on the mode chosen in the config.
@rtype: tuple
@return: tuple is returned containing 2 values:
* a bool indicating successful acquiry
* a string giving a reason for failure or an informational msg
"""
if not self.locktype:
# we are not using edit locking, so always succeed
return 1, ''
_ = self._
#!!! race conditions, need to lock file during queries & update
self._readLockFile()
bumptime = self.request.user.getFormattedDateTime(self.now + self.timeout)
timestamp = self.request.user.getFormattedDateTime(self.timestamp)
owner = self.owner_html
secs_valid = self.timestamp + self.timeout - self.now
# do we own the lock, or is it stale?
if self.owner is None or self.uid == self.owner or secs_valid < 0:
# create or bump the lock
self._writeLockFile()
msg = []
if self.owner is not None and -10800 < secs_valid < 0:
mins_ago = secs_valid / -60
msg.append(_(
"The lock of %(owner)s timed out %(mins_ago)d minute(s) ago,"
" and you were granted the lock for this page."
) % {'owner': owner, 'mins_ago': mins_ago})
if self.locktype == 'lock':
msg.append(_(
"Other users will be ''blocked'' from editing this page until %(bumptime)s.",
wiki=True) % {'bumptime': bumptime})
else:
msg.append(_(
"Other users will be ''warned'' until %(bumptime)s that you are editing this page.",
wiki=True) % {'bumptime': bumptime})
msg.append(_(
"Use the Preview button to extend the locking period."
))
result = 1, '\n'.join(msg)
else:
mins_valid = (secs_valid+59) / 60
if self.locktype == 'lock':
# lout out user
result = 0, _(
"This page is currently ''locked'' for editing by %(owner)s until %(timestamp)s,"
" i.e. for %(mins_valid)d minute(s).",
wiki=True) % {'owner': owner, 'timestamp': timestamp, 'mins_valid': mins_valid}
else:
# warn user about existing lock
result = 1, _(
"""This page was opened for editing or last previewed at %(timestamp)s by %(owner)s.<<BR>>
'''You should ''refrain from editing'' this page for at least another %(mins_valid)d minute(s),
to avoid editing conflicts.'''<<BR>>
To leave the editor, press the Cancel button.""", wiki=True) % {
'timestamp': timestamp, 'owner': owner, 'mins_valid': mins_valid}
return result
def release(self, force=0):
""" Release lock, if we own it.
@param force: if 1, unconditionally release the lock.
"""
if self.locktype:
# check that we own the lock in order to delete it
#!!! race conditions, need to lock file during queries & update
self._readLockFile()
if force or self.uid == self.owner:
self._deleteLockFile()
def _filename(self):
""" Get path and filename for edit-lock file. """
return self.pageobj.getPagePath('edit-lock', isfile=1)
def _readLockFile(self):
""" Load lock info if not yet loaded. """
_ = self._
self.owner = None
self.owner_html = wikiutil.escape(_("<unknown>"))
self.timestamp = 0
if self.locktype:
try:
entry = editlog.EditLog(self.request, filename=self._filename()).next()
except StopIteration:
entry = None
if entry:
self.owner = entry.userid or entry.addr
self.owner_html = entry.getEditor(self.request)
self.timestamp = wikiutil.version2timestamp(entry.ed_time_usecs)
def _writeLockFile(self):
""" Write new lock file. """
self._deleteLockFile()
try:
editlog.EditLog(self.request, filename=self._filename(), force_ip=True).add(
self.request, wikiutil.timestamp2version(self.now), 0, "LOCK", self.page_name)
except IOError:
pass
def _deleteLockFile(self):
""" Delete the lock file unconditionally. """
try:
os.remove(self._filename())
except OSError:
pass
/*
Really dumb and basic RST syntax highlighter. Does all sorts of things wrong but
does give some basic highlighting which may help users. RestructuredText just so
happens to be a very hard thing to parse given how codemirror is organized.
*/
function spacenizer(source, state) {
// Newlines are always a separate token.
function isWhiteSpace(ch) {
// The messy regexp is because IE's regexp matcher is of the
// opinion that non-breaking spaces are no whitespace.
return ch != "\n" && /^[\s\u00a0]*$/.test(ch);
}
var tokenizer = {
state: state,
leading: false,
take: function(type) {
if (typeof(type) == "string")
type = {style: type, type: type};
type.content = (type.content || "") + source.get();
if (!/\n$/.test(type.content))
source.nextWhile(isWhiteSpace);
type.value = type.content + source.get();
return type;
},
next: function () {
if (!source.more()) throw StopIteration;
var type;
if (source.equals("\n")) {
source.next();
this.leading = false;
return this.take("whitespace");
}
if (source.applies(isWhiteSpace)){
type = "whitespace";
this.leading = true;
} else {
while (!type){
type = this.state(source, this.leading)
}
}
return this.take(type);
}
};
return tokenizer;
}
var RstParser = Editor.Parser = (function() {
var tokenizeRst = (function() {
function normal(source, leadingWhite) {
var ch = source.next();
// our parser always comes in at the start of a line
// leading whitespace usually means we are something different
if (leadingWhite &&
(ch != '*' && !/(\d)/.test(ch))){
readToNewline(source);
return "rst-indented";
}
// h1
if (ch == '=') {
source.nextWhileMatches(/=/);
return "rst-h1";
}
// h2
if (ch == '-'){
source.nextWhileMatches(/-/);
return "rst-h2";
}
// h3
if (ch == '~') {
source.nextWhileMatches(/~/);
return "rst-h3";
}
// bullet or number list
if ((ch == '*' && source.peek() == ' ') ||
(/(\d)/.test(ch) && source.peek() == '.')){
readToNewline(source);
return "rst-list";
}
// directive
if (ch == '.' && source.peek() == '.'){
readToNewline(source);
return "rst-directive";
}
// otherwise read to the end of the line so we start at the right position
// on the next iteration
readToNewline(source);
return 'rst-plain';
}
// just advances our head to the end of the line
function readToNewline(source){
while (!source.endOfLine()){
source.next()
}
}
return function(source, startState) {
return spacenizer(source, normal);
};
})();
function parseRst(source, basecolumn) {
var tokens = tokenizeRst(source);
var iter = {
next: function() {
var token = tokens.next(), style = token.style, content = token.content;
return token;
},
copy: function() {
return function(source) {
tokens = tokenizeRst(source);
return iter;
};
}
};
return iter;
}
return { make: parseRst };
})();
#editor-textarea-div {
border: 1px solid black;
}
td.linenos { background-color: #f0f0f0; padding-right: 10px; }
span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }
pre { line-height: 125%; }
body .hll { background-color: #ffffff }
body { background: #eeeeee; }
body .c { color: #408080; font-style: italic } /* Comment */
body .err { border: 1px solid #FF0000 } /* Error */
body .k { color: #008000; font-weight: bold } /* Keyword */
body .o { color: #666666 } /* Operator */
body .cm { color: #408080; font-style: italic } /* Comment.Multiline */
body .cp { color: #BC7A00 } /* Comment.Preproc */
body .c1 { color: #408080; font-style: italic } /* Comment.Single */
body .cs { color: #408080; font-style: italic } /* Comment.Special */
body .gd { color: #A00000 } /* Generic.Deleted */
body .ge { font-style: italic } /* Generic.Emph */
body .gr { color: #FF0000 } /* Generic.Error */
body .gh { color: #000080; font-weight: bold } /* Generic.Heading */
body .gi { color: #00A000 } /* Generic.Inserted */
body .go { color: #808080 } /* Generic.Output */
body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
body .gs { font-weight: bold } /* Generic.Strong */
body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
body .gt { color: #0040D0 } /* Generic.Traceback */
body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
body .kp { color: #008000 } /* Keyword.Pseudo */
body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
body .kt { color: #B00040 } /* Keyword.Type */
body .m { color: #666666 } /* Literal.Number */
body .s { color: #BA2121 } /* Literal.String */
body .na { color: #7D9029 } /* Name.Attribute */
body .nb { color: #008000 } /* Name.Builtin */
body .nc { color: #0000FF; font-weight: bold } /* Name.Class */
body .no { color: #880000 } /* Name.Constant */
body .nd { color: #AA22FF } /* Name.Decorator */
body .ni { color: #999999; font-weight: bold } /* Name.Entity */
body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
body .nf { color: #0000FF } /* Name.Function */
body .nl { color: #A0A000 } /* Name.Label */
body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
body .nt { color: #008000; font-weight: bold } /* Name.Tag */
body .nv { color: #19177C } /* Name.Variable */
body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
body .w { color: #bbbbbb } /* Text.Whitespace */
body .mf { color: #666666 } /* Literal.Number.Float */
body .mh { color: #666666 } /* Literal.Number.Hex */
body .mi { color: #666666 } /* Literal.Number.Integer */
body .mo { color: #666666 } /* Literal.Number.Oct */
body .sb { color: #BA2121 } /* Literal.String.Backtick */
body .sc { color: #BA2121 } /* Literal.String.Char */
body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
body .s2 { color: #BA2121 } /* Literal.String.Double */
body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
body .sh { color: #BA2121 } /* Literal.String.Heredoc */
body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
body .sx { color: #008000 } /* Literal.String.Other */
body .sr { color: #BB6688 } /* Literal.String.Regex */
body .s1 { color: #BA2121 } /* Literal.String.Single */
body .ss { color: #19177C } /* Literal.String.Symbol */
body .bp { color: #008000 } /* Name.Builtin.Pseudo */
body .vc { color: #19177C } /* Name.Variable.Class */
body .vg { color: #19177C } /* Name.Variable.Global */
body .vi { color: #19177C } /* Name.Variable.Instance */
body .il { color: #666666 } /* Literal.Number.Integer.Long */
.editbox {
margin: .4em;
padding: 0;
font-family: monospace;
font-size: 10pt;
color: black;
}
.editbox p {
margin: 0;
}
.editbox span.rst-h1 {
color: #48a;
font-weight: bold;
}
.editbox span.rst-h2 {
color: #281;
font-weight: bold;
}
.editbox span.rst-h3 {
color: #a70;
font-weight: bold;
}
.editbox span.rst-directive {
color: #a0b;
font-weight: bold;
}
.editbox span.rst-indented {
color: #555;
}
.editbox span.rst-list {
color: #355;
}
.editbox span.rst-plain {
color: black;
}
# -*- coding: iso-8859-1 -*-
"""
MoinMoin - ReStructured Text Parser
@copyright: 2004 Matthew Gilbert <gilbert AT voxmea DOT net>,
2004 Alexander Schremmer <alex AT alexanderweb DOT de>
@license: GNU GPL, see COPYING for details.
REQUIRES docutils 0.3.10 or later (must be later than December 30th, 2005)
"""
import re
import new
import StringIO
import __builtin__
import sys
# docutils imports are below
from MoinMoin.parser.text_moin_wiki import Parser as WikiParser
from MoinMoin.Page import Page
from MoinMoin.action import AttachFile
from MoinMoin import wikiutil
Dependencies = [] # this parser just depends on the raw text
# --- make docutils safe by overriding all module-scoped names related to IO ---
# TODO: Add an error message to dummyOpen so that the user knows that
# they requested an unsupported feature of Docutils in MoinMoin.
def dummyOpen(x, y=None, z=None): return
class dummyIO(StringIO.StringIO):
def __init__(self, destination=None, destination_path=None,
encoding=None, error_handler='', autoclose=1,
handle_io_errors=1, source_path=None):
StringIO.StringIO.__init__(self)
class dummyUrllib2:
def urlopen(a):
return StringIO.StringIO()
urlopen = staticmethod(urlopen)
# # # All docutils imports must be contained below here
try:
import docutils
from docutils.core import publish_parts
from docutils.writers import html4css1
from docutils.nodes import reference
from docutils.parsers import rst
from docutils.parsers.rst import directives, roles
# # # All docutils imports must be contained above here
ErrorParser = None # used in the case of missing docutils
docutils.io.FileOutput = docutils.io.FileInput = dummyIO
except ImportError:
# we need to workaround this totally broken plugin interface that does
# not allow us to raise exceptions
class ErrorParser:
caching = 0
Dependencies = Dependencies # copy dependencies from module-scope
def __init__(self, raw, request, **kw):
self.raw = raw
self.request = request
def format(self, formatter):
_ = self.request.getText
from MoinMoin.parser.text import Parser as TextParser
self.request.write(formatter.sysmsg(1) +
formatter.rawHTML(_('Rendering of reStructured text is not possible, please install Docutils.')) +
formatter.sysmsg(0))
TextParser(self.raw, self.request).format(formatter)
# Create a pseudo docutils environment
docutils = html4css1 = dummyUrllib2()
html4css1.HTMLTranslator = html4css1.Writer = object
def safe_import(name, globals = None, locals = None, fromlist = None):
mod = __builtin__.__import__(name, globals, locals, fromlist)
if mod:
mod.open = dummyOpen
mod.urllib2 = dummyUrllib2
return mod
# Options
# ~~~~~~~
# Set to True if you want inline CSS styles instead of classes
INLINESTYLES = False
from pygments.formatters import HtmlFormatter
# The default formatter
DEFAULT = HtmlFormatter(noclasses=INLINESTYLES)
# Add name -> formatter pairs for every variant you want to use
VARIANTS = {
# 'linenos': HtmlFormatter(noclasses=INLINESTYLES, linenos=True),
}
from docutils import nodes
from docutils.parsers.rst import directives, Directive
from pygments import highlight
from pygments.lexers import get_lexer_by_name, TextLexer
class Pygments(Directive):
""" Source code syntax hightlighting.
"""
required_arguments = 0
optional_arguments = 1
final_argument_whitespace = True
option_spec = dict([(key, directives.flag) for key in VARIANTS])
has_content = True
def run(self):
self.assert_has_content()
if len(self.arguments):
try:
lexer = get_lexer_by_name(self.arguments[0])
except ValueError:
# no lexer found - use the text one instead of an exception
lexer = TextLexer()
else:
lexer = get_lexer_by_name('python')
# take an arbitrary option if more than one is given
formatter = self.options and VARIANTS[self.options.keys()[0]] or DEFAULT
parsed = highlight(u'\n'.join(self.content), lexer, formatter)
return [nodes.raw('', parsed, format='html')]
directives.register_directive('sourcecode', Pygments)
# Go through and change all docutils modules to use a dummyOpen and dummyUrllib2
# module. Also make sure that any docutils imported modules also get the dummy
# implementations.
for i in sys.modules.keys():
if i.startswith('docutils') and sys.modules[i]:
sys.modules[i].open = dummyOpen
sys.modules[i].urllib2 = dummyUrllib2
sys.modules[i].__import__ = safe_import
# --- End of dummy-code --------------------------------------------------------
def html_escape_unicode(node):
# Find Python function that does this for me. string.encode('ascii',
# 'xmlcharrefreplace') only 2.3 and above.
for i in node:
if ord(i) > 127:
node = node.replace(i, '&#%d;' % (ord(i)))
return node
class MoinWriter(html4css1.Writer):
config_section = 'MoinMoin writer'
config_section_dependencies = ('writers', )
#"""Final translated form of `document`."""
output = None
def wiki_resolver(self, node):
"""
Normally an unknown reference would be an error in an reST document.
However, this is how new documents are created in the wiki. This
passes on unknown references to eventually be handled by
MoinMoin.
"""
if hasattr(node, 'indirect_reference_name'):
node['refuri'] = node.indirect_reference_name
elif (len(node['ids']) != 0):
# If the node has an id then it's probably an internal link. Let
# docutils generate an error.
return False
elif node.hasattr('name'):
node['refuri'] = node['name']
else:
node['refuri'] = node['refname']
del node['refname']
node.resolved = 1
self.nodes.append(node)
return True
wiki_resolver.priority = 1
def __init__(self, formatter, request):
html4css1.Writer.__init__(self)
self.formatter = formatter
self.request = request
# Add our wiki unknown_reference_resolver to our list of functions to
# run when a target isn't found
self.unknown_reference_resolvers = [self.wiki_resolver]
# We create a new parser to process MoinMoin wiki style links in the
# reST.
self.wikiparser = WikiParser('', self.request)
self.wikiparser.formatter = self.formatter
self.wikiparser.hilite_re = None
self.nodes = []
# Make sure it's a supported docutils version.
required_version = (0, 3, 10)
current_version = tuple([int(i) for i in (docutils.__version__.split('.') + ['0', '0'])[:3]])
if current_version < required_version:
err = 'ERROR: The installed docutils version is %s;' % ('.'.join([str(i) for i in current_version]))
err += ' version %s or later is required.' % ('.'.join([str(i) for i in required_version]))
raise RuntimeError, err
def translate(self):
visitor = MoinTranslator(self.document,
self.formatter,
self.request,
self.wikiparser,
self)
self.document.walkabout(visitor)
self.visitor = visitor
# Docutils 0.5 and later require the writer to have the visitor
# attributes.
if (hasattr(html4css1.Writer, 'visitor_attributes')):
for attr in html4css1.Writer.visitor_attributes:
setattr(self, attr, getattr(visitor, attr))
self.output = html_escape_unicode(visitor.astext())
# mark quickhelp as translatable
_ = lambda x: x
class Parser:
caching = 1
Dependencies = Dependencies # copy dependencies from module-scope
extensions = ['.rst', '.rest', ]
quickhelp = _("""\
{{{
Emphasis: *italic* **bold** ``monospace``
Headings: Heading 1 Heading 2 Heading 3
========= --------- ~~~~~~~~~
.. sourcecode::
def syntax(self, highlighted, code)
Horizontal rule: ----
Links: TrailingUnderscore_ `multi word with backticks`_ external_
.. _external: http://external-site.example.org/foo/
Lists: * bullets; 1., a. numbered items.
}}}
(!) For more help, see the
[[http://docutils.sourceforge.net/docs/user/rst/quickref.html|reStructuredText Quick Reference]].
""")
def __init__(self, raw, request, **kw):
self.raw = raw
self.request = request
self.form = request.form
def format(self, formatter):
# Create our simple parser
parser = MoinDirectives(self.request)
parts = publish_parts(
source=self.raw,
writer=MoinWriter(formatter, self.request),
settings_overrides={
'halt_level': 5,
'traceback': True,
'file_insertion_enabled': 0,
'raw_enabled': 0,
'stylesheet_path': '',
'template': '',
}
)
html = []
if parts['title']:
# Document title.
html.append(formatter.rawHTML('<h1>%s</h1>' % parts['title']))
# If there is only one subtitle it is propagated by Docutils
# to a document subtitle and is held in parts['subtitle'].
# However, if there is more than one subtitle then this is
# empty and fragment contains all of the subtitles.
if parts['subtitle']:
html.append(formatter.rawHTML('<h2>%s</h2>' % parts['subtitle']))
if parts['docinfo']:
html.append(parts['docinfo'])
html.append(parts['fragment'])
self.request.write(html_escape_unicode('\n'.join(html)))
class RawHTMLList(list):
"""
RawHTMLList catches all html appended to internal HTMLTranslator lists.
It passes the HTML through the MoinMoin rawHTML formatter to strip
markup when necessary. This is to support other formatting outputs
(such as ?action=show&mimetype=text/plain).
"""
def __init__(self, formatter):
self.formatter = formatter
def append(self, text):
f = sys._getframe()
if f.f_back.f_code.co_filename.endswith('html4css1.py'):
if isinstance(text, (str, unicode)):
text = self.formatter.rawHTML(text)
list.append(self, text)
class MoinTranslator(html4css1.HTMLTranslator):
def __init__(self, document, formatter, request, parser, writer):
html4css1.HTMLTranslator.__init__(self, document)
self.formatter = formatter
self.request = request
# Using our own writer when needed. Save the old one to restore
# after the page has been processed by the html4css1 parser.
self.original_write, self.request.write = self.request.write, self.capture_wiki_formatting
self.wikiparser = parser
self.wikiparser.request = request
# MoinMoin likes to start the initial headers at level 3 and the title
# gets level 2, so to comply with their styles, we do here also.
# TODO: Could this be fixed by passing this value in settings_overrides?
self.initial_header_level = 3
# Temporary place for wiki returned markup. This will be filled when
# replacing the default writer with the capture_wiki_formatting
# function (see visit_image for an example).
self.wiki_text = ''
self.setup_wiki_handlers()
self.setup_admonitions_handlers()
# Make all internal lists RawHTMLLists, see RawHTMLList class
# comment for more information.
for i in self.__dict__:
if isinstance(getattr(self, i), list):
setattr(self, i, RawHTMLList(formatter))
def depart_docinfo(self, node):
"""
depart_docinfo assigns a new list to self.body, we need to re-make that
into a RawHTMLList.
"""
html4css1.HTMLTranslator.depart_docinfo(self, node)
self.body = RawHTMLList(self.formatter)
def capture_wiki_formatting(self, text):
"""
Captures MoinMoin generated markup to the instance variable
wiki_text.
"""
# For some reason getting empty strings here which of course overwrites
# what we really want (this is called multiple times per MoinMoin
# format call, which I don't understand).
self.wiki_text += text
def process_wiki_text(self, text):
"""
This sequence is repeated numerous times, so its captured as a
single call here. Its important that wiki_text is blanked before we
make the format call. format will call request.write which we've
hooked to capture_wiki_formatting. If wiki_text is not blanked
before a call to request.write we will get the old markup as well as
the newly generated markup.
TODO: Could implement this as a list so that it acts as a stack. I
don't like having to remember to blank wiki_text.
"""
self.wiki_text = ''
self.wikiparser.raw = text
self.wikiparser.format(self.formatter)
def add_wiki_markup(self):
"""
Place holder in case this becomes more elaborate someday. For now it
only appends the MoinMoin generated markup to the html body and
raises SkipNode.
"""
self.body.append(self.wiki_text)
self.wiki_text = ''
raise docutils.nodes.SkipNode
def astext(self):
self.request.write = self.original_write
return html4css1.HTMLTranslator.astext(self)
def fixup_wiki_formatting(self, text):
replacement = {'\n': '', '> ': '>'}
for src, dst in replacement.items():
text = text.replace(src, dst)
# Fixup extraneous markup
# Removes any empty span tags
text = re.sub(r'\s*<\s*span.*?>\s*<\s*/\s*span\s*>', '', text)
# Removes the first paragraph tag
text = re.sub(r'^\s*<\s*p[^>]*?>', '', text)
# Removes the ending paragraph close tag and any remaining whitespace
text = re.sub(r'<\s*/\s*p\s*>\s*$', '', text)
return text
def visit_reference(self, node):
"""
Pass links to MoinMoin to get the correct wiki space url. Extract
the url and pass it on to the html4css1 writer to handle. Inline
images are also handled by visit_image. Not sure what the "drawing:"
link scheme is used for, so for now it is handled here.
Also included here is a hack to allow MoinMoin macros. This routine
checks for a link which starts with "<<". This link is passed to the
MoinMoin formatter and the resulting markup is inserted into the
document in the place of the original link reference.
"""
if 'refuri' in node.attributes:
refuri = node['refuri']
prefix = ''
link = refuri
if ':' in refuri:
prefix, link = refuri.lstrip().split(':', 1)
# First see if MoinMoin should handle completely. Exits through add_wiki_markup.
if refuri.startswith('<<') and refuri.endswith('>>'): # moin macro
self.process_wiki_text(refuri)
self.wiki_text = self.fixup_wiki_formatting(self.wiki_text)
self.add_wiki_markup()
if prefix == 'drawing':
self.process_wiki_text("[[%s]]" % refuri)
self.wiki_text = self.fixup_wiki_formatting(self.wiki_text)
self.add_wiki_markup()
# From here down, all links are handled by docutils (except
# missing attachments), just fixup node['refuri'].
if prefix == 'attachment':
if not AttachFile.exists(self.request, self.request.page.page_name, link):
# Attachment doesn't exist, give to MoinMoin to insert upload text.
self.process_wiki_text("[[%s]]" % refuri)
self.wiki_text = self.fixup_wiki_formatting(self.wiki_text)
self.add_wiki_markup()
# Attachment exists, just get a link to it.
node['refuri'] = AttachFile.getAttachUrl(self.request.page.page_name, link, self.request)
if not [i for i in node.children if i.__class__ == docutils.nodes.image]:
node['classes'].append(prefix)
elif prefix == 'wiki':
wiki_name, page_name = wikiutil.split_interwiki(link)
wikitag, wikiurl, wikitail, err = wikiutil.resolve_interwiki(self.request, wiki_name, page_name)
wikiurl = wikiutil.mapURL(self.request, wikiurl)
node['refuri'] = wikiutil.join_wiki(wikiurl, wikitail)
# Only add additional class information if the reference does
# not have a child image (don't want to add additional markup
# for images with targets).
if not [i for i in node.children if i.__class__ == docutils.nodes.image]:
node['classes'].append('interwiki')
elif prefix != '':
# Some link scheme (http, file, https, mailto, etc.), add class
# information if the reference doesn't have a child image (don't
# want additional markup for images with targets).
# Don't touch the refuri.
if not [i for i in node.children if i.__class__ == docutils.nodes.image]:
node['classes'].append(prefix)
else:
# Default case - make a link to a wiki page.
pagename, anchor = wikiutil.split_anchor(refuri)
page = Page(self.request, wikiutil.AbsPageName(self.formatter.page.page_name, pagename))
node['refuri'] = page.url(self.request, anchor=anchor)
if not page.exists():
node['classes'].append('nonexistent')
html4css1.HTMLTranslator.visit_reference(self, node)
def visit_image(self, node):
"""
Need to intervene in the case of inline images. We need MoinMoin to
give us the actual src line to the image and then we can feed this
to the default html4css1 writer. NOTE: Since the writer can't "open"
this image the scale attribute doesn't work without directly
specifying the height or width (or both).
TODO: Need to handle figures similarly.
"""
uri = node['uri'].lstrip()
prefix = '' # assume no prefix
attach_name = uri
if ':' in uri:
prefix = uri.split(':', 1)[0]
attach_name = uri.split(':', 1)[1]
# if prefix isn't URL, try to display in page
if not prefix.lower() in ('file', 'http', 'https', 'ftp'):
if not AttachFile.exists(self.request, self.request.page.page_name, attach_name):
# Attachment doesn't exist, MoinMoin should process it
if prefix == '':
prefix = 'attachment:'
self.process_wiki_text("{{%s%s}}" % (prefix, attach_name))
self.wiki_text = self.fixup_wiki_formatting(self.wiki_text)
self.add_wiki_markup()
# Attachment exists, get a link to it.
# create the url
node['uri'] = AttachFile.getAttachUrl(self.request.page.page_name, attach_name, self.request, addts=1)
if not node.hasattr('alt'):
node['alt'] = node.get('name', uri)
html4css1.HTMLTranslator.visit_image(self, node)
def create_wiki_functor(self, moin_func):
moin_callable = getattr(self.formatter, moin_func)
def visit_func(self, node):
self.wiki_text = ''
self.request.write(moin_callable(1))
self.body.append(self.wiki_text)
def depart_func(self, node):
self.wiki_text = ''
self.request.write(moin_callable(0))
self.body.append(self.wiki_text)
return visit_func, depart_func
def setup_wiki_handlers(self):
"""
Have the MoinMoin formatter handle markup when it makes sense. These
are portions of the document that do not contain reST specific
markup. This allows these portions of the document to look
consistent with other wiki pages.
Setup dispatch routines to handle basic document markup. The
hanlders dict is the html4css1 handler name followed by the wiki
handler name.
"""
handlers = {
# Text Markup
'emphasis': 'emphasis',
'strong': 'strong',
'literal': 'code',
# Blocks
'literal_block': 'preformatted',
# Simple Lists
# bullet-lists are handled completely by docutils because it uses
# the node context to decide when to make a compact list
# (no <p> tags).
'list_item': 'listitem',
# Definition List
'definition_list': 'definition_list',
}
for rest_func, moin_func in handlers.items():
visit_func, depart_func = self.create_wiki_functor(moin_func)
visit_func = new.instancemethod(visit_func, self, MoinTranslator)
depart_func = new.instancemethod(depart_func, self, MoinTranslator)
setattr(self, 'visit_%s' % (rest_func), visit_func)
setattr(self, 'depart_%s' % (rest_func), depart_func)
# Enumerated list takes an extra paramter so we handle this differently
def visit_enumerated_list(self, node):
self.wiki_text = ''
self.request.write(self.formatter.number_list(1, start=node.get('start', None)))
self.body.append(self.wiki_text)
def depart_enumerated_list(self, node):
self.wiki_text = ''
self.request.write(self.formatter.number_list(0))
self.body.append(self.wiki_text)
# Admonitions are handled here -=- tmacam
def create_admonition_functor(self, admotion_class):
def visit_func(self, node):
self.wiki_text = ''
self.request.write(self.formatter.div(1,
attr={'class': admotion_class},
allowed_attrs=[]))
self.body.append(self.wiki_text)
def depart_func(self, node):
self.wiki_text = ''
self.request.write(self.formatter.div(0))
self.body.append(self.wiki_text)
return visit_func, depart_func
def setup_admonitions_handlers(self):
"""
Admonitions are handled here... We basically surround admonitions
in a div with class admonition_{name of the admonition}.
"""
handled_admonitions = [
'attention',
'caution',
'danger',
'error',
'hint',
'important',
'note',
'tip',
'warning',
]
for adm in handled_admonitions:
visit_func, depart_func = self.create_admonition_functor(adm)
visit_func = new.instancemethod(visit_func, self, MoinTranslator)
depart_func = new.instancemethod(depart_func, self, MoinTranslator)
setattr(self, 'visit_%s' % (adm), visit_func)
setattr(self, 'depart_%s' % (adm), depart_func)
class MoinDirectives:
"""
Class to handle all custom directive handling. This code is called as
part of the parsing stage.
"""
def __init__(self, request):
self.request = request
# include MoinMoin pages
directives.register_directive('include', self.include)
# used for MoinMoin macros
directives.register_directive('macro', self.macro)
# disallow a few directives in order to prevent XSS
# for directive in ('meta', 'include', 'raw'):
for directive in ('meta', 'raw'):
directives.register_directive(directive, None)
# disable the raw role
roles._roles['raw'] = None
# As a quick fix for infinite includes we only allow a fixed number of
# includes per page
self.num_includes = 0
self.max_includes = 10
# Handle the include directive rather than letting the default docutils
# parser handle it. This allows the inclusion of MoinMoin pages instead of
# something from the filesystem.
def include(self, name, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):
# content contains the included file name
_ = self.request.getText
# Limit the number of documents that can be included
if self.num_includes < self.max_includes:
self.num_includes += 1
else:
lines = [_("**Maximum number of allowed includes exceeded**")]
state_machine.insert_input(lines, 'MoinDirectives')
return
if len(content):
pagename = content[0]
page = Page(page_name=pagename, request=self.request)
if not self.request.user.may.read(pagename):
lines = [_("**You are not allowed to read the page: %s**") % (pagename, )]
else:
if page.exists():
text = page.get_raw_body()
lines = text.split('\n')
# Remove the "#format rst" line
if lines[0].startswith("#format"):
del lines[0]
else:
lines = [_("**Could not find the referenced page: %s**") % (pagename, )]
# Insert the text from the included document and then continue parsing
state_machine.insert_input(lines, 'MoinDirectives')
return
include.has_content = include.content = True
include.option_spec = {}
include.required_arguments = 1
include.optional_arguments = 0
# Add additional macro directive.
# This allows MoinMoin macros to be used either by using the directive
# directly or by using the substitution syntax. Much cleaner than using the
# reference hack (`<<SomeMacro>>`_). This however simply adds a node to the
# document tree which is a reference, but through a much better user
# interface.
def macro(self, name, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):
# content contains macro to be called
if len(content):
# Allow either with or without brackets
if content[0].startswith('<<'):
macro = content[0]
else:
macro = '<<%s>>' % content[0]
ref = reference(macro, refuri=macro)
ref['name'] = macro
return [ref]
return
macro.has_content = macro.content = True
macro.option_spec = {}
macro.required_arguments = 1
macro.optional_arguments = 0
if ErrorParser: # fixup in case of missing docutils
Parser = ErrorParser
del _
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment