git » wikiri » commit 8d33180

Implement preview.

author Alberto Bertogli
2008-01-01 17:57:57 UTC
committer Alberto Bertogli
2008-01-01 17:57:57 UTC
parent 81d5200bdd29e1fce1aa6e17684da229867378c3

Implement preview.

Signed-off-by: Alberto Bertogli <albertito@gmail.com>

wikiri.cgi +26 -11

diff --git a/wikiri.cgi b/wikiri.cgi
index 0c31a1b..cf7fd3b 100755
--- a/wikiri.cgi
+++ b/wikiri.cgi
@@ -182,7 +182,8 @@ default_edit_page = """
 <input id="comment" type="text" name="comment"
   size="50" value=""/><p/>
 
-<input type="submit" value="Save"/>
+<button name="submit" type="submit" value="submit">Save</button>
+<button name="preview" type="submit" value="preview">Preview</button>
 </form>
 <p/>
 </div>
@@ -544,16 +545,15 @@ class Templates (object):
 #
 
 class Article (object):
-	def __init__(self, name, content = None):
+	def __init__(self, name, title = None, content = None):
 		self.name = name
 		self.qname = urllib.quote_plus(name, safe = "")
 		self.updated = None
 
 		self.loaded = False
 
-		self.preloaded_content = None
-		if content:
-			self.preloaded_content = content
+		self.preloaded_title = title
+		self.preloaded_content = content
 
 		# loaded on demand
 		self.attrs = {}
@@ -589,20 +589,24 @@ class Article (object):
 			raw = [ s + '\n' for s in raw.split('\n') ]
 			self.updated = datetime.datetime.now()
 
-		count = 0
+		hdr_lines = 0
 		for l in raw:
 			if ':' in l:
 				name, value = l.split(':', 1)
 				name = name.lower().strip()
 				self.attrs[name] = value.strip()
+				hdr_lines += 1
 
 			elif l == '\n':
 				# end of header
 				break
-			count += 1
-		self._raw_content = ''.join(raw[count + 1:])
+
+		self._raw_content = ''.join(raw[hdr_lines:])
 		self.loaded = True
 
+		if self.preloaded_title:
+			self.attrs['title'] = self.preloaded_title
+
 	def save(self, newtitle, newcontent, raw = False):
 		fd = open(data_path + '/' + self.qname, 'w+')
 		if raw:
@@ -832,10 +836,14 @@ def render_article(art):
 	print template.get_article_footer(art)
 	print template.get_main_footer()
 
-def render_edit(art):
+def render_edit(art, preview = False):
 	template = Templates()
 	print 'Content-type: text/html; charset=utf-8\n'
 	print template.get_main_header()
+	if preview:
+		print template.get_article_header(art)
+		print art.to_html()
+		print template.get_article_footer(art)
 	print template.get_edit_page(art)
 	print template.get_main_footer()
 
@@ -886,6 +894,7 @@ def handle_cgi():
 
 	newcontent = form.getfirst("newcontent", '')
 	newtitle = form.getfirst("newtitle", '').strip()
+	preview = form.getfirst("preview", '').strip()
 	comment = form.getfirst("comment", 'No comment').strip()
 	if not comment:
 		comment = "No comment"
@@ -918,8 +927,14 @@ def handle_cgi():
 		render_help()
 	elif artname == 'about':
 		render_about()
-	elif edit:
-		render_edit(Article(artname))
+	elif edit or (save and preview):
+		if preview:
+			art = Article(artname,
+				title = newtitle,
+				content = newcontent)
+			render_edit(art, preview = True)
+		else:
+			render_edit(Article(artname))
 	elif save:
 		h = History()
 		a = Article(artname)