The Single Function Module

A function, all by itself:

module.exports = function(string) {
  return string.replace(/&(?!\w+;|#\d+;|#x[\da-f]+;)/gi, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#x27;').replace(/\//g,'&#x2F;');
};

This is the escapeHTML() function from Backbone.js, which is hidden behind a closure. Thankfully it’s now in the latest version of Underscore.js, which is depended upon by Backbone. If an old version of underscore was being used and escapeHTML() was the one missing utility function that a developer needed, putting it in a module by itself would be one way of supplying it.

Side note: Backbone and Underscore escape the exact six characters specified in Rule #1 of the Open Source Web Application Security Project (OWASP)’s XSS (Cross Site Scripting) Prevention Cheat Sheet.

Posted in uncategorized | Tagged , , , | Leave a comment

br-jquery: a jquery package for the browserify javascript packager for node.js

I published a node.js package to npm called br-jquery, which contains jQuery and provides an alternate path to downloading jQuery and putting it in a repository when starting a jQuery project. It is a lot like jquery-browserify, but it contains a newer version of jQuery (1.7.1) and has the minified version as well. Additionally it has a build script which fetches jQuery and a full example.

Browserify creates bundles from node.js-style CommonJS modules. It stubs out some core functionality so node.js modules with minimal dependencies can be used in the browser. Here is some JavaScript taken from the example, that creates a bundle:

var js = require('browserify')({
  require: {jquery: 'br-jquery'},
  entry: 'hello.js'
}).bundle()

The require property is used to , and the entry property is used to include a module that will be run when the bundle is loaded. The entry module starts by requiring jQuery:

var $ = require('jquery');
$(function() {
  // manipulate the DOM here...
});

I really like the browserify way of doing things. Instead of adapting browser code to run on the server, it adapts server code to run on the browser!

Repositorybr-jquery
Last Commit2012-01-22
Watchers4
Repositorynode-browserify
Last Commit2012-01-26
Watchers615
Repositoryjquery
Ownerjquery
Last Commit2012-02-01
Watchers11794
Posted in uncategorized | Tagged , , , | Leave a comment

One Goal For 2012

This isn’t my only goal for 2012, but it’s the only goal from my only set of goals for 2012 which contains only one goal.

My goal is to, each night before I go to bed, prepare for the next morning. This means having a default choice of what to wear, knowing what I need to pack and where I’m first going to go, and a draft of a schedule and to-do list for the day.

My inspiration for this comes from a few different sources:

This matters most to me because I feel that my best self can figure out all of the other stuff, but if I don’t start my mornings off right, I’m unlikely to be my best self.

Posted in uncategorized | Tagged , | Leave a comment

rbenv and virtualenv performance tip: load on demand

I found that new terminal sessions were taking a while to load, so I started commenting things out in my bash profile (How to be a Programmer: A Short, Comprehensive, and Personal Summary calls it a Binary Search). I found that it was snappy when I commented out my calls to virtualenvwrapper and rbenv.

Of course, with these calls I lost the ability to use them, and I lost the defaults. To fix this I chose a faster way to load the defaults and made functions for loading the full functionality of these. Here is the relevant part of ~/.bash_profile.

# Python
export VIRTUAL_ENV_DISABLE_PROMPT=1
# my virtualenv is called "batv", replace this with what you passed to mkvirtualenv
source $HOME/.virtualenvs/batv/bin/activate
loadvirtualenv() {
  # the next two lines might not be necessary for you; I only have them on OS X and not Linux
  export PYTHONPATH=/Library/Python/2.7/site-packages/virtualenv-1.6.4-py2.7.egg
  export PYTHONPATH=/Library/Python/2.7/site-packages/virtualenvwrapper-2.10.1-py2.7.egg:$PYTHONPATH
  source /usr/local/bin/virtualenvwrapper.sh
  workon batv
}

# Ruby
export PATH="$HOME/.rbenv/shims/:$HOME/.rbenv/bin:$PATH"
loadrbenv() {
  eval "$(rbenv init -)"
}

To use my default virtualenv or to use rbenv without the rbenv shell command, I don’t have to run either of these functions. If I want to do advanced stuff with virtualenvwrapper, I type loadvirtualenv and hit enter, and then I can use all of virualenvwrapper in that terminal session. If I want to set a rbenv environment for my current shell, I type loadrbenv.

Before I made these changes, the contents each of these functions were run each time I created a terminal. Now they’re run only on demand and opening a new terminal tab is much snappier!

Posted in uncategorized | Leave a comment

Links: Parsing in Ruby and JavaScript

I’ve been reading quite a bit about parsing and templating in ruby as I attempt to port a templating engine from JavaScript to Ruby. Here are some scattered links:

Jison

Jison is a parser generator for JavaScript that has separate lex and bnf definitions. It’s used by Handlebars.js.

Repositoryjison
Ownerzaach
Last Commit2012-02-05
Watchers482

Treetop

Treetop is a parser generator for Ruby. It’s installed with Ruby On Rails, through the mail gem which is installed by ActionMailer.

Repositorytreetop
Last Commit2011-12-19
Watchers433

Citrus

Citrus is another promising parsing gem for Ruby. It seems to be very easy to get started with, and I like many of the design decisions.

Repositorycitrus
Last Commit2011-12-29
Watchers167

Temple

Temple is a templating-specific library that helps with a lot of the AST transformation. It doesn’t seem to have a CFG syntax so it seems that using treetop or citrus would make sense for complex grammars. Otherwise, strscan could be used.

Repositorytemple
Ownerjudofyr
Last Commit2012-01-27
Watchers150

temple-mustache

This is an implementation of a mustache renderer in . It uses strscan to generate the initial parse tree, and Temple to generate the ruby code. It supports mustache sections.

Repositorytemple-mustache
Ownerminad
Last Commit2011-05-04
Watchers1

Slim

Slim is a Haml-like templating library for Ruby that’s used in production by many. It uses Temple, with a line-based parser, which works well because it uses significant indentation for nesting.

Repositoryslim
Ownerstonean
Last Commit2012-02-03
Watchers930
Posted in uncategorized | Tagged , , , , , | Leave a comment