-
Notifications
You must be signed in to change notification settings - Fork 15
/
site.js
70 lines (55 loc) · 2.21 KB
/
site.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
---
layout: null
---
// TODO: Rewrite in JQuery. Add unit tests
function pointCount(n) {
var text = n.data;
// look for (x pts) where x is some sequence of [0-9]+
// save the result of [0-9]+ in result[1]
var pointRegExp = /\([ ]*([0-9]+)[ ]*pt[s]?[ ]*\)/i;
var result = pointRegExp.exec(text);
return result ? parseInt(result[1]) : 0;
// if there was a match, result is truthy
// if there was a match, numeric part is in result[1]
// otherwise result is 0 points
}
// TODO: Rewrite in JQuery
function countPoints(n) { // n is a Node
if (n.nodeType == 3 /*Node.TEXT_NODE*/) // Check if n is a Text object
return pointCount(n); // If so, parse its text and return number of points
// Otherwise, iterate through n's children, totalling up the points
var numpoints = 0;
for(var m = n.firstChild; m != null; m = m.nextSibling) {
numpoints += countPoints(m);
}
return numpoints; // Return total of all children's points
}
$(document).ready(function(){
console.log("site.js: document is ready");
$('.template').each(function(i) {
$(this).css('display','none');
});
// Use with <div class="copy-of" data-id="foo"></div>
// Use <div id="foo" class="template"></div> on the stuff you want to copy
// The class="template" will hide it the first time.
// The class="copy-of" data-id="foo" signals that you want a copy of foo inserted here.
$('.copy-of').each(function(i) {
var id = $(this).data('id')
$(this).html($(document.getElementById(id)).clone().html());
});
$('[data-hfj]').each(function() {
var chapter_num = $(this).data('hfj');
var href = "{{ site.hfj_chapter_url_prefix }}" + chapter_num;
$(this).html($('<a href="' + href + '">HFJ Chapter ' + $(this).data('hfj') + '</a>'));
});
$('[data-hfdp]').each(function() {
var chapter_num = $(this).data('hfdp');
var href = "{{ site.hfdp_chapter_url_prefix }}" + chapter_num;
$(this).html($('<a href="' + href + '">HFDP Chapter ' + $(this).data('hfdp') + '</a>'));
});
if ($(".pointCount").length > 0 ) {
var total = countPoints(document.body);
$(".pointCount").html(total);
}
console.log("site.js: done");
});