diff --git a/CHANGES.rst b/CHANGES.rst index 6559e53..3ed60fd 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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] diff --git a/README.rst b/README.rst index 87b6edb..9209a5e 100644 --- a/README.rst +++ b/README.rst @@ -94,3 +94,15 @@ 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 +---------------- + +This package looks for every text into the ``#content`` div, process it and send to `ResponsiveVoice `_ to play the text. + +The text processing is done in this way: + +1. Looks text line by line. +2. Remove extra space. +3. Ignore invisible elements and empty lines. +4. Add ending period if it is missing. diff --git a/src/collective/texttospeech/static/main.js b/src/collective/texttospeech/static/main.js index 5a7f708..6f71981 100644 --- a/src/collective/texttospeech/static/main.js +++ b/src/collective/texttospeech/static/main.js @@ -23,6 +23,28 @@ var MainView = (function() { this.$button.html(this.label_stopped); this.$button.attr('class', 'stopped'); }; + MainView.prototype.extract_text = function() { + var i, len, ref, results, $el, text; + // create an array with the text extracted + results = []; + ref = $('#content > *'); + for (i = 0, len = ref.length; i < len; i++) { + $el = $(ref[i]); + // 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 + return results.join(' '); + }; MainView.prototype.play_pause = function(e) { e.preventDefault(); if (this.playing) { @@ -39,8 +61,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)