Forum Moderators: not2easy

Message Too Old, No Replies

program to read html file and create default css file

program to read html file and create default css file

         

drooh

12:26 am on Jan 14, 2010 (gmt 0)

10+ Year Member



I am wondering if there is a program which you could drop an html file in it and it would scan the html code for id's and class's and make a default css file with all the id's and class's entered into it without any attributes. So for instance it would spit out a css file like such

#wrap {

}
#nav {

}
#nav .subs {

}
#mod1 {

}
#footer {

}

alias

11:55 am on Jan 14, 2010 (gmt 0)

10+ Year Member



No programs like that I would be aware of.

BUT it would be relatively easy to write a script in any programming language to accomplish this task.

CSS_Kidd

4:49 pm on Jan 14, 2010 (gmt 0)

10+ Year Member



No alias... I heard about a program like this. I don't remember what it is called but they teach a course on it at Hogwarts.

Sorry I couldn't resist.

However... It may be easy to write a script, but what would be the point when you have to write out the styles anyways... Might as well write out the tags, ids and classes while your at it. Right?

drooh

7:07 pm on Jan 14, 2010 (gmt 0)

10+ Year Member



I actually wrote up a php script that does this for me. And it works great.

For CSS I use stylizer and it makes things very easy.

However... It may be easy to write a script, but what would be the point when you have to write out the styles anyways... Might as well write out the tags, ids and classes while your at it. Right?

When you have a large site there can be alot of ids and classes and this would save time typing that out. Maybe 20 mins or so.

drooh

7:12 pm on Jan 14, 2010 (gmt 0)

10+ Year Member



Here is the script I came up with. Create a php file called get_css_from_html.php and copy all this code into it and upload it to your webserver. After its uploaded you can simply copy and paste your html code in.


<?php
function getId($input){
$first = ' id="';
$field = substr(strstr(strstr($input, $first), '"'),1);
$end = strstr($field, "\"");
$field = str_replace($end, "", $field);
return "#".$field." {\n\t\n}";
}
function getClass($input){
$first = ' class="';
$field = substr(strstr(strstr($input, $first), '"'),1);
$end = strstr($field, "\"");
$field = str_replace($end, "", $field);
return ".".$field." {\n\t\n}";
}
$sent = false;
if(isset($_POST['sub'])){
$cssfilename = $_POST['cssfilename'];
$string = $_POST['string'];
$lines = explode("\n", $string);
$n = count($lines);
$i=0;
header('Content-Disposition: attachment; filename="'.$cssfilename.'.css"');
$ar = null;
while($i < $n){
$var = $lines[$i];
$dif = $n-1;
if($i != $dif){
$var = substr($var, 0, -1);
}
if(getId($var)){
$ar .= getId($var);
$ar .= ",";
}
if(getClass($var)){
$ar .= getClass($var);
$ar .= ",";
}
$i++;
}
$array = substr($ar,0,-1);
$array = explode(",", $array);
$array = array_unique($array);
$nan1 = "# {\n\t\n}";
$nan2 = ". {\n\t\n}";
foreach($array as $value){
if($value != $nan1 && $value != $nan2){
echo $value."\n";
}
}
$sent = true;
}
?>
<?if(!$sent):?>
<br />Get CSS from HTML<br /><br />
<form action="get_css_from_html.php" method="post" />
Your CSS File Name<input type="text" name="cssfilename" value="style" size="7" />.css
<br />
Enter HTML Here<br />
<textarea name="string" rows="20" cols="100"></textarea>
<br />
<input type="submit" name="sub" value="enter" />
</form>
<?endif;?>

CSS_Kidd

8:34 pm on Jan 14, 2010 (gmt 0)

10+ Year Member



Nice Script!

However... Maybe it is just me, but I still can't rationalize the point to this. And this is my reason why:

I usually write my CSS as I am constructing my pages. When I create a div (or any other element) I automatically style it. I personally haven't met anyone who will create a whole site(small or huge), only to style for it after the fact. Even if you take over a project that was created by someone else, 9 times out of 10 there is already a working css file to edit.

Now I do see this useful for html tags. For me it is usually later in my process when I do this. Stuff like p, ul li (unless I am building a h-menu), h1/h2..., and so on, I just throw in and style it last. But by looking at your PHP it looks like this was left out. I know little about PHP, so is it possible to do this for the HTML tags?

As of right now, using Notepad++, I have created macros that will auto generate empty html pages of each doctype that include the basics of an html document(head with empty title/meta/css link and empty body ready for me to fill in the blanks. I also have a macro set to generate a css document that has all the basic classes, ids, tags, and rules that I most commonly use.

Sorry. I wasn't trying to steal your thunder or anything. I personally don't see how I would benefit from something like this. But I do like it!

drooh

9:30 pm on Jan 14, 2010 (gmt 0)

10+ Year Member



Hey CSS_Kidd, i too use notepad++. And I actually have a keyboad that has marcro buttons that Ive programmed with all sorts of tid bits. So the point is to increase your productivity. We all have different methods of how we build sites, and I agree that alot of the time something like this may not be useful. But primarily in my case, starting a site from scratch, I usually know 75% of what the markup will be and I can go ahead and write the html with the div id's and class's. After I get that, Ill open up stylizer, from there this is where my little script saves me some time. It creates the basic css file needed to start the design/presentation.

I am all for writing code by hand because you need to understand the code. But once you understand the code then I believe some tools can be helpful and save time.

Stylizer is not really a code writer or wizzy wig. If you know css and understand it, then this tool will help speed things up. And give you more accurate layouts. With writing css by hand you have to save, refresh, save, refresh.

I do like your idea of storing an html & css template in the macros.

swa66

10:49 pm on Jan 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Scary discussion.

Honestly: why bother with that ?

I think it'll distract greatly from the important things such as selectors having specificity, the cascade, etc.

It might well look like a good idea on first looks, but I can't think of a real life scenario where it would be productive in the end, even in the hands of somebody who really knows CSS inside-out.

drooh

5:31 am on Jan 15, 2010 (gmt 0)

10+ Year Member



So then why did I think of it? Don't inventions come and the hand of a need? I saw a need and made an invention.

dreamcatcher

6:58 pm on Jan 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is software out there to convert table designs to CSS. Just google for table to css converters. I can`t think of anything that produces blank css classes though. Not something I would bother with myself.

dc

CSS_Kidd

7:08 pm on Jan 15, 2010 (gmt 0)

10+ Year Member



Dreamcatcher,

Although not very well, there is a feature in Dreamweaver that does this. However when it assigns style names it does it like this:
.style1
.style2
.style3
and so on.

You would still have to go back and edit regardless. As I have already stated, interesting concept but useless in the long run.

I will stick with my macros and create my styles as I construct the layout.

alias

7:33 pm on Jan 15, 2010 (gmt 0)

10+ Year Member



I think you all are taking the question too personally.

Try thinking of how the script in drooh's example might be useful, and under which circumstances one might need it.

For example, I would definitely find it useful (with adjustments to keep the IDs and classes semantically grouped one after another) for a breakdown of the IDs and classes used in the HTML code, which also might be useful in cases you are not allowed to modify the HTML code, or need to process the page in bulk, etc.

We all have our own way of work, and, while suggestions are always good, no need to criticize each others' ways too much, also having in mind that the issue has been actually resolved.

drooh

9:59 pm on Jan 15, 2010 (gmt 0)

10+ Year Member



thanks alias for your comment

IDs and classes semantically grouped one after another

I forgot to mention that, Ive always liked to have my css go in the order of my document from top to bottom. this definitely does that for you.

Also, this idea stems from the whole separation of content and design. If you have fully thought out the design and template then you should be able to run with the markup for quite awhile before needing to tweak things.

this idea is all meant to be for the start of your project, the start of your styling. From there its obvious that you would need to edit the css but at least this way they are in order and the base it there for you.