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 | ||||
|     :license: GPLv3+, see COPYING file for details. | ||||
| """ | ||||
| __version__ = '0.2.0' | ||||
| 
 | ||||
| import re | ||||
| 
 | ||||
|  | @ -24,15 +23,20 @@ from sphinx.locale import l_ | |||
| from sphinx.roles import XRefRole | ||||
| from sphinx.util.nodes import make_refnode | ||||
| 
 | ||||
| __version__ = '0.2.0' | ||||
| 
 | ||||
| 
 | ||||
| def ip_object_anchor(typ, path): | ||||
|     path = re.sub(r'[.:/]', '-', 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): | ||||
|  | @ -42,7 +46,7 @@ class IPXRefRole(XRefRole): | |||
|     def __init__(self, method, index_type, **kwargs): | ||||
|         self.method = method | ||||
|         self.index_type = index_type | ||||
|         innernodeclass=None | ||||
|         innernodeclass = None | ||||
|         if method in ('v4', 'v6'): | ||||
|             innernodeclass = ip_node | ||||
|         super(IPXRefRole, self).__init__( | ||||
|  | @ -234,19 +238,46 @@ def sort_ip_info(item): | |||
|     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): | ||||
|     env = app.builder.env | ||||
|     domaindata = env.domaindata[IPDomain.name] | ||||
| 
 | ||||
|     header = (l_('IP address'), l_('Used by')) | ||||
|     colwidths = (1, 3) | ||||
| 
 | ||||
|     for node in doctree.traverse(ip_range): | ||||
|         content = [] | ||||
|         net = Network(node['rangespec']) | ||||
|         for ip_info in sorted(domaindata['ips'], key=sort_ip_info): | ||||
|             if ip_info['ip'] in net: | ||||
|         ips = [ip_info for ip_info in | ||||
|                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.literal('', ip_info['ip']) | ||||
|                 ids = ip_object_anchor(ip_info['typ'], ip_info['ip']) | ||||
|                 para['ids'].append(ids) | ||||
| 
 | ||||
|                 domaindata[ip_info['typ']][ids] = (fromdocname, '') | ||||
|                 newnode = nodes.reference('', '', internal=True) | ||||
|                 try: | ||||
|  | @ -257,9 +288,13 @@ def process_ip_nodes(app, doctree, fromdocname): | |||
|                 title = env.titles[ip_info['docname']] | ||||
|                 innernode = nodes.Text(title.astext()) | ||||
|                 newnode.append(innernode) | ||||
|                 para += nodes.Text(" in ") | ||||
|                 para += newnode | ||||
|                 content.append(para) | ||||
|                 refnode = nodes.paragraph() | ||||
|                 refnode.append(newnode) | ||||
|                 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) | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue