Package: xplant
XPLANTPure pythonic tree structure builder and markup language translator.
No dependencies, quite fast, pretty syntax, no templates.Enter tree nodes with python’s context managers.Cast the tree to given markup, just by callingstron it.Enjoy 1:1 translation from python to given markup.Basically it’s suited for XML and similar markups (SVG, HTML) but it can be used
for probably any structured markup.Written mostly to be an python inline html generator.XML Examplefromxplantimportxml_plantx=xml_plant()withx.node("section_a",attribute_1=1):withx.node("nested_1",empty=True):passwithx.node("nested_2"):x.comment("Can handle also comments.")fornumberinrange(3):x.leaf("a_number_{:02}".format(number),num=number)Will give:<?xml version="1.0" encoding="utf-8"?><section_aattribute_1="1"><nested_1empty="true"></nested_1><nested_2><!-- Can handle also comments. --><a_number_00num="0"/><a_number_01num="1"/><a_number_02num="2"/></nested_2></section_a>HTML generation Examplecss="""
    ul.navigation {
        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: #f2fff5;
    }
    li {
      float: left;
    }
    li a {
      display: block;
      color: white;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
    }
    code {
        background-color: #ded;
        padding: 4px;
        border-radius: 3px;
    }
"""navigation=[("Home","../home.html","red"),("Things","stuff.html","green"),("About","../about.html","blue"),]text=["This page has ben generated with python's <code> xplant.html.html5_plant </code>.","Enjoy pure pythonic <code>1:1 python -> xml</code> translation.","break","Did you ever had hard times with learning <code>HTML template language</code>? ","It's a crude way to mix HTML with any logics like iterators, classes, conditions.","break","You know what? You already have all of it (and much more) in <code>python</code>! ","HTML templates is a blind alley. HTML does not miss server-side scripting.","The python miss a good HTML generator not vice versa.",]fromxplantimporthtml5_plantx=html5_plant()withx.html():withx.head():x.meta(charset="utf-8")x.meta(http_equiv="Content-Security-Policy")x.line("title","no templates, no headache")# line is a helper for creating text in a xml tagwithx.style():x.text(css)withx.body(style="margin:28px;"):withx.header():x.line("h2","XPLANT",id="title")x.line("h4","It makes good things for you")x.comment("HERE COMES THE NAVIGATION")withx.ul(id="navigation"):x.comment("CHECK OUT THIS LIST")forname,link_url,link_colorinnavigation:withx.li():withx.a(href=link_url,style="color:%s;"%link_color):x.text("%sin%s"%(name,link_color))x.comment("HERE COMES MAIN SECTION")withx.main(style="margin:20px;"):forparagraphintext:withx.p():ifparagraph=="break":x.br()else:x.text(paragraph)print(p)Gives such a string:<!DOCTYPE html><html><head><metacharset="utf-8"/><metahttp-equiv="Content-Security-Policy"/><title>no templates, no headache</title><style>ul.navigation{list-style-type:none;margin:0;padding:0;overflow:hidden;background-color:#f2fff5;}li{float:left;}lia{display:block;color:white;text-align:center;padding:14px16px;text-decoration:none;}code{background-color:#ded;padding:4px;border-radius:3px;}</style></head><bodystyle="margin:28px;"><header><h2id="title">XPLANT</h2><h4>It makes good things for you</h4></header><!-- HERE COMES THE NAVIGATION --><ulid="navigation"><!-- CHECK OUT THIS LIST --><li><ahref="../home.html"style="color:red;">Home in red</a></li><li><ahref="stuff.html"style="color:green;">Things in green</a></li><li><ahref="../about.html"style="color:blue;">About in blue</a></li></ul><!-- HERE COMES MAIN SECTION --><mainstyle="margin:20px;"><p>This page has ben generated with python's<code>xplant.html.html5_plant</code>.</p><p>Enjoy pure pythonic<code>1:1 python -> xml</code>translation.</p><p><br/></p><p>Did you ever had hard times with learning<code>HTML template language</code>?</p><p>It's a crude way to mix HTML with any logics like iterators, classes, conditions.</p><p><br/></p><p>You know what? You already have all of it (and much more) in<code>python</code>!</p><p>HTML templates is a blind alley. HTML does not miss server-side scripting.</p><p>The python miss a good HTML generator not vice versa.</p></main></body></html>