def syntax_pdf(data): """ Returns a PDF string """ from mcdp_report.html import ast_to_html s = data['s'] s = s.replace('\t', ' ') lines_to_hide = get_lines_to_hide(data['params']) def ignore_line(lineno): return lineno in lines_to_hide contents = ast_to_html(s, ignore_line=ignore_line, parse_expr=Syntax.ndpt_dp_rvalue, ) html = get_minimal_document(contents) d = mkdtemp() f_html = os.path.join(d, 'file.html') with open(f_html, 'w') as f: f.write(html) try: f_pdf = os.path.join(d, 'file.pdf') cmd= ['wkhtmltopdf','-s','A1',f_html,f_pdf] system_cmd_result( d, cmd, display_stdout=False, display_stderr=False, raise_on_error=True) f_pdf_crop = os.path.join(d, 'file_crop.pdf') cmd = ['pdfcrop', '--margins', '4', f_pdf, f_pdf_crop] system_cmd_result( d, cmd, display_stdout=False, display_stderr=False, raise_on_error=True) with open(f_pdf_crop) as f: data = f.read() f_png = os.path.join(d, 'file.png') cmd = ['convert', '-density', '600', f_pdf_crop, '-background', 'white', '-alpha', 'remove', '-resize', '50%', f_png] system_cmd_result( d, cmd, display_stdout=False, display_stderr=False, raise_on_error=True) with open(f_png) as f: pngdata = f.read() return [('pdf', 'syntax_pdf', data), ('png', 'syntax_pdf', pngdata)] except CmdException as e: raise e
def syntax_doc(data): from mcdp_report.html import ast_to_html s = data['s'] lines_to_hide = get_lines_to_hide(data['params']) def ignore_line(lineno): return lineno in lines_to_hide body_contents = ast_to_html(s, ignore_line=ignore_line, parse_expr=Syntax.ndpt_dp_rvalue) res = get_minimal_document(body_contents, add_markdown_css=True) # TODO: add minimal document res1 = ('html', 'syntax_doc', res) return [res1]
def render(library, docname, data, realpath, out_dir, generate_pdf): out = os.path.join(out_dir, docname + ".html") html_contents = render_complete( library=library, s=data, raise_errors=True, realpath=realpath, generate_pdf=generate_pdf ) doc = get_minimal_document(html_contents, add_markdown_css=True) d = os.path.dirname(out) if not os.path.exists(d): os.makedirs(d) with open(out, "w") as f: f.write(doc) logger.info("Written %s " % out)
def render(libname, docname, generate_pdf): librarian = get_test_librarian() library = librarian.load_library('manual') d = tempfile.mkdtemp() library.use_cache_dir(d) l = library.load_library(libname) basename = docname + '.' + MCDPLibrary.ext_doc_md f = l._get_file_data(basename) data = f['data'] realpath = f['realpath'] html_contents = render_complete(library=l, s=data, raise_errors=True, realpath=realpath, generate_pdf=generate_pdf) doc = get_minimal_document(html_contents, add_markdown_css=True) return ((libname, docname), doc)
def check_rendering(libname, filename): library = get_test_library(libname) import codecs data = codecs.open(filename, encoding='utf-8').read().encode('utf-8') tmpdir = tempfile.mkdtemp(prefix='mcdplibrary_cache') library.use_cache_dir(tmpdir) contents = render_complete(library, data, raise_errors=True, realpath=filename) html = get_minimal_document(contents, add_markdown_css=True) basename = os.path.basename(filename) fn = os.path.join('out', 'check_rendering', libname, basename + '.html') d = os.path.dirname(fn) if not os.path.exists(d): os.makedirs(d) with open(fn, 'w') as f: f.write(html) print('written to %r ' % fn) shutil.rmtree(tmpdir)