nullshell.com

Woo, progress! Sunday, May 20th, 2012

Oh hai guy. I deed naht c u thar.

Hadurr, Batch tutorials!

Yes, I got a few Batch tutorials released today. I think I am on like lesson number 32 or something and I should be moving into loops next.

I'll obviously teaching the crazy for loop, but I'm pondering whether or not I should share the way we can fake a while loop. It's just some labels and goto jumps, but it builds a loop similar to what the while loop would do. Obviously you can do a lot of messed up stuff with labels and goto, but building a loop is a bit more practical, I suppose.

Anyway, the final video I released today was on Delayed Variable Expansion. This turned out to be a longer video than I expected, and after it I just sort of slid out of the 'tutorial-making-mood'.

So I thought about doing something else.

The freedom the weekend offers~

I was still in the mode to generate content for you guys and think about nullshell, but at the same I still want to continue my escapade with SDL and C++. I guess I didn't really know what to do.

I figured I would write a nullshell post (much like this one) wondering whether or not it is okay for me to take breaks from the Batch tutorial series and actually learn some more things that benefit me. Don't get me wrong, I love making tutorials and all and sharing the love and knowledge out with you guys and everyone else, but I still want to spend some of my time bettering my own self. And hey, the more I know, the more I can teach and share.

But then I remembered-- since I had installed a fresh version of Ubuntu 12.04 LTS, I did not have the pages from Lazy Foo's tutorials. Obviously I went ahead and downloaded them all with a little wget magic, but I still wanted to change some of the colors and make the tutorials a little more lively.

That's really just a matter of changing some properties in the CSS file. Easy peasy.

But then!

'Oh hey, why don't I add the Syntax Highlighter?'

Hehehe. Boy, that unleashed a fun project.

What's the plan of attack?

I knew I needed to add some lines for JavaScript includes on every page. Some for a little bit of jQuery, and then obviously the source files for the code highlighter.

Don't forget who I am now. I won't take the time modifying all 117 webpages manually (yes, I checked with a little ls -r|grep ".php^"|wc -l); I'll write something crazy to modify all these for me.

The thing is, there are of course files, but there are some folders that link to more files and maybe even more folders that just bring up the same dilemma. I need something recursive.

Python to the rescue!

I figured it was about time to brush the dust off IDLE and give my favorite language some love.

Hell yeah, Python.

Check out some crazy code.

#!/usr/bin/env python

import os;

def attack( path ): #{
    print( '' );
    
    if ( os.path.isdir( path )): #{
        print( 'Got path ' + path + ', looking for folders...' );

        files = os.listdir( path );
        if ( files is not None ): #{

            target = '<link REL="stylesheet" TYPE="text/css" href="../../layout/lazy.css">';
            change_to = '<link REL="stylesheet" TYPE="text/css" href="../../layout/lazy.css"><link REL="stylesheet" TYPE="text/css" href="../../layout/shCore.css"><link REL="stylesheet" TYPE="text/css" href="../../layout/shThemeDefault.css">'
            
            print( 'We have files and folders to look through, awesome.' );

            for file in files: #{
                item = path + file;

                if ( os.path.isfile( item ) ): #{
                    if ( not item.endswith('.php') ):
                        continue
                    print item;
                    file_handle = open( item, 'r' );
                    contents = file_handle.read();

                    if ( target in contents ): #{
                        print( item + '  -  Target acquired.' );
                        file_handle.close();
                        
                        contents = contents.replace( target, change_to );

                        file_handle = open( item, 'w' );
                        print( item + ' modified' );
                        file_handle.write( contents );
                        file_handle.close();
                    #}
                #}
                else: #{
                    if ( os.path.isdir( item ) ): #{
                        attack( item + '/' );
                    #}
                #}
            #}
        #}
        else: #{
            print( 'Could not find files or folders in ' + path );
        #}
    #}
    else: #{
        print( 'Could not grab path.' );
        return 1;
    #}
#}

def main(  ): #{

    path = 'localhost/sdl/';
    attack( path );
    
    return 0;
    
#}


if ( __name__ == '__main__' ): #{

    main();
#}


Now I would run this code a whole ton of times, changing the target and change_to variables over and over again, to finally reach the right effect. Adding new lines for JavaScript includes, supplying the right CSS files, changing divs to pres, etc.

After a half hour or so, I got the whole 'website' to look exactly how I wanted it. Clean, tidy, and full of colorful code.

The tutorials and I should play a little bit nicer now.

That sure is weird code!

Yeah, keep in mind this was just a quick script I whipped out to get the job done. Eventually I ended up with invalid HTML, but it at least renders the way I want it to and it's not like these are being published anywhere.

I'll leave it up to you to decipher the code (as usual), but Python has awesome readability, I'm sure you will be fine.

You know, Python is just awesome in general.

Awesome.








<