def tryit(s, write_to=None, forbid=[]): library = MCDPLibrary() raise_errors = True realpath = 'transformations.py' s2 = render_complete(library, s, raise_errors, realpath, generate_pdf=False) if False: if write_to is not None: doc = get_minimal_document(s2, add_manual_css=True) with open(write_to, 'wb') as f: f.write(doc) print('written to %s' % write_to) tests = { 'doctype': not 'DOCTYPE' in s2, 'warn_caption': not 'caption' in s2, 'warn_centering': not 'centering' in s2, 'warn_tabular': not 'tabular' in s2, 'funny': not '&#96;' in s2, 'dollarfix': not TAG_DOLLAR in s2, # assert not '`' in s2 # assert not '&' in s2 } for x in forbid: tests['contains %r' % x] = not x in s2 msg = '' summary = {'warn': 0, 'error': 0} for k in sorted(tests): level = 'warn' if 'warn' in k else 'error' passed = tests[k] if not passed: summary[level] += 1 mark = '✓' if passed else 'no' msg += '\n %20s : %s' % (k, mark) if summary['error']: if len(s2) < 10000: msg += '\n\n' + indent(s2, '> ') if write_to is not None: msg += '\nSee output in %s' % (write_to) raise_desc(Exception, msg) return s2
def check_rendering(libname, filename): library = get_test_library(libname) data = read_file_encoded_as_utf8(filename) expected = get_expected_exceptions(data) with with_library_cache_dir(library): try: contents = render_complete(library, data, raise_errors=True, realpath=filename) # write html html = get_minimal_document(contents, add_markdown_css=True) basename = os.path.basename(filename) fn = os.path.join('out', 'check_rendering', libname, basename + '.html') write_file_encoded_as_utf8(fn, html) except expected: return
def syntax_doc(data, mark_warnings=True): 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 library = data['library'] context = library._generate_context_with_hooks() class Tmp: string_nospaces_parse_tree_interpreted = None def postprocess(block): x = parse_ndp_refine(block, context) Tmp.string_nospaces_parse_tree_interpreted = x return x body = ast_to_html(s, ignore_line=ignore_line, parse_expr=Syntax.ndpt_dp_rvalue, postprocess=postprocess) from mcdp_web.editor_fancy.app_editor_fancy_generic import html_mark if mark_warnings: for w in context.warnings: if w.where is not None: body = html_mark(body, w.where, "language_warning", tooltip=w.format_user()) res = get_minimal_document(body, add_markdown_css=True) # TODO: add minimal document res1 = ('html', 'syntax_doc', res) return [res1]
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