|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 sphinx.builders.webapp |
|
4 ~~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 A web application builder. |
|
7 |
|
8 :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 import os |
|
13 import sys |
|
14 import codecs |
|
15 import shutil |
|
16 import cPickle |
|
17 from os import path |
|
18 from hashlib import md5 |
|
19 from types import ListType, TupleType |
|
20 import re |
|
21 |
|
22 |
|
23 import jinja2 as j2 |
|
24 |
|
25 #from mercurial import commands, ui |
|
26 #from mercurial.hg import repository |
|
27 |
|
28 from sphinx.builders.html import StandaloneHTMLBuilder |
|
29 from sphinx.errors import SphinxError |
|
30 from sphinx.web.dbutils import PidDb |
|
31 from sphinx.web.webconfig import WebConfig |
|
32 from sphinx.writers.html import HTMLTranslator |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 class SimpleCommentHTMLBuilder(StandaloneHTMLBuilder): |
|
40 """ Static html pages with paragraph ids. |
|
41 """ |
|
42 def init(self): |
|
43 self.app.add_javascript('simplecomment.js') |
|
44 self.id_file_loc=os.path.join(self.outdir,'paragraph_id.py') |
|
45 self.id_file=open(self.id_file_loc,'w') |
|
46 self.beginning="p_list= " |
|
47 self.id_dictionary={} |
|
48 |
|
49 StandaloneHTMLBuilder.init(self) |
|
50 |
|
51 def get_target_uri(self, docname, typ=None): |
|
52 return docname + self.link_suffix |
|
53 |
|
54 def write_doc(self, docname, doctree): |
|
55 StandaloneHTMLBuilder.write_doc(self, docname, doctree) |
|
56 |
|
57 def prepare_writing(self, docnames): |
|
58 StandaloneHTMLBuilder.prepare_writing(self, docnames) |
|
59 |
|
60 def finish(self): |
|
61 StandaloneHTMLBuilder.finish(self) |
|
62 self.add_pids_to_paragraphs() |
|
63 self.id_file.write(self.beginning+str(self.id_dictionary)) |
|
64 |
|
65 |
|
66 def add_pids_to_paragraphs(self): |
|
67 all_files=[] |
|
68 for path,dir,filenames in os.walk(self.outdir): |
|
69 for filename in filenames: |
|
70 all_files.append(os.path.join(path, filename)) |
|
71 |
|
72 for element in all_files : |
|
73 if element.split('.')[1]=='html': |
|
74 self.id_list=[] |
|
75 self.taggen(element) |
|
76 self.id_dictionary[self.chapter.split('/')[-1]]=self.id_list |
|
77 |
|
78 else : |
|
79 pass |
|
80 |
|
81 def retag(self,s): |
|
82 self.biggest_id += 1 |
|
83 id_name="%s_%x" % (self.chapter.split('/')[-1],self.biggest_id) |
|
84 self.id_list.append(id_name) |
|
85 |
|
86 return '<p id="%s">' %id_name |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 def taggen(self,html_file_name): |
|
92 tagged = re.compile('<p id="x_([0-9a-f]+)"[^>]*>', re.M) |
|
93 self.biggest_id=0 |
|
94 self.chapter=html_file_name.split('.')[0] |
|
95 |
|
96 try: |
|
97 f = open(html_file_name).read() |
|
98 f1 = re.sub('<p>',self.retag, f ) |
|
99 |
|
100 |
|
101 if f1 != f: |
|
102 tmpname = html_file_name+ '.tmp' |
|
103 fp = open(tmpname, 'w') |
|
104 fp.write(f1) |
|
105 fp.close() |
|
106 os.rename(tmpname,html_file_name) |
|
107 |
|
108 except IOError: |
|
109 pass |
|
110 |
|
111 |
|
112 |