Copy Wiki functionality from HelloForge into ForgeWiki

Wolf Wolf 2009-12-09

added ForgeWiki/forgewiki/static/Wiki/css/hilite.css
added ForgeWiki/forgewiki/templates/page_history.html
added ForgeWiki/forgewiki/templates/page_edit.html
added ForgeWiki/forgewiki/templates/page_diff.html
changed ForgeWiki/forgewiki/model/__init__.py
changed ForgeWiki/forgewiki/templates/index.html
changed ForgeWiki/forgewiki/templates/lib.html
changed ForgeWiki/forgewiki/main.py
copied ForgeWiki/forgewiki/model/artifacts.py -> ForgeWiki/forgewiki/model/wiki.py
copied ForgeWiki/forgewiki/templates/artifact.html -> ForgeWiki/forgewiki/templates/page_view.html
ForgeWiki/forgewiki/static/Wiki/css/hilite.css Diff Switch to side-by-side view
Loading...
ForgeWiki/forgewiki/templates/page_history.html Diff Switch to side-by-side view
Loading...
ForgeWiki/forgewiki/templates/page_edit.html Diff Switch to side-by-side view
Loading...
ForgeWiki/forgewiki/templates/page_diff.html Diff Switch to side-by-side view
Loading...
ForgeWiki/forgewiki/model/__init__.py Diff Switch to side-by-side view
Loading...
ForgeWiki/forgewiki/templates/index.html Diff Switch to side-by-side view
Loading...
ForgeWiki/forgewiki/templates/lib.html Diff Switch to side-by-side view
Loading...
ForgeWiki/forgewiki/main.py Diff Switch to side-by-side view
Loading...
ForgeWiki/forgewiki/model/artifacts.py to ForgeWiki/forgewiki/model/wiki.py
--- a/ForgeWiki/forgewiki/model/artifacts.py
+++ b/ForgeWiki/forgewiki/model/wiki.py
@@ -1,19 +1,37 @@
 from time import sleep
 
-from pylons import c
+from pylons import g #g is a namespace for globally accessable app helpers
+from pylons import c as context
+import re
+
 from pymongo.errors import OperationFailure
 
-from ming import Field, schema
+from ming import schema
+from ming import Field
+
 from pyforge.model import VersionedArtifact, Snapshot, Message
 
-class MyArtifactHistory(Snapshot):
+wikiwords = [
+    (r'\b([A-Z]\w+[A-Z]+\w+)', r'<a href="../\1/">\1</a>'),
+    ]
+
+wikiwords = [
+    (re.compile(pattern), replacement)
+    for pattern, replacement in wikiwords ]
+
+def to_html(text):
+    content = g.markdown.convert(text)
+    for pattern, replacement in wikiwords:
+        content = pattern.sub(replacement, content)
+    return content
+
+class PageHistory(Snapshot):
     class __mongometa__:
-        name='my_artifact_history'
-    type_s='MyArtifact Snapshot'
+        name='page_history'
 
     def original(self):
-        return MyArtifact.m.get(_id=self.artifact_id)
-
+        return Page.m.get(_id=self.artifact_id)
+        
     def shorthand_id(self):
         return '%s#%s' % (self.original().shorthand_id(), self.version)
 
@@ -24,64 +42,103 @@
         result = Snapshot.index(self)
         result.update(
             title_s='Version %d of %s' % (
-                self.version, self.original().shorthand_id()),
-            type_s=self.type_s,
+                self.version,self.original().title),
+            type_s='WikiPage Snapshot',
             text=self.data.text)
         return result
 
-class MyArtifact(VersionedArtifact):
+class Page(VersionedArtifact):
     class __mongometa__:
-        name='my_artifact'
-        history_class = MyArtifactHistory
-    type_s = 'MyArtifact'
+        name='page'
+        history_class = PageHistory
 
-    text = Field(str, if_missing='')
-        
+    title=Field(str)
+    text=Field(schema.String, if_missing='')
+
     def url(self):
-        return c.app.script_name + '/' + self._id.url_encode() + '/'
-    
+        return context.app.script_name + '/' + self.title + '/'
+
     def shorthand_id(self):
-        return '%s/%s' % (self.type_s, self._id.url_encode())
+        return self.title
 
     def index(self):
         result = VersionedArtifact.index(self)
-        result.update(type_s=self.type_s, text=self.text)
+        result.update(
+            title_s=self.title,
+            version_i=self.version,
+            type_s='WikiPage',
+            text=self.text)
         return result
+    
+    @classmethod
+    def upsert(cls, title, version=None):
+        """Update page with `title` or insert new page with that name"""
+        if version is None:
+            q = dict(
+                project_id=context.project._id,
+                title=title)
+            #Check for existing page object    
+            obj = cls.m.get(
+                app_config_id=context.app.config._id,
+                title=title)
+            if obj is None:
+                obj = cls.make(dict(
+                        title=title,
+                        app_config_id=context.app.config._id,
+                        ))
+            new_obj = dict(obj, version=obj.version + 1)
+            return cls.make(new_obj)
+        else:
+            pg = cls.upsert(title)
+            HC = cls.__mongometa__.history_class
+            ss = HC.m.find({'artifact_id':pg._id, 'version':int(version)}).one()
+            new_obj = dict(ss.data, version=version+1)
+            return cls.make(new_obj)
 
-    def root_comments(self):
-        return MyArtifactComment.m.find(dict(artifact_id=self._id, parent_id=None))
+    @property
+    def html_text(self):
+        """A markdown processed version of the page text"""
+        return to_html(self.text)
+
     def reply(self):
         while True:
             try:
-                c = MyArtifactComment.make(dict(artifact_id=self._id))
-                c.m.insert()
+                c = Comment.make(dict(page_id=self._id))
+                context.m.insert()
                 return c
             except OperationFailure:
                 sleep(0.1)
                 continue
 
-class MyArtifactComment(Message):
+    def root_comments(self):
+        if '_id' in self:
+            return Comment.m.find(dict(page_id=self._id, parent_id=None))
+        else:
+            return []
+
+class Comment(Message):
     class __mongometa__:
-        name='my_artifact_comment'
-    type_s = 'MyArtifact Comment'
-
-    artifact_id=Field(schema.ObjectId)
+        name='comment'
+    page_id=Field(schema.ObjectId)
 
     def index(self):
         result = Message.index(self)
         author = self.author()
         result.update(
-            title_s='Comment on %s by %s' % (
-                self.artifact.shorthand_id(), author.display_name),
-            type_s=self.type_s)
+            title_s='Comment on page %s by %s' % (
+                self.page.title, author.display_name),
+            type_s='Comment on WikiPage',
+            page_title_t=self.page.title)
         return result
 
     @property
-    def artifact(self):
-        return MyArtifact.m.get(_id=self.artifact_id)
+    def page(self):
+        """The page this comment connects too"""
+        return Page.m.get(_id=self.page_id)
 
     def url(self):
-        return self.artifact.url() + '#comment-' + self._id
-                          
+        """The URL for the page for this comment"""
+        return self.page.url() + '#comment-' + self._id
+
     def shorthand_id(self):
-        return '%s-%s' % (self.artifact.shorthand_id, self._id)
+        return '%s-%s' % (self.page.title, self._id)
ForgeWiki/forgewiki/templates/artifact.html to ForgeWiki/forgewiki/templates/page_view.html
--- a/ForgeWiki/forgewiki/templates/artifact.html
+++ b/ForgeWiki/forgewiki/templates/page_view.html
@@ -6,20 +6,22 @@
   <xi:include href="${g.pyforge_templates}/master.html"/>
   <xi:include href="${c.app.templates}/lib.html" />
 
-  <?python from pprint import pformat ?>
-
   <head>
     <meta content="text/html; charset=UTF-8" http-equiv="content-type" py:replace="''"/>
-    <title>${artifact.shorthand_id()}</title>
+    <title>$page.title</title>
+    <link rel="stylesheet" type="text/css" 
+          href="${g.app_static('css/hilite.css')}"/>
     <style>
       .hidden { display: None }
     </style>
   </head>
   
   <body>
-    <h1>${artifact.shorthand_id()}</h1>
+    <h1>$page.title</h1>
     <div>
       <a href="..">PluginRoot</a>
+      <a href="edit">Edit Page</a>
+      <a href="history">Page History</a>
       <form style="display:inline; float:right"
             method="GET"
             action="../search">
@@ -33,7 +35,7 @@
     <a href=".">Latest</a>
     <hr/>
     <div>
-      <pre py:content="pformat(artifact)"/>
+      ${Markup(page.html_text)}
     </div>
 
     <hr/>
@@ -45,7 +47,7 @@
         <input type="submit"/>
       </form>
     </div>
-    <py:for each="cmt in artifact.root_comments()">
+    <py:for each="cmt in page.root_comments()">
       ${display_comment(cmt)}
     </py:for>
   </body>