-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPageDoc.py
146 lines (119 loc) · 4.5 KB
/
PageDoc.py
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
# -*- Mode: python; coding: utf-8 -*-
#
# Cherokee Web Site
#
# Authors:
# Alvaro Lopez Ortega <[email protected]>
#
# Copyright (C) 2001-2011 Alvaro Lopez Ortega
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
import os
import re
import CTK
import Page
import config
URL_BASE = "/doc"
class Index (CTK.Box):
def __init__ (self):
CTK.Box.__init__ (self)
local_file = os.path.join (config.DOC_LOCAL, "index.txt")
lines = open(local_file,'r').readlines()
n = 0
while n < len(lines):
line = lines[n].strip()
if not line:
n += 1
continue
elif line.startswith("=="):
n += 1
continue
elif line.startswith("*"*10):
# *********************************
# link:basics.html[Getting started]: Cherokee basics
# *********************************
n += 1
line = lines[n].strip()
tmp = re.findall (r'link:(\S+)\[(.+)\](.*)$', line)
if tmp:
self += CTK.RawHTML ('<div class="section-title"><a href="%s">%s</a><br/><span>%s</span></div>' %(tmp[0][0], tmp[0][1], tmp[0][2].replace(": ","")))
else:
self += CTK.RawHTML ('<div class="section-title">%s</div>' %(line))
n += 2
continue
elif line.startswith(". "):
#
# . link:dev_quickstart.html[Quickstart]: Where to start?.
# . link:dev_debug.html[Debugging]: Resources available to debug Cherokee.
#
l = CTK.List({'class': 'section-list'})
self += l
while True:
tmp = re.findall (r'link:(\S+)\[(.+)\](.*)$', line)
l += CTK.RawHTML ('<a href="%s">%s</a>%s' %(tmp[0][0], tmp[0][1], tmp[0][2]))
n += 1
if lines[n].strip().startswith(". "):
line = lines[n].strip()
continue
else:
break
else:
n+= 1
class PageDoc:
def __call__ (self):
# Handle request
if CTK.request.url in ('/doc', '/doc/', '/doc/index', '/doc/index.html'):
request = "/doc/index.html"
sidebar = False
else:
request = CTK.request.url
sidebar = True
tmp = re.findall (r'^%s/(.+\.html)'%(URL_BASE), request)
if not tmp:
return CTK.HTTP_Response (404)
help_file = tmp[0]
# Title
title = help_file.replace ('_', ' ').replace('.html', '') + ": Documentation"
page = Page.Page_Menu_Side (title=title)
page += CTK.RawHTML ("<h1>Documentation</h1>")
# Security check
local_file = os.path.abspath (os.path.join (config.DOC_LOCAL, help_file))
if not local_file.startswith (config.DOC_LOCAL):
page += CTK.RawHTML ('no way')
return page.Render()
if not os.path.exists(local_file):
return CTK.HTTP_Response (404)
# Read it
html = open(local_file, 'r').read()
tmp = re.findall ('<body.*?>(.*?)</body>', html, re.IGNORECASE|re.DOTALL)
if not tmp:
page += CTK.RawHTML ('no body')
return page.Render()
body = tmp[0]
# Layout
if sidebar:
page.sidebar += Index()
page += CTK.RawHTML (body)
return CTK.HTTP_Cacheable (60, body=page.Render())
class PageMedia:
def __call__ (self):
tmp = re.findall (r'^%s(/media/.*)$'%(URL_BASE), CTK.request.url)
if not tmp:
return HTTP_Error (404)
local_file = config.DOC_LOCAL + tmp[0]
return CTK.HTTP_XSendfile (local_file)
CTK.publish (URL_BASE, PageDoc)
CTK.publish ('^%s/media'%(URL_BASE), PageMedia)