Since html2css is built upon Beautiful Soup, it is relatively easy to monkeypatch it. (Especially since there is a shortcut in the code, that lets Soup objects be used directly, achieving greater speed.)
Monkeypatching BS to use html2css is as easy as this:
>>> from beautifulsoup import BeautifulSoup
>>> from html2css import html2css
>>> setattr(BeautifulSoup, 'toCss', lambda soup: html2css(soup).display())
>>> soup = BeautifulSoup('<html id="foo"><body></body></html>')
>>> print soup.toCss()
html {}
body {}
html#foo {}
html#foo body {}
There you go. (This also illustrates an advantage of Python’s explicit self
declaration, in that toCss
actually takes an argument (soup
), which is then used as a self
-declaration, meaning that toCss
can be called without arguments — otherwise, it would have been soup.toCss(soup)
, which really isn’t optimal.)
Marx quotes. Great stuff.
“Catch a man a fish, and you can sell it to him. Teach a man to fish, and you ruin a wonderful business opportunity.”
Brilliant. Potentially very dangerous, but indeed genious.
I tested today, and found that you can actually extend a class, on itself. Not just by using setattr(), but also by creating a new class:
>>> class bar:
... def monkey(self):
... print 'neat'
...
>>> class bar(bar):
... def horse(self):
... print 'sweet'
...
>>> bar().monkey()
neat
>>> bar().horse()
sweet
Again, this might be in the manual, I just didn’t know of it until some ten minutes ago.
It’s liberty for all
‘Cause democracy’s our style
Unless you are against us
Then it’s prison without trial.
It worked for Zeldman, so I’ll have a shot: Why isn’t apple.com/osx/ pointing to a relevant page? I mean, sure, apple.com/macosx/ works, but it would make sense that /osx/ did so as well.
But that’s just my opinion.
A friend in need’s a friend indeed,
A friend with weed is better.
I just started my first project at Google code: html2css, a project I’ve been working on for some weeks now.
html2css has one simple function: Turn an HTML file into a CSS file. This is a functionality I’ve become increasingly aware that I need; I often find myself in a situation where I’ve mocked up the entire HTML document, and then have to perform the tedious task of applying it all in my CSS file, which often leads to a disorganized mess.
It uses the incredible Beautiful Soup library, meaning that it can tolerate even the worst piece of HTML — it manages to produce a valid representation of a Google search.
There are two files of interest in the project (discarding the bundled beautifulsoup.py). The first one is html2css.py where most of the magic happens. The other is writecss.py which is a useful extension of html2css. It allows you to turn your HTML into CSS, respecting earlier generations from html2css. This means you can generate a CSS file for one HTML structure, add some CSS rules, modify the HTML, regenerate the CSS, and your rules will still be present, given that you haven’t modified the element they’re attached to.
This is a 0.1 release, but it should be pretty stable. Comments appreciated.
Revolution, the only solution.