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 --- BUGS | 2 -- INSTALL | 26 +++++++++----- Makefile | 11 ++++++ README | 23 ++++++++++++ TODO | 4 +-- doap.ttl | 25 +++++++++++++ readable.js | 110 +++++++++++++++++++++++++++++---------------------------- summary.sh | 41 +++++++++++++++++++++ webheader.html | 15 ++++++++ 9 files changed, 189 insertions(+), 68 deletions(-) delete mode 100644 BUGS create mode 100644 Makefile create mode 100644 README create mode 100644 doap.ttl create mode 100644 summary.sh create mode 100644 webheader.html diff --git a/BUGS b/BUGS deleted file mode 100644 index 9a44697..0000000 --- a/BUGS +++ /dev/null @@ -1,2 +0,0 @@ -At the moment we're returning without being in a function, which is wrong. Just want to stop execution of the script... - consider wrapping in a function, and calling that from the bookmarklet (will this work with uzbl? - maybe we can do some sort of __main__() thing?) diff --git a/INSTALL b/INSTALL index 46fab4e..5f649dc 100644 --- a/INSTALL +++ b/INSTALL @@ -1,24 +1,29 @@ +Readable is written in ECMAScript, and how to launch it varies a +little depending on browser. + # Uzbl -To use this from uzbl, add the following line to the config file (replacing -'s' with the key to bind to): +To use this from uzbl, add the following line to the config file +(replacing 's' with the key to bind to): @cbind s = script file:///path/to/readable.js # Surf -To use it from surf, add the following to script.js: +To use it from surf, add the following to script.js (replacing the +keyIdentifier if you want to bind to a key other than Ctrl-S): (function() { document.addEventListener('keydown', keybind, false); - })(); +})(); - if(original === undefined) var original = false; - function keybind(e) { - if (!(e.keyIdentifier == "U+0053" && e.ctrlKey)) return 0; // ^s -/* copy readable.js here, removing the line starting 'if(original' */ +function keybind(e) { + if (e.keyIdentifier == "U+0053" && e.ctrlKey) // ^s + readable(); } +/* copy readable.js here, removing the final line 'readable()' */ + # Other browsers; 'bookmarklet' @@ -26,4 +31,7 @@ To use this as a 'bookmarklet', create a new bookmark with an address of: javascript:(function(){document.body.appendChild(document.createElement('script')).src='file:///path/to/readable.js';})(); -Note that due to browsers being afraid of mixing local and remote files this may not work for http:// addresses. If this is the case, you can either run the script from a remote location (eg your webserver), or integrate it into a browser-specific extension. +Note that due to browsers being afraid of mixing local and remote files +this may not work for http:// addresses. If this is the case, you can +either run the script from a remote location (eg your webserver), or +integrate it into a browser-specific extension. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ac0a4a0 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +index.html: doap.ttl README + @echo making webpage + @cat < webheader.html > $@ + @smu < README >> $@ + @echo '

Download Readable
' >> $@ + @echo 'GPG signature

' >> $@ + @echo '
' >> $@ + @sh summary.sh doap.ttl | smu >> $@ + @echo '' >> $@ + +.SUFFIXES: ttl html diff --git a/README b/README new file mode 100644 index 0000000..16c6662 --- /dev/null +++ b/README @@ -0,0 +1,23 @@ +Readable - Makes webpages more readable +======================================= + +Readable is a simple program to remove unwanted distractions from +webpages, in order to better enjoy reading on the web. It removes +everything but the article from the webpage, displaying it +simply and attractively. + +Running the program multiple times switches between showing the +original page and the 'readable' version. + +Read the file INSTALL for information on installing it into your +browser. + +This project is technically inspired by Arc90's Readability, +but aims to be much simpler, faster, and more usable. More +generally, it is inspired by Sean Birketts' *The Gutenberg +Elegies* and Nicholas Carr's *The Shallows*. + +"The ever-present awareness of possibility to either make or +refuse choice, was to preempt my creating any meditative space +for myself" + - *Sean Birketts, The Gutenberg Elegies* diff --git a/TODO b/TODO index 12fd7a8..2df90e5 100644 --- a/TODO +++ b/TODO @@ -1,4 +1,2 @@ -create makefile for script.js, and web -create readme & ttl, to combine for web -define width at top +create makefile rule for packaging releases it'd be nice to be able to run tests diff --git a/doap.ttl b/doap.ttl new file mode 100644 index 0000000..09c1fa8 --- /dev/null +++ b/doap.ttl @@ -0,0 +1,25 @@ +@prefix rdf: . +@prefix doap: . +@prefix foaf: . +@prefix : . + +:p a doap:Project; + doap:name "Readable"; + doap:shortdesc "Makes webpages more readable"; + doap:homepage ; + doap:repository :repo; + doap:programming-language "ECMAScript"; + doap:license ; + doap:release :v0.1; + doap:maintainer . + + a foaf:Person; + foaf:name "Nick White"; + foaf:homepage . + +:repo a doap:GitRepository; + doap:location . + +:v0.1 a doap:Version; + doap:revision "0.1"; + doap:file-release . 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() diff --git a/summary.sh b/summary.sh new file mode 100644 index 0000000..453b25f --- /dev/null +++ b/summary.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +test $# -ne 1 && echo "usage: $0 doap" && exit 1 + +rdf="$1" + +q="PREFIX doap: +PREFIX foaf: +SELECT ?home ?repo ?license ?maintainer ?maintainerhome ?lang +WHERE { +?p a doap:Project; + doap:homepage ?home; + doap:repository ?r; + doap:license ?license; + doap:programming-language ?lang; + doap:maintainer ?m. +?r doap:location ?repo. +?m foaf:name ?maintainer; + foaf:homepage ?maintainerhome. +}" + +roqet -q -r csv -e "$q" -D /dev/stdin < $rdf | sed '/^Result/d' \ +| while read r; do + home=`echo $r | awk -F , '{print $2}'| sed -e 's/uri(\(.*\))/\1/'` + repo=`echo $r | awk -F , '{print $3}'| sed -e 's/uri(\(.*\))/\1/'` + licenseuri=`echo $r | awk -F , '{print $4}'| sed -e 's/uri(\(.*\))/\1/'` + maint=`echo $r | awk -F , '{print $5}'| sed -e 's/"\(.*\)"/\1/'` + mainthome=`echo $r | awk -F , '{print $6}'| sed -e 's/uri(\(.*\))/\1/'` + lang=`echo $r | awk -F , '{print $7}'| sed -e 's/"\(.*\)"/\1/'` + test "$licenseuri" = "http://www.gnu.org/licenses/gpl.html" && license="GPL" + test "$licenseuri" = "http://www.gnu.org/licenses/agpl.html" && license="AGPL" + test "$licenseuri" = "http://creativecommons.org/licenses/MIT/" && license="MIT" + + cat <<- _EOF_ +- Project homepage: [$home]($home) +- Code repository: [$repo]($repo) +- Maintainer: [$maint]($mainthome) +- Language: $lang +- License: [$license]($licenseuri) +_EOF_ +done diff --git a/webheader.html b/webheader.html new file mode 100644 index 0000000..1c615af --- /dev/null +++ b/webheader.html @@ -0,0 +1,15 @@ + + + +Readable - Makes webpages more readable + -- cgit v1.2.3