| 1 |
#! /usr/bin/python3 |
| 2 |
|
| 3 |
import sys |
| 4 |
from zzipdoc.match import * |
| 5 |
from zzipdoc.options import * |
| 6 |
from zzipdoc.textfile import * |
| 7 |
from zzipdoc.textfileheader import * |
| 8 |
from zzipdoc.functionheader import * |
| 9 |
from zzipdoc.functionprototype import * |
| 10 |
from zzipdoc.commentmarkup import * |
| 11 |
from zzipdoc.functionlisthtmlpage import * |
| 12 |
from zzipdoc.functionlistreference import * |
| 13 |
from zzipdoc.dbk2htm import * |
| 14 |
from zzipdoc.htmldocument import * |
| 15 |
from zzipdoc.docbookdocument import * |
| 16 |
|
| 17 |
def _src_to_xml(text): |
| 18 |
return text.replace("&", "&").replace("<", "<").replace(">", ">") |
| 19 |
def _email_to_xml(text): |
| 20 |
return text & Match("<([^<>]*@[^<>]*)>") >> "<\\1>" |
| 21 |
|
| 22 |
class PerFileEntry: |
| 23 |
def __init__(self, header, comment): |
| 24 |
self.textfileheader = header |
| 25 |
self.filecomment = comment |
| 26 |
class PerFile: |
| 27 |
def __init__(self): |
| 28 |
self.textfileheaders = [] |
| 29 |
self.filecomments = [] |
| 30 |
self.entries = [] |
| 31 |
def add(self, textfileheader, filecomment): |
| 32 |
self.textfileheaders += [ textfileheader ] |
| 33 |
self.filecomments += [ filecomment ] |
| 34 |
self.entries += [ PerFileEntry(textfileheader, filecomment) ] |
| 35 |
def where_filename(self, filename): |
| 36 |
for entry in self.entries: |
| 37 |
if entry.textfileheader.get_filename() == filename: |
| 38 |
return entry |
| 39 |
return None |
| 40 |
def print_list_mainheader(self): |
| 41 |
for t_fileheader in self.headers: |
| 42 |
print(t_fileheader.get_filename(), t_fileheader.src_mainheader()) |
| 43 |
|
| 44 |
class PerFunctionEntry: |
| 45 |
def __init__(self, header, comment, prototype): |
| 46 |
self.header = header |
| 47 |
self.comment = comment |
| 48 |
self.prototype = prototype |
| 49 |
def get_name(self): |
| 50 |
return self.prototype.get_name() |
| 51 |
def get_titleline(self): |
| 52 |
return self.header.get_titleline() |
| 53 |
def get_head(self): |
| 54 |
return self.prototype |
| 55 |
def get_body(self): |
| 56 |
return self.comment |
| 57 |
class PerFunction: |
| 58 |
def __init__(self): |
| 59 |
self.headers = [] |
| 60 |
self.comments = [] |
| 61 |
self.prototypes = [] |
| 62 |
self.entries = [] |
| 63 |
def add(self, functionheader, functioncomment, functionprototype): |
| 64 |
self.headers += [ functionheader ] |
| 65 |
self.comments += [ functionprototype ] |
| 66 |
self.prototypes += [ functionprototype ] |
| 67 |
self.entries += [ PerFunctionEntry(functionheader, functioncomment, |
| 68 |
functionprototype) ] |
| 69 |
def print_list_titleline(self): |
| 70 |
for funcheader in self.headers: |
| 71 |
print(funcheader.get_filename(), "[=>]", funcheader.get_titleline()) |
| 72 |
def print_list_name(self): |
| 73 |
for funcheader in self.prototypes: |
| 74 |
print(funcheader.get_filename(), "[>>]", funcheader.get_name()) |
| 75 |
|
| 76 |
class PerFunctionFamilyEntry: |
| 77 |
def __init__(self, leader): |
| 78 |
self.leader = leader |
| 79 |
self.functions = [] |
| 80 |
def contains(self, func): |
| 81 |
for item in self.functions: |
| 82 |
if item == func: return True |
| 83 |
return False |
| 84 |
def add(self, func): |
| 85 |
if not self.contains(func): |
| 86 |
self.functions += [ func ] |
| 87 |
def get_name(self): |
| 88 |
if self.leader is None: return None |
| 89 |
return self.leader.get_name() |
| 90 |
class PerFunctionFamily: |
| 91 |
def __init__(self): |
| 92 |
self.functions = [] |
| 93 |
self.families = [] |
| 94 |
self.retarget = {} |
| 95 |
self.entries = [] |
| 96 |
def add_PerFunction(self, per_list): |
| 97 |
for item in per_list.entries: |
| 98 |
add_PerFunctionEntry(item) |
| 99 |
def add_PerFunctionEntry(self, item): |
| 100 |
self.functions += [ item ] |
| 101 |
def get_function(self, name): |
| 102 |
for item in self.functions: |
| 103 |
if item.get_name() == name: |
| 104 |
return item |
| 105 |
return None |
| 106 |
def get_entry(self, name): |
| 107 |
for item in self.entries: |
| 108 |
if item.get_name() == name: |
| 109 |
return item |
| 110 |
return None |
| 111 |
def fill_families(self): |
| 112 |
name_list = {} |
| 113 |
for func in self.functions: |
| 114 |
name = func.get_name() |
| 115 |
name_list[name] = func |
| 116 |
for func in self.functions: |
| 117 |
name = func.get_name() |
| 118 |
line = func.get_titleline() |
| 119 |
is_retarget = Match("=>\s*(\w+)") |
| 120 |
if line & is_retarget: |
| 121 |
into = is_retarget[1] |
| 122 |
self.retarget[name] = is_retarget[1] |
| 123 |
lead_list = [] |
| 124 |
for name in self.retarget: |
| 125 |
into = self.retarget[name] |
| 126 |
if into not in name_list: |
| 127 |
print("function '"+name+"' retarget into '"+into+ |
| 128 |
"' does not exist - keep alone") |
| 129 |
if into in self.retarget: |
| 130 |
other = self.retarget[into] |
| 131 |
print("function '"+name+"' retarget into '"+into+ |
| 132 |
"' which is itself a retarget into '"+other+"'") |
| 133 |
if into not in lead_list: |
| 134 |
lead_list += [ into ] |
| 135 |
for func in self.functions: |
| 136 |
name = func.get_name() |
| 137 |
if name not in lead_list and name not in self.retarget: |
| 138 |
lead_list += [ name ] |
| 139 |
for name in lead_list: |
| 140 |
func = self.get_function(name) |
| 141 |
if func is not None: |
| 142 |
entry = PerFunctionFamilyEntry(func) |
| 143 |
entry.add(func) # the first |
| 144 |
self.entries += [ entry ] |
| 145 |
else: |
| 146 |
print("head function '"+name+" has no entry") |
| 147 |
for func in self.functions: |
| 148 |
name = func.get_name() |
| 149 |
if name in self.retarget: |
| 150 |
into = self.retarget[name] |
| 151 |
entry = self.get_entry(into) |
| 152 |
if entry is not None: |
| 153 |
entry.add(func) # will not add duplicates |
| 154 |
else: |
| 155 |
print("into function '"+name+" has no entry") |
| 156 |
def print_list_name(self): |
| 157 |
for family in self.entries: |
| 158 |
name = family.get_name() |
| 159 |
print(name, ":", end = " ") |
| 160 |
for item in family.functions: |
| 161 |
print(item.get_name(), ",", end = " ") |
| 162 |
print("") |
| 163 |
class HtmlManualPageAdapter: |
| 164 |
def __init__(self, entry): |
| 165 |
""" usually takes a PerFunctionEntry """ |
| 166 |
self.entry = entry |
| 167 |
def get_name(self): |
| 168 |
return self.entry.get_name() |
| 169 |
def _head(self): |
| 170 |
return self.entry.get_head() |
| 171 |
def _body(self): |
| 172 |
return self.entry.get_body() |
| 173 |
def head_xml_text(self): |
| 174 |
return self._head().xml_text() |
| 175 |
def body_xml_text(self, name): |
| 176 |
return self._body().xml_text(name) |
| 177 |
def head_get_prespec(self): |
| 178 |
return self._head().get_prespec() |
| 179 |
def head_get_namespec(self): |
| 180 |
return self._head().get_namespec() |
| 181 |
def head_get_callspec(self): |
| 182 |
return self._head().get_callspec() |
| 183 |
def get_title(self): |
| 184 |
return self._body().header.get_title() |
| 185 |
def get_filename(self): |
| 186 |
return self._body().header.get_filename() |
| 187 |
def src_mainheader(self): |
| 188 |
return self._body().header.parent.textfile.src_mainheader() |
| 189 |
def get_mainheader(self): |
| 190 |
return _src_to_xml(self.src_mainheader()) |
| 191 |
class RefEntryManualPageAdapter: |
| 192 |
def __init__(self, entry, per_file = None): |
| 193 |
""" usually takes a PerFunctionEntry """ |
| 194 |
self.entry = entry |
| 195 |
self.per_file = per_file |
| 196 |
def get_name(self): |
| 197 |
return self.entry.get_name() |
| 198 |
def _head(self): |
| 199 |
return self.entry.get_head() |
| 200 |
def _body(self): |
| 201 |
return self.entry.get_body() |
| 202 |
def _textfile(self): |
| 203 |
return self._body().header.parent.textfile |
| 204 |
def head_xml_text(self): |
| 205 |
return self._head().xml_text() |
| 206 |
def body_xml_text(self, name): |
| 207 |
return self._body().xml_text(name) |
| 208 |
def get_title(self): |
| 209 |
return self._body().header.get_title() |
| 210 |
def get_filename(self): |
| 211 |
return self._body().header.get_filename() |
| 212 |
def src_mainheader(self): |
| 213 |
return self._textfile().src_mainheader() |
| 214 |
def get_mainheader(self): |
| 215 |
return _src_to_xml(self.src_mainheader()) |
| 216 |
def get_includes(self): |
| 217 |
return "" |
| 218 |
def list_seealso(self): |
| 219 |
return self._body().header.get_alsolist() |
| 220 |
def get_authors(self): |
| 221 |
comment = None |
| 222 |
if self.per_file: |
| 223 |
entry = self.per_file.where_filename(self.get_filename()) |
| 224 |
if entry: |
| 225 |
comment = entry.filecomment.xml_text() |
| 226 |
if comment: |
| 227 |
check = Match(r"(?s)<para>\s*[Aa]uthors*\b:*" |
| 228 |
r"((?:.(?!</para>))*.)</para>") |
| 229 |
if comment & check: return _email_to_xml(check[1]) |
| 230 |
return None |
| 231 |
def get_copyright(self): |
| 232 |
comment = None |
| 233 |
if self.per_file: |
| 234 |
entry = self.per_file.where_filename(self.get_filename()) |
| 235 |
if entry: |
| 236 |
comment = entry.filecomment.xml_text() |
| 237 |
if comment: |
| 238 |
check = Match(r"(?s)<para>\s*[Cc]opyright\b" |
| 239 |
r"((?:.(?!</para>))*.)</para>") |
| 240 |
if comment & check: return _email_to_xml(check[0]) |
| 241 |
return None |
| 242 |
|
| 243 |
def makedocs(filenames, o): |
| 244 |
textfiles = [] |
| 245 |
for filename in filenames: |
| 246 |
textfile = TextFile(filename) |
| 247 |
textfile.parse() |
| 248 |
textfiles += [ textfile ] |
| 249 |
per_file = PerFile() |
| 250 |
for textfile in textfiles: |
| 251 |
textfileheader = TextFileHeader(textfile) |
| 252 |
textfileheader.parse() |
| 253 |
filecomment = CommentMarkup(textfileheader) |
| 254 |
filecomment.parse() |
| 255 |
per_file.add(textfileheader, filecomment) |
| 256 |
funcheaders = [] |
| 257 |
for textfile in per_file.textfileheaders: |
| 258 |
funcheader = FunctionHeaderList(textfile) |
| 259 |
funcheader.parse() |
| 260 |
funcheaders += [ funcheader ] |
| 261 |
per_function = PerFunction() |
| 262 |
for funcheader in funcheaders: |
| 263 |
for child in funcheader.get_children(): |
| 264 |
funcprototype = FunctionPrototype(child) |
| 265 |
funcprototype.parse() |
| 266 |
funccomment = CommentMarkup(child) |
| 267 |
funccomment.parse() |
| 268 |
per_function.add(child, funccomment, funcprototype) |
| 269 |
per_family = PerFunctionFamily() |
| 270 |
for item in per_function.entries: |
| 271 |
per_family.add_PerFunctionEntry(item) |
| 272 |
per_family.fill_families() |
| 273 |
# debug output.... |
| 274 |
# per_file.print_list_mainheader() |
| 275 |
# per_function.print_list_titleline() |
| 276 |
# per_function.print_list_name() |
| 277 |
# per_family.print_list_name() |
| 278 |
# |
| 279 |
html = FunctionListHtmlPage(o) |
| 280 |
for item in per_family.entries: |
| 281 |
for func in item.functions: |
| 282 |
func_adapter = HtmlManualPageAdapter(func) |
| 283 |
if o.onlymainheader and not (Match("<"+o.onlymainheader+">") |
| 284 |
& func_adapter.src_mainheader()): |
| 285 |
continue |
| 286 |
html.add(func_adapter) |
| 287 |
html.cut() |
| 288 |
html.cut() |
| 289 |
class _Html_: |
| 290 |
def __init__(self, html): |
| 291 |
self.html = html |
| 292 |
def html_text(self): |
| 293 |
return section2html(paramdef2html(self.html.xml_text())) |
| 294 |
def get_title(self): |
| 295 |
return self.html.get_title() |
| 296 |
HtmlDocument(o).add(_Html_(html)).save(o.output+o.suffix) |
| 297 |
# |
| 298 |
man3 = FunctionListReference(o) |
| 299 |
for item in per_family.entries: |
| 300 |
for func in item.functions: |
| 301 |
func_adapter = RefEntryManualPageAdapter(func, per_file) |
| 302 |
if o.onlymainheader and not (Match("<"+o.onlymainheader+">") |
| 303 |
& func_adapter.src_mainheader()): |
| 304 |
continue |
| 305 |
man3.add(func_adapter) |
| 306 |
man3.cut() |
| 307 |
man3.cut() |
| 308 |
DocbookDocument(o).add(man3).save(o.output+o.suffix) |
| 309 |
|
| 310 |
|
| 311 |
if __name__ == "__main__": |
| 312 |
filenames = [] |
| 313 |
o = Options() |
| 314 |
o.package = "ZZipLib" |
| 315 |
o.program = sys.argv[0] |
| 316 |
o.html = "html" |
| 317 |
o.docbook = "docbook" |
| 318 |
o.output = "zziplib" |
| 319 |
o.suffix = "" |
| 320 |
for item in sys.argv[1:]: |
| 321 |
if o.scan(item): continue |
| 322 |
filenames += [ item ] |
| 323 |
makedocs(filenames, o) |
| 324 |
|
| 325 |
|