Format IP lists as tables
This commit changes the formatting of IP address lists from paragraph to table markup. The commit fixes PEP8 violations.
This commit is contained in:
		
							parent
							
								
									a44fff3e81
								
							
						
					
					
						commit
						f812a91310
					
				
					 1 changed files with 44 additions and 9 deletions
				
			
		|  | @ -8,7 +8,6 @@ | ||||||
|     :copyright: Copyright (c) 2016 Jan Dittberner |     :copyright: Copyright (c) 2016 Jan Dittberner | ||||||
|     :license: GPLv3+, see COPYING file for details. |     :license: GPLv3+, see COPYING file for details. | ||||||
| """ | """ | ||||||
| __version__ = '0.2.0' |  | ||||||
| 
 | 
 | ||||||
| import re | import re | ||||||
| 
 | 
 | ||||||
|  | @ -24,15 +23,20 @@ from sphinx.locale import l_ | ||||||
| from sphinx.roles import XRefRole | from sphinx.roles import XRefRole | ||||||
| from sphinx.util.nodes import make_refnode | from sphinx.util.nodes import make_refnode | ||||||
| 
 | 
 | ||||||
|  | __version__ = '0.2.0' | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| def ip_object_anchor(typ, path): | def ip_object_anchor(typ, path): | ||||||
|     path = re.sub(r'[.:/]', '-', path) |     path = re.sub(r'[.:/]', '-', path) | ||||||
|     return typ.lower() + '-' + path |     return typ.lower() + '-' + path | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class ip_node(nodes.Inline, nodes.TextElement): pass | class ip_node(nodes.Inline, nodes.TextElement): | ||||||
|  |     pass | ||||||
| 
 | 
 | ||||||
| class ip_range(nodes.General, nodes.Element): pass | 
 | ||||||
|  | class ip_range(nodes.General, nodes.Element): | ||||||
|  |     pass | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class IPXRefRole(XRefRole): | class IPXRefRole(XRefRole): | ||||||
|  | @ -42,7 +46,7 @@ class IPXRefRole(XRefRole): | ||||||
|     def __init__(self, method, index_type, **kwargs): |     def __init__(self, method, index_type, **kwargs): | ||||||
|         self.method = method |         self.method = method | ||||||
|         self.index_type = index_type |         self.index_type = index_type | ||||||
|         innernodeclass=None |         innernodeclass = None | ||||||
|         if method in ('v4', 'v6'): |         if method in ('v4', 'v6'): | ||||||
|             innernodeclass = ip_node |             innernodeclass = ip_node | ||||||
|         super(IPXRefRole, self).__init__( |         super(IPXRefRole, self).__init__( | ||||||
|  | @ -234,19 +238,46 @@ def sort_ip_info(item): | ||||||
|     return Network(item['ip']).ip |     return Network(item['ip']).ip | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | def create_table_row(rowdata): | ||||||
|  |     row = nodes.row() | ||||||
|  |     for cell in rowdata: | ||||||
|  |         entry = nodes.entry() | ||||||
|  |         row += entry | ||||||
|  |         entry += cell | ||||||
|  |     return row | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| def process_ip_nodes(app, doctree, fromdocname): | def process_ip_nodes(app, doctree, fromdocname): | ||||||
|     env = app.builder.env |     env = app.builder.env | ||||||
|     domaindata = env.domaindata[IPDomain.name] |     domaindata = env.domaindata[IPDomain.name] | ||||||
| 
 | 
 | ||||||
|  |     header = (l_('IP address'), l_('Used by')) | ||||||
|  |     colwidths = (1, 3) | ||||||
|  | 
 | ||||||
|     for node in doctree.traverse(ip_range): |     for node in doctree.traverse(ip_range): | ||||||
|         content = [] |         content = [] | ||||||
|         net = Network(node['rangespec']) |         net = Network(node['rangespec']) | ||||||
|         for ip_info in sorted(domaindata['ips'], key=sort_ip_info): |         ips = [ip_info for ip_info in | ||||||
|             if ip_info['ip'] in net: |                sorted(domaindata['ips'], key=sort_ip_info) | ||||||
|  |                if ip_info['ip'] in net] | ||||||
|  |         if ips: | ||||||
|  |             table = nodes.table() | ||||||
|  |             tgroup = nodes.tgroup(cols=len(header)) | ||||||
|  |             table += tgroup | ||||||
|  |             for colwidth in colwidths: | ||||||
|  |                 tgroup += nodes.colspec(colwidth=colwidth) | ||||||
|  |             thead = nodes.thead() | ||||||
|  |             tgroup += thead | ||||||
|  |             thead += create_table_row([ | ||||||
|  |                 nodes.paragraph(text=label) for label in header]) | ||||||
|  |             tbody = nodes.tbody() | ||||||
|  |             tgroup += tbody | ||||||
|  |             for ip_info in ips: | ||||||
|                 para = nodes.paragraph() |                 para = nodes.paragraph() | ||||||
|                 para += nodes.literal('', ip_info['ip']) |                 para += nodes.literal('', ip_info['ip']) | ||||||
|                 ids = ip_object_anchor(ip_info['typ'], ip_info['ip']) |                 ids = ip_object_anchor(ip_info['typ'], ip_info['ip']) | ||||||
|                 para['ids'].append(ids) |                 para['ids'].append(ids) | ||||||
|  | 
 | ||||||
|                 domaindata[ip_info['typ']][ids] = (fromdocname, '') |                 domaindata[ip_info['typ']][ids] = (fromdocname, '') | ||||||
|                 newnode = nodes.reference('', '', internal=True) |                 newnode = nodes.reference('', '', internal=True) | ||||||
|                 try: |                 try: | ||||||
|  | @ -257,9 +288,13 @@ def process_ip_nodes(app, doctree, fromdocname): | ||||||
|                 title = env.titles[ip_info['docname']] |                 title = env.titles[ip_info['docname']] | ||||||
|                 innernode = nodes.Text(title.astext()) |                 innernode = nodes.Text(title.astext()) | ||||||
|                 newnode.append(innernode) |                 newnode.append(innernode) | ||||||
|                 para += nodes.Text(" in ") |                 refnode = nodes.paragraph() | ||||||
|                 para += newnode |                 refnode.append(newnode) | ||||||
|                 content.append(para) |                 tbody += create_table_row([para, refnode]) | ||||||
|  |             content.append(table) | ||||||
|  |         else: | ||||||
|  |             para = nodes.paragraph(l_('No IP addresses in this range')) | ||||||
|  |             content.append(para) | ||||||
|         node.replace_self(content) |         node.replace_self(content) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue