| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
from zzipdoc.options import * |
| 4 |
from zzipdoc.match import Match |
| 5 |
|
| 6 |
import os |
| 7 |
import os.path |
| 8 |
|
| 9 |
def short(filename): |
| 10 |
while filename.startswith("../"): |
| 11 |
filename = filename[3:] |
| 12 |
if filename.startswith(os.sep): |
| 13 |
parts = ["",""] + filename.split(os.sep) |
| 14 |
return os.path.join(parts[-2], parts[-1]) |
| 15 |
return filename |
| 16 |
|
| 17 |
class FunctionListHtmlPage: |
| 18 |
""" The main part here is to create a TOC (table of contents) at the |
| 19 |
start of the page - linking down to the descriptions of the functions. |
| 20 |
Sure we need to generate anchors on the fly. Additionally, all the |
| 21 |
non-html (docbook-like) markup needs to be converted for ouput. - |
| 22 |
each element to be added should implement get_name(), get_head() and |
| 23 |
get_body() with the latter two having a xml_text() method.""" |
| 24 |
_null_table100 = '<table border="0" width="100%"' \ |
| 25 |
' cellpadding="0" cellspacing="0">' |
| 26 |
_ul_start = '<table width="100%">' |
| 27 |
_ul_end = '</table>' + "\n" |
| 28 |
_li_start = '<tr><td valign="top">' |
| 29 |
_li_end = '</td></tr>' + "\n" |
| 30 |
http_opengroup = "http://www.opengroup.org/onlinepubs/000095399/functions/" |
| 31 |
http_zlib = "http://www.zlib.net/manual.html" |
| 32 |
def __init__(self, o = None): |
| 33 |
self.toc = "" |
| 34 |
self.text = "" |
| 35 |
self.head = "" |
| 36 |
self.body = "" |
| 37 |
self.anchors = [] |
| 38 |
self.o = o |
| 39 |
if self.o is None: self.o = Options() |
| 40 |
self.not_found_in_anchors = [] |
| 41 |
def cut(self): |
| 42 |
self.text += ("<dt>"+self._ul_start+self.head+self._ul_end+"</dt>"+"\n"+ |
| 43 |
"<dd>"+self._ul_start+self.body+self._ul_end+"</dd>"+"\n") |
| 44 |
self.head = "" |
| 45 |
self.body = "" |
| 46 |
def add(self, entry): |
| 47 |
name = entry.get_name() |
| 48 |
head_text = entry.head_xml_text() |
| 49 |
body_text = entry.body_xml_text(name) |
| 50 |
if not head_text: |
| 51 |
print("no head_text for " + name) |
| 52 |
return |
| 53 |
try: |
| 54 |
prespec = entry.head_get_prespec() |
| 55 |
namespec = entry.head_get_namespec() |
| 56 |
callspec = entry.head_get_callspec() |
| 57 |
head_text = ("<code><b><function>"+namespec+"</function></b>" |
| 58 |
+callspec+" : "+prespec+"</code>") |
| 59 |
except Exception as e: |
| 60 |
pass |
| 61 |
try: |
| 62 |
extraline = "" |
| 63 |
title = entry.get_title() |
| 64 |
filename = short(entry.get_filename()) |
| 65 |
if title: |
| 66 |
subtitle = ' <em>'+title+'</em>' |
| 67 |
extraline = (self._null_table100+'<td> '+subtitle+' </td>'+ |
| 68 |
'<td align="right"> '+ |
| 69 |
'<em><small>'+filename+'</small></em>'+ |
| 70 |
'</td></table>' + "\n") |
| 71 |
body_text = extraline + body_text |
| 72 |
except Exception as e: |
| 73 |
pass |
| 74 |
def link(text): |
| 75 |
return (text & Match("<function>(\w*)</function>") |
| 76 |
>> "<link>\\1</link>") |
| 77 |
def here(text): |
| 78 |
has_function = Match("<function>(\w*)</function>") |
| 79 |
if text & has_function: |
| 80 |
func = has_function[1] |
| 81 |
self.anchors += [ func ] |
| 82 |
return (text & has_function |
| 83 |
>> '<a name="'+"\\1"+'">'+"\\1"+'</a>') |
| 84 |
else: |
| 85 |
return text |
| 86 |
self.toc += self._li_start+self.sane(link(head_text))+self._li_end |
| 87 |
self.head += self._li_start+self.sane(here(head_text))+self._li_end |
| 88 |
self.body += self._li_start+self.sane(body_text)+self._li_end |
| 89 |
def get_title(self): |
| 90 |
return self.o.package+" Library Functions" |
| 91 |
def xml_text(self): |
| 92 |
self.cut() |
| 93 |
return ("<h2>"+self.get_title()+"</h2>"+ |
| 94 |
self.version_line()+ |
| 95 |
self.mainheader_line()+ |
| 96 |
self._ul_start+ |
| 97 |
self.resolve_links(self.toc)+ |
| 98 |
self._ul_end+ |
| 99 |
"<h3>Documentation</h3>"+ |
| 100 |
"<dl>"+ |
| 101 |
self.resolve_links(self.text)+ |
| 102 |
"</dl>") |
| 103 |
def version_line(self): |
| 104 |
if self.o.version: |
| 105 |
return "<p>Version "+self.o.version+"</p>" |
| 106 |
return "" |
| 107 |
def mainheader_line(self): |
| 108 |
if self.o.onlymainheader: |
| 109 |
include = "#include <"+self.o.onlymainheader+">" |
| 110 |
return "<p><big><b><code>"+include+"</code></b></big></p>" |
| 111 |
return "" |
| 112 |
def resolve_links(self, text): |
| 113 |
text &= (Match("(?s)<link>([^<>]*)(\(\d\))</link>") |
| 114 |
>> (lambda x: self.resolve_external(x.group(1), x.group(2)))) |
| 115 |
text &= (Match("(?s)<link>(\w+)</link>") |
| 116 |
>> (lambda x: self.resolve_internal(x.group(1)))) |
| 117 |
if len(self.not_found_in_anchors): |
| 118 |
print("not found in anchors: {}".format(self.not_found_in_anchors)) |
| 119 |
return (text & Match("(?s)<link>([^<>]*)</link>") |
| 120 |
>> "<code>\\1</code>") |
| 121 |
def resolve_external(self, func, sect): |
| 122 |
x = Match() |
| 123 |
if func & x("^zlib(.*)"): |
| 124 |
return ('<a href="'+self.http_zlib+x[1]+'">'+ |
| 125 |
"<code>"+func+sect+"</code>"+'</a>') |
| 126 |
if sect & x("[23]"): |
| 127 |
return ('<a href="'+self.http_opengroup+func+'.html">'+ |
| 128 |
"<code>"+func+sect+"</code>"+'</a>') |
| 129 |
return "<code>"+func+"<em>"+sect+"</em></sect>" |
| 130 |
def resolve_internal(self, func): |
| 131 |
if func in self.anchors: |
| 132 |
return '<code><a href="#'+func+'">'+func+"</a></code>" |
| 133 |
if func not in self.not_found_in_anchors: |
| 134 |
self.not_found_in_anchors += [ func ] |
| 135 |
return "<code><u>"+func+"</u></code>" |
| 136 |
def sane(self, text): |
| 137 |
return (text |
| 138 |
.replace("<function>", "<code>") |
| 139 |
.replace("</function>", "</code>")) |
| 140 |
|