Mercurial > public > sg101
comparison core/management/commands/make_fixed_page.py @ 591:1982996ce365
Created a "fixed page" facility.
Reworked the last few commits. We now generate HTML snippets from
restructured text files. These are {% include'd %} by a fixed
page template. This is for bitbucket issue #8.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 12 May 2012 14:57:45 -0500 |
parents | core/management/commands/make_flatpage.py@7ed2101df789 |
children |
comparison
equal
deleted
inserted
replaced
590:ce73771b6cd0 | 591:1982996ce365 |
---|---|
1 """make_fixed_page.py | |
2 | |
3 A management command to generate HTML files that can be {% include'd %} by a | |
4 generic flatpage-like system. We currently support restructured text as input. | |
5 | |
6 I chose "fixed" page since I didn't want to confuse this with Django's flatpage | |
7 system. | |
8 | |
9 This command reads input files from the directory PROJECT_PATH/fixed and writes | |
10 the HTML output to PROJECT_PATH/templates/fixed. | |
11 | |
12 """ | |
13 from __future__ import with_statement | |
14 import os.path | |
15 import glob | |
16 | |
17 import docutils.core | |
18 from django.core.management.base import LabelCommand, CommandError | |
19 from django.conf import settings | |
20 | |
21 | |
22 class Command(LabelCommand): | |
23 help = "Generate HTML from restructured text files" | |
24 args = "<inputfile1> <inputfile2> ... | all" | |
25 | |
26 def handle_label(self, filename, **kwargs): | |
27 """Process input file(s)""" | |
28 | |
29 if not hasattr(settings, 'PROJECT_PATH'): | |
30 raise CommandError("Please add a PROJECT_PATH setting") | |
31 | |
32 self.src_dir = os.path.join(settings.PROJECT_PATH, 'fixed') | |
33 self.dst_dir = os.path.join(settings.PROJECT_PATH, 'templates', 'fixed') | |
34 | |
35 if filename == 'all': | |
36 files = glob.glob("%s%s*.rst" % (self.src_dir, os.path.sep)) | |
37 files = [os.path.basename(f) for f in files] | |
38 else: | |
39 files = [filename] | |
40 | |
41 for f in files: | |
42 self.process_page(f) | |
43 | |
44 def process_page(self, filename): | |
45 """Processes one fixed page""" | |
46 | |
47 # retrieve source text | |
48 src_path = os.path.join(self.src_dir, filename) | |
49 try: | |
50 with open(src_path, 'r') as f: | |
51 src_text = f.read() | |
52 except IOError, ex: | |
53 raise CommandError(str(ex)) | |
54 | |
55 # transform text | |
56 content = self.transform_input(src_text) | |
57 | |
58 # write output | |
59 basename = os.path.splitext(os.path.basename(filename))[0] | |
60 dst_path = os.path.join(self.dst_dir, '%s.html' % basename) | |
61 | |
62 try: | |
63 with open(dst_path, 'w') as f: | |
64 f.write(content.encode('utf-8')) | |
65 except IOError, ex: | |
66 raise CommandError(str(ex)) | |
67 | |
68 prefix = os.path.commonprefix([src_path, dst_path]) | |
69 self.stdout.write("%s -> %s\n" % (filename, dst_path[len(prefix):])) | |
70 | |
71 def transform_input(self, src_text): | |
72 """Transforms input restructured text to HTML""" | |
73 | |
74 return docutils.core.publish_parts(src_text, writer_name='html', | |
75 settings_overrides={ | |
76 'doctitle_xform': False, | |
77 'initial_header_level': 2, | |
78 })['html_body'] |