Textile & Markdown in Ruby

Posted by John
on Thursday, 13 November 2008
Textile

Simple to implement with the redcloth gem

sudo gem install RedCloth

Usage,

require 'redcloth'
s = RedCloth.new "p. format this with textile"
puts s
Markdown

With markdown you've got three implementations to try,

  • bluecloth gem (last maintained 2005, slow)
  • rdiscount gem (better c implementation, fast!)
  • maruku gem (pure ruby implementation)

To implement with BlueCloth

sudo gem install bluecloth

Then,

require 'bluecloth'
s = BlueCloth::new("# format this with markdown")
puts s

Or RDiscount

sudo gem install rdiscount

Then,

require 'rdiscount'
s = RDiscount.new("# format this with markdown")
puts s

Record Grouping

Posted by John
on Tuesday, 01 April 2008

Example to go thru your User model and return all the signups by month / year, doing conditionals on them and grouping,

@users = User.find(
  :all, 
  :select => "MONTH(created_at) as month, YEAR(created_at) as year, COUNT(*) AS records",
  :conditions => ["admin = 0'"], 
  :group => "year, month")

You can use this in your view like,

<% for @user in @users %>
  <%=@user.month%>
  <%=@user.year%>
  <%=@user.records%> signups
<% end %>

Multi-Boot Vista + ArchLinux with GRUB

Posted by John
on Thursday, 20 March 2008

After setting up my new HP laptop with Vista and ArchLinux I thought I'd post how to replicate the same setup to help anyone else wanting to do the same, so here's a guide to do this with the latest Windows Vista and Arch Linux; enjoy!

Install Windows Vista

Vista

First off install Vista. Now when it gets to asking you about how much disk space to use, say half (250gb drive => 128gb approx), this will mean the other half will be unused space (perfect for your linux build).

Install Vista and set it up, when you're happy and it's running ok continue to the next step.

Get ArchLinux

By now you should have two hard-drive partitions, one with Vista (NTFS) and one blank; now we're going to sort that second one out.

First download the relevant ISO disc image,

Download it and burn it to CD, it's only 120mb so shouldn't take too long. Once done insert it into your laptop and re-boot with it so it starts up the installer on bootup.

Install ArchLinux

ArchLinux

When the installer boots up and shows the blue install screen you'll see 6 options, let's go thru each one...

1. Prepare Hard Drive

Don't choose Auto-Prepare, it'll wipe your hardrive and destroy Vista, select Partition Hard Drives which will put you in the disk partition tool.

You should see two entries, one will be...

1. [boot] [primary] [ntfs]
2. ...... [unknown] [unknown]

The first is setup as the boot partition (the one which will boot on startup) and you know it's Vista as the filesystem is marked as NTFS, the other is unknown; our target for the Linux install.

So select the unknown one and create, mark it as a primary partition, select write to write the change and then quit to continue. Note the name of the new partition (e.g. hd0/sda2) and goto step 2.

Note: SDA = SATA, the machine i'm using has SATA drives, if you've got older IDE drives it'll be HDA0.

2. Set Filesystem Mountpoints

With this we'll want to set which partition we want to use as a swap disc, but as we didn't create one, select none.

Next you'll be asked which partition will be used as the root one for this install, select hd0/sda2 (or whichever name you wrote down in step 1). Continue to step 3.

3. Select Packages

Here we select which packages we want to install, ArchLinux is broken up into 4 separate blocks; base being the core o/s. Select all four as we'll be wanting all the tools to practice (otherwise you can choose 'base' and customise later). Step 4...

4. Install Packages

This will install ArchLinux to your target FileSystem, shouldn't take long; once done goto 5...

5. Configure System

Now configure your system, with most options choose the defaulty chosen options; afterwards your configuration will be written to the new system. Now onto the important step 6.

6. Install Bootloader

Now we'll install the bootloader to allow you to select on-bootup which O/S to start. Choose GRUB as your bootloader, you'll then be taken to the bootloader's config file.

Scroll down to the bottom of the file and uncomment the bottom entry until it resembles...

# (2) Windows Vista
title Windows Vista
rootnoverify (hd0,0)
makeactive
chainloader +1

This will add to your selection list an entry pointing to your first partition so you can select to boot Vista. Scroll up to the top of the file and check these lines, adjust as appropriately...

  • timeout 5 ...this sets the seconds to wait until booting the default entry

  • default 1 ...once the timeout expires GRUB will boot the option 1, ArchLinux, if you want to boot Vista instead, change it to 2.

  • fallback 0 ...if the default can't be booted, the fallback will be started, in this case the ArchLinux FallBack system created when we just installed ArchLinux (think of it as it's Linux's safe-mode, only much more powerful).

If you're happy with the setup, press CTRL+X to save the file and continue.

You'll next be asked where to place the Bootloader, we want to place it inside the MBR of the Drive so it will be launched when the disc spins up. The MBR is the first sector of the Hard Drive and commonly called the Master Boot Record.

You'll be presented with probably 3 options...

/SDA
/SDA1
/SDA2

Choose the first, this will be where the MBR is located.

Now you're finished, exit the Installer by selecting Exit Install, then type REBOOT to reboot.

The computer should restart and present you with the GRUB bootloader menu, where you'll now be able to select which O/S to start; job done.

Setting ROOT Password

Please note that with this initial install your ROOT account won't have a password assigned to it, posing a poblem later on down the line.

So when you've booted into ArchLinux, and typed 'root' as the username and hit ENTER to login, type...

passwd

To set the 'root' account's password.

Changing The Config

To edit the GRUB config later on, do...

nano /boot/grub/menu.lst

Settings

So as you know, the system config files are located in...

  • /etc/rc.conf (main system config)
  • /boot/grub/menu.lst (grub boot menu)
  • /etc/lilo.conf (lilo boot menu)
  • /etc/mkinitrd.conf
  • /etc/hosts
  • /etc/fstab
  • /etc/modprobe.conf
  • /etc/modules.conf
  • /etc/resolv.conf
  • /etc/conf.d/*
  • /etc/profile

JavaScript Dates

Posted by John
on Tuesday, 25 December 2007

One sometimes misunderstood element of JavaScript is how it handles dates, so in this article I'm going to try and conquer that little confusion.

P.S. Happy Christmas!!!

Dates in Depth

Returning the Current Date

Simple,

document.write(Date());

Creating a Date Object

First off you'll need to create a Date object, this is accomplished by...

var d = new Date();

This creates a new variable with the type as a date object, named d, with the default value as the current date & time.

Setting it's Value

var d = new Date("11/23/2007");

This will create a new Data object and set it's initial date to November 23rd 2007 (note it uses the American format mm/dd/yyyy).

You can set it's date afterwards with the .setFullYear();

d.setFullYear(1997,11,3);

Using the format YYYY/MM/DD.

Days

To return the day value of a Date object do,

document.write(d.getDate());  
// returns => 23 from 23rd Nov 2006
// (1..31)

document.write(d.getDay());   
// returns day of week value lands on, e.g. 2 for Monday for 23rd
// (0..6)

Yes this can be confusing, getDay() does not return what you expect (e.g. 23 from 11/23/2007), but the numeric value for the day of the week the 23rd lands on, so if the 23rd lands on a Tuesday it'd be 3. Use getDate() to return 23.

Setting the Day

document.write(d.setDate(11));
// sets the day digit to 11

Months

To return the month value of a Date object do,

document.write(d.getMonth());
// returns values from 0..11 (0=Jan 11=Dec)
// (0..11)

Because Month is an array, you'll need to add 1 to it to return days from 1..12,

document.write(d.getMonth()+1);
// returns values from 1..12 (corrected by adding 1 to the value)

Setting the Month,

d.setMonth(mm,dd);

// e.g.
document.write(d.setMonth(11,1));
// set the month to December the 1st

Years

To return the Year value of an Date object do,

document.write(d.getFullYear());
// returns 2007 for 11/23/2007

Note, there was a function called getYear() but this returns a 2-4 digit number and is not recommended to be used; depreciated.

Setting the year,

document.write(d.setFullYear(1992));
// sets the year to 1992

// also can be used like, 
d.setFullYear(yyyy, mm, dd);

Time

To return the Time value of a Date object do,

d.getTime();
// returns the number of seconds from midnight

Adding seconds to a day, e.g. advance 1 day would be...

d.setTime(86400);
// adds 86400 seconds to the current date value
// 86400 seconds = 1 day

Hours, Minutes, Seconds,

This can be done like,

document.write(d.getHours());
// (0..23), returns the hour of the day, 11 = 11 O'clock

document.write(d.getMinutes());
// (0..59), returns the minutes, 23 = 23 minutes past

document.write(d.getSeconds());
// (0..59), returns the seconds, 12 = 12 seconds past

document.write(d.getMilliseconds());
// (0..999), returns the milliseconds past
// 1000 milliseconds in a second

Setting the values,

d.setHours(hour,min,sec,milli);

// so..
document.write(d.setHours(12));
// set to 12 noon

d.setMinutes(min,sec,milli);
document.write(d.setMinutes(2));

d.setSeconds(sec,milli);

d.setMilliseconds(milli);

To String

To return a Date object as a String value,

document.write(d.toString());
// returns Tue Dec 25 2007 12:44:05 GMT+0000 (GMT)

Calculations

Days in Month

This function returns the max days in the chosen month, so 2, 2008 returns => 29.

function daysInMonth(iMonth, iYear)
{
return 32 - new Date(iYear, iMonth, 32).getDate();
}

Thanks goes to Charlie for that...

Comparing Dates

if (Date.parse("11/11/2007") < Date.parse("11/11/2008")) {
document.write("wooah!");
}

NOTE: remember to use a lowercase if* rather than *If otherwise it'll throw up a missing ; error.