Fetching contributors…
Cannot retrieve contributors at this time
executable file 414 lines (335 sloc) 16.7 KB
\n''' rels = get_rst_rels(current_doc, current_project) def_multirel = get_def_rel("multinuc",current_doc, current_project) def_rstrel = get_def_rel("rst",current_doc, current_project) multi_options ="" rst_options ="" rel_kinds = {} for rel in rels: if rel[1]=="multinuc": multi_options += "' rel_kinds[rel[0]] = "multinuc" else: rst_options += "' rel_kinds[rel[0]] = "rst" multi_options += "" nodes={} rows = get_rst_doc(current_doc,current_project,user) for row in rows: if row[7] in rel_kinds: relkind = rel_kinds[row[7]] else: relkind = "span" if row[5] == "edu": nodes[row[0]] = NODE(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],relkind) else: nodes[row[0]] = NODE(row[0],0,0,row[3],row[4],row[5],row[6],row[7],relkind) for key in nodes: node = nodes[key] get_depth(node,node,nodes) for key in nodes: if nodes[key].kind == "edu": get_left_right(key, nodes,0,0,rel_kinds) anchors = {} pix_anchors = {} # Calculate anchor points for nodes # First get proportional position for anchor for key in sorted(nodes, key = lambda id: nodes[id].depth, reverse=True): node = nodes[key] if node.kind=="edu": anchors[node.id]= "0.5" if node.parent!="0": parent = nodes[node.parent] parent_wid = (parent.right- parent.left+1) * 100 - 4 child_wid = (node.right- node.left+1) * 100 - 4 if node.relname == "span": if node.id in anchors: anchors[parent.id] = str(((node.left - parent.left)*100)/parent_wid+float(anchors[node.id])*float(child_wid/parent_wid)) else: anchors[parent.id] = str(((node.left - parent.left)*100)/parent_wid+(0.5*child_wid)/parent_wid) elif node.relkind=="multinuc" and parent.kind =="multinuc": # For multinucs, the anchor is in the middle between leftmost and rightmost of the multinuc children # (not including other rst children) lr = get_multinuc_children_lr(node.parent,current_doc,current_project,user) lr_wid = (lr[0] + lr[1]) /2 lr_ids = get_multinuc_children_lr_ids(node.parent,lr[0],lr[1],current_doc,current_project,user) left_child = lr_ids[0] right_child = lr_ids[1] if left_child == right_child: anchors[parent.id] = "0.5" else: if left_child in anchors and right_child in anchors: #both leftmost and rightmost multinuc children have been found len_left = nodes[left_child].right-nodes[left_child].left+1 len_right = nodes[right_child].right-nodes[right_child].left+1 anchors[parent.id] = str(((float(anchors[left_child]) * len_left*100 + float(anchors[right_child]) * len_right * 100 + (nodes[right_child].left - parent.left) * 100)/2)/parent_wid) else: anchors[parent.id] = str((lr_wid - parent.left+1) / (parent.right - parent.left+1)) else: if not parent.id in anchors: anchors[parent.id] = "0.5" # Place anchor element to center on proportional position relative to parent for key in nodes: node = nodes[key] pix_anchors[node.id] = str(int(3+node.left * 100 -100 - 39 + float(anchors[node.id])*((node.right- node.left+1) * 100 - 4))) for key in nodes: node = nodes[key] if node.kind != "edu": g_wid = str(int((node.right- node.left+1) *100 -4 )) cpout += '
\n' cpout += '\t
\n
\n' cpout += '
\n' cpout += '\t\n\t\t
'+str(int(node.left))+"-"+str(int(node.right))+'
' cpout += '\n\t\t\t\n' cpout += '\t\n\n
\n\n' elif node.kind=="edu": cpout += '
\n' cpout += '\t
' cpout += '\n\t
' cpout += '\n\t\t\n\t\t\t
'+str(int(node.left))+'
' cpout += '\n\t\t\t\t\n' cpout += '\n'+node.text+'\n' max_right = get_max_right(current_doc,current_project,user) cpout += '''\n ''' return cpout def rs3topng(rs3_filepath, png_filepath=None, base64_encoded=False): """Convert a RS3 file into a PNG image of the RST tree. If no output filename is given, the PNG image is returned (as a binary string). If base64 is True, the output file or the return value will contain the PNG image in base64 encoded form. """ try: from selenium import webdriver from selenium.common.exceptions import WebDriverException except ImportError: raise ImportError( 'Please install selenium: pip install selenium') html_str = rs3tohtml(rs3_filepath) temp = tempfile.NamedTemporaryFile(suffix='.html', delete=False) temp.write(html_str.encode('utf8')) temp.close() try: opts = webdriver.firefox.options.Options() opts.add_argument('--headless') driver = webdriver.Firefox(options=opts) except WebDriverException as err: raise WebDriverException( 'Please install Firefox and geckodriver: https://github.com/mozilla/geckodriver/releases\n' + err.msg) driver.get("file://{}".format(temp.name)) os.unlink(temp.name) doc_height = driver.execute_script(JS_GET_DOCUMENT_HEIGHT) doc_width = driver.execute_script(JS_GET_DOCUMENT_WIDTH) driver.set_window_size(height=doc_height, width=doc_width) png_str = driver.get_screenshot_as_png() if base64_encoded is True: png_str = base64.b64encode(png_str) if png_filepath: with open(png_filepath, 'w') as png_file: png_file.write(png_str) else: return png_str def embed_rs3_image(rs3_filepath, shrink_to_fit=True): """Render an RST tree given the path to an .rs3 file.""" from IPython.display import display, Image display(Image(rs3topng(rs3_filepath), unconfined=not(shrink_to_fit))) def embed_rs3str_image(rs3_string, shrink_to_fit=True): """Render an RST tree given the string content of a .rs3 file.""" temp = tempfile.NamedTemporaryFile(suffix='.rs3', delete=False) temp.write(rs3_string.encode('utf8')) temp.close() embed_rs3_image(temp.name, shrink_to_fit=shrink_to_fit) def cli(argv=sys.argv[1:]): parser = argparse.ArgumentParser( description="Convert an RS3 file into an HTML file containing the RST tree.") parser.add_argument('rs3_file') parser.add_argument('output_file', nargs='?') parser.add_argument( '-f', '--output-format', nargs='?', default='html', help="output format: html (default), png, png-base64") parser.add_argument( '-d', '--debug', action='store_true', help="run the program in pudb") args = parser.parse_args(argv) if args.debug: import pudb; pudb.set_trace() if args.output_format == 'png': if args.output_file: rs3topng(args.rs3_file, args.output_file) sys.exit(0) else: sys.stderr.write("No PNG output file given.\n") sys.exit(1) elif args.output_format == 'png-base64': if args.output_file: rs3topng(args.rs3_file, args.output_file, base64_encoded=True) sys.exit(0) else: base64_png_str = rs3topng(args.rs3_file, base64_encoded=True) sys.stdout.write(base64_png_str) sys.exit(0) if args.output_file: with codecs.open(args.output_file, 'w', 'utf8') as outfile: outfile.write(rs3tohtml(args.rs3_file)) else: sys.stdout.write(rs3tohtml(args.rs3_file).encode('utf8')) if __name__ == '__main__': cli(sys.argv[1:])
You can’t perform that action at this time.