Skip to content

Commit

Permalink
Merge 749c3f2 into 0513414
Browse files Browse the repository at this point in the history
  • Loading branch information
rodfersou committed Jul 7, 2016
2 parents 0513414 + 749c3f2 commit 0718a7c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
1.0b2 (unreleased)
------------------

- Review text extraction.
[rodfersou]

- To avoid displaying the 'Listen' button with an incorrect voice,
the feature is now globally disabled by default at installation time.
[hvelarde]
Expand Down
15 changes: 15 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,18 @@ A viewlet with a 'Listen' button will be displayed on objects with the feature e
The Text-To-Speech feature enabled.

You can pause/resume the reader at any time by selecting 'Pause'/'Resume'.

How does it work
----------------

Text to speech extract the text of the page, and send the text to `ResponsiveVoice <http://responsivevoice.org/>`_ to play the text.

By default, the text extraction need to ignore some elements that should not be played:

* Blockquotes
* IFrames
* Image captions

And there are other custom elements that should be ignored, to manage these custom elements we use a blacklist.

The blacklist is filled into the package configlet and should be filled with one CSS selector by line that should be ignored.
49 changes: 47 additions & 2 deletions src/collective/texttospeech/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,52 @@ var MainView = (function() {
this.$button.html(this.label_stopped);
this.$button.attr('class', 'stopped');
};
MainView.prototype.extract_text = function() {
var i, j, len, len2, ref, results, $el, text, selector, ignore;
// blacklist example
var blacklist = ['.slideshow-link'];
// create an array with the text extracted
results = [];
// http://stackoverflow.com/questions/4602431/what-is-the-most-efficient-way-to-get-leaf-nodes-with-jquery
ref = $('#content *:not(:has(*))');
for (i = 0, len = ref.length; i < len; i++) {
$el = $(ref[i]);
// ignore blacklist
ignore = false;
for (j = 0, len2 = blacklist.length; j < len2; j++) {
selector = blacklist[j];
if ($el.is(selector) || $el.parents(selector).length > 0) {
ignore = true;
break;
}
}
if (ignore) {
continue;
}
// ignore blockquotes
if ($el.is('blockquote') || $el.parents('blockquote').length > 0) {
continue;
}
// ignore image captions
if ($el.hasClass('image-caption')) {
continue;
}
// Remove extra spaces
text = $el.text().replace(/\s+/g, ' ').trim();
// ignore invisible elements or empty lines
if (!$el.is(':visible') || text.length === 0) {
continue;
}
// ensure there is a pause after every line adding a period
if (!/[.,;:!?—]$/.test(text)) {
text += '.';
}
results.push(text);
}
// join array with texts
console.log(results);
return results.join(' ');
};
MainView.prototype.play_pause = function(e) {
e.preventDefault();
if (this.playing) {
Expand All @@ -39,8 +85,7 @@ var MainView = (function() {
}
} else {
responsiveVoice.speak(
// remove spaces to avoid issues with some Firefox versions
$('#content').text().replace(/\s+/g, ' ').trim(),
this.extract_text(),
this.voice, {
onstart: $.proxy(this.onstart, this),
onend: $.proxy(this.onend, this)
Expand Down

0 comments on commit 0718a7c

Please sign in to comment.