annotate core/management/commands/make_fixed_page.py @ 869:63209e53f3aa

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