views:

134

answers:

3

I think, CSS syntax and base principles can be very useful, and not only for style. Is there any PARSE engines which can operate with CSS-like rules, like the ones for XML?


For example, we can create something like framework (yes, another one), in which we define pages in xml style (JUST EXAMPLE, maybe very stupid or two complicated):

<page id="index" url="/" controller="staticpage" />
<page id="about" url="/" controller="staticpage" action="about" />
<page id="post" url="/post/(\d+)" type="regex" controller="post" class="">
     <param id="1" name="post_id" />
</page>
<page id="post_comment" url="/comment/(\d+)" type="regex" controller="post" action="comment" class="authneeded">
     <param id="1" name="post_id" />
</page>
<page id="post_write" url="/write" type="regex" controller="staticpage" action="write" class="authneeded" />

and then write a "CSS" for it:

* {
     layout: "layout.html"; // default layout
}
*[action=''] {
     action: "index"; // default action
}
#post_write {
     layout: "adminlayout.html";
}
.authneeded {
     redirect: "/";
}
.authneeded:loggedin {  // pseudoclass which is set only if the user logged in. 
                        // (maybe POSTS:loggedin .authneeded to apply only one
                        // pseudoclass)

     redirect: false;   // no, we don't need to redirect then the user logged in
}

Isn't it an interesting way to configurate? Even better, we can create an admin script (inspired by jquery ;)

./admin #about addClass authneeded 
./admin "#post PARAM" attr id param_post


So, Is there any engines which can operate with CSS-like rules?

A: 

Not exactly CSS, but since you mention XML, there is always XSLT, which can be used to transform XML in different ways. For example, you could build an HTML list from a simple XML file and so on.

Jani Hartikainen
No, I want a CSS parser
valya
Maybe you should say "I want a CSS parser" in your question then.
Jani Hartikainen
A: 

I'd say it has already been done? Below are some urlmappings in grails & django. It isn't CSS, but then again, CSS is supposed to contain style not actions, and the syntax isn't that different.

In grails, UrlMapping.groovy could look something like:

class UrlMappings {
    static mappings = {
      "/$controller/$action?/$id?"{
        constraints {
       // apply constraints here
      }
    }
    "/"(controller:"static")
    "500"(view:'/error')
    "/product/" (controller:"myController", action:"show")
    "/old/**" (view:"/index")
    "/uploads/$requestedFile**" (controller:"processFile")
  }
}

In python:

urlpatterns = patterns('',
    # Example:
    (r'^$', direct_to_template, {'template': 'pages/front.html' }),
    (r'^about/',direct_to_template, {'template': 'pages/about.html' }),
    (r'^demo/',direct_to_template, {'template':'pages/demo.html'}),
    (r'^accounts/', include('apps.accounts.urls')),
    (r'^forms/', include('apps.forms.urls')),
    (r'^admin/doc/', include('django.contrib.admindocs.urls')),
    (r'^admin/(.*)', admin.site.root),
    {'document_root': settings.MEDIA_ROOT}),
)

My personal preference is for Grails, which happens to closer resembles the script you presented. In grails most of the urls/actions would be autogenerated by your controller so you wouldn't need the configuration, but it is there for cases where the world is less perfect ;).

Bear
I dont need a framework, I need a CSS parser
valya
A: 

I've seen a demo of an HTML/CSS editor for Mac, using CSS selectors to generate HTML markup. Don't remember the name of the program, but it seems a very elegant and efficient way to write HTML.

To give you an idea of how it works, lets say you write this in the editor:

ul#navigation li a.active

And press some magic shortcut key (or just Enter) to transform it to this:

<ul id="navigation">
    <li><a class="active"></a></li>
</ul>

To me, this seems like a logical use for CSS. Using it for metadata, as you described, could be useful if you want to have it separated.

In your example however, the two are very close related. And setting the default values and conditions in the XML just makes sense (just like the param tag is).

Ronald
Yeah, I've seen it :) I'll check the parser they're using, thanks
valya