Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

I am trying to make a javascript code that can compile something like this:

html{
    background: #2E2E2E;
    body{
        height: 200px;
        width: 100%;
        background: #fff;
        a{
            color: rgb(0, 0, 255);
            &:hover{
                color: rgb(200, 0, 255);
            }
        }
    }
}


Into generic CSS:

html{
    background: #2E2E2E;
}
html body{
    height: 200px;
    width: 100%;
    background: #fff;
}
html body a{
    color: rgb(0, 0, 255);
}
html body a:hover{
    color: rgb(200, 0, 255);
}


How can I do this?

Thanks for any help.
Posted
Comments
Sergey Alexandrovich Kryukov 10-Mar-14 22:15pm    
What does it mean, "compile CSS rules"? :-)
—SA
Member 10305199 10-Mar-14 22:20pm    
I am basically making my own css compiler, like sass, less, compass, etc.
Sergey Alexandrovich Kryukov 10-Mar-14 22:25pm    
1) Why doing that? 2) compile to what?
—SA
Member 10305199 11-Mar-14 8:29am    
Compile my version of CSS, the one with nested rules, to plain CSS
Sergey Alexandrovich Kryukov 11-Mar-14 10:24am    
Thank you for confirmation, now: why?
—SA

1 solution

Thank you for all the clarifications.

There is nothing special in this work, but it needs time and effort, of course. First of all, note that both your "nested CSS" and required CSS are the examples of the tree structure:
http://en.wikipedia.org/wiki/Tree_%28graph_theory%29[^],
http://en.wikipedia.org/wiki/Tree_%28data_structure%29[^].

This is one of the most trivial kinds of graphs, widely used in programming. The method of operations on trees are also well known. You plan can be this:
  1. Develop a parser parsing the CSS text into some DOM tree structure.
  2. Define the mapping rules from your "nested CSS" structure into required target CSS structure.
  3. Implement the transformation of the tree structure using the mapping rules and the input structure.
  4. Develop code generator which uses your transformed CSS structure and generate appropriate text, target CSS code.


Most labor-taking item would be development of the parser. At the same time, you could use one of the available parsers. It turns out that parsing CSS is pretty popular activity. I would suggest to start here: http://codetheory.in/parsing-css-in-javascript[^].

This article contains short overview and a list of available parsers you can use. You can review all of then and choose the one which you find most convenient for your purpose. In worst case, you will have to modify the parser code to adopt your CSS structure.

—SA
 
Share this answer
 
Comments
Member 10305199 12-Mar-14 21:10pm    
You are correct, but how can I do this.
Sergey Alexandrovich Kryukov 12-Mar-14 22:26pm    
Well, this is not a question. What do you mean "how"? Which part of the above is not clear?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900