forked from mrBliss/cpl-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bib-formatter.js
111 lines (103 loc) · 3.28 KB
/
bib-formatter.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var formatAuthors = function(authors) {
return authors.map(function(author) {
return author[0] + ', '
+ author.splice(1).map(function(name) {
return name[0] + '.';
}).join('');
}).join(', & ');
};
var Book = function(name, fields) {
this.name = name;
for (var field in fields) {
this[field] = fields[field];
}
};
Book.prototype.toHTML = function() {
var fmtAuthors = formatAuthors(this.authors);
// We need pattern-matching
var fmtEdition = null;
switch (this.edition) {
case 1: fmtEdition = '1st'; break;
case 2: fmtEdition = '2nd'; break;
case 3: fmtEdition = '3rd'; break;
default: fmtEdition = this.edition + 'th';
};
return '<li class="book">'
+ '<span class="author">' + fmtAuthors + '</span> '
+ '<span class="year">(' + this.year + ')</span>. '
+ '<span class="title">' + this.title + '</span>'
+ (this.edition > 1
? ' <span class="edition">(' + fmtEdition + ' ed.)</span>. '
: '. ')
+ '<span class="publisher-address">' + this.address + '</span>: '
+ '<span class="publisher">' + this.publisher + '</span>.</li>';
};
var Wikipedia = function(name, fields) {
this.name = name;
for (var field in fields) {
this[field] = fields[field];
}
};
Wikipedia.prototype.toHTML = function() {
return '<li class="encyc"><span class="title">'
+ this.title + '</span>. In <span class="encyc">Wikipedia</span>.'
+ ' Retrieved ' + this.retrieved + ', from <a href="' +
this.url + '">' + this.url + '</a></li>';
};
var Website = function(name, fields) {
this.name = name;
for (var field in fields) {
this[field] = fields[field];
}
};
Website.prototype.toHTML = function() {
return '<li class="website"><span class="siteName">'
+ this.siteName + '</span>. '
+ (this.year ? '(' + this.year + '). ': '')
+ '<span class="title">' + this.title + '</span>. '
+ (this.retrieved
? 'Retrieved ' + this.retrieved + ', from '
: '')
+ '<a href="' + this.url + '">' + this.url + '</a></li>';
};
var Blog = function(name, fields) {
this.name = name;
for (var field in fields) {
this[field] = fields[field];
}
};
Blog.prototype.toHTML = function() {
return '<li class="blog"><span class="authors">'
+ formatAuthors(this.authors) + '</span> '
+ (this.published
? '(' + this.published + '). '
: '')
+ '<span class="title">' + this.title + '</span>. '
+ 'Message posted to '
+ '<a href="' + this.url + '">' + this.url + '</a></li>';
};
// Ugly, but easy
eval(require('fs').readFileSync('./bibliography.js', 'utf8'));
console.log('<ul>');
console.log(entries.sort(function(a, b) {
if (a.authors && b.authors) {
return (a.authors < b.authors)
? -1
: (a.authors > b.authors)
? 1
: 0;
} else if (a.authors) {
return -1;
} else if (b.authors) {
return 1;
} else {
return (a.title < b.title)
? -1
: (a.title > b.title)
? 1
: 0;
}
}).map(function(book) {
return book.toHTML();
}).join('\n'));
console.log('</ul>');