Macro madness

3 views
Skip to first unread message

Cliff Wells

unread,
Apr 18, 2008, 11:23:48 PM4/18/08
to breve-...@googlegroups.com, cbrain
I just had to share this (requires trunk/1.2.1 due to small change in
Tag.walk after 1.2):


assign ( 'classes', list ( ) ),

macro ( 'walk_classes', lambda tag, is_tag:
classes.extend ( [ "%s.%s { }" % ( tag.name, _v ) for _k, _v in tag.attrs.items ( )
if _k in ( '_class', 'class_', '_class_' ) ] )
),

macro ( 'walk_ids', lambda tag, is_tag:
classes.extend ( [ "%s#%s { }" % ( tag.name, _v ) for _k, _v in tag.attrs.items ( )
if _k in ( 'id', 'id_', '_id', '_id_' ) ] )
),

macro ( 'extract_css', lambda tag:
tag.walk ( walk_classes, True ) and tag.walk ( walk_ids, True ) and tag
),

macro ( 'css_results', lambda:
ul ( style = 'list-style: none;' ) [
[ li [ _c ] for _c in classes ]
]
),

html [
head [
title [ 'Extract styles' ]
],

body [ extract_css (
div ( class_ = 'text', id = 'main-content' ) [
img ( src = '/images/breve-logo.png', alt = 'breve logo' ),
br,
span ( class_='bold' ) [ '''Hello from Breve!''' ]
]
), css_results ( ) ]
]


This outputs the original page with the CSS selectors at the end like so:

div.text { }
span.bold { }
div#main-content { }


I'm probably going to redo this as a tool (CSS extractor), but I have to
admit I thought it was pretty cool that it could be done with macros =)

Thanks Sven, for an effing great idea!

Regards,
Cliff


Cliff Wells

unread,
Apr 18, 2008, 11:55:33 PM4/18/08
to breve-...@googlegroups.com

On Fri, 2008-04-18 at 20:23 -0700, Cliff Wells wrote:

> This outputs the original page with the CSS selectors at the end like so:
>
> div.text { }
> span.bold { }
> div#main-content { }
>

Let me add that this is probably the worst possible thing to do in a
template, I just did it to see if it would work =)

Professional-driver-on-closed-course'ly yrs,
Cliff

cbrain

unread,
Apr 20, 2008, 5:13:49 AM4/20/08
to brevé template engine
Hi Cliff!

Really cool :-)

The strength of the macros is even more than I thought initially, I
was only going to use it for sub-template purposes. But now I see much
more is possible!

Thanks for a great templating engine!

--
Sven
Reply all
Reply to author
Forward
0 new messages