JavaScript Cookies

People of Earth!  Cookies will not be saved by your browser when operating from a local file:// URL!  Do not waste time writing your own little cookie functions out of the forlorn, astounded believe that everyone else out there, e.g., jQuery, is somehow just crazy and doing something wrong!

function getCookie(get)
{
  var cookies = document.cookie.split(";");
  var i, name;
  for (i = 0; i < cookies.length; i++) {
    name = cookies[i].substr(0,cookies[i].indexOf("="))
      .replace(/^\s+|\s+$/g,"");
    if (name == get) {
      return unescape(cookies[i].substr(cookies[i].indexOf("=")+1));
    }
  }
  return null;
}

function setCookie(name, value, days)
{
  var expDate = new Date();
  expDate.setDate(expDate.getDate() + days);
  var cookie = escape(value) +
               ((days==null) ? "" : "; expires="+expDate.toUTCString());
  document.cookie = name + "=" + cookie;
}

function deleteCookie(name)
{
  setCookie(name, null, -1);
}

Ember.js Minimal Example

Recently I have been fooling with Ember.js.  Documentation is… not adequate.  At first it seems pretty good, but then it turns out:

  • The main documentation is terrible about showing anything but isolated pieces, and there are no simple but full examples.
  • Lots of the blog posts and other notes out there were invalidated by some major API changes over the last couple months.

So, it’s a fairly steep slog to get started.  Expect some ongoing notes.  I think this is about the smallest example you can put together that does anything:

<html>
<head>
<script  type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script  type='text/javascript' src="http://cloud.github.com/downloads/emberjs/ember.js/ember-0.9.6.min.js"></script>

<script type='text/javascript'>
App = Ember.Application.create({
  ready: function() {
  }
});

App.Dude = Ember.Object.extend({
  name: null
});

App.myDude = App.Dude.create({
  name: "Broderick"
});
</script>
</head>

<body>
<script type="text/x-handlebars">
{{ App.myDude.name }}
</script>
</body>
</html>

Linebreak “Fixing” in Emacs

Screenshot - 02082013 - 11:15:28 AMBack in the stone ages, everybody knew that whitespace was unimportant and people should be free to format text, code, whatever, any damn way they please.  NO LONGER!  In our brave new world all those line breaks, indents, tabbing, and so on are all critical and we must all do them the same way!  Or else!

The latest offender is GitLab‘s slightly modified Markdown, which treats linebreaks in text blocks as actual linebreaks and inserts hard breaks in the output HTML. Somewhat disappointing for an otherwise largely stellar package…

In any event, that means you need to be careful about how you format your README.md and such if you want it to print reasonably in GitLab’s web view. For all those Emacs devotees used to religiously META-Q‘ing text paragraphs to be terminal formatted—as any sane person would!—you wind up with a ton of short lines in the display.

This is a little helper I wrote and include in my .emacs to deal with this and similar problems:

(defun fix-lines ()
  (interactive "*")
  (save-excursion
    (save-restriction
      (if (region-active-p)
          (progn  (setq start (region-beginning) end (region-end))
                  (narrow-to-region start end)
                  (goto-char (point-min))))
         (while (re-search-forward "\\([^\n]\\)\n\\([^\n]\\)" nil t)
              (replace-match "\\1 \\2" nil nil)))))

(global-set-key "\C-xf" 'fix-lines)

CTRL-xf will now get rid of all those newly eviiil linebreaks. If a region is active only that text will be affected, otherwise the text from the current point on is modified. Hooray!