Skip to content

Commit 73230e6

Browse files
committed
init
0 parents  commit 73230e6

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed

application.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/python26
2+
# vi:si:et:sw=4:sts=4:ts=4
3+
# -*- coding: UTF-8 -*-
4+
# -*- Mode: Python -*-
5+
#
6+
# Copyright (C) 2011 Bertera Pietro
7+
8+
import web
9+
import mimetypes
10+
11+
import markdown2
12+
13+
# pages is a list of dictionary: the value of key name represent the name\
14+
# of page, the value of link represent the routing pattern,
15+
# and content_file is the path of markdown file with the page content
16+
pages = [
17+
{ "name": "Home",
18+
"link": "/",
19+
"content_file": "contents/home.md"
20+
},
21+
{ "name": "Info",
22+
"link": "/info.html",
23+
"content_file": "contents/info.md"
24+
},
25+
]
26+
27+
# this is the directory with static files (images, css, ...)
28+
static_dir = "public"
29+
30+
# the view, layout.html is a template file
31+
htmlview = web.template.render('views', cache=False, base="layout",\
32+
globals={'pages':pages, 'ctx': web.ctx})
33+
34+
# generic controller for Markdown pages:
35+
class PageClassTemplate:
36+
content_file = ""
37+
38+
def GET(self):
39+
html = markdown2.markdown_path(self.content_file)
40+
return htmlview.page(html)
41+
42+
# Controller for static files
43+
class Public:
44+
def GET(self):
45+
try:
46+
file_name = web.ctx.path.split('/')[-1]
47+
web.header('Content-type', mime_type(file_name))
48+
return open('.' + web.ctx.path, 'rb').read()
49+
except IOError:
50+
raise web.notfound()
51+
52+
# mime type interpreter
53+
def mime_type(filename):
54+
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
55+
56+
# initialize the application
57+
myApp = web.application(mapping=(), fvars=globals())
58+
59+
for page in pages:
60+
pattern = page["link"]
61+
globals()[page["name"]] = type(page["name"],\
62+
(PageClassTemplate,object,), dict(content_file=page["content_file"]))
63+
myApp.add_mapping(pattern, page["name"])
64+
65+
# add static file handler:
66+
try:
67+
if static_dir:
68+
myApp.add_mapping("/%s/.+" % static_dir, "Public")
69+
except AttributeError:
70+
pass
71+
72+
# RUN!
73+
if __name__ == "__main__":
74+
myApp.run()

contents/hello.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# hello
2+
3+
## hello everyone!

contents/home.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Welcome !!
2+
3+
### This is an Hello word Page!
4+
5+
**web.py** the best python framework for webapps [web.py][].
6+
7+
This is a simple page from a markdown file
8+
9+
[web.py]: http://www.webpy.org

contents/info.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Info
2+
3+
this stupid idea is made by [bertera pietro](http://www.bertera.it)

views/layout.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
$def with (page, title="")
2+
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
5+
<head>
6+
<title>Single Page Markdown CMS</title>
7+
</head>
8+
<body>
9+
10+
<h1>
11+
Custom header here
12+
</h1>
13+
14+
<ul>
15+
$for item in pages:
16+
$ attr = ''
17+
$if ctx.path == item['link']:
18+
$ attr=' id="current"'
19+
<li$:(attr)><a href="$item['link']">$item['name']</a></li>
20+
$pass
21+
</ul>
22+
23+
<hr/>
24+
$:page
25+
<hr/>
26+
27+
Custom footer here
28+
</body>
29+
</html>

views/page.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$def with (html)
2+
3+
$:html

0 commit comments

Comments
 (0)