1# Copyright (c) Aleksey Gurtovoy 2001-2009 2# 3# Distributed under the Boost Software License, Version 1.0. 4# (See accompanying file LICENSE_1_0.txt or copy at 5# http://www.boost.org/LICENSE_1_0.txt) 6 7import fnmatch 8import os 9import sys 10import re 11import string 12 13underlines = ['+', '/'] 14special_cases = [ 'inserter', '_1,_2,..._n' ] 15 16 17def __section_header(section): 18 parts = section.split('/') 19 underline = underlines[len(parts) - 1] * len(parts[-1]) 20 if len(parts) > 0: 21 hidden_target = '.. _`label-%s`:' % '-'.join( parts ) 22 return '\n%s\n%s\n%s\n\n' % (parts[-1], underline, hidden_target ) 23 else: 24 return '\n%s\n%s\n\n' % (parts[-1], underline ) 25 26 27def __section_intro(section): 28 parts = section.split('/') 29 return '%s.rst' % '-'.join( [x.split(' ')[0] for x in parts] ) 30 31 32def __include_page( output, src_dir, page, name = None ): 33 output.write( '.. include:: %s\n' % os.path.join( src_dir, page ) ) 34 # output.write( '.. raw:: LaTeX\n\n' ) 35 # output.write( ' \\newpage\n\n') 36 37 if name and name not in special_cases: ref = name 38 else: ref = '/'.join( page.split('.')[0].split('-') ) 39 if ref.upper() == ref or ref.lower() == ref: 40 output.write( 41 ( '.. |%(ref)s| replace:: `%(ref)s`_\n' ) 42 % { 'ref': ref } 43 ) 44 else: 45 if ref.find( '/' ) == -1: 46 ref = ' '.join( filter( lambda x: len( x.strip() ) > 0, re.split( '([A-Z][a-z]+)', ref ) ) ) 47 output.write( '.. |%(ref)s| replace:: `%(ref)s`_\n' % { 'ref': ref } ) 48 49 output.write( '\n' ) 50 51 52def __write_index( filename, index ): 53 index_file = open( filename, 'w' ) 54 index.sort() 55 for x in index: 56 index_file.write( '* |%s|\n' % x ) 57 58 index_file.close() 59 60 61def main( filename, src_dir, build_dir ): 62 sources = filter( 63 lambda x: fnmatch.fnmatch(x,"*.rst") and x != filename 64 , os.listdir( src_dir ) 65 ) 66 67 toc = [ t.strip() for t in open( os.path.join( src_dir, '%s.toc' % filename) ).readlines() ] 68 topics = {} 69 for t in toc: topics[t] = [] 70 71 concept_index = [] 72 index = [] 73 74 output = open( os.path.join( build_dir, '%s.gen' % filename ), 'w') 75 output.writelines( open( os.path.join( src_dir, '%s.rst' % filename ), 'r' ).readlines() ) 76 re_topic = re.compile(r'^..\s+(.+?)//(.+?)(\s*\|\s*(\d+))?\s*$') 77 for src in sources: 78 placement_spec = open( os.path.join( src_dir, src ), 'r' ).readline() 79 80 topic = 'Unclassified' 81 name = None 82 order = -1 83 84 match = re_topic.match(placement_spec) 85 if match: 86 topic = match.group(1) 87 name = match.group(2) 88 if match.group(3): 89 order = int(match.group(4)) 90 91 if not topics.has_key(topic): 92 topics[topic] = [] 93 94 topics[topic].append((src, order, name)) 95 96 if name: 97 if topic.find( '/Concepts' ) == -1: 98 index.append( name ) 99 else: 100 concept_index.append( name ) 101 102 103 for t in toc: 104 content = topics[t] 105 content.sort( lambda x,y: x[1] - y[1] ) 106 107 output.write( __section_header(t) ) 108 109 intro = __section_intro( t ) 110 if os.path.exists( os.path.join( src_dir, intro ) ): 111 __include_page( output, src_dir, intro ) 112 113 for src in content: 114 __include_page( output, src_dir, src[0], src[2] ) 115 116 output.close() 117 118 __write_index( os.path.join( build_dir, 'concepts.gen' ), concept_index ) 119 __write_index( os.path.join( build_dir, 'index.gen' ), index ) 120 121 122 123main( 'refmanual', os.path.dirname( __file__ ), sys.argv[1] ) 124