blob: 5393a3d157cdd1aae6b1edcc0a2becba745bf1c1 [file] [log] [blame]
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05301# ERPNext - web based ERP (http://erpnext.com)
2# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
Anand Doshi486f9df2012-07-19 13:40:31 +053017from __future__ import unicode_literals
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053018import webnotes
19from webnotes.model.doc import Document
20
Anand Doshi51146c02012-07-12 18:41:12 +053021def scrub_page_name(page_name):
22 if page_name.endswith('.html'):
23 page_name = page_name[:-5]
24
25 return page_name
26
Rushabh Mehta949496c2012-01-25 18:48:46 +053027def make_template(doc, path, convert_fields = ['main_section', 'side_section']):
28 """make template"""
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053029 import os, jinja2
Rushabh Mehta949496c2012-01-25 18:48:46 +053030
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053031 markdown(doc, convert_fields)
Rushabh Mehta949496c2012-01-25 18:48:46 +053032
33 # write template
34 with open(path, 'r') as f:
35 temp = jinja2.Template(f.read())
36
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053037 return temp.render(doc = doc.fields)
38
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053039def page_name(title):
Anand Doshi72c945b2012-06-22 20:01:07 +053040 """make page name from title"""
41 import re
42 name = title.lower()
43 name = re.sub('[~!@#$%^&*()<>,."\']', '', name)
44 return '-'.join(name.split()[:4])
Rushabh Mehtaa4fe7182012-08-02 13:07:23 +053045
46def render(page_name):
47 """render html page"""
48 import webnotes
49 try:
50 if page_name:
51 html = get_html(page_name)
52 else:
53 html = get_html('index')
54 except Exception, e:
55 html = get_html('404')
56
Rushabh Mehta49dc5da2012-08-03 16:02:24 +053057 from webnotes.handler import eprint, print_zip
58 eprint("Content-Type: text/html")
59 print_zip(html)
Rushabh Mehtaa4fe7182012-08-02 13:07:23 +053060
61def get_html(page_name):
62 """get page html"""
63 page_name = scrub_page_name(page_name)
64 comments = get_comments(page_name)
65
66 import website.web_cache
67 html = website.web_cache.get_html(page_name, comments)
68 return html
69
70def get_comments(page_name):
71 import webnotes
72
73 if page_name == '404':
74 comments = """error: %s""" % webnotes.getTraceback()
75 else:
76 comments = """page: %s""" % page_name
77
78 return comments