def test_simple__multiline_paragraph(): test = \ """ This is a test of the assembler. This line should be followed by a br tag """ test = textwrap.dedent(test).strip() expected_text =\ [ "This is a test of the assembler. ", "This line should be followed by a ", E("br"), "br tag" ] root_kwargs = {"class": "brutemark_root"} root = E("div", E("p", *expected_text), **root_kwargs) expected = tostring(root, pretty_print=True, encoding="unicode") actual_string = markdown(test) assert expected == actual_string
def Render(cls, elements): lines = [] for element in elements: # Codeline does not have preprocessed bodies lines.append(element.content) body = "\n".join(lines) return E("pre", E("code", body))
def Render(cls, elements): lines = [] for token in elements: if hasattr(token, "render"): lines.append(token.render()) else: lines.append(E("li", token.content)) return E("ul", *lines)
def Render(cls, elements): lines = [] for token in elements: if type(token) != cls: lines.append(token.Render([token.content] + [token.children])) elif hasattr(token, "render"): lines.append(token.render()) else: lines.append(E("li", token.content)) return E("ol", *lines)
def test_simple_multiple_ordered_list(): test = "\n".join(["1. First item", "2. Second item"]) root_kwargs = {"class": "brutemark_root"} expected = tostring(E( "div", E("ol", E("li", "First item"), E("li", "Second item")), **root_kwargs), pretty_print=True, encoding="unicode") actual = markdown(test) assert actual == expected
def test_simple_generation_single_header(): test = \ textwrap.dedent(""" # Hello World """).strip() expected = tostring(E("div", E("h1", "Hello World"), **{"class": "brutemark_root"}), pretty_print=True, encoding="unicode") actual = markdown(test) assert actual == expected
def Render(cls, children): lines = [] for child_token in children: child_token.render(lines) paragraph = E("p", *lines) return paragraph
def __init__(self, app=None, ignore_uncap=False, ignore_wrappers=False, cloth=None, cloth_parser=None, polymorphic=True, doctype="<!DOCTYPE html>", root_tag='div', child_tag='div', field_name_attr='class', field_name_tag=None, field_name_class='field_name', before_first_root=None): """Protocol that returns the response object according to the "html microformat" specification. See https://en.wikipedia.org/wiki/Microformats for more info. The simple flavour is like the XmlDocument protocol, but returns data in <div> or <span> tags. :param app: A spyne.application.Application instance. :param root_tag: The type of the root tag that encapsulates the return data. :param child_tag: The type of the tag that encapsulates the fields of the returned object. :param field_name_attr: The name of the attribute that will contain the field names of the complex object children. """ super(HtmlMicroFormat, self).__init__(app=app, ignore_uncap=ignore_uncap, ignore_wrappers=ignore_wrappers, cloth=cloth, cloth_parser=cloth_parser, polymorphic=polymorphic, hier_delim=None, doctype=doctype) if six.PY2: text_type = basestring else: text_type = str assert isinstance(root_tag, text_type) assert isinstance(child_tag, text_type) assert isinstance(field_name_attr, text_type) assert field_name_tag is None or isinstance(field_name_tag, text_type) self.root_tag = root_tag self.child_tag = child_tag self.field_name_attr = field_name_attr self.field_name_tag = field_name_tag if field_name_tag is not None: self.field_name_tag = E(field_name_tag) self._field_name_class = field_name_class if before_first_root is not None: self.event_manager.add_listener("before_first_root", before_first_root) self.serialization_handlers = cdict({ Array: self.array_to_parent, AnyUri: self.anyuri_to_parent, AnyHtml: self.html_to_parent, ImageUri: self.imageuri_to_parent, ByteArray: self.not_supported, ModelBase: self.model_base_to_parent, ComplexModelBase: self.complex_model_to_parent, })
def render(self): pieces = [] for element in self.content: if hasattr(element, "render"): pieces.append(element.render()) else: pieces.append(element) return E("li", *pieces)
def render(self, container: list): for body_token in self.content: container.append(body_token.render()) if hasattr(container[-1], "endswith"): if container[-1].endswith(" "): container.append(E("br")) return container
def render(self, content, children): leafs = [] for child in children: if hasattr(child, "render"): leafs.append(child.render) else: leafs.append(child) return E("div", **leafs)
def Render(cls, elements): lines = [] for element in elements: lines.append(element.render()) body = "\n".join(lines) element = E(cls.__name__) element.text = body return element
def test_simple_multiline_code(): test = [" This is a code block", " that spans", " multiple lines"] test = "\n".join(test) expected_text = \ [ "This is a code block\n", "that spans\n", "multiple lines" ] root_kwargs = {"class": "brutemark_root"} expected = tostring(E("div", E("pre", E("code", *expected_text)), **root_kwargs), pretty_print=True, encoding="unicode") actual = markdown(test) assert actual == expected
def __init__(self, app=None, ignore_uncap=False, ignore_wrappers=False, cloth=None, attr_name='spyne_id', root_attr_name='spyne', cloth_parser=None, root_tag='div', child_tag='div', field_name_attr='class', field_name_tag=None, field_name_class='field_name'): """Protocol that returns the response object according to the "html microformat" specification. See https://en.wikipedia.org/wiki/Microformats for more info. The simple flavour is like the XmlDocument protocol, but returns data in <div> or <span> tags. :param app: A spyne.application.Application instance. :param validator: The validator to use. Ignored. :param root_tag: The type of the root tag that encapsulates the return data. :param child_tag: The type of the tag that encapsulates the fields of the returned object. :param field_name_attr: The name of the attribute that will contain the field names of the complex object children. """ super(HtmlMicroFormat, self).__init__(app=app, ignore_uncap=ignore_uncap, ignore_wrappers=ignore_wrappers, cloth=cloth, attr_name=attr_name, root_attr_name=root_attr_name, cloth_parser=cloth_parser) assert root_tag in ('div', 'span') assert child_tag in ('div', 'span') assert field_name_attr in ('class', 'id') assert field_name_tag in (None, 'span', 'div') self.root_tag = root_tag self.child_tag = child_tag self.field_name_attr = field_name_attr self.field_name_tag = field_name_tag if field_name_tag is not None: self.field_name_tag = E(field_name_tag) self._field_name_class = field_name_class self.serialization_handlers = cdict({ ModelBase: self.model_base_to_parent, AnyUri: self.anyuri_to_parent, AnyHtml: self.anyhtml_to_parent, ImageUri: self.imageuri_to_parent, ByteArray: self.not_supported, Attachment: self.not_supported, ComplexModelBase: self.complex_model_to_parent, Array: self.array_to_parent, })
def model_base_to_parent(self, ctx, cls, inst, parent, name, **kwargs): retval = E(self.child_tag, **{self.field_name_attr: name}) data_str = self.to_string(cls, inst) if self.field_name_tag is not None: field_name = cls.Attributes.translations.get(name) field_name_tag = self.field_name_tag( field_name, **{'class': self._field_name_class}) field_name_tag.tail = data_str retval.append(field_name_tag) else: retval.text = data_str parent.write(retval)
def render(self, parent_element=None): if self.type == ROOT_CONTAINER: kwargs = {"class": "brutemark_root"} parent_element = E("div", **kwargs) for branch in self.children: branch.render(parent_element) else: branches = [self.content] branches.extend([x.content for x in self.children]) child_element = self.type.Render(branches) parent_element.append(child_element) return parent_element
def test_multidepth_lists(): test = "\n".join([ "1. item 1", " * subitem A", " * subitem B", " 12. super nested item" "2. item 2" ]) expected = tostring(E( "div", E( "ol", E( "li", "item 1", E("ul", E("li", "subitem A"), E("li", "subitem B", E("ol", E("li", "super nested item"))))), E("li", "item 2")), **{"class": "brutemark_root"}), pretty_print=False, encoding='unicode').replace("\n", "") actual = markdown(test, pretty_print=False) assert actual == expected
def test_mixed_lists(): test = "\n".join(["1. item 1", " * subitem A", "2. item 2"]) expected = tostring(E( "div", E("ol", E("li", "item 1", E("ul", E("li", "subitem A"))), E("li", "item 2")), **{"class": "brutemark_root"}), pretty_print=True, encoding="unicode") actual = markdown(test) assert actual == expected
def test_inline_HTML(): test = \ """<table> <tr> <td>Foo</td> </tr> </table>""" root = E("div", **{"class": "markdown_root"}) expected = \ """<div class="brutemark_root"><table> <tr> <td>Foo</td> </tr> </table></div>""" actual = markdown(test, pretty_print=False) assert actual == expected
def render(self): from collections import defaultdict pieces = [] grouped = defaultdict(list) for element in self.content: grouped[type(element)].append(element) for content_type, elements in grouped.items(): if content_type not in [ body_tokens.Text, body_tokens.EmphasisText, body_tokens.StrongText ]: pieces.append(content_type.Render(elements)) else: for element in elements: pieces.append(element.render()) return E("li", *pieces)
def any_dict_to_parent(self, ctx, cls, inst, parent, name, **kwargs): elt = E('foo') dict_to_etree(inst, elt) parent.write(elt[0])
def serialize_null(prot, cls, locale, name): return [E(prot.child_tag, **{prot.field_name_attr: name})]
def null_to_parent(self, ctx, cls, inst, parent, name, **kwargs): cls_attrs = self.get_cls_attrs(cls) if cls_attrs.min_occurs >= 1: parent.write(E(name))
def serialize_model_base(self, cls, value, locale, name): return [ E(self.child_tag, cls.to_string(value), **{self.field_name_attr: name}) ]
def Render(cls, elements): #No matter how many blanklines, just do one <br/> return E("br")
def null_to_parent(self, ctx, cls, inst, parent, name, **kwargs): return [E(self.child_tag, **{self.field_name_attr: name})]
def Render(cls, elements=None): assert len(elements) == 1 sub_elements = [x.render() for x in elements[0].content] header = E(f"h{elements[0].level}", *sub_elements) return header