Feb 13, 2010

Blog - Syntax Highlight, Code Formatting - Part2

So in Part 1, I described how to spruce up code in a blog. It worked well but had a few shortcomings. Largest of which, it didn't automatically wrap lines. So if your code was wider than your blog template, it would get cut off. Another problem was, you needed to find a place to host the javascript and css files.

Since then, I've been pointed to the this blog that shows easy syntax highlighting for blogger. This has several strengths over Google's version that I described. In addition to solving the two problems above, it also offers a great "copy" feature: after hovering over a code section, links appear allowing viewers to copy everything, view the code separately, or even print it!

Here's an example of code highlighted the new way:
<div class="sidebar">
<a href="http://super_long_url_that_makes_this_line_long" target="_blank" name="someName" id="someId">click here for awesome</a>
</div>
The only downside I see is that this version takes a long time to load because it's using javascript files, css files AND a Flash SWF file. I may end up hosting all of these locally (on google's server) to see if that improves speed. Also, it may impact the iphone/ipad (non-flash) view of the site. I'll have to test what happens when a user doesn't have flash (or maybe even javascript) enabled.

All in all, I will keep the previous blog the way it was but, going forward, I'm using the awesome highlighting, instead of Google's! As far as formatting is concerned, I'm still testing different external blog tools. The next part will cover the one I chose and why.

Feb 11, 2010

Blog - Syntax Highlight, Code Formatting - Part1

So, I started this blog and I'm going to be writing a lot of code. I want that code to be readable and I don't feel like formatting it manually.

Syntax Highlighting - Setup
After a little searching, I found that Google has both a CSS file and a JavaScript file you can download that will handle highlighting for you. Here's the direct link:
http://code.google.com/p/google-code-prettify/downloads/list
  1. Download the "small" version. All you need are these two files:
    prettify.css
    prettify.js
  2. Place them on a webserver somewhere.
  3. Edit your blog template to link to them (on blogger go to Layout > Edit HTML) place the following code just after the opening <head> tag . . . and you're done!!
    <link href="[Path_To_Server]prettify.css" type="text/css" rel="stylesheet" />
    <script type="text/javascript" src="[Path_To_Server]prettify.js"></script>
    
    Where [Path_To_Server] represents the location on your webhost where you placed those files.
For TESTING PURPOSES ONLY you can use the following code, verbatim, at your own risk (it links directly to Google's hosted version of the files. hehe):
<link href="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js"></script>
Seriously, though make sure you host those files yourself.

Syntax Highlighting - Use

Place the code you want highlighted in

    <pre class="prettyprint">...</pre>
or
    <code class="prettyprint">...</code>
and it will automatically be pretty printed.

Checkout the Readme File for more details.

Syntax Highlighting - Customizing
Of course, you can modify the stylesheet to get the look you prefer. On my site, I also modified the CSS in my template for the code and pre tags. I "borrowed" the css code from the stylesheet at StackOverFlow.com and modified it slightly. This way, I can use plain <code> tags without having to specify the class attribute (like I just did):
pre, code {  
  background-color:#efefef;
  font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New;
  border: 0px;
}
Code Formatting
I'll save this for part 2. My plan is to find an external editor to handle the formatting for me. From there, I will link the editor to my Blogger site and all will be merry. Face it, deep down, I really don't want to keep putting the markup in these blog posts by hand! There's a solution out there, somewhere.

Mac OS X : Open Ports, Network Connections, PIDs

Yesterday, my MacBook Pro was acting VERY strangely. I was hacked! (or so I thought) I feared that some evildoer hacked my home network and ran amuck.

Quickly, I had to figure out what was happening on my network. I wanted to know what ports were open, what connections were live and which applications were using those connections. Finding Linux info was easy. Mac OS X info? Not so much!

After hunting dozens of Google pages, here's what I've come up with:

Check All Network Connections
Run the following shell command to display all network connections:
netstat -an | grep 'LIS\|WAIT\|Recv-Q'

This will show all ESTABLISHED, LISTEN and TIME_WAIT connections and also displays the header for convenience.
Show Processes On Network
The command above is good but it doesn't show you WHO is on those ports. The following allows us to see the PID and Name of each process using the network!
sudo lsof -Pnl +M -i

Note: the first flag is -PNL (the last character is not a one). The first time I ran the lsof command, I couldn't see the ports that were listening. This was my main concern (evildoers love listening on ports). The key is to run this command as root (via sudo). Also note, with the netstat call, you can use egrep 'LIS|WAIT'. Honestly, I haven't gotten around to learning what egrep does. All I know is you don't have to escape the | symbol with egrep.