better judge regex
[sjy/austlii.git] / austlii.js
index e506188..1908d47 100644 (file)
@@ -17,14 +17,13 @@ window.austlii = {
         s.rel = "stylesheet"; s.type = "text/css"; s.href = url; 
         document.getElementsByTagName('head').item(0).appendChild(s); return;
     },
-    main: function() {
-        austlii.prune("austlii", "jquery");
+    init: function() {
         if (window.location.host.slice(-14) != "austlii.edu.au" ||
             window.location.pathname.slice(0,10) != "/au/cases/" ||
             window.location.pathname.slice(-5) != ".html")
             console.log("You're not looking at a case on AustLII.");
         else
-            austlii.paintMenu();
+            austlii.main();
     },
     prune: function() {
         // Remove all but the newest script tag containing the queries passed in as arguments.
@@ -39,48 +38,81 @@ window.austlii = {
     },
 };
 
-austlii.paintMenu = function() {
+austlii.main = function() {
+
+    // grab austlii.css from the same directory as this JavaScript
+    var cssUrl = $('script[src$="austlii.js"]').attr('src').replace(/austlii\.js/, "austlii.css");
+    austlii.style(cssUrl);
+
+    austlii.cleanup();
+    austlii.addMarkup();
+    austlii.paintMenu();
 
-    austlii.style("http://splintax.ucc.asn.au/austlii/austlii.css");
+};
 
+austlii.cleanup = function() {
     // remove text nodes ([] surrounding "Download", etc)
-    var textNodes = $("body").contents().filter(function(){return this.nodeType == 3;}).remove();
-    // hide other unnecessary menu elements
-    var $els = $("body").children(); // excludes text nodes
-    var index = 0;
-    while ($els[index].tagName != "H1") {
-        $els[index].style.display = "none"; index++;
-    }
-    // add back to top
+    // var textNodes = $("body").contents().filter(function(){return this.nodeType == 3;}).remove();
+    // remove unnecessary <br />s
+    $("br").remove();
+};
+
+austlii.addMarkup = function() {
+
+    // add back to top around header
     $("h1").eq(1).wrap('<a name="top">');
 
     // highlight case title
     $("center i").first().parent().addClass("citation");
 
     // find and markup judges
-    var judgeExp = /([A-Zc, ]+)((&nbsp;| )C?JJ?\.)/g;
+    // var judgeExp = /([A-Zc]+\s)([A-Zc,\s]+\sC?JJ?\.)/g;
+    var judgeExp = /([A-Z]{3})((?:[A-Z\s,]|&nbsp;)+C?J?J\.)/g
     var replacementText = document.body.innerHTML.replace(judgeExp, '<a class="judge" name="$1">$1$2</a>');
     document.body.innerHTML = replacementText;
 
-    // add menu
-    var $menu = $('<div id="menu"><a href="#top">Top</a></div>');
+};
+
+austlii.paintMenu = function() {
+
+    // create menu 
+    var $menu = $('<div id="menu"><ol></ol></div>');
+    $("body").append($menu);
+
+    // add fixed link to headnote
+    $("#menu ol").append('<li><a href="#top">Headnote</a></li>')
+
     // add jumplinks for each judge
-    var $judges = $('<ol id="judges"></ol>');
     $("a.judge").each(function(){
         var $li = $('<li><a href="#' + this.name + '">' + $(this).html() + '</a></li>');
-        $judges.append($li);
-    }); $menu.append($judges);
+        $("#menu ol").append($li);
+    });
+
+    // add fixed link to footnotes
+    $("#menu ol").append('<li><a href="#fn1">Footnotes</a></li>');
+
     // add annotation features
-    $menu.append($('<a id="trigger-highlight" href="#">Highlight</a>'));
-    $("body").append($menu);
+    //$menu.append($('<a id="trigger-highlight" href="#">Highlight</a>'));
 
     // enable smooth scrolling on anchors
-    austlii.load("http://splintax.ucc.asn.au/cases/jquery.scrollTo-1.4.2-min.js", function() {
-        austlii.load("http://splintax.ucc.asn.au/cases/jquery.localscroll-1.2.7-min.js", function() {
+    austlii.load("http://splintax.ucc.asn.au/austlii/jquery.scrollTo-1.4.2-min.js", function() {
+        austlii.load("http://splintax.ucc.asn.au/austlii/jquery.localscroll-1.2.7-min.js", function() {
             $.localScroll({duration: '200'});
         });
     });
 
+    // update current judge on scroll
+    $(window).scroll(function() {
+        $("#menu ol li a").each(function(){
+            // if the relevant link appearsmore than half way down the page
+            var $anchor = $('a[name="'+$(this).attr("href").slice(1)+'"]');
+            if ($(window).scrollTop() + $(window).height()/2 > $anchor.offset().top) {
+                austlii.$currentSection = $(this);
+            }
+        });
+        $("#menu ol li a").not(austlii.$currentSection).removeClass("current");
+        austlii.$currentSection.addClass("current");
+    }); $(window).scroll(); // and run it once
 
     $("#trigger-highlight").click(austlii.highlight);
 
@@ -108,5 +140,9 @@ austlii.highlight = function(event) {
     else {console.log("You haven't highlighted anything.")}
 };
 
-console.log("Loaded austlii.js v"+austlii.VERSION+".");
-austlii.load('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', austlii.main);
+if (document.getElementsByTagName("script").length == 1) {
+    console.log("Loaded austlii.js v"+austlii.VERSION+".");
+    austlii.load('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', austlii.init);
+} else {
+    alert("austlii.js will not run when there are other scripts on the page.");
+}

UCC git Repository :: git.ucc.asn.au