def __init__(self, generator, ecl_file): self.xml_file = joinpath(generator.xml_root, ecl_file + '.xml') self.txt_file = joinpath(generator.txt_root, ecl_file + '.txt') self.template = generator.content_template self.options = generator.options os.makedirs(dirname(self.txt_file), exist_ok=True)
def gen(self, key, node, content_root) : ''' Recursively parse source tree dictionary. If current_node is file : parse file using parseHTML Else If : current_node is directory : recurse and generate pkg.toc.html for that dir (optionally generate bundle info if present) :param key: string | the name of current_node in path tree :param node: dict | the parent tree of current_node :param content_root: string | real path to current node in html doc dir ''' current_node = node[key] if type(current_node) != dict: if key == 'bundle.ecl' : return None parser = ParseHTML(self, node[key]) parser.parse() file = {'name' : key, 'target' : key + '.html', 'type' : 'file', 'doc' : parser.docstring() } return file else : file = {'name' : key, 'target': joinpath(key, 'pkg.toc.html'), 'type': 'dir', 'doc' : '' } bundle = None if 'bundle.ecl' in current_node : bundle_xml_path = joinpath(self.xml_root, dirname(current_node['bundle.ecl']), 'bundle.xml') bundle = etree.parse(bundle_xml_path).getroot() license = bundle.find('License') license.text = '<a href="' + license.text + '">' + license.text + '</a>' file['type'] = 'bundle' childfiles = [] keys = sorted(current_node.keys(), key=str.lower) for chkey in keys : child_root = joinpath(content_root, chkey) child_dict = self.gen(chkey, current_node, child_root) if child_dict is not None : childfiles.append(child_dict) childfiles = sorted(childfiles, key=lambda x : x['type']) root_relpath = relpath(self.output_root, content_root) parent_relpath = '' if content_root != self.html_root : parent_relpath = relpath(dirname(content_root), content_root) render = self.toc_template.render(name=key, files=childfiles, parent=joinpath(parent_relpath, 'pkg.toc.html'), output_root=root_relpath, bundle=bundle) os.makedirs(content_root, exist_ok=True) render_path = joinpath(content_root, 'pkg.toc.html') write_to_file(render_path, render) return file
def run(self) : ''' Main function called by ecldoc ''' print("\nGenerating HTML Documentation ... ") self.gen('root', self.ecl_file_tree, self.html_root) if os.path.exists(joinpath(self.output_root, 'css')) : shutil.rmtree(joinpath(self.output_root, 'css')) shutil.copytree(joinpath(self.template_dir, 'css'), joinpath(self.output_root, 'css'))
def __init__(self, generator, ecl_file) : self.output_root = generator.output_root self.xml_file = joinpath(generator.xml_root, (ecl_file + '.xml')) self.html_file = joinpath(generator.html_root, (ecl_file + '.html')) self.template = generator.content_template ### Parent node of given file in path tree self.parent = getRoot(generator.ecl_file_tree, ecl_file) self.options = generator.options os.makedirs(dirname(self.html_file), exist_ok=True)
def __init__(self, generator, ecl_file): self.xml_file = joinpath(generator.xml_root, ecl_file + '.xml') self.tex_file = joinpath(generator.tex_root, ecl_file + '.tex') tex_relpath = relpath(self.tex_file, generator.tex_path) self.dirname = dirname(tex_relpath) self.template = generator.content_template self.options = generator.options os.makedirs(dirname(self.tex_file), exist_ok=True)
def __init__(self, input_root, output_root, ecl_file_tree, options) : self.input_root = input_root self.output_root = output_root self.html_root = joinpath(output_root, 'html') self.xml_root = joinpath(output_root, 'xml') self.template_dir = HTML_TEMPLATE_DIR self.content_template = html_jinja_env.get_template(joinpath(self.template_dir, 'content.tpl.html')) self.toc_template = html_jinja_env.get_template(joinpath(self.template_dir, 'toc.tpl.html')) self.ecl_file_tree = ecl_file_tree self.options = options
def run(self): ''' Main Function called by ECLDOC ''' print("\nGenerating PDF Documentation ... ") self.gen('root', self.ecl_file_tree, self.tex_root) render_path = joinpath(self.tex_root, 'pkg.toc.tex') start_path = relpath(render_path, self.tex_path) render = self.index_template.render(root=start_path) write_to_file(joinpath(self.tex_path, 'index.tex'), render) subprocess.run(['pdflatex index.tex'], cwd=self.tex_path, shell=True)
def parse(self) : tree = etree.parse(self.xml_file) root = tree.getroot() src = root.find('Source') self.src = src self.doc = src.find('Documentation') for child in root.iter() : attribs = child.attrib ### Convert links from XML FOrmat to HTML Format if 'target' in attribs : attribs['target'] = re.sub(r'\$\$_ECLDOC-FORM_\$\$', 'html', attribs['target']) attribs['target'] = re.sub(r'\.xml$', '.html', attribs['target']) ### Fullname act as HTML Id for Definitions ### '.' is invalid char in HTML Id so replaced by '-' if 'fullname' in attribs : attribs['origfn'] = attribs['fullname'] attribs['fullname'] = re.sub(r'\.', '-', attribs['fullname']) self.parseSource() files = [] keys = sorted(self.parent.keys(), key=str.lower) for key in keys : if type(self.parent[key]) != dict : file = {'name': key, 'target': key + '.html', 'type': 'file'} files.append(file) else : file = {'name': key, 'target': joinpath(key, 'pkg.toc.html'), 'type': 'dir'} files.append(file) files = sorted(files, key=lambda x : x['type']) parent = 'pkg.toc.html' output_relpath = relpath(self.output_root, dirname(self.html_file)) render = self.template.render(src=src, files=files, parent=parent, output_root=output_relpath, defn_tree=self.defn_tree) write_to_file(self.html_file, render)
def parse(self): root = etree.parse(self.xml_file).getroot() src = root.find('Source') self.src = src self.doc = src.find('Documentation') for child in root.iter(): attribs = child.attrib if 'target' in attribs: attribs['target'] = re.sub(r'\$\$_ECLDOC-FORM_\$\$', joinpath('tex', 'root'), attribs['target']) attribs['target'] = re.sub(r'\.xml$', '.tex', attribs['target']) name = src.attrib['name'].split('.') self.parseSource() render = self.template.render(name=name, src=src, defn_tree=self.defn_tree, up=('toc:' + self.dirname)) write_to_file(self.tex_file, render)
def __init__(self, input_root, output_root, ecl_file_tree, options): self.input_root = input_root self.output_root = output_root self.tex_path = joinpath(output_root, 'tex') self.tex_root = joinpath(output_root, 'tex', 'root') self.xml_root = joinpath(output_root, 'xml') os.makedirs(self.tex_root, exist_ok=True) self.template_dir = TEX_TEMPLATE_DIR self.content_template = latex_jinja_env.get_template( joinpath(self.template_dir, 'content.tpl.tex')) self.toc_template = latex_jinja_env.get_template( joinpath(self.template_dir, 'toc.tpl.tex')) self.index_template = latex_jinja_env.get_template( joinpath(self.template_dir, 'index.tpl.tex')) self.ecl_file_tree = ecl_file_tree self.options = options
from .genTEX import latex_jinja_env, TEX_TEMPLATE_DIR, escape_tex from ecldoc.Utils import call_macro_by_name, joinpath from ecldoc.parseDoc import construct_type import lxml.html as H latex_jinja_env.filters['macro'] = call_macro_by_name tag_template = latex_jinja_env.get_template( joinpath(TEX_TEMPLATE_DIR, 'taglets.tpl.tex')) ############################################################### def convertToLatex(html_text): ''' Convert HTML String to Markdown Format ''' root = H.fragment_fromstring(html_text, create_parent='div') text = parseHTMltoLatex(root) return text def parseHTMltoLatex(element): ''' Convert Single HTML element to Markdown Text (recursive) ''' text = '' if element.text: text += escape_tex(element.text) for e in element.iterchildren(): text += parseHTMltoLatex(e) if e.tail:
import os, re, shutil from lxml import etree from ecldoc.Utils import getRoot, write_to_file from ecldoc.Utils import joinpath, relpath, dirname ################################################################## from ecldoc.Constants import TEMPLATE_DIR HTML_TEMPLATE_DIR = joinpath(TEMPLATE_DIR, 'html') ################################################################## import jinja2 html_jinja_env = jinja2.Environment( loader = jinja2.FileSystemLoader(os.path.abspath('/')) ) ################################################################# from ecldoc.parseDoc import getTags from ecldoc.Taglets import taglets from .tagHTML import tag_renders class ParseHTML(object) : ''' Main class to generate HTML Documentation for given ecl file from its XML Repr ''' def __init__(self, generator, ecl_file) : self.output_root = generator.output_root
def gen(self, key, node, content_root): ''' Recursively parse source tree dictionary. If current_node is file : parse file using parseTEX Else If : current_node is directory : recurse and generate pkg.toc.tex for that dir (optionally generate bundle info if present) :param key: string | the name of current_node in path tree :param node: dict | the parent tree of current_node :param content_root: string | real path to current node in tex doc dir ''' current_node = node[key] if type(current_node) != dict: if key == 'bundle.ecl': return parser = ParseTEX(self, current_node) parser.parse() file = {'name': key, 'type': 'file', 'doc': parser.docstring()} file['target'] = relpath(parser.tex_file, self.tex_path) file['label'] = parser.src.attrib['name'] return file else: os.makedirs(content_root, exist_ok=True) render_path = joinpath(content_root, 'pkg.toc.tex') temptoc_render_path = joinpath(content_root, 'pkg.tmp.tex') index_render_path = joinpath(content_root, 'index.tex') tex_relpath = relpath(content_root, self.tex_path) target_relpath = relpath(render_path, self.tex_path) file = { 'name': key, 'type': 'dir', 'doc': '', 'target': target_relpath, 'label': tex_relpath } bundle = None if 'bundle.ecl' in current_node: bundle_xml_path = joinpath(self.xml_root, dirname(current_node['bundle.ecl']), 'bundle.xml') bundle = etree.parse(bundle_xml_path).getroot() file['type'] = 'bundle' childfiles = [] child_keys = sorted(current_node.keys(), key=str.lower) for chkey in child_keys: child_root = joinpath(content_root, chkey) child_dict = self.gen(chkey, current_node, child_root) if child_dict is not None: childfiles.append(child_dict) childfiles = sorted(childfiles, key=lambda x: x['type'], reverse=True) render = self.toc_template.render(name=key, files=childfiles, bundle=bundle, label=tex_relpath, up=dirname(tex_relpath)) write_to_file(render_path, render) render = self.toc_template.render( name=key, files=[x for x in childfiles if x['type'] == 'file'], bundle=bundle, label=tex_relpath, up="") write_to_file(temptoc_render_path, render) start_path = relpath(temptoc_render_path, self.tex_path) render = self.index_template.render(root=start_path) write_to_file(index_render_path, render) subprocess.run([ 'pdflatex ' + '-output-directory ' + relpath(content_root, self.tex_path) + ' ' + relpath(index_render_path, self.tex_path) ], cwd=self.tex_path, shell=True) return file
import os import re import subprocess from lxml import etree from ecldoc.Utils import write_to_file from ecldoc.Utils import joinpath, relpath, dirname ############################################################## from ecldoc.Constants import TEMPLATE_DIR TEX_TEMPLATE_DIR = joinpath(TEMPLATE_DIR, 'tex') ############################################################## import jinja2 latex_jinja_env = jinja2.Environment(block_start_string='\\BLOCK{', block_end_string='}', variable_start_string='\\VAR{', variable_end_string='}', comment_start_string='\\#{', comment_end_string='}', line_statement_prefix='%%', line_comment_prefix='%#', trim_blocks=True, autoescape=False, loader=jinja2.FileSystemLoader( os.path.abspath('/'))) LATEX_SUBS = ( (re.compile(r'\\'), r'\\textbackslash '),
import os import re from lxml import etree from ecldoc.Utils import write_to_file from ecldoc.Utils import joinpath, dirname ################################################################### from ecldoc.Constants import TEMPLATE_DIR TXT_TEMPLATE_DIR = joinpath(TEMPLATE_DIR, 'txt') ################################################################### import jinja2 txt_jinja_env = jinja2.Environment( loader=jinja2.FileSystemLoader(os.path.abspath('/'))) def indent_doc(s, level, width=4): indention = (u' ' * (width - 1) + u'| ') * level rv = (u'\n' + indention).join(s.splitlines()) rv = indention + rv return rv txt_jinja_env.filters['indent_doc'] = indent_doc ##################################################################
from .genHTML import html_jinja_env, HTML_TEMPLATE_DIR from ecldoc.Utils import call_macro_by_name, joinpath from ecldoc.parseDoc import construct_type html_jinja_env.filters['macro'] = call_macro_by_name tag_template = html_jinja_env.get_template( joinpath(HTML_TEMPLATE_DIR, 'taglets.tpl.html')) def render_param(tag_param): if len(tag_param.tuples['tuples']) == 0: return '' html_tuples = [] for t in tag_param.tuples['tuples']: html_tuples.append((t[0], construct_type(t[2]), t[1])) render = tag_template.render(render_name='threetag', args=['Parameters', html_tuples]) return render def render_field(tag_field): if len(tag_field.tuples['tuples']) == 0: return '' html_tuples = [] for t in tag_field.tuples['tuples']: html_tuples.append((t[0], construct_type(t[2]), t[1])) return tag_template.render(render_name='threetag', args=['Fields', html_tuples]) def render_return(tag_return):