Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[r3398]: projects / ToolBox2 / trunk / toolbox / controllers / wikidoki.py Maximize Restore History

Download this file

wikidoki.py    161 lines (145 with data), 4.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#coding=utf-8
"""WikiDoki gadget"""
from tg import expose, TurboGearsController
from pylons.helpers import redirect_to
from pylons import config
import pkg_resources
import re
import os
from mopowg import mopowg
class DocFactory(object):
""""""
def __init__(self):
self.boot_strap()
def boot_strap(self):
"""
a bootstraper
if no docs folder::
1. create docs folder
2. create default index.txt
3. create default css style
"""
self.path = os.getcwd()
self.doc_root = os.path.join(self.path, 'docs')
self.frontpage = os.path.join(self.doc_root,'index.txt')
if not os.path.isdir(self.doc_root):
print "create docs folder"
try:
os.mkdir(self.doc_root)
except OSError, e:
print e
return
if not os.path.isfile(self.frontpage):
try:
df = open(self.frontpage,'w')
df.write("Document\n=========\nWrite docs for your project in rst format")
df.close()
print "create index.txt in docs folder"
except OSError, e:
print e
return
#write css
mopowg.css_writer(self.doc_root, style=mopowg.default_style)
return
def filelist(self):
"""
scan files and return the file list
"""
files = mopowg.scanner(self.doc_root)
records = [record for record in files if os.path.splitext(record)[1] in ['.txt']]
return records
def get_by(self, pagename):
"""get doc path"""
RST_EXT = 'txt'
if os.path.splitext(pagename)[1]==".html":
pagename = os.path.splitext(pagename)[0]
filename = '%s.%s'% (pagename, RST_EXT)
self.pagep = os.path.join(self.doc_root, filename)
return self.pagep
def get_data(self, path):
"""
get content
"""
print "get data"
gf = file(path, 'r')
self.data = gf.read()
#gf.close()
return self.data
def save_data(self, path, content):
output = os.path.split(path)[0]
print "saved to : %s"%output
mopowg.saver(path, content, output, nostyle=True, ext='.txt')
Page = DocFactory()
class WikiDoki(TurboGearsController):
"""Edit Docs as wiki in project
"""
__label__ ="Wiki Doki"
__version__ = "0.2"
__author__ = "Fred Lin"
__email__ = "gasolin+tg2@gmail.com"
__copyright__ = "Copyright 2007 Fred Lin"
__license__ = "MIT"
__group__ = "App"
__icon__ = "info.png"
need_project = True
@expose('genshi:toolbox.templates.wikidokilist')
def list(self):
pages = Page.filelist()
return dict(pages=pages)
@expose('genshi:toolbox.templates.wikidoki')
def index(self, pagename="index", **kw):
"""
new commer
1. find index.txt
2. render rst with wiki word
"""
try:
page = Page.get_by(pagename=pagename)
data = Page.get_data(page)
except:
raise redirect_to("edit", pagename = pagename)
content = mopowg.convertor(path=page, rich=True, wikiword=True,
wikipattern=r'<a href="/wikidoki/index?pagename=\1">\1</a>')
return dict(data=content, page=pagename)
@expose(template="genshi:toolbox.templates.wikidokiedit")
def edit(self, pagename, preview=0, **kw):
"""
edit and new page:
1.click wiki word::
2. find WIkiWord.txt
if find:
render it
else:
show empty form
"""
#FIXME: enable preview function
if preview=='1':
print "get temp: %s"%self.temp_content
content = self.temp_content
else:
page = Page.get_by(pagename=pagename)
if os.path.exists(page):
content = Page.get_data(page)
else:
content = ""
return dict(data = content, page=pagename)
@expose()
def save(self, pagename, data, submit=None, preview=None, **kw):
"""
while save:
create real file
"""
print "params: %s"%kw
page = Page.get_by(pagename=pagename)
if preview:
preview = True
self.temp_content = data
raise redirect_to("/wikidoki/edit?pagename=%s&preview=1"%str(pagename))
if submit:
preview = False
Page.save_data(page, data)
#flash("Changes saved!")
raise redirect_to("/wikidoki/index?pagename=%s"%str(pagename))
@expose()
def default(self, pagename, **kw):
return self.index(pagename)