Mercurial > public > sg101
view core/management/commands/make_fixed_page.py @ 943:cf9918328c64
Haystack tweaks for Django 1.7.7.
I had to upgrade to Haystack 2.3.1 to get it to work with Django
1.7.7. I also had to update the Xapian backend. But I ran into
problems.
On my laptop anyway (Ubuntu 14.0.4), xapian gets mad when search terms
are greater than 245 chars (or something) when indexing. So I created
a custom field that would simply omit terms greater than 64 chars and
used this field everywhere I previously used a CharField.
Secondly, the custom search form was broken now. Something changed in
the Xapian backend and exact searches stopped working. Fortunately the
auto_query (which I was using originally and broke during an upgrade)
started working again. So I cut the search form back over to doing an
auto_query. I kept the form the same (3 fields) because I didn't want
to change the form and I think it's better that way.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 13 May 2015 20:25:07 -0500 |
parents | 1982996ce365 |
children |
line wrap: on
line source
"""make_fixed_page.py A management command to generate HTML files that can be {% include'd %} by a generic flatpage-like system. We currently support restructured text as input. I chose "fixed" page since I didn't want to confuse this with Django's flatpage system. This command reads input files from the directory PROJECT_PATH/fixed and writes the HTML output to PROJECT_PATH/templates/fixed. """ from __future__ import with_statement import os.path import glob import docutils.core from django.core.management.base import LabelCommand, CommandError from django.conf import settings class Command(LabelCommand): help = "Generate HTML from restructured text files" args = "<inputfile1> <inputfile2> ... | all" def handle_label(self, filename, **kwargs): """Process input file(s)""" if not hasattr(settings, 'PROJECT_PATH'): raise CommandError("Please add a PROJECT_PATH setting") self.src_dir = os.path.join(settings.PROJECT_PATH, 'fixed') self.dst_dir = os.path.join(settings.PROJECT_PATH, 'templates', 'fixed') if filename == 'all': files = glob.glob("%s%s*.rst" % (self.src_dir, os.path.sep)) files = [os.path.basename(f) for f in files] else: files = [filename] for f in files: self.process_page(f) def process_page(self, filename): """Processes one fixed page""" # retrieve source text src_path = os.path.join(self.src_dir, filename) try: with open(src_path, 'r') as f: src_text = f.read() except IOError, ex: raise CommandError(str(ex)) # transform text content = self.transform_input(src_text) # write output basename = os.path.splitext(os.path.basename(filename))[0] dst_path = os.path.join(self.dst_dir, '%s.html' % basename) try: with open(dst_path, 'w') as f: f.write(content.encode('utf-8')) except IOError, ex: raise CommandError(str(ex)) prefix = os.path.commonprefix([src_path, dst_path]) self.stdout.write("%s -> %s\n" % (filename, dst_path[len(prefix):])) def transform_input(self, src_text): """Transforms input restructured text to HTML""" return docutils.core.publish_parts(src_text, writer_name='html', settings_overrides={ 'doctitle_xform': False, 'initial_header_level': 2, })['html_body']