Number of Words Function

How could Trunk be improved?

Number of Words Function

Postby fogleman » Thu Jun 17, 2010 5:46 pm

Any chance that a function that yields the number of words on a page could be added? It would be nice if there could be an option to include the number of words on the page in the page's header. I figure that if a function existed, then I could add this feature in a custom CSS page.
fogleman
 
Posts: 55
Joined: Sun Jun 13, 2010 1:53 pm

Re: Number of Words Function

Postby mgkennard » Thu Jun 17, 2010 7:54 pm

This could be done as a JavaScript plugin. The next version, which should be on the App Store any day, allows you to get the current page text via an API call. I may use your idea as the basis for a blog article on how to write simple plugins.

Matthew
Apps On The Move
mgkennard
Site Admin
 
Posts: 763
Joined: Mon Jul 21, 2008 2:40 pm

Re: Number of Words Function

Postby fogleman » Fri Jun 18, 2010 12:37 pm

Great idea. Thanks!

- Jay F.
fogleman
 
Posts: 55
Joined: Sun Jun 13, 2010 1:53 pm

Re: Number of Words Function

Postby Alamut » Sun Sep 19, 2010 7:02 pm

This could be done as a JavaScript plugin. The next version, which should be on the App Store any day, allows you to get the current page text via an API call. I may use your idea as the basis for a blog article on how to write simple plugins.


Bump! Yes please! I'd love to hear how to write simple plugins for Trunk Notes! If I've missed a page or link somewhere please say so.
Alamut
 
Posts: 10
Joined: Tue Sep 07, 2010 8:17 am
Location: Rotterdam NL

Re: Number of Words Function

Postby Jill Arroway » Wed Oct 06, 2010 3:33 pm

Bump

Yes Please! I originally came to this thread because I wanted a word count (which I still want), but obviously I agree - a plugin architecture would be brilliant! Any update on this?
Jill Arroway
 
Posts: 8
Joined: Wed Oct 06, 2010 3:27 pm

Re: Number of Words Function

Postby mgkennard » Wed Oct 06, 2010 9:47 pm

Hello,

I got a bit distracted with some other features and haven't quite delivered the promised plugin architecture. I partly stopped because of a fairly high chance such an architecture would get rejected by Apple :?

However here is a (not great!) example of how you can word count a page using what I started implementing. This would need to be improved to be useful!

Code: Select all
<script>
function count_words (text) {

show_word_count = true;

show_char_count = false;

var char_count = text.length;
var fullStr = text + " ";
var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
var splitString = cleanedStr.split(" ");
var word_count = splitString.length -1;
if (fullStr.length <2) {
word_count = 0;
}
if (word_count == 1) {
wordOrWords = " word";
}
else {
wordOrWords = " words";
}
if (char_count == 1) {
charOrChars = " character";
} else {
charOrChars = " characters";
}

alert ("Word Count:  " + word_count + wordOrWords);

}

function count_words_on_this_page() {
    var page = trunkNotes.get('HomePage');
    count_words(page.contents);
}
</script>

<a href="javascript:count_words_on_this_page()">Word count my home page</a>


This will word count your HomePage. You can see that there is a function trunkNotes.get which will retrieve a page for you. The object returned has two attributes - contents and title. Note that contents is the raw Markdown, so the word count above will not be accurate on pages which make use of functions.

(The actual word count code I borrowed from http://javascript.internet.com/forms/word-count.html)

I look forward to seeing how someone can turn this into something useful!

Matthew
Apps On The Move
mgkennard
Site Admin
 
Posts: 763
Joined: Mon Jul 21, 2008 2:40 pm

Re: Number of Words Function

Postby Jill Arroway » Thu Oct 07, 2010 6:19 pm

The function trunkNotes.get('HomePage') returns an empty object for me. Is there something I'm not doing?

You might be right that Apple won't allow plugins. But they do allow bookmarklets. The Atomic browser has them, for instance. Bookmarklets would do the job.

I think the Javascript would be more useful in a bookmarklet than embedded in a <script> tag, because you wouldn't want the script executing when you viewed the wiki via the web interface from a computer, nor would you want the link that launched it. With a bookmarklet the same script could run on any page.
Jill Arroway
 
Posts: 8
Joined: Wed Oct 06, 2010 3:27 pm

Re: Number of Words Function

Postby Jill Arroway » Fri Oct 08, 2010 3:30 pm

I have succeded in making a wordcount function. Just create a page with the following content, and then include it in every other page using the {{include}} function...

<script>
function wordcount()
{
    // get page content
    var s = document.getElementsByTagName("body")[0].innerHTML;

    // squish whitespace
    s = s.replace(/\s+/g, " ");
 
    // remove scripts
    s = s.replace(new RegExp("<s" + "cript.*?<" + "/script>", "g"), "");

    // remove styles
    s = s.replace(new RegExp("<s" + "tyle.*?<" + "/style>", "g"), "");

    // remove all remaining tags
    s = s.replace(/<[^>]*>/g, "");

    // squish whitespace (again)
    s = (" " + s + " ").replace(/ +/g, " ");
 
    alert((s.split(" ").length - 10) + " words");
}

</script>
<a href="javascript:wordcount()">Wordcount</a>

***
Jill Arroway
 
Posts: 8
Joined: Wed Oct 06, 2010 3:27 pm


Return to Suggestions

Who is online

Users browsing this forum: No registered users and 1 guest

cron