Skip to content

Instantly share code, notes, and snippets.

@nathanborror
Created July 27, 2011 06:55
Show Gist options
  • Save nathanborror/1108817 to your computer and use it in GitHub Desktop.
Save nathanborror/1108817 to your computer and use it in GitHub Desktop.

Revisions

  1. nathanborror revised this gist Jul 31, 2011. 1 changed file with 5 additions and 42 deletions.
    47 changes: 5 additions & 42 deletions xmltohtml.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    from lxml import etree
    from lxml import objectify

    from django import template
    from django.template.loader import render_to_string
    @@ -12,8 +12,8 @@ def __init__(self, xml):

    def render(self, context):
    output = self.xml.render(context)
    xml = etree.fromstring(output)
    context['object'] = dictlist(xml)
    xml = objectify.fromstring(output)
    context['object'] = xml
    return render_to_string('xml/%s.html' % xml.tag, context)


    @@ -35,49 +35,12 @@ def do_xml(parser, token):
    Which renders the template `xml/tabs.html` where you can:
    <div class="tabs">
    {% for tab in object.value %}
    <a href="{{ tab.attributes.url }}">{{ tab.attributes.label }}</a>
    {% for tab in object.tab %}
    <a href="{{ tab.attrib.url }}">{{ tab.attrib.label }}</a>
    {% endfor %}
    </div>
    """
    bits = token.split_contents()
    xml = parser.parse(('endxml',))
    parser.delete_first_token()
    return XMLNode(xml)


    #
    # XML to Dict
    #

    def dictlist(node):
    res = {}
    res[node.tag] = []
    xmltodict(node, res[node.tag])
    return {
    'value': res[node.tag],
    'attributes': node.attrib
    }


    def xmltodict(node,res):
    rep = {}
    if len(node):
    for n in list(node):
    rep[node.tag] = []
    value = xmltodict(n, rep[node.tag])
    if len(n):
    value = {
    'value': rep[node.tag],
    'attributes': n.attrib
    }
    res.append(value)
    else :
    res.append(rep[node.tag][0])
    else:
    value = {
    'value': node.text,
    'attributes': node.attrib
    }
    res.append(value)
    return
  2. nathanborror revised this gist Jul 27, 2011. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions xmltohtml.py
    Original file line number Diff line number Diff line change
    @@ -20,17 +20,19 @@ def render(self, context):
    @register.tag('xml')
    def do_xml(parser, token):
    """
    Convert XML within a template into an object to be used in a corresponding
    Convert template XML to an object which renders a corresponding
    HTML template. Lets you embed components using pseudo markup.
    In the main template you'll have something like:
    {% xml %}
    <tabs>
    <tabs url="http://facebook.com" label="Facebook" />
    <tabs url="http://apple.com" label="Apple" />
    </tabs>
    {% endxml %}
    Renders the template `xml/tabs.html` where you can:
    Which renders the template `xml/tabs.html` where you can:
    <div class="tabs">
    {% for tab in object.value %}
  3. nathanborror revised this gist Jul 27, 2011. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions xmltohtml.py
    Original file line number Diff line number Diff line change
    @@ -7,11 +7,11 @@


    class XMLNode(template.Node):
    def __init__(self, nodelist):
    self.nodelist = nodelist
    def __init__(self, xml):
    self.xml = xml

    def render(self, context):
    output = self.nodelist.render(context)
    output = self.xml.render(context)
    xml = etree.fromstring(output)
    context['object'] = dictlist(xml)
    return render_to_string('xml/%s.html' % xml.tag, context)
    @@ -39,9 +39,9 @@ def do_xml(parser, token):
    </div>
    """
    bits = token.split_contents()
    nodelist = parser.parse(('endxml',))
    xml = parser.parse(('endxml',))
    parser.delete_first_token()
    return XMLNode(nodelist)
    return XMLNode(xml)


    #
  4. nathanborror revised this gist Jul 27, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion xmltohtml.py
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ def do_xml(parser, token):
    <div class="tabs">
    {% for tab in object.value %}
    <a href="{{ tab.attributes.url }}">{{ tab.attributes.label }}</a>
    <a href="{{ tab.attributes.url }}">{{ tab.attributes.label }}</a>
    {% endfor %}
    </div>
    """
  5. nathanborror created this gist Jul 27, 2011.
    81 changes: 81 additions & 0 deletions xmltohtml.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    from lxml import etree

    from django import template
    from django.template.loader import render_to_string

    register = template.Library()


    class XMLNode(template.Node):
    def __init__(self, nodelist):
    self.nodelist = nodelist

    def render(self, context):
    output = self.nodelist.render(context)
    xml = etree.fromstring(output)
    context['object'] = dictlist(xml)
    return render_to_string('xml/%s.html' % xml.tag, context)


    @register.tag('xml')
    def do_xml(parser, token):
    """
    Convert XML within a template into an object to be used in a corresponding
    HTML template. Lets you embed components using pseudo markup.
    {% xml %}
    <tabs>
    <tabs url="http://facebook.com" label="Facebook" />
    <tabs url="http://apple.com" label="Apple" />
    </tabs>
    {% endxml %}
    Renders the template `xml/tabs.html` where you can:
    <div class="tabs">
    {% for tab in object.value %}
    <a href="{{ tab.attributes.url }}">{{ tab.attributes.label }}</a>
    {% endfor %}
    </div>
    """
    bits = token.split_contents()
    nodelist = parser.parse(('endxml',))
    parser.delete_first_token()
    return XMLNode(nodelist)


    #
    # XML to Dict
    #

    def dictlist(node):
    res = {}
    res[node.tag] = []
    xmltodict(node, res[node.tag])
    return {
    'value': res[node.tag],
    'attributes': node.attrib
    }


    def xmltodict(node,res):
    rep = {}
    if len(node):
    for n in list(node):
    rep[node.tag] = []
    value = xmltodict(n, rep[node.tag])
    if len(n):
    value = {
    'value': rep[node.tag],
    'attributes': n.attrib
    }
    res.append(value)
    else :
    res.append(rep[node.tag][0])
    else:
    value = {
    'value': node.text,
    'attributes': node.attrib
    }
    res.append(value)
    return