-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.tldr.js
232 lines (205 loc) · 8.21 KB
/
jquery.tldr.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
TL;DR jQuery Plugin
Brett Terpstra 2013
Description:
Scans for headlines in a block of content and creates a "TL;DR" element at the top
with the headline and a summary generated either from a data-tldr attribute on the
headline or from the first sentence of the next paragraph after.
Options/defaults:
// GENERAL
target: '', // target to prepend output to, defaults to the context of the plugin
insertAfterLead: false, // insert the output before the first headline
// instead of at the top
headlineElements: 'h2,h3,h4', // Elements to consider headlines
dataAttribute: 'tldr', // data attribute for summaries
minimumHeadlines: 3 // minimum number of headlines in text required to execute, 0 no limit
useMetaDescription: true // Prepend description or og:description contents
// FORMATTING
wrapper: '<div class=tldr-wrapper>', // element to wrap the list in
listType: 'ol', // ol or ul
headlineTag: 'strong', // element to wrap the headlines in
headerTag: 'h4', // the tag for the header above the list
headerTitle: 'TL;DR', // the title of the block
collapsed: false, // if true, summaries collapse and expand on headline clicks
addClickHandler: true, // add handler to open collapsed summary on click
// set to false if you're providing a custom handler
// ignored unless collapsed is true
accordion: true, // only one visible summary at a time,
// ignored unless collapsed is true
// SUMMARIZATION
grafsInSummary: 1, // number of paragraphs to summarize
sentencesPerGraf: 1, // sentences per graf in summary
maxSummaryLength: 200, // maximum characters per summary, 0 for unlimited
Copyright (c) 2014 Brett Terpstra, http://brettterpstra.com
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function($) {
$.fn.tldr = function(options) {
var opts = $.extend({}, $.fn.tldr.defaults, options);
return this.each(function() {
$this = $(this);
output = $.fn.tldr.build($this, opts);
if (output) {
if (opts.insertAfterLead) {
output.insertBefore($(opts.headlineElements,opts.target).first());
} else {
output.prependTo($(opts.target === '' ? $this : opts.target));
}
}
});
};
function trim(text) {
return text.replace(/^\s*/,'').replace(/\s*$/,'').replace(/\n+/,' ');
}
function getSentences(text, count) {
text = trim(text);
var matches = text.match(/\w.+?[\?\.\!]/g);
if (matches && matches.length > 0) {
if (count === 0) {
return matches.join(' ');
} else {
return matches.slice(0,count).join(' ');
}
} else {
return text;
}
}
function truncateAtWord(text) {
return text.substring(0,text.lastIndexOf(" ")).replace(/[\?\!\.,;:-]$/,'') + "…";
}
function convertToTag(input) {
if (!/^<.*>$/.test(input)) {
return "<" + input + ">";
} else {
return input;
}
}
$.fn.tldr.build = function(el, opts) {
var $this = el,
headers = [],
wrapper = $(convertToTag(opts.wrapper)),
output = $(/(ol|ul)/.test(opts.listType) ? convertToTag(opts.listType) : '<ol>'),
title = $(convertToTag(opts.headerTag)).html(opts.headerTitle),
$headlineElements = $this.find(opts.headlineElements);
if (opts.minimumHeadlines > 0 && $headlineElements.length < opts.minimumHeadlines) {
return false;
}
$headlineElements.each(function(i, n) {
var summary = "", target;
if ($(n).data(opts.dataAttribute)) {
summary = $(n).data(opts.dataAttribute);
if (opts.maxSummaryLength > 0) {
summary = summary.substring(0,opts.maxSummaryLength);
}
} else {
var grafs = $(n).nextUntil(opts.headlineElements),
totalLength = 0;
if (opts.grafsInSummary > 0) {
grafs = grafs.slice(0,opts.grafsInSummary)
}
if (grafs && grafs.length > 0) {
for ( p = 0; p < grafs.length; p++ ) {
var summaryText = getSentences($(grafs[p]).text(),opts.sentencesPerGraf),
prefix = p > 0 ? '<span class="prefix">[…]</span> ' : '';
if (opts.maxSummaryLength > 0) {
if (totalLength + summaryText.length < opts.maxSummaryLength) {
summary += '<p>' + prefix + summaryText + '</p> ';
totalLength += summaryText.length;
} else {
summaryText = truncateAtWord(summaryText.substring(0,opts.maxSummaryLength - totalLength));
if (summaryText.length > 35) {
summary += '<p>' + prefix + summaryText + '</p> ';
}
break;
}
} else {
summary += '<p>' + summaryText + '</p> ';
}
}
}
}
if ($(n).attr('id')) {
target = "#" + $(n).attr('id');
} else {
var id = $(n).text().replace(/[^a-z]/gi, '');
$(n).attr('id',id);
target = "#" + id;
}
headers.push({
target: target,
title: $(n).text(),
summary: summary
});
});
$(headers).each(function(i, n) {
var headline = $(convertToTag(opts.headlineTag)).text(n.title).addClass('tldr-headline');
if (opts.collapsed && opts.addClickHandler) {
headline.css('cursor','pointer').click(function(ev) {
if (ev.target.tagName === 'A') {
return true;
}
ev.preventDefault();
if (opts.accordion) {
$(this).closest('ul,ol').find('.summary').slideUp();
}
var nextSummary = $(this).next('.summary');
if (nextSummary.is(':visible')) {
nextSummary.slideUp();
} else {
nextSummary.slideDown();
}
});
headline.append($('<a>').attr('href',n.target).html(' →'));
var content = $('<div class=summary>').html(n.summary).css({
display: 'none'
});
output.append($('<li>').append(headline).append(content));
} else {
var content = $('<div class=summary>').html(n.summary);
output.append($('<li>').append(headline).append(content));
headline.wrap('<a href="'+n.target+'" />');
}
});
if (opts.useMetaDescription && $('meta[property$=description]').length > 0) {
var intro = truncateAtWord($('meta[property$=description]').attr('content'));
output.prepend($('<li>').html(intro));
}
return wrapper.append(title).append(output);
};
// Defaults
//
$.fn.tldr.defaults = {
headlineElements: 'h2,h3', // Elements to consider headlines
target: '', // target to prepend output to
wrapper: '<div class=tldr-wrapper>', // element to wrap the list in
listType: 'ol', // ol or ul
headlineTag: 'strong', // element to wrap the headlines in
headerTag: 'h4', // the tag for the header above the list
headerTitle: 'TL;DR', // the title of the block
dataAttribute: 'tldr', // data attribute for summaries
collapsed: false, // if true, summaries collapse and expand on headline clicks
addClickHandler: true, // add handler to open collapsed summary on click, ignored unless collapsed is true
accordion: true, // only one visible summary at a time, ignored unless collapsed is true
insertAfterLead: false, // insert the output before the first headline instead of at the top
grafsInSummary: 1, // number of paragraphs to summarize, 0 for unlimited
sentencesPerGraf: 1, // sentences per graf in summary, 0 for unlimited
maxSummaryLength: 200, // maximum characters in summary, 0 for unlimited
minimumHeadlines: 3, // minimum number of headlines in text required to execute
useMetaDescription: true // Prepend description or og:description contents
};
})(jQuery);