1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-16 10:29:54 +00:00

Text-slicer: improved generation of paragraph names

This commit is contained in:
Jermolene 2015-08-10 20:32:30 +01:00
parent 2eb645e5e5
commit abcb2d3a1c

View File

@ -37,6 +37,7 @@ function Slicer(wiki,sourceTitle) {
this.sourceTitle = sourceTitle;
this.currentId = 0;
this.iframe = null; // Reference to iframe used for HTML parsing
this.stopWordList = "the and a of on i".split(" ");
}
Slicer.prototype.destroy = function() {
@ -75,6 +76,31 @@ Slicer.prototype.getSourceDocument = function() {
}
};
Slicer.prototype.makeParagraphTitle = function(title,text) {
// Remove characters other than lowercase alphanumeric and spaces
var self = this,
cleanText = text.toLowerCase().replace(/[^\s\xA0]/mg,function($0,$1,$2) {
if(($0 >= "a" && $0 <= "z") || ($0 >= "0" && $0 <= "9")) {
return $0;
} else {
return " ";
}
});
// Split on word boundaries
var words = cleanText.split(/[\s\xA0]+/mg);
// Remove common words
words = words.filter(function(word) {
return word && (self.stopWordList.indexOf(word) === -1);
});
// Accumulate the number of words that will fit
var c = 0,
s = "";
while(c < words.length && (s.length + words[c].length + 1) < 20) {
s += "-" + words[c++];
}
return this.wiki.generateNewTitle("para" + s);
};
// Slice a tiddler into individual tiddlers
Slicer.prototype.sliceTiddler = function(title) {
var self = this,
@ -169,7 +195,7 @@ Slicer.prototype.sliceTiddler = function(title) {
if(!isBlank(text)) {
parentTitle = parentStack[parentStack.length - 1].title;
addToList(parentTitle,addTiddler({
title: title + "-para-" + self.nextId(),
title: self.makeParagraphTitle(title,text),
text: text,
tags: [parentTitle]
}));