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