From 75645602dd70f079ccf550bb5e90dffc21693429 Mon Sep 17 00:00:00 2001 From: Nick White Date: Sat, 23 Oct 2010 23:53:06 +0100 Subject: Update to run in a function, lots of doc additions - Updated readable to run from one function, so returns work nicely now, and things are generally cleaner. - Added a readme - Added a doap rdf file - Added webpage generation to the makefile --- readable.js | 110 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 56 insertions(+), 54 deletions(-) (limited to 'readable.js') diff --git a/readable.js b/readable.js index 3d31c4f..1446240 100755 --- a/readable.js +++ b/readable.js @@ -5,63 +5,65 @@ * */ -/* NOTE: if you're using this code in another function, move this - * line out of it, into the global scope */ if(original === undefined) var original = false; -/* count the number of

tags that are direct children of parenttag */ -function count_p(parenttag) +function readable() { - var n = 0; - var c = parenttag.childNodes; - for (var i = 0; i < c.length; i++) { - if (c[i].tagName == "p" || c[i].tagName == "P") - n++; + /* count the number of

tags that are direct children of parenttag */ + function count_p(parenttag) + { + var n = 0; + var c = parenttag.childNodes; + for (var i = 0; i < c.length; i++) { + if (c[i].tagName == "p" || c[i].tagName == "P") + n++; + } + return n; } - return n; -} - -/* if original is set, then the readable version is currently active, - * so switch to the original html */ -if (original) { - document.body.innerHTML = original; - for (var i = 0; i < document.styleSheets.length; i++) - document.styleSheets[i].disabled = false; - original = false - return 0; -} - -original = document.body.innerHTML; - -var biggest_num = 0; -var biggest_tag; - -/* search for tag with most direct children

tags */ -var t = document.getElementsByTagName("*"); -for (var i = 0; i < t.length; i++) { - var p_num = count_p(t[i]); - if (p_num > biggest_num) { - biggest_num = p_num; - biggest_tag = t[i]; + + /* if original is set, then the readable version is currently active, + * so switch to the original html */ + if (original) { + document.body.innerHTML = original; + for (var i = 0; i < document.styleSheets.length; i++) + document.styleSheets[i].disabled = false; + original = false + return 0; } + + original = document.body.innerHTML; + + var biggest_num = 0; + var biggest_tag; + + /* search for tag with most direct children

tags */ + var t = document.getElementsByTagName("*"); + for (var i = 0; i < t.length; i++) { + var p_num = count_p(t[i]); + if (p_num > biggest_num) { + biggest_num = p_num; + biggest_tag = t[i]; + } + } + + if (biggest_num == 0) { + alert("Can't find any content"); + return 1; + } + + /* save and sanitise content of chosen tag */ + var fresh = document.createElement("div"); + fresh.innerHTML = biggest_tag.innerHTML; + fresh.innerHTML = fresh.innerHTML.replace(/<\/?font[^>]*>/g, ""); + fresh.innerHTML = fresh.innerHTML.replace(/style="[^"]*"/g, ""); + fresh.innerHTML = fresh.innerHTML.replace(/<\/?a[^>]*>/g, ""); + + for (var i = 0; i < document.styleSheets.length; i++) + document.styleSheets[i].disabled = true; + + document.body.innerHTML = + "

" + + "

" + document.title + "

" + + fresh.innerHTML + "
"; } - -if (biggest_num == 0) { - alert("Can't find any content"); - return 1; -} - -/* save and sanitise content of chosen tag */ -var fresh = document.createElement("div"); -fresh.innerHTML = biggest_tag.innerHTML; -fresh.innerHTML = fresh.innerHTML.replace(/<\/?font[^>]*>/g, ""); -fresh.innerHTML = fresh.innerHTML.replace(/style="[^"]*"/g, ""); -fresh.innerHTML = fresh.innerHTML.replace(/<\/?a[^>]*>/g, ""); - -for (var i = 0; i < document.styleSheets.length; i++) - document.styleSheets[i].disabled = true; - -document.body.innerHTML = - "
" + - "

" + document.title + "

" + - fresh.innerHTML + "
"; +readable() -- cgit v1.2.3