# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-__version__ = '$Revision: 1.63+gerry+bernie $'[11:-2];
+__version__ = '$Revision: 1.63+gerry+bernie $'[11:-2]
import cgi, sys, string, os, re, errno, time, stat
from os import path, environ
# CGI stuff ---------------------------------------------------------
-def get_scriptname():
+def script_name():
return environ.get('SCRIPT_NAME', '')
+def privileged_path():
+ return privileged_url or script_name()
+
def remote_user():
- return environ.get('REMOTE_USER', 'AnonymousCoward')
+ user = environ.get('REMOTE_USER', '')
+ if user is None or user == '' or user == 'anonymous':
+ user = 'AnonymousCoward'
+ return user
def remote_host():
return environ.get('REMOTE_ADDR', '')
from socket import gethostbyaddr
return gethostbyaddr(addr)[0] + ' (' + addr + ')'
except:
- return addr;
+ return addr
# Formatting stuff --------------------------------------------------
print
def send_guru(msg, msg_type):
- if msg is None or len(msg) == 0: return
+ if msg is None or msg == '': return
print '<pre id="guru" onclick="this.style.display = \'none\'" class="' + msg_type + '">'
if msg_type == 'error':
print ' Software Failure. Press left mouse button to continue.\n'
if msg_type == 'error':
print ' Guru Meditation #DEADBEEF.ABADC0DE'
print '</pre>'
- # FIXME: This simple JS code is harder to pass than ACID 3.0
+ # FIXME: This simple JS snippet is harder to pass than ACID 3.0
print """
<script language="JavaScript" type="text/javascript">
var guru = document.getElementById('guru');
}
</script>"""
-def send_title(name, text="Limbo", msg=None, msg_type = 'error'):
+def send_title(name, text="Limbo", msg=None, msg_type='error'):
global title_done
if title_done: return
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
"""
print "<head><title>%s: %s</title>" % (site_name, text)
- print ' <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'
+ print ' <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />'
if not name:
- print ' <meta name="robots" content="noindex,nofollow">'
+ print ' <meta name="robots" content="noindex,nofollow" />'
if css_url:
print ' <link rel="stylesheet" type="text/css" href="%s" />' % css_url
print '</head>'
# Body
- if name and allow_edit:
- print '<body ondblclick="location.href=\'?edit=' + name + '\'">'
+ if name and privileged_url is not None:
+ print '<body ondblclick="location.href=\'' + privileged_path() + '?edit=' + name + '\'">'
else:
print '<body>'
+ title_done = True
send_guru(msg, msg_type)
# Navbar
- print '<div class="navigator">'
- print ' <b>' + site_name + ': '
+ print '<div class="nav">'
+ print ' <b>' + site_name + ': ',
if name:
print link_tag('?fullsearch=' + name, text) + '</b> '
else:
print text + '</b> '
print ' | ' + link_tag('FrontPage', 'Front Page', 'navlink')
print ' | ' + link_tag('FindPage', 'Find Page', 'navlink')
- print ' | <a href="/wikigit/wiki.git" class="navlink">Recent Changes</a>'
+ print ' | <a href="' + history_url + '" class="navlink">Recent Changes</a>'
if name:
- print ' | <a href="/wikigit/wiki.git?a=history;f=' + name + '" class="navlink">Page History</a>'
+ print ' | <a href="' + history_url + '?a=history;f=' + name + '" class="navlink">Page History</a>'
print ' | ' + link_tag('?raw=' + name, 'Raw Text', 'navlink')
- if allow_edit:
- print ' | ' + link_tag('?edit=' + name, 'Edit Page', 'navlink')
+ if privileged_url is not None:
+ print ' | ' + link_tag('?edit=' + name, 'Edit Page', 'navlink', authentication=True)
else:
print ' | <i>Immutable Page</i>'
user = remote_user()
if user != 'AnonymousCoward':
- print ' | <i>logged in as <b>' + cgi.escape(user) + '</b></i>'
-
- print '</div>'
- title_done = True
+ print ' | <span class="login"><i>logged in as <b>' + cgi.escape(user) + '</b></i></span>'
+ print '<hr /></div>'
-def link_tag(params, text=None, ss_class=None):
+def link_tag(params, text=None, ss_class=None, authentication=False):
if text is None:
text = params # default
classattr = ''
- # Prevent crawlers from following links to generated pages
- # and links added by potential spammers
- if ss_class == 'external' or ss_class == 'navlink':
- classattr += 'rel="nofollow" '
if ss_class:
classattr += 'class="%s" ' % ss_class
- return '<a %shref="%s/%s">%s</a>' % (classattr, get_scriptname(),
- params, text)
+ # Prevent crawlers from following links to generated pages
+ # and links added by potential spammers
+ if ss_class == 'external' or ss_class == 'navlink':
+ classattr += 'rel="nofollow" '
+ if authentication:
+ path = privileged_path()
+ else:
+ path = script_name()
+ return '<a %shref="%s/%s">%s</a>' % (classattr, path, params, text)
# Search ---------------------------------------------------
Page(pagename).send_raw()
def do_savepage(pagename):
- if not allow_edit:
+ if privileged_url is None:
raise 'editing disallowed for ' + pagename
global form
def send_footer(name, mod_string=None):
- print '<div class="footer">'
+ if debug_cgi:
+ cgi.print_arguments()
+ cgi.print_form(cgi.FieldStorage())
+ cgi.print_environ()
+ print '<div id="footer"><hr />'
+ print '<p class="copyright">Powered by <a href="http://www.codewiz.org/wiki/GeekiGeeki">GeekiGeeki</a></p>'
if mod_string:
- print "last modified %s" % mod_string
+ print '<p class="modified">last modified %s</p>' % mod_string
print '</div></body></html>'
# ----------------------------------------------------------
# Macros
-def _macro_TitleSearch():
+def _macro_TitleSearch(*vargs):
return _macro_search("titlesearch")
-def _macro_FullSearch():
+def _macro_FullSearch(*vargs):
return _macro_search("fullsearch")
def _macro_search(type):
default = form["value"].value
else:
default = ''
- return """<form method="get"><input name="%s" size="30" value="%s"><input type="submit" value="Go"></form>""" % (type, default)
+ return """<form method="get"><input name="%s" size="30" value="%s" /><input type="submit" value="Search" /></form>""" % (type, default)
-def _macro_WordIndex():
+def _macro_WordIndex(*vargs):
s = make_index_key()
pages = list(page_list())
map = {}
return s
-def _macro_TitleIndex():
+def _macro_TitleIndex(*vargs):
s = make_index_key()
pages = list(page_list())
pages.sort()
self.is_em = self.is_b = 0
self.h_level = 0
self.list_indents = []
- self.in_pre = 0
- self.in_var = 0
+ self.in_pre = False
+ self.in_table = False
+ self.tr_cnt = 0
+ self.in_var = False
self.in_header = True
def _emph_repl(self, word):
if len(word) == 3:
self.is_b = not self.is_b
- return ['</b>', '<b>'][self.is_b]
+ return ['</strong>', '<strong>'][self.is_b]
else:
self.is_em = not self.is_em
return ['</em>', '<em>'][self.is_em]
else:
self.h_level = len(word) - 1
result = "<h%d>" % self.h_level
- return result;
+ return result
def _rule_repl(self, word):
s = self._undent()
return Page(word).link_to()
def _img_repl(self, word):
- return '<img border="0" src="%s/%s" />' % (get_scriptname(), word)
+ path = script_name() + '/' + word;
+ return '<a href="%s"><img border="0" src="%s" /></a>' % (path, path)
def _url_repl(self, word):
if img_re.match(word):
- return '<img border="0" src="%s" />' % word
+ return '<a href="%s"><img border="0" src="%s" /></a>' % (word, word)
else:
return '<a href="%s" rel="nofollow" class="external">%s</a>' % (word, word)
def _hurl_repl(self, word):
- m = re.compile("\[\[(\S+)\ (.+)\]\]").match(word)
- anchor = m.group(1)
- descr = m.group(2)
- if img_re.match(anchor):
- return '<img border="0" src="%s" alt="%s" />' % (anchor, descr)
- elif url_re.match(anchor):
- return '<a href="%s" rel="nofollow" class="external">%s</a>' % (anchor, descr)
- elif anchor.startswith('/'):
- return '<a href="%s">%s</a>' % (anchor, descr)
+ m = re.compile("\[\[(\S+)([^\]]*)\]\]").match(word)
+ name = m.group(1)
+ descr = m.group(2).strip() or name
+
+ macro = globals().get('_macro_' + name)
+ if macro:
+ return apply(macro, (name, descr))
+ elif img_re.match(name):
+ return '<a href="%s"><img border="0" src="%s" alt="%s" /></a>' % (name, name, descr)
+ elif url_re.match(name):
+ return '<a href="%s" rel="nofollow" class="external">%s</a>' % (name, descr)
+ elif name.startswith('/'):
+ return '<a href="%s">%s</a>' % (name, descr)
else:
- return link_tag(anchor, descr)
+ return link_tag(name, descr)
def _email_repl(self, word):
return '<a href="mailto:%s">%s</a>' % (word, word)
def _pre_repl(self, word):
if word == '{{{' and not self.in_pre:
- self.in_pre = 1
+ self.in_pre = True
return '<pre>'
elif self.in_pre:
- self.in_pre = 0
+ self.in_pre = False
return '</pre>'
- else:
- return ''
+ return ''
+
+ def _hi_repl(self, word):
+ if word == 'FIXME':
+ cl = 'error'
+ elif word == 'DONE':
+ cl = 'success'
+ elif word == 'TODO':
+ cl = 'notice'
+ return '<strong class="highlight ' + cl + '">' + word + '</strong>'
def _var_repl(self, word):
if word == '{{' and not self.in_var:
- self.in_var = 1
+ self.in_var = True
return '<code>'
elif self.in_var:
- self.in_var = 0
+ self.in_var = False
return '</code>'
- else:
- return ''
- def _macro_repl(self, word):
- macro_name = word[2:-2]
- # TODO: Somehow get the default value into the search field
- return apply(globals()['_macro_' + macro_name], ())
+ return ''
+
+ def _tr_repl(self, word):
+ out = ''
+ if not self.in_table:
+ self.in_table = True
+ self.tr_cnt = 0
+ out = '</p><table><tbody>\n'
+ self.tr_cnt += 1
+ return out + '<tr class="' + ['even', 'odd'][self.tr_cnt % 2] + '"><td>'
+
+ def _tre_repl(self, word):
+ if self.in_table:
+ return '</td></tr>'
+ return ''
+
+ def _td_repl(self, word):
+ if self.in_table:
+ return '</td><td>'
+ return ''
def _indent_level(self):
return len(self.list_indents) and self.list_indents[-1]
# For each line, we scan through looking for magic
# strings, outputting verbatim any intervening text
+ # TODO: highlight search words (look at referer)
scan_re = re.compile(
r"(?:"
+ # Formatting
+ r"(?P<emph>'{2,3})"
+ r"|(?P<tit>\={2,6})"
+ + r"|(?P<rule>^-{3,})"
+ r"|(?P<ent>[<>&])"
+ + r"|(?P<hi>\b(FIXME|TODO|DONE)\b)"
+
+ # Links
+ r"|(?P<img>\b[a-zA-Z0-9_-]+\.(png|gif|jpg|jpeg|bmp))"
+ r"|(?P<word>\b(?:[A-Z][a-z]+){2,}\b)"
- + r"|(?P<rule>^-{3,})"
- + r"|(?P<hurl>\[\[\S+\s+.+\]\])"
- + r"|(?P<url>(http|ftp|nntp|news|mailto)\:[^\s'\"]+\S)"
+ + r"|(?P<hurl>\[\[\S+[^\]]*\]\])"
+ + r"|(?P<url>(http|https|ftp|mailto)\:[^\s'\"]+\S)"
+ r"|(?P<email>[-\w._+]+\@[\w.-]+)"
+
+ # Lists, divs, spans
+ r"|(?P<li>^\s+\*)"
+ r"|(?P<pre>(\{\{\{|\s*\}\}\}))"
+ r"|(?P<var>(\{\{|\}\}))"
- + r"|(?P<macro>\[\[(TitleSearch|FullSearch|WordIndex|TitleIndex)\]\])"
+
+ # Tables
+ + r"|(?P<tr>^\s*\|\|\s*)"
+ + r"|(?P<tre>\s*\|\|\s*$)"
+ + r"|(?P<td>\s*\|\|\s*)"
+ r")")
pre_re = re.compile(
r"(?:"
+ r"(?P<pre>\s*\}\}\})"
+ r")")
- blank_re = re.compile("^\s*$")
- indent_re = re.compile("^\s*")
- eol_re = re.compile(r'\r?\n')
+ blank_re = re.compile(r"^\s*$")
+ indent_re = re.compile(r"^\s*")
+ tr_re = re.compile(r"^\s*\|\|")
+ eol_re = re.compile(r"\r?\n")
raw = string.expandtabs(self.raw)
for line in eol_re.split(raw):
# Skip ACLs
if line.startswith('#'):
continue
self.in_header = False
+
if self.in_pre:
print re.sub(pre_re, self.replace, line)
else:
- # XXX: Should we check these conditions in this order?
+ if self.in_table and not tr_re.match(line):
+ self.in_table = False
+ print '</tbody></table><p>'
+
if blank_re.match(line):
print '</p><p>'
- continue
- indent = indent_re.match(line)
- print self._indent_to(len(indent.group(0)))
- print re.sub(scan_re, self.replace, line)
+ else:
+ indent = indent_re.match(line)
+ print self._indent_to(len(indent.group(0)))
+ print re.sub(scan_re, self.replace, line)
+
if self.in_pre: print '</pre>'
+ if self.in_table: print '</tbody></table><p>'
print self._undent()
print "</p></div>"
+ ' for ' + cgi.escape(remote_user())
+ ' from ' + cgi.escape(get_hostname(remote_host()))
+ '</b></p>')
- print '<div class="editor"><form method="post" action="%s/%s">' % (get_scriptname(), self.page_name)
+ print '<div class="editor"><form method="post" action="%s/%s">' % (script_name(), self.page_name)
print '<input type="hidden" name="savepage" value="%s">' % (self.page_name)
print """<textarea wrap="virtual" id="editor" name="savetext" rows="17" cols="80">%s</textarea>""" % (preview or self.get_raw_body())
print """
print "<div class='preview'>"
PageFormatter(preview).print_html()
print "</div>"
+ send_footer(self.page_name)
def send_raw(self):
emit_header("text/plain")
emit_header(mime_type)
sys.stdout.write(data)
-# ---------------------------------------------------------------
+# Main ---------------------------------------------------------------
try:
# Configuration values
+ site_name = 'Codewiz'
+
+ # set to None for read-only sites
+ # leave empty ('') to allow anonymous edits
+ # otherwise, set to a URL that requires authentication
+ privileged_url = 'https://www.codewiz.org/~bernie/wiki'
+
data_dir = '/home/bernie/public_html/wiki'
text_dir = path.join(data_dir, 'text')
- allow_edit = True # Is it possible to edit pages?
- site_name = 'codewiz.org'
- changed_time_fmt = '[%I:%M %p] '
- date_fmt = '%a %d %b %Y'
+ css_url = '../wikidata/geekigeeki.css' # optional stylesheet link
+ history_url = '../wikigit/wiki.git'
+ post_edit_hook = './post_edit_hook.sh'
datetime_fmt = '%a %d %b %Y %I:%M %p'
+ allow_edit = True # Is it possible to edit pages?
show_hosts = True # show hostnames?
- css_url = '../wikidata/geekigeeki.css' # optional stylesheet link
nonexist_qm = False # show '?' for nonexistent?
- post_edit_hook = './post_edit_hook.sh'
+ debug_cgi = False # Set to True for CGI var dump
form = cgi.FieldStorage()
if word_re.match(query):
Page(query).send_page()
elif img_re.match(query):
+ #FIXME: use correct mime type
send_verbatim(query, 'image/jpeg')
else:
send_verbatim(query)