Scala syntax extension

Follow up with yesterday:
you can get my Scala syntax gem extension here, and read below for the instructions on how to install it.

First here is the result:


/**
 so much fun
*/
class Auction(seller: Actor, minBid: Int, closing: Date) extends Actor {
  val StringForFun = "hello"
  val timeToShutdown = 36000000 // msec
  val bidIncrement = 10
  def act() {
    var maxBid = minBid - bidIncrement
    var maxBidder: Actor = null
    var running = true
    while (running) {
      receiveWithin ((closing.getTime() - new Date().getTime())) {
        case Offer(bid, client) =>
          if (bid >= maxBid + bidIncrement) {
            if (maxBid >= minBid) maxBidder ! BeatenOffer(bid)
            maxBid = bid; maxBidder = client; client ! BestOffer
          } else {
            client ! BeatenOffer(maxBid)
          }
        case Inquire(client) =>
          client ! Status(maxBid, closing)
        case TIMEOUT =>
          if (maxBid >= minBid) {
            val reply = AuctionConcluded(seller, maxBidder)
            maxBidder ! reply; seller ! reply
          } else {
            seller ! AuctionFailed
          }
          receiveWithin(timeToShutdown) {
            case Offer(_, client) => client ! AuctionOver
            case TIMEOUT => running = false
          }
      }
    }
  }
}

You will need to add those CSS elements to display things correctly:

pre {
	background: #000000 repeat-x;
	color: #00FF00;
	font-family: arial, 'lucida console', sans-serif;
	line-height: 160%;
	font-size: 120%;
}

code {
	color: #00EE00;
	font-style: bold;
	font-family: arial, 'lucida console', sans-serif;
}	

.comment { color: #333; font-style: italic; }
.keyword { color: #eff; font-weight: bold; }
.punct { color: #444; font-weight: bold; }
.symbol { color: #0bb; }
.string { color: #6b4; }
.ident { color: #00b; }
.constant { color: #66f; }
.regex { color: #a82; }
.number { color: #a33; }
.expr { color: #227; }

Then on your machine, you will need ruby and rubygems installed, and install redcloth and syntax:

gem install syntax
gem install redcloth

Put your sample into a text file in the same folder as run.rb, then run:

ruby run.rb myscala.txt > output.html

That’s about it. It’d be great to develop the same things for Java and CSS. In the mean time, enjoy!

Textile

When creating the documentation for svn2rss, I fell on the awesome “newgem” gem that Rubyforge provides to help outputting a website and releasing gems.

It was really very helpful, and I liked the way the syntax gem just transformed my ruby code into a nice color block. (I am still looking for the right shade of grey for the comments though)

I would like to provide an extension of the syntax gem to do the same thing for scala. (I still have to learn the language first, hopefully the pdfs on the website will be entertaining).

I haven’t been able to find a previous work in this area. Feel free to comment if you happen to have something already working.