btbytes / webgen.py
- Source
- Commits
- Network (1)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
ec82d50
webgen.py / webgen.py
| b276ada6 » | btbytes | 2008-10-07 | 1 | #!/usr/bin/env python | |
| 2 | ''' | ||||
| 3 | webgen.py | ||||
| 4 | |||||
| 5 | Generate a static website | ||||
| 6 | |||||
| 7 | Assumptions: | ||||
| 8 | |||||
| 9 | * copying any media files over to the output directory is done by the user. | ||||
| 8ae4e620 » | Tarrant Rollins | 2008-12-08 | 10 | * Web site can not have any subdirectories that are named the same as the input directory. | |
| b276ada6 » | btbytes | 2008-10-07 | 11 | ||
| 12 | ''' | ||||
| 13 | import os | ||||
| 14 | import sys | ||||
| 15 | import string | ||||
| 16 | from string import Template | ||||
| 17 | from config import * | ||||
| 18 | |||||
| 19 | |||||
| 20 | # should work even without having any of these. | ||||
| 21 | def dummy(s, **kw): | ||||
| 22 | print 'Processor missing' | ||||
| 23 | return s | ||||
| 24 | |||||
| 25 | try: | ||||
| 26 | from markdown2 import markdown | ||||
| 27 | except: | ||||
| 28 | markdown = dummy | ||||
| 29 | |||||
| 30 | try: | ||||
| 31 | from textile import textile | ||||
| 32 | except: | ||||
| 33 | textile = dummy | ||||
| 34 | |||||
| 35 | |||||
| 36 | def ext(s): | ||||
| 37 | bits = s.split('.') | ||||
| 38 | if len(bits)>1: | ||||
| 39 | return bits[1] | ||||
| 40 | return '' | ||||
| 41 | |||||
| 42 | def process(fname): | ||||
| 43 | f = open(fname, 'r') | ||||
| 44 | try: | ||||
| 45 | head, body = f.read().split('\n\n') | ||||
| 46 | body | ||||
| 47 | except: | ||||
| 48 | print 'Invalid file format : ', fname | ||||
| 49 | |||||
| 50 | def parse(fname): | ||||
| 51 | f = open(fname, 'r') | ||||
| 52 | raw = f.read() | ||||
| 53 | f.close() | ||||
| 54 | headers = {} | ||||
| 55 | try: | ||||
| 56 | (header_lines,body) = raw.split("\n\n", 1) | ||||
| 57 | for header in header_lines.split("\n"): | ||||
| 58 | (name, value) = header.split(": ", 1) | ||||
| 59 | headers[name.lower()] = unicode(value.strip()) | ||||
| 60 | return headers, body | ||||
| 61 | except: | ||||
| 62 | raise TypeError, "Invalid page file format." | ||||
| 63 | |||||
| 64 | get_outp = lambda s:'.'.join(s.split('.')[:-1]) + '.html' | ||||
| 65 | |||||
| ec82d507 » | btbytes | 2009-01-01 | 66 | def get_template(template): | |
| c423ec16 » | Tarrant Rollins | 2008-12-08 | 67 | """Takes the directory where templates are located and the template name. Returns a blob containing the template.""" | |
| 68 | template = os.path.join(template_dir, template) | ||||
| 69 | |||||
| b276ada6 » | btbytes | 2008-10-07 | 70 | f = open(template, 'r') | |
| 71 | blob = Template(f.read()) | ||||
| 72 | f.close() | ||||
| c423ec16 » | Tarrant Rollins | 2008-12-08 | 73 | return blob | |
| 74 | |||||
| 75 | def parse_directory(current_dir, files, output_dir): | ||||
| 76 | files = [f for f in files if ext(f) in options['extensions']] | ||||
| b276ada6 » | btbytes | 2008-10-07 | 77 | for f in files: | |
| ec82d507 » | btbytes | 2009-01-01 | 78 | inp = os.path.join(current_dir, f) | |
| 79 | outp = get_outp(os.path.join(output_dir, f)) | ||||
| c423ec16 » | Tarrant Rollins | 2008-12-08 | 80 | ||
| b276ada6 » | btbytes | 2008-10-07 | 81 | headers, body = parse(inp) | |
| c423ec16 » | Tarrant Rollins | 2008-12-08 | 82 | ||
| 83 | # Attempt to use the file specified template, if not fall back to default. | ||||
| ec82d507 » | btbytes | 2009-01-01 | 84 | blob = get_template(template) | |
| c423ec16 » | Tarrant Rollins | 2008-12-08 | 85 | try: | |
| ec82d507 » | btbytes | 2009-01-01 | 86 | blob = get_template(headers['template']) | |
| c423ec16 » | Tarrant Rollins | 2008-12-08 | 87 | except: | |
| 88 | pass | ||||
| 89 | |||||
| b276ada6 » | btbytes | 2008-10-07 | 90 | format = options['format'] | |
| 91 | try: | ||||
| 92 | format = headers['content-type'] | ||||
| 93 | except: | ||||
| 94 | pass | ||||
| 95 | |||||
| 96 | content = {u'text/plain': lambda s: u'<pre>%s</pre>' % s, | ||||
| 97 | u'text/x-markdown': lambda s: u'%s' % markdown(s), | ||||
| 98 | u'text/x-textile': lambda s: u'%s' % textile(s,head_offset=0, validate=0, | ||||
| 99 | sanitize=1, encoding='utf-8', output='utf-8'), | ||||
| 100 | u'text/html': lambda s: s}[format](body) | ||||
| 101 | |||||
| 102 | values = headers | ||||
| 103 | values.update({'content':content}) | ||||
| 104 | values.update(options) | ||||
| 105 | output = blob.safe_substitute(**values) | ||||
| 106 | outf = open(outp, 'w') | ||||
| 107 | outf.write(output) | ||||
| 108 | outf.close() | ||||
| 8ae4e620 » | Tarrant Rollins | 2008-12-08 | 109 | ||
| 110 | def main(): | ||||
| 111 | ### Walks through the input dir creating finding all subdirectories. | ||||
| 112 | for root, dirs, files in os.walk(input_dir): | ||||
| 113 | output = root.replace(input_dir, output_dir) | ||||
| 114 | ### Checks if the directory exists in output and creates it if false. | ||||
| 115 | if not os.path.isdir(output): | ||||
| 116 | os.makedirs(output) | ||||
| 117 | |||||
| 118 | parse_directory(root, files, output) | ||||
| 119 | |||||
| b276ada6 » | btbytes | 2008-10-07 | 120 | ||
| 121 | if __name__ == '__main__': | ||||
| 8ae4e620 » | Tarrant Rollins | 2008-12-08 | 122 | main() | |
