var WikiGraph = {};

WikiGraph.WIKI_URL_RAW = 'http://en.wikipedia.org/w/index.php?title={{ title }}&action=raw';
WikiGraph.LINK_TEMPLATE = '<a {{#blank}}target="_blank"{{/blank}} href="{{ url }}">' +
                          '{{ text }}</a>';

WikiGraph.getDataForPage = function(page) {
  $('#links').html('<i class="icon-spinner icon-spin icon-4x"></i>');
  $.ajax('/page', {
    method: 'get',
    data: {page: page},
    success: function(data) {
      data = JSON.parse(data);
      var linkify = function(text) {
        var url = Mustache.render(WikiGraph.WIKI_URL_RAW,
                                  {title: encodeURIComponent(text)});
        var result = Mustache.render(WikiGraph.LINK_TEMPLATE,
                                     {url: url, text: text, blank: true});
        return result;
      };

      result = _.map(data, linkify).join('<br>');
      $('#links').html(result);
      var baseUrl = Mustache.render(WikiGraph.WIKI_URL_RAW,
                                    {title: encodeURIComponent(page)});
      var baseLink = Mustache.render(WikiGraph.LINK_TEMPLATE,
                                     {url: baseUrl, text: 'page', blank: true});
      console.log(baseLink);
      $('#count-links').html(data.length);
      $('#label-page').html(baseLink);
    },
    error: function() {
      $('#links').html('<h3>That page doesn\'t exist.</h3>');
      $('#count-links').html('0');
    }
  });
};

WikiGraph.init = function() {
  $('#btn-refresh').click(function() {
    WikiGraph.getDataForPage($('#input-page').val());
  });

  $('#input-page').keypress(function(e) {
    // Enter key refreshes the links.
    if (e.which === 13) {
      e.preventDefault();
      $('#btn-refresh').click();
    }
  });
};

WikiGraph.init();

@@ -32,20 +32,16 @@
 WIKI_URL
-_RAW
  = 'http
@@ -87,16 +87,53 @@
 %7B title 
+%7D%7D' +%0A                        '%7B%7B#raw
 %7D%7D&actio
@@ -137,16 +137,24 @@
 tion=raw
+%7B%7B/raw%7D%7D
 ';%0AWikiG
@@ -551,24 +551,75 @@
 ion(text) %7B%0A
+        var encodedUrl = encodeURIComponent(text);%0A
         var 
@@ -650,36 +650,32 @@
 kiGraph.WIKI_URL
-_RAW
 ,%0A              
@@ -712,150 +712,731 @@
 code
-URIComponent(text)%7D);%0A        var result = Mustache.render(WikiGraph.LINK_TEMPLATE,%0A                                     %7Burl: url, text: text
+dUrl%7D);%0A        var urlRaw = Mustache.render(WikiGraph.WIKI_URL,%0A                                     %7Btitle: encodedUrl,%0A                                      raw: true%7D);%0A        var result = Mustache.render(WikiGraph.LINK_TEMPLATE,%0A                                     %7Burl: url,%0A                                      text: text,%0A                                      blank: true%7D);%0A        result += '-';%0A        result += Mustache.render(WikiGraph.LINK_TEMPLATE,%0A                                  %7Burl: '#' + encodedUrl,%0A                                   text: '%5BGraph%5D'%7D);%0A        result += ' ';%0A        result += Mustache.render(WikiGraph.LINK_TEMPLATE,%0A                                  %7Burl: urlRaw, text: '%5BRaw%5D'
 , bl
@@ -1621,12 +1621,8 @@
 _URL
-_RAW
 ,%0A  
@@ -1687,16 +1687,64 @@
 nt(page)
+,%0A                                     raw: true
 %7D);%0A    
@@ -1886,37 +1886,8 @@
 %7D);%0A
-      console.log(baseLink);%0A
     
@@ -2122,66 +2122,331 @@
 aph.
-init = function() %7B%0A  $('#btn-refresh').click(function() %7B
+reloadHash = function() %7B%0A  var page = decodeURIComponent(window.location.hash.split('#')%5B1%5D);%0A  $('#input-page').val(page);%0A  WikiGraph.getDataForPage(page);%0A%7D;%0A%0AWikiGraph.init = function() %7B%0A  $('#btn-refresh').click(function() %7B%0A    var page = $('#input-page').val();%0A    window.location.hash = encodeURIComponent(page);
 %0A   
@@ -2471,38 +2471,20 @@
 ForPage(
-$('#input-page').val()
+page
 );%0A  %7D);
@@ -2653,32 +2653,174 @@
 k();%0A    %7D%0A  %7D);
+%0A%0A  if (window.location.hash) %7B%0A    WikiGraph.reloadHash();%0A  %7D%0A%0A  $(window).bind('hashchange', function() %7B%0A    WikiGraph.reloadHash();%0A  %7D);
 %0A%7D;%0A%0AWikiGraph.i

Add hash navigation and more links.