A command line syntax highlighter written in Ruby

Here is the syntax highlighting cat, which I shall call cax.

#!/usr/bin/env ruby
require 'syntax/convertors/html'
require 'rainbow'

filename = ARGV[0]
puts ARGV[0]

line_num=0

tokenizer = Syntax.load "ruby"

file = File.new(filename,"r")
while (line = file.gets)

c_line = ""

tokenizer.tokenize( line ) do |token|
case token.group.to_s
when "string" then c_line = c_line + token.color(:green)
when "ident" then c_line = c_line + token.color(:magenta)
when "normal" then c_line = c_line + token.color(:cyan)
when "keyword" then c_line = c_line + token.color(:red)
when "punct" then c_line = c_line + token.color(:white)
when "symbol" then c_line = c_line + token.color(:white)
when "number" then c_line = c_line + token.color(:white)
when "expr" then c_line = c_line + token.color(:white)
when "comment" then c_line = c_line + token.color(:yellow)
when "constant" then c_line = c_line + token.color(85,85,85) # only named colors show on win32
else c_line = c_line + "  group: #{token.group}"
end
end

line_num = line_num + 1

print (line_num.to_s.rjust(4) + ":").color(:white) + c_line

end

And this is what the output looks like:

It’s obviously missing the theming of the coloured output which I could add in another version.

It requires the rainbow and syntax gems to get working. and on windows you’ll need the win32console gem to see the colours.

Posted in Uncategorized

Threaded array library for Codeigniter

This library take an array of comments (can also be anything with ids and parent_ids)

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class threaded
{

    public $parents  = array();
    public $children = array();

    /**
     * @param array $comments
     */
    public function arrange($comments)
    {
        foreach ($comments as $comment)
        {
            if ($comment['parent_id'] === NULL)
            {
                $this->parents[$comment['id']][] = $comment;
            }
            else
            {
                $this->children[$comment['parent_id']][] = $comment;
            }
        }
        $this->print_comments();
    }

    private function tabulate($depth)
    {
        for ($depth; $depth > 0; $depth--)
        {
            echo "t";
        }
    }

    /**
     * @param array $comment
     * @param int $depth
     */
    private function format_comment($comment, $depth)
    {

        echo "n";

        $this->tabulate($depth+1);

        echo "<li>";
        echo $comment['text'];
        echo "</li>n";
    }

    /**
     * @param array $comment
     * @param int $depth
     */
    private function print_parent($comment, $depth = 0)
    {
        $this->tabulate($depth);
        echo "<ul>";
        foreach ($comment as $c)
        {
            $this->format_comment($c, $depth);

            if (isset($this->children[$c['id']]))
            {
                $this->print_parent($this->children[$c['id']], $depth + 1);
            }
        }
        $this->tabulate($depth);
        echo "</ul>n";
    }

    private function print_comments()
    {
        foreach ($this->parents as $c)
        {
            $this->print_parent($c);
        }
    }

}

To use the library, put it in “libraries” and load in codeigniter with

$this->load->library('threaded');

and name it threaded.php

This library is based on the following post, http://www.jongales.com/blog/2009/01/27/php-class-for-threaded-comments/

So the array we would use is the same as the above blog post,

$comments = array(  array('id'=>1, 'parent_id'=>NULL,   'text'=>'Parent'),
                   array('id'=>2, 'parent_id'=>1,      'text'=>'Child'),
                   array('id'=>3, 'parent_id'=>2,      'text'=>'Child Third level'),
                   array('id'=>4, 'parent_id'=>NULL,   'text'=>'Second Parent'),
                   array('id'=>5, 'parent_id'=>4,   'text'=>'Second Child')
             );

and you process the array into unordered lists like this.

$this->threaded->arrange($comments);

This is the result you should get in html

<ul>
    <li>Parent</li>
    <ul>
        <li>Child</li>
        <ul>
            <li>Child Third level</li>
        </ul>
    </ul>
</ul>
<ul>
    <li>Second Parent</li>
    <ul>
        <li>Second Child</li>
    </ul>
</ul>
Posted in Uncategorized