X Tutup
""" Copyright 2006-2008 SpringSource (http://springsource.com), All Rights Reserved Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ import re intrawikiR = re.compile("\[\[(?P.*?)(\|(?P.*?))?\]\](?P[a-zA-Z0-9]*)") externalLinkR = re.compile("\[(?P.*?)\s(?P.*)\]") header1R = re.compile("=(?P
.*)=") header2R = re.compile("==(?P
.*)==") header3R = re.compile("===(?P
.*)===") header4R = re.compile("====(?P
.*)====") header5R = re.compile("=====(?P
.*)=====") class Page(object): def __init__(self, article, wikitext, controller): self.article = article self.wikitext = wikitext self.controller = controller def makeAList(self, wikitext): keyCharR = re.compile("(([*#]+)(.*))") tokens = wikitext.split("\n") bulletCount = 0 currentLevel = 0 tagStack = [] for i in range(1,len(tokens)): if tokens[i]: match = keyCharR.match(tokens[i]) if match: token = match.groups()[1] text = match.groups()[2] if len(token) > currentLevel: tokens[i] = "" for j in range(currentLevel, len(token)): if token[j] == "*": tagStack.append("") tokens[i] += "
    " elif token[j] == "#": tagStack.append("") tokens[i] += "
      " tokens[i] += "
    1. " + text + "
    2. " currentLevel = len(token) elif len(token) < currentLevel: tokens[i] = "" for j in range(len(token), currentLevel): tokens[i] += tagStack.pop() tokens[i] += "
    3. " + text + "
    4. " currentLevel = len(token) else: if token[-1] == "*": tokens[i] = "
    5. " + text + "
    6. " elif token[-1] == "#": tokens[i] = "
    7. " + text + "
    8. " else: if currentLevel > 0: tokens[i] = "" for j in range(0, currentLevel): tokens[i] += tagStack.pop() currentLevel = 0 return "\n".join(tokens) def intrawikiSubstitution(self, match): g = match.groupdict() if self.controller.exists(g["link"]): str = ' SpringWiki :: a Spring Python demonstration

      """ + self.article + """

      From Springwiki

      """ def footer(self, selected = "article"): """Standard footer used for all pages.""" footer = """
        """ if selected == "article" and self.article.split(":")[0] == "Talk": selected = "discussion" if selected == "article": if self.controller.exists(self.article.split(":")[-1]): footer += """
      • article
      • \n""" else: footer += """
      • article
      • \n""" if self.controller.exists("Talk:" + self.article.split(":")[-1]): footer += """
      • discussion
      • \n""" else: footer += """
      • discussion
      • \n""" footer += """
      • edit
      • \n""" if self.controller.exists(self.article.split(":")[-1]): footer += """
      • history
      • delete
      • """ elif selected == "discussion": footer += """
      • article
      • discussion
      • edit
      • """ if self.controller.exists("Talk:" + self.article.split(":")[-1]): footer += """
      • history
      • delete
      • """ elif selected == "edit": if self.controller.exists(self.article.split(":")[-1]): footer += """
      • article
      • \n""" else: footer += """
      • article
      • \n""" if self.controller.exists("Talk:" + self.article.split(":")[-1]): footer += """
      • discussion
      • \n""" else: footer += """
      • discussion
      • \n""" footer += """
      • edit
      • """ if self.controller.exists(self.article): footer += """
      • history
      • delete
      • """ elif selected == "history": footer += """
      • article
      • """ if self.controller.exists("Talk:" + self.article.split(":")[-1]): footer += """
      • discussion
      • \n""" else: footer += """
      • discussion
      • \n""" footer += """
      • edit
      • history
      • delete
      • """ elif selected == "delete": footer += """
      • article
      • """ if self.controller.exists("Talk:" + self.article.split(":")[-1]): footer += """
      • discussion
      • \n""" else: footer += """
      • discussion
      • \n""" footer += """
      • edit
      • history
      • delete
      • """ footer += """
      """ + self.icon() + """ """ + self.navigationHeader() + """
      """ return footer def icon(self): return """ """ def navigationHeader(self): """Left hand HTML""" sidebar = self.controller.getPage("Springwiki Sidebar") results = """ """ listStack = [] for eachLine in sidebar.wikitext.split("\n"): if len(eachLine) > 1 and eachLine[0:2] == "**": temp = re.compile("\[.*\]").findall(eachLine[2:])[0] wikiLink = re.compile("[\[]+(.*?)[\]]+").findall(temp)[0] if temp[0:2] == "[[": pipe = wikiLink.split("|")[-1] link = wikiLink.split("|")[0] results += """
    9. %s
    10. """ % (link, link, pipe) else: pipe = " ".join(wikiLink.split(" ")[1:]) link = wikiLink.split(" ")[0] results += """
    11. %s
    12. """ % (link, link, pipe) elif len(eachLine) > 0 and eachLine[0] == "*": target = eachLine[1:] if len(listStack) > 0: results += """
""" % listStack[-1] listStack.pop() results += """
%s
    """ % (target, target) listStack.append(target) if len(listStack) > 0: results += """
""" % listStack[-1] listStack.pop() return results def wikiToHtml(self): htmlText = self.wikitext htmlText = header5R.sub('
\g
', htmlText) htmlText = header4R.sub('

\g

', htmlText) htmlText = header3R.sub('

\g

', htmlText) htmlText = header2R.sub('

\g

', htmlText) htmlText = header1R.sub('

\g

', htmlText) htmlText = intrawikiR.sub(self.intrawikiSubstitution, htmlText) htmlText = externalLinkR.sub('\g', htmlText) htmlText = self.makeAList(htmlText) return htmlText def html(self): results = self.header() results += """ """ results += self.wikiToHtml() results += """ """ results += self.footer(selected="article") return results class EditPage(Page): def __init__(self, article, wikitext, controller): Page.__init__(self, article, wikitext, controller) def header(self): """Standard header used for all pages""" return """ SpringWiki :: a Spring Python demonstration

Editing """ + self.article + """

From Springwiki

""" def wikiToHtml(self): htmlText = """ """ htmlText += """

Summary:

Cancel
""" return htmlText def html(self): results = self.header() results += """ """ results += self.wikiToHtml() results += """ """ results += self.footer(selected="edit") return results class PreviewEditPage(Page): def __init__(self, article, wikitext, controller): Page.__init__(self, article, wikitext, controller) def header(self): """Standard header used for all pages""" return """ SpringWiki :: a Spring Python demonstration

Editing """ + self.article + """

From Springwiki

""" def html(self): results = self.header() results += '

Preview

' results += Page(self.article, self.wikitext, self.controller).wikiToHtml() results += '
' results += EditPage(self.article, self.wikitext, self.controller).wikiToHtml() results += self.footer() return results class Version(object): def __init__(self, article, versionNumber, wikitext, summary, date, editor): self.article = article self.versionNumber = versionNumber self.wikitext = wikitext self.summary = summary self.date = date self.editor = editor class HistoryPage(Page): def __init__(self, article, controller, history): Page.__init__(self, article, 'History', controller) self.history = [] for rev in history: self.history.append(Version(article=article, versionNumber=len(self.history), wikitext=rev[0], summary=rev[1], date=rev[2], editor=rev[3])) def header(self): """Standard header used for all pages""" return """ SpringWiki :: a Spring Python demonstration

""" + self.article + """

From Springwiki

Revision history
""" def generateHistory(self): print self.history results = """

Diff selection: mark the radio boxes of the versions to compare and hit enter or the button at the bottom.
Legend: (cur) = difference with current version, (last) = difference with preceding version, M = minor edit.

    """ if len(self.history) > 0: for rev in reversed(self.history): hiddenStyle = "" if rev == self.history[-1]: hiddenStyle = 'style="visibility:hidden"' userStyle = "" if self.controller.exists(rev.editor): userStyle = 'class="new"' try: editor = rev.editor.split(":")[1] except: editor = rev.editor results += """
  • %s %s (->%s)
  • """ % (rev.versionNumber, hiddenStyle, rev.versionNumber, rev.article, rev.versionNumber, rev.date, rev.editor, editor, userStyle, rev.summary) results += """
""" return results def html(self): results = self.header() results += """ """ results += self.generateHistory() results += """ """ results += self.footer(selected="history") return results class OldPage(Page): def __init__(self, article, wikitext, controller): Page.__init__(self, article, wikitext, controller) class NoPage(Page): def __init__(self, article, controller): Page.__init__(self, article, "This page does not yet exist.", controller) class DeletePage(Page): def __init__(self, article, controller): Page.__init__(self, article, "This page does not yet exist.", controller) def header(self): """Standard header used for all pages""" return """ SpringWiki :: a Spring Python demonstration

""" + self.article + """

From Springwiki

(Deleting "%s")
""" % self.article def html(self): results = self.header() results += """ """ results += """ Warning: The page you are about to delete has a history

You are about to permanently delete a page or image along with all of its history from the database. Please confirm that you intend to do this, that you understand the consequences.

 

Return to %s.

""" % (self.article, self.article, self.article, self.article) results += """ """ results += self.footer(selected="delete") return results class ActionCompletedPage(Page): def __init__(self, article, controller): Page.__init__(self, article, "This page does not yet exist.", controller) def html(self): results = self.header() results += """ """ results += """

"%s" has been deleted.

Return to Main Page.

""" % self.article results += """ """ results += self.footer(selected="article") return results
X Tutup