| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
"""build_docs command for setup.py""" |
| 21 |
|
| 22 |
from distutils.core import Command |
| 23 |
from distutils.dep_util import newer |
| 24 |
from distutils import log |
| 25 |
import os |
| 26 |
import sys |
| 27 |
|
| 28 |
|
| 29 |
class build_docs(Command): |
| 30 |
|
| 31 |
description = 'translate txt files from docs/ to html with docutils' |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
user_options = [('output-dir=', 'o', 'Directory for output html files'), |
| 38 |
('force', 'f', 'Force creation of html files'), |
| 39 |
('clean', None, 'Clean html files'), |
| 40 |
] |
| 41 |
boolean_options = ['force', 'clean'] |
| 42 |
|
| 43 |
def initialize_options(self): |
| 44 |
self.output_dir = None |
| 45 |
self.force = None |
| 46 |
self.clean = None |
| 47 |
|
| 48 |
def finalize_options(self): |
| 49 |
pass |
| 50 |
|
| 51 |
def iter_txt_files(self, from_dir, out_dir=None): |
| 52 |
"""For each txt file yields 2 paths: source txt and target html.""" |
| 53 |
if out_dir is None: |
| 54 |
out_dir = from_dir |
| 55 |
if not from_dir.endswith(os.sep): |
| 56 |
from_dir = from_dir + os.sep |
| 57 |
n = len(from_dir) |
| 58 |
for root, dirs, files in os.walk(from_dir): |
| 59 |
for f in files: |
| 60 |
if f.endswith('.txt'): |
| 61 |
source = os.path.join(root, f) |
| 62 |
relpath = source[n:] |
| 63 |
target = os.path.join(out_dir, relpath[:-3]+'html') |
| 64 |
yield source, target |
| 65 |
|
| 66 |
def do_clean(self): |
| 67 |
"""Clean all generated html files""" |
| 68 |
for source, target in self.iter_txt_files('docs', self.output_dir): |
| 69 |
if os.path.isfile(target): |
| 70 |
log.info('removing ' + target) |
| 71 |
os.remove(target) |
| 72 |
|
| 73 |
def run(self): |
| 74 |
"""Run rst2html for each txt file""" |
| 75 |
if self.clean: |
| 76 |
self.do_clean() |
| 77 |
return |
| 78 |
|
| 79 |
rst2html = ['rst2html.py'] |
| 80 |
if sys.platform == 'win32': |
| 81 |
rst2html = ['python', |
| 82 |
os.path.join(sys.prefix, 'Scripts', 'rst2html.py')] |
| 83 |
|
| 84 |
for source, target in self.iter_txt_files('docs', self.output_dir): |
| 85 |
if self.force or newer(source, target): |
| 86 |
log.info('translating ' + source) |
| 87 |
self.spawn(rst2html + [source, target]) |