Comments, anyone?

This commit is contained in:
Morten Delenk 2016-06-12 18:04:01 +02:00
parent 32ec77da50
commit 6947f2a5c7
25 changed files with 58 additions and 114 deletions

Binary file not shown.

43
comments.py Executable file
View file

@ -0,0 +1,43 @@
#!/usr/bin/env python3
from htmlgen import *
import storage
import cgi
import random
import base64
from io import BytesIO
from captcha.image import ImageCaptcha
import time
print("Content-type: text/html\r\n\r\n")
aid=form["aid"]
form=cgi.FieldStorage()
try:
seed=form["seed"]
random.seed(seed)
checkstr=list("".join(random.choice(string.digits+string.ascii_lowercase) for _ in range(5)))
if not checkstr == form["checkstr"]:
print("Captcha's wrong")
raise Exception()
username=form["username"]
message=form["message"]
timestamp=int(time.time())
data={"name":"","markdown":message,"author":username,"date":timestamp}
storage.append("comments-%i"%aid,data)
except:
pass
seed=random.SystemRandom().randint(0,2**24)
random.seed(seed)
checkstr=list("".join(random.choice(string.digits+string.ascii_lowercase) for _ in range(5)))
image = ImageCaptcha()
capt = image.generate(checkstr)
html=htmlgen.HTMLgen(pagelayout.getLayoutXML().decode('utf-8'),"Comments")
html.appendHTML("<form action=\"comments.py?aid=%i\" method=\"POST\""%aid)
html.appendHTML("<input type=\"hidden\" name=\"seed\" value=\"%i\" />"%seed)
html.appendHTML("<input placeholder=\"Username\" name=\"username\" /><br />")
html.appendHTML("<textarea name=\"message\" placeholder=\"Compose your message. Markdown is enabled.\" /><br />")
html.appendHTML("<img src=\"data:image/png;base64,%s alt=\"Captcha image\" />" % base64.b64encode(capt.getvalue()))
html.appendHTML("<input placeholder=\"Captcha. lowercase only. case sensitive\" name=\"checkstr\" />")
html.appendHTML("<input type=\"submit\" /></form>")
count=storage.count("comments-%i"%aid)
for i in range(count):
html.addArticle(**(storage.get("comments-%i"%aid,i)))

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -1,6 +1,7 @@
import cgitb
import markdown2
import datetime
import storage
cgitb.enable()
class HTMLgen:
def __init__(self, layout, title):
@ -10,21 +11,27 @@ class HTMLgen:
self.authors=[]
self.dates=[]
self.title=title
self.asideHTML=""
def addArticle(self, name, markdown, author="darklink", date=0):
self.articles.append(markdown2.markdown(markdown, extras=["tables","spoiler"]))
self.titles.append(name)
self.authors.append(author)
self.dates.append(date)
def prependHTML(self, text):
self.asideHTML=text+self.asideHTML
def appendHTML(self, text):
self.asideHTML=self.asideHTML+text
def renderSite(self):
nav=""
x=0
for title in self.titles:
x=len(self.titles)-1
for title in self.titles[::-1]:
nav=nav+("<a href=\"#%i\">%s</a><br/>" % (x, title))
x=x+1
x=x-1
main=""
x=0
for article in self.articles:
x=len(self.articles)-1
for article in self.articles[::-1]:
main=main+("<h2 id=\"%i\">%s</h2><p>Written on <time datetime=\"%s\">%s</time> by %s</p><article>%s</article>" %(x,self.titles[x],datetime.datetime.fromtimestamp(self.dates[x]).strftime("%Y-%m-%d %H:%M:%S"),datetime.datetime.fromtimestamp(self.dates[x]).strftime("%c"),self.authors[x],article))
x=x+1
styleargs = {"title":self.title,"nav":nav,"main":main,"aside":str(x),"footer":"Copyright 2016 Morten"}
main=main+("<a href=\"comments.py?aid=%i\">Comments (%i)</a>") % (x, storage.count("comments-%i"%x))
x=x-1
styleargs = {"title":self.title,"nav":nav,"main":main,"aside":asideHTML,"footer":"Copyright 2016 Morten"}
return self.layout%styleargs

108
index.py
View file

@ -6,112 +6,6 @@ html=htmlgen.HTMLgen(pagelayout.getLayoutXML().decode('utf-8'),"Home Page")
count=storage.count("articles")
for i in range(count):
html.addArticle(**(storage.get("articles",i)))
print("<!DOCTYPE html>")
print(html.renderSite())
html.addArticle("Markdown test","""*
* 1
* 2
* 3
# 1
# 2
# 3
## half adder truth table
| | 0 | 1 |
|---|---|---|
| 0 |0 0|0 1|
| 1 |0 1|1 0|
""")
html.addArticle("Markdown test","""*Hello, World!*
test
test
**HI**
SPOILERS:
>! SPOILERED!
>! ロボボプラネットはいいぞ
""")
html.addArticle("Markdown test","""*Hello, World!*
test
test
**HI**
**TEST**""")
html.addArticle("Markdown test","""*Hello, World!*
test
test
**HI**
**TEST**""")
html.addArticle("Markdown test","""*Hello, World!*
test
test
**HI**
**TEST**""")
html.addArticle("Markdown test","""*Hello, World!*
test
test
**HI**
**TEST**""")
html.addArticle("Markdown test","""*Hello, World!*
test
test
**HI**
**TEST**""")
html.addArticle("Markdown test","""*Hello, World!*
test
test
**HI**
**TEST**""")
html.addArticle("Markdown test","""*Hello, World!*
test
test
**HI**
**TEST**""")
html.addArticle("Markdown test","""*Hello, World!*
test
test
**HI**
**TEST**""")
html.addArticle("Markdown test","""*Hello, World!*
test
test
**HI**
**TEST**""")