def insert_comprehensive_test_button(page): '''inserts a button to enable all tests on a page be executed with a single click''' style = "margin-left: 400px; font-size: 40pt" text = "Run all tests" action = "check_all_code_samples('%s')" % page.pageid label = 'btn1_' + page.pageid btn1 = Element("button", style=style, onclick=action, id=label, label=label) btn1.text = text page.body.insert(0, btn1) # insert at the top label = 'btn2_' + page.pageid btn2 = Element("button", style=style, onclick=action, id=label, label=label) btn2.text = text page.body.append(btn2) # append at the bottom return
def insert_code(page, elem, uid, lines, lineno): '''insert the code in the element and style it appropriately''' # Note: in developping this plugin, we observed that the code was styled # automatically - that is the "div/getsource" handler was called before the # "pre" handler was. This could just be a coincidence on which we can not # rely. pre = Element("pre") if 'editor' in elem.attrib['title']: vlam = "editor" elif 'interpreter' in elem.attrib['title']: vlam = "interpreter" elif python_version < 3: vlam = "python" else: vlam = "python3" if "linenumber" in elem.attrib['title']: vlam += " linenumber=%s"%lineno pre.attrib['title'] = vlam pre.text ="".join(lines) if 'editor' in vlam: insert_editor(page, elem, uid, lines, lineno) elif 'interpreter' in vlam: insert_interpreter(page, elem, uid, lines, lineno) else: dummy, dummy = plugin['services'].style(page, pre, None, vlam) elem.append(pre) # prevent any further processing pre.attrib["title"] = "no_vlam" return
def insert_security_info(page, *dummy): """Inserts security information on a page""" if not len(page.body): return create_security_menu_item(page) # Next, the hidden container for the full security information if not page.includes("security_included"): page.add_include("security_included") page.insert_js_file("/javascript/security.js") info_container = Element("div", id="security_info") # prompt user to approve sites as soon as the first page is loaded # if there are sites for which to confirm the security level. if ((page.url.startswith("/index") or page.url=="/") # will work with /index_fr.html ... and config[page.username]['site_security'] # something to confirm and not config[page.username]['initial_security_set']): # only do it once per session confirm_at_start(page, info_container) config[page.username]['initial_security_set'] = True else: page.add_css_code(security_css%('none','none')) format_report(page, info_container) page.body.append(info_container) info_container_x = Element("div", id="security_info_x", onclick="hide_security_info()") info_container_x.text = "X" page.body.append(info_container_x)
def code_setup_process(page, elem, uid): """Style and saves a copy of the setup code. This code currently does not work because it depends on the obsoleted style_pycode service in the colourize plugin.""" raise NotImplementedError() vlam = elem.attrib["title"] name = extract_name(vlam) # next, we style the code, also extracting it in a useful form setup_code, markup, error = plugin['services'].style_pycode(page, elem, css_class='test_setup') if error is not None: markup = copy.deepcopy(elem) # which we store code_setups[name] = setup_code # reset the original element to use it as a container. For those # familiar with dealing with ElementTree Elements, in other context, # note that the style_pycode() method extracted all of the existing # text, removing any original markup (and other elements), so that we # do not need to save either the "text" attribute or the "tail" one # before resetting the element. elem.clear() elem.tag = "div" elem.attrib["id"] = "div_" + uid elem.attrib['class'] = "test_setup" # We insert the styled setup code inside this container element: elem.append(markup) # Create a title h4 = Element('h4') h4.text = "Setup code; name= %s" % name elem.insert(0, h4)
def code_setup_process(page, elem, uid): """Style and saves a copy of the setup code. This code currently does not work because it depends on the obsoleted style_pycode service in the colourize plugin.""" raise NotImplementedError() vlam = elem.attrib["title"] name = extract_name(vlam) # next, we style the code, also extracting it in a useful form setup_code, markup, error = plugin['services'].style_pycode( page, elem, css_class='test_setup') if error is not None: markup = copy.deepcopy(elem) # which we store code_setups[name] = setup_code # reset the original element to use it as a container. For those # familiar with dealing with ElementTree Elements, in other context, # note that the style_pycode() method extracted all of the existing # text, removing any original markup (and other elements), so that we # do not need to save either the "text" attribute or the "tail" one # before resetting the element. elem.clear() elem.tag = "div" elem.attrib["id"] = "div_" + uid elem.attrib['class'] = "test_setup" # We insert the styled setup code inside this container element: elem.append(markup) # Create a title h4 = Element('h4') h4.text = "Setup code; name= %s" % name elem.insert(0, h4)
def insert_security_info(page, *dummy): """Inserts security information on a page""" if not len(page.body): return create_security_menu_item(page) # Next, the hidden container for the full security information if not page.includes("security_included"): page.add_include("security_included") page.insert_js_file("/javascript/security.js") info_container = Element("div", id="security_info") # prompt user to approve sites as soon as the first page is loaded # if there are sites for which to confirm the security level. if ((page.url.startswith("/index") or page.url == "/") # will work with /index_fr.html ... and config[page.username]['site_security'] # something to confirm and not config[page.username]['initial_security_set']): # only do it once per session confirm_at_start(page, info_container) config[page.username]['initial_security_set'] = True else: page.add_css_code(security_css % ('none', 'none')) format_report(page, info_container) page.body.append(info_container) info_container_x = Element("div", id="security_info_x", onclick="hide_security_info()") info_container_x.text = "X" page.body.append(info_container_x)
def code_sample_process(page, elem, uid): """Style and saves a copy of the sample code. This code currently does not work because it depends on the obsoleted style_pycode service in the colourize plugin.""" raise NotImplementedError() vlam = elem.attrib["title"] name = extract_name(vlam) names[uid] = name # When a security mode is set to "display ...", we only parse the # page, but no Python execution from is allowed from that page. # If that is the case, we won't include javascript either, to make # thus making the source easier to read. if 'display' not in config[page.username]['page_security_level'](page.url): if not page.includes("code_test_included") : page.add_include("code_test_included") page.add_js_code(code_test_jscode) page.add_js_code(complete_test_jscode) page.add_css_code(css) insert_comprehensive_test_button(page) # next, we style the code, also extracting it in a useful form sample_code, markup, error = plugin['services'].style_pycode(page, elem) if error is not None: markup = copy.deepcopy(elem) # which we store code_samples[name] = sample_code # reset the original element to use it as a container. For those # familiar with dealing with ElementTree Elements, in other context, # note that the style_pycode() method extracted all of the existing # text, removing any original markup (and other elements), so that we # do not need to save either the "text" attribute or the "tail" one # before resetting the element. elem.clear() elem.tag = "div" elem.attrib["id"] = "div_" + uid elem.attrib['class'] = "crunchy" # We insert the styled sample code inside this container element: elem.append(markup) # Create a title h4 = Element('h4') h4.text = "Sample code; name= %s" % name elem.insert(0, h4) #some spacing: SubElement(elem, "br") # the actual button used for code execution: btn = SubElement(elem, "button") btn.text = "Run code check" btn.attrib["onclick"] = "exec_code_check('%s')" % uid SubElement(elem, "br") # finally, an output subwidget: plugin['services'].insert_io_subwidget(page, elem, uid) return
def code_sample_process(page, elem, uid): """Style and saves a copy of the sample code. This code currently does not work because it depends on the obsoleted style_pycode service in the colourize plugin.""" raise NotImplementedError() vlam = elem.attrib["title"] name = extract_name(vlam) names[uid] = name # When a security mode is set to "display ...", we only parse the # page, but no Python execution from is allowed from that page. # If that is the case, we won't include javascript either, to make # thus making the source easier to read. if 'display' not in config[page.username]['page_security_level'](page.url): if not page.includes("code_test_included"): page.add_include("code_test_included") page.add_js_code(code_test_jscode) page.add_js_code(complete_test_jscode) page.add_css_code(css) insert_comprehensive_test_button(page) # next, we style the code, also extracting it in a useful form sample_code, markup, error = plugin['services'].style_pycode(page, elem) if error is not None: markup = copy.deepcopy(elem) # which we store code_samples[name] = sample_code # reset the original element to use it as a container. For those # familiar with dealing with ElementTree Elements, in other context, # note that the style_pycode() method extracted all of the existing # text, removing any original markup (and other elements), so that we # do not need to save either the "text" attribute or the "tail" one # before resetting the element. elem.clear() elem.tag = "div" elem.attrib["id"] = "div_" + uid elem.attrib['class'] = "crunchy" # We insert the styled sample code inside this container element: elem.append(markup) # Create a title h4 = Element('h4') h4.text = "Sample code; name= %s" % name elem.insert(0, h4) #some spacing: SubElement(elem, "br") # the actual button used for code execution: btn = SubElement(elem, "button") btn.text = "Run code check" btn.attrib["onclick"] = "exec_code_check('%s')" % uid SubElement(elem, "br") # finally, an output subwidget: plugin['services'].insert_io_subwidget(page, elem, uid) return
def insert_tooltip(page, *dummy): '''inserts a (hidden) tooltip object in a page''' if not page.includes("tooltip_included") and len(page.body): borg_console[page.pageid] = interpreter.BorgConsole(group=page.pageid) page.add_include("tooltip_included") page.insert_js_file("/javascript/tooltip.js") page.add_js_code(tooltip_js) tooltip = Element("div") tooltip.attrib["id"] = "interp_tooltip" tooltip.text = " " page.body.append(tooltip) help_menu = Element("div") help_menu.attrib["id"] = "help_menu" help_menu.text = " " page.body.append(help_menu) help_menu_x = Element("div") help_menu_x.attrib["id"] = "help_menu_x" help_menu_x.attrib["onclick"] = "hide_help();" help_menu_x.text = "X" page.body.append(help_menu_x)
def insert_browser(page, *dummy): # tested '''Inserts a default file/url browser at the bottom of a page''' div = Element("div") div.text = ' ' browser = plugin.get(config[page.username]['power_browser']) if not browser: return try: browser(page, div, 'dummy') page.body.append(div) except: print(traceback.format_exc()) return
def insert_markup(elem, uid, vlam, markup, interactive_type): '''clears an element and inserts the new markup inside it''' elem.clear() elem.tag = "div" elem.attrib["id"] = "div_"+uid elem.attrib['class'] = interactive_type # 'editor', 'doctest', 'interpreter' if not "no_pre" in vlam: try: new_div = Element("div") new_div.append(markup) new_div.attrib['class'] = 'sample_python_code' elem.insert(0, new_div) except AssertionError: # this should never happen elem.insert(0, Element("br")) bold = Element("b") span = Element("span") span.text = "AssertionError from ElementTree" bold.append(span) elem.insert(1, bold)
def transform_rst(page): """transforms the elements in an rst page so that they can be merged into a template. It is assumed that the entire body content is to be used as the content inside the template.""" for div in page.tree.findall(".//div"): if "class" in div.attrib: if div.attrib["class"] == "document" or div.attrib["class"] == "section": div.tag = "span" new_body = copy.deepcopy(page.body) new_body.tag = "div" new_body.attrib["id"] = "content" page.body.clear() page.body.text = "" page.body.append(new_body) for title in page.tree.findall(".//title"): # there should be only one: div = Element("div") div.attrib["id"] = "main_title" div.text = title.text page.body.insert(0, div) break
def create_show_vlam(page, elem, vlam): '''Creates a <code> element showing the complete vlam options used, as well as the element type.''' if 'show_vlam' not in vlam: return None cssclass = config[page.username]['style'] attributes = ' title="%s"' % vlam for attr in elem.attrib: if attr != 'title': attributes += ' %s="%s"' % (attr, elem.attrib[attr]) elem_info = '<%s%s> ... </%s>' % (elem.tag, attributes, elem.tag) styled_elem_info = _style(elem_info, 'html', cssclass) show_vlam = fromstring(styled_elem_info) show_vlam.tag = 'code' show_vlam.attrib['class'] = CRUNCHY_PYGMENTS display = Element('h3') display.attrib['class'] = "show_vlam" display.text = "VLAM = " display.append(show_vlam) return display
def insert_markup(elem, uid, vlam, markup, interactive_type): '''clears an element and inserts the new markup inside it''' elem.clear() elem.tag = "div" elem.attrib["id"] = "div_" + uid elem.attrib[ 'class'] = interactive_type # 'editor', 'doctest', 'interpreter' if not "no_pre" in vlam: try: new_div = Element("div") new_div.append(markup) new_div.attrib['class'] = 'sample_python_code' elem.insert(0, new_div) except AssertionError: # this should never happen elem.insert(0, Element("br")) bold = Element("b") span = Element("span") span.text = "AssertionError from ElementTree" bold.append(span) elem.insert(1, bold)
def transform_rst(page): '''transforms the elements in an rst page so that they can be merged into a template. It is assumed that the entire body content is to be used as the content inside the template.''' for div in page.tree.findall(".//div"): if 'class' in div.attrib: if (div.attrib['class'] == 'document' or div.attrib['class'] == 'section'): div.tag = 'span' new_body = copy.deepcopy(page.body) new_body.tag = "div" new_body.attrib["id"] = "content" page.body.clear() page.body.text = '' page.body.append(new_body) for title in page.tree.findall(".//title"): # there should be only one: div = Element("div") div.attrib["id"] = "main_title" div.text = title.text page.body.insert(0, div) break
def wrap_in_div(elem, uid, vlam, element_type, show_vlam): '''wraps a styled code inside a div''' elem_copy = copy.deepcopy(elem) elem.clear() elem.text = '' elem.tag = "div" elem.attrib["id"] = "div_" + uid username = names[uid.split("_")[0]] # element_type = 'editor', 'doctest', etc. elem.attrib['class'] = element_type + " " + config[username]['style'] if not "no_pre" in vlam: try: elem.append(elem_copy) except AssertionError: # this should never happen elem.insert(0, Element("br")) bold = Element("b") span = Element("span") span.text = "AssertionError from ElementTree" bold.append(span) elem.insert(1, bold) return if show_vlam is not None: elem.insert(0, show_vlam)
def wrap_in_div(elem, uid, vlam, element_type, show_vlam): '''wraps a styled code inside a div''' elem_copy = copy.deepcopy(elem) elem.clear() elem.text = '' elem.tag = "div" elem.attrib["id"] = "div_"+uid username = names[uid.split("_")[0]] # element_type = 'editor', 'doctest', etc. elem.attrib['class'] = element_type + " " + config[username]['style'] if not "no_pre" in vlam: try: elem.append(elem_copy) except AssertionError: # this should never happen elem.insert(0, Element("br")) bold = Element("b") span = Element("span") span.text = "AssertionError from ElementTree" bold.append(span) elem.insert(1, bold) return if show_vlam is not None: elem.insert(0, show_vlam)