Rushabh Mehta | 3966f1d | 2012-02-23 12:35:32 +0530 | [diff] [blame] | 1 | # 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 Doshi | 486f9df | 2012-07-19 13:40:31 +0530 | [diff] [blame] | 17 | from __future__ import unicode_literals |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 18 | import webnotes |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 19 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 20 | def scrub_page_name(page_name): |
| 21 | if page_name.endswith('.html'): |
| 22 | page_name = page_name[:-5] |
| 23 | |
| 24 | return page_name |
| 25 | |
Rushabh Mehta | 949496c | 2012-01-25 18:48:46 +0530 | [diff] [blame] | 26 | def make_template(doc, path, convert_fields = ['main_section', 'side_section']): |
| 27 | """make template""" |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 28 | import os, jinja2 |
Rushabh Mehta | 949496c | 2012-01-25 18:48:46 +0530 | [diff] [blame] | 29 | |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 30 | markdown(doc, convert_fields) |
Rushabh Mehta | 949496c | 2012-01-25 18:48:46 +0530 | [diff] [blame] | 31 | |
| 32 | # write template |
| 33 | with open(path, 'r') as f: |
| 34 | temp = jinja2.Template(f.read()) |
| 35 | |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 36 | return temp.render(doc = doc.fields) |
| 37 | |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 38 | def page_name(title): |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 39 | """make page name from title""" |
| 40 | import re |
| 41 | name = title.lower() |
| 42 | name = re.sub('[~!@#$%^&*()<>,."\']', '', name) |
| 43 | return '-'.join(name.split()[:4]) |
Rushabh Mehta | a4fe718 | 2012-08-02 13:07:23 +0530 | [diff] [blame] | 44 | |
| 45 | def render(page_name): |
| 46 | """render html page""" |
| 47 | import webnotes |
| 48 | try: |
| 49 | if page_name: |
| 50 | html = get_html(page_name) |
| 51 | else: |
| 52 | html = get_html('index') |
| 53 | except Exception, e: |
| 54 | html = get_html('404') |
| 55 | |
Rushabh Mehta | 49dc5da | 2012-08-03 16:02:24 +0530 | [diff] [blame] | 56 | from webnotes.handler import eprint, print_zip |
| 57 | eprint("Content-Type: text/html") |
| 58 | print_zip(html) |
Rushabh Mehta | a4fe718 | 2012-08-02 13:07:23 +0530 | [diff] [blame] | 59 | |
| 60 | def get_html(page_name): |
| 61 | """get page html""" |
| 62 | page_name = scrub_page_name(page_name) |
| 63 | comments = get_comments(page_name) |
| 64 | |
| 65 | import website.web_cache |
| 66 | html = website.web_cache.get_html(page_name, comments) |
| 67 | return html |
| 68 | |
| 69 | def get_comments(page_name): |
| 70 | import webnotes |
| 71 | |
| 72 | if page_name == '404': |
| 73 | comments = """error: %s""" % webnotes.getTraceback() |
| 74 | else: |
| 75 | comments = """page: %s""" % page_name |
| 76 | |
| 77 | return comments |