def run(self, root): elements = list( root) # should be an iterator, but 2.6 getiterator vs 2.7 iter. root.clear() target = root example_num = 0 for element in elements: if HEADER_R.match(element.tag): target = root elif element.tag == 'pre': sub = list(element) assert len(sub) == 1 code_el = sub[0] assert code_el.tag == 'code' format, code, args = splitcode(code_el.text) divclasses = [format] if args == 'forbidden': divclasses.append(args) if format.lower() == 'n3': divclasses.append('mlr') subdiv = etree.Element('div', {'class': ' '.join(divclasses)}) subdiv.append(element) element = subdiv if target is root: example_num += 1 if self.buttons: button = etree.Element('button', { 'onclick': "$('#eg%d').toggle();" % (example_num, ) }) button.text = 'Example' root.append(button) divattr = { "class": "example", 'id': 'eg%d' % (example_num, ) } if self.hide_eg: divattr['style'] = 'display:none' div = etree.Element('div', divattr) if not self.delete_eg: root.append(div) target = div target.append(element) return root
def test_document(): m = markdown.Markdown() data = open('rationale.md').read().decode('utf-8') root = m.parser.parseDocument(data.split('\n')).getroot() data = [] title = '' for element in root.getiterator(): if HEADER_R.match(element.tag): if data: yield t_function, data, title data = [] title = element.text if element.tag == 'pre': sub = list(element) assert len(sub) == 1 and sub[0].tag == 'code' format, text, args = splitcode(sub[0].text) if format: data.append((format, text, args)) if data: yield t_function, data, title
def run(self, root): elements = list(root) # should be an iterator, but 2.6 getiterator vs 2.7 iter. root.clear() target = root example_num = 0 for element in elements: if HEADER_R.match(element.tag): target = root elif element.tag == 'pre': sub = list(element) assert len(sub) == 1 code_el = sub[0] assert code_el.tag == 'code' format, code, args = splitcode(code_el.text) divclasses = [format] if args == 'forbidden': divclasses.append(args) if format.lower() == 'n3': divclasses.append('mlr') subdiv = etree.Element('div', {'class': ' '.join(divclasses)}) subdiv.append(element) element = subdiv if target is root: example_num += 1 if self.buttons: button = etree.Element('button', {'onclick': "$('#eg%d').toggle();" % (example_num,)}) button.text = 'Example' root.append(button) divattr = {"class": "example", 'id': 'eg%d' % (example_num,)} if self.hide_eg: divattr['style'] = 'display:none' div = etree.Element('div', divattr) if not self.delete_eg: root.append(div) target = div target.append(element) return root
def run(self, root): elements = list(root) # should be an iterator, but 2.6 getiterator vs 2.7 iter. root.clear() target = root graphs = [] error = False example_num = 0 for element in elements: if HEADER_R.match(element.tag): if graphs and not error: response = self.make_response(graphs) if response: target.append(response) target = root graphs = [] error = False print(" " * int(element.tag[1]) + element.text) elif element.tag == 'pre': sub = list(element) assert len(sub) == 1 code_el = sub[0] assert code_el.tag == 'code' format, code, args = splitcode(code_el.text) code_el.text = ":::%s\n%s" % (format, code) # remove args divclasses = [format] if args == 'forbidden': divclasses.append(args) if format.lower() == 'n3': divclasses.append('mlr') subdiv = etree.Element('div', {'class': ' '.join(divclasses)}) subdiv.append(element) element = subdiv if target is root: example_num += 1 if self.buttons: button = etree.Element('button', {'onclick': "$('#eg%d').toggle();" % (example_num,)}) button.text = 'Example' root.append(button) divattr = {"class": "example", 'id': 'eg%d' % (example_num,)} if self.hide_eg: divattr['style'] = 'display:none' div = etree.Element('div', divattr) if not self.delete_eg: root.append(div) target = div if format.lower() == 'xml': try: self.graph_tester.set_lom(code) except Exception as e: target.append(element) p2 = etree.Element('pre', {"class": "error"}) tr = etree.Element('code') p2.append(tr) tr.text = ":::Python Traceback\n" + traceback.format_exc() element = p2 print('*', e) error = True elif format.lower() == 'n3': try: graph, errors = self.graph_tester.test_n3(code, args) if errors: target.append(element) diverrors = etree.Element('div', {"class": "error"}) p = etree.Element('p') p.text = "Erreur. Obtenu: " diverrors.append(p) pre = etree.Element('pre') diverrors.append(pre) graph_e = etree.Element('code') pre.append(graph_e) result = graph.serialize(format='n3', encoding='utf-8') graph_e.text = ':::N3\n' + self.remove_namespace(result).decode('utf-8') for err_type, error in errors: p = etree.Element('p') error = tuple(map(stringify_error_element, error)) if err_type == GraphTester.MISSING: p.text = u"Il manque < %s %s %s >." % error elif err_type == GraphTester.UNEXPECTED: p.text = u"< %s %s %s < est présent et ne devrait pas l'être." % error print('*', p.text) diverrors.append(p) element = diverrors except Exception as e: target.append(element) p2 = etree.Element('pre', {"class": "error"}) tr = etree.Element('code') p2.append(tr) tr.text = ":::Python Traceback\n" + traceback.format_exc() element = p2 print('*', e) error = True target.append(element) if graphs and not error: response = self.make_response(graphs) if response: target.append(response) return root