Giter Site home page Giter Site logo

pyhtml's Introduction

pyhtml

CircleCI codecov License

For those who love Python and, for reasons unknown, still like to write and format HTML manually.

Installation

  1. Add pyhtml.py to your project
  2. import pyhtml

Reference

new

This is the first thing to call and will return a new instance of PyHTML.

>>> pyhtml.new()

It can take the following arguments:

  • doctype: str - doctype declaration (default: "")
    • valid doctypes: html5, html4.01s, html4.01t, html4.01f, xhtml1.1, xhtml1.0s, xhtml1.0t, xhtml1.0f
  • spaces: int - number of spaces used for indentation (default: 4)

For html4.01 and xhtml1.0: s, t, and f stands for strict, transitional, and frameset, respectively.

attr

Strings and tuples are stringified into HTML attribute form.

>>> pyhtml.attr(("src", "simpsons.webm"), "autoplay", ("width", 800), ("height", 600))
'src="simpsons.webm" autoplay width="800" height="600"'

Strings are treated as boolean attributes and 2-tuples are treated as value attributes. Tuples can take the form (str, str) or (str, int). Multiple comma separated strings and tuples can be specified.

append

  • string: str -- add arbitrary text to the HTML

indent

Add indentation. Indentation is handled automatically by default. This is useful during manual spacing.

newline

Add a newline. Newlines are handled automatically by default. This is useful during manual spacing or when additional newlines are desired.

vwrap

Add a void element. These are elements that do not have a closing tag.

  • elem: str -- an HTML void element
  • attrs: str -- element attributes (default: "")
>>> html = pyhtml.new()
>>> html.vwrap("img", pyhtml.attr(("src", "kwikemart.png"), ("width", 358), ("height", 278)))
>>> print(html)
<img src="kwikemart.png" width="358" height="278">

wrap

Add an element. The closing tag will be inserted automatically.

  • elem: str -- an HTML element
  • attrs: str -- element attributes (default: "")
>>> html = pyhtml.new()
>>> with html.wrap("div", pyhtml.attr(("class", "big"))):
...     html.append("Embiggened!")
...
>>> print(html)
<div class="big">
    Embiggened!
</div>

manual_spacing

Disable automatic indentation and newlines. Statements within the block will not have automatic indentation or newlines.

>>> html = pyhtml.new()
>>> with html.manual_spacing():
...     with html.wrap("em"):
...         html.append("Itchy")
...     html.append(" & ")
...     with html.wrap("em"):
...         html.append("Scratchy")
...
>>> print(html)
<em>Itchy</em> & <em>Scratchy</em>

Examples

>>> import pyhtml
>>> html = pyhtml.new()
>>> with html.wrap("html"):
...     with html.wrap("head"):
...         with html.wrap("title"):
...             html.append("Dr. Nick")
...     with html.wrap("body"):
...         with html.wrap("p"):
...             html.append("Hi, everybody!")
...
>>> print(html)
<html>
    <head>
        <title>
            Dr. Nick
        </title>
    </head>
    <body>
        <p>
            Hi, everybody!
        </p>
    </body>
</html>
# Disable auto-spacing when you want to have
# a wrapped element on a single line.

>>> import pyhtml
>>> html = pyhtml.new()
>>> with html.wrap("html"):
...     with html.wrap("head"):
...         with html.manual_spacing():
...             html.indent()
...             with html.wrap("title"):
...                 html.append("Ned Flanders")
...             html.newline()
...     with html.wrap("body"):
...         with html.manual_spacing():
...             html.indent()
...             with html.wrap("p"):
...                 html.append("Hi diddly ho!")
...             html.newline()
...
>>> print(html)
<html>
    <head>
        <title>Ned Flanders</title>
    </head>
    <body>
        <p>Hi diddly ho!</p>
    </body>
</html>
# Tip: Context managers can be nested.

>>> import pyhtml
>>> html = pyhtml.new()
>>> family = ["homer", "marge", "bart", "lisa", "maggie"]
>>> with html.wrap("ul"), html.manual_spacing():
...     for member in family:
...         html.indent()
...         with html.wrap("li"):
...             html.append(member)
...         html.newline()
...
>>> print(html)
<ul>
    <li>homer</li>
    <li>marge</li>
    <li>bart</li>
    <li>lisa</li>
    <li>maggie</li>
</ul>

pyhtml's People

Contributors

chingc avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.