Skip to content

Commit 0ebf734

Browse files
committed
mod application.py
1 parent 73230e6 commit 0ebf734

File tree

3 files changed

+94
-27
lines changed

3 files changed

+94
-27
lines changed

application.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,7 @@
1313
# pages is a list of dictionary: the value of key name represent the name\
1414
# of page, the value of link represent the routing pattern,
1515
# 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-
]
16+
pages = []
2617

2718
# this is the directory with static files (images, css, ...)
2819
static_dir = "public"
@@ -31,13 +22,6 @@
3122
htmlview = web.template.render('views', cache=False, base="layout",\
3223
globals={'pages':pages, 'ctx': web.ctx})
3324

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)
4125

4226
# Controller for static files
4327
class Public:
@@ -55,12 +39,24 @@ def mime_type(filename):
5539

5640
# initialize the application
5741
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"])
42+
myApp.add_mapping('/refesh', 'Refesh')
43+
myApp.add_mapping('/([^\.]*)', 'Controller')
44+
45+
class Controller:
46+
def GET(self,key):
47+
html = markdown2.markdown_path("contents/"+key+'.md')
48+
return htmlview.page(html)
49+
50+
class Refesh:
51+
def GET(self):
52+
import os
53+
dir= 'contents'
54+
files = os.listdir(dir)
55+
global pages
56+
for f in files:
57+
pages.append(f.replace('.md',''))
58+
print pages
59+
return 'success'
6460

6561
# add static file handler:
6662
try:

application.py.bak

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()

views/layout.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ <h1>
1313

1414
<ul>
1515
$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>
16+
<li><a href="$item">$item</a></li>
2017
$pass
2118
</ul>
2219

0 commit comments

Comments
 (0)