Text-slicer: initial support for anchors

This commit is contained in:
Jermolene 2015-09-16 18:59:03 +01:00
parent 207406eeb3
commit 7b8cb928cf
6 changed files with 44 additions and 8 deletions

View File

@ -17,13 +17,13 @@ var SLICER_OUTPUT_TITLE = "$:/TextSlicer";
function Slicer(wiki,sourceTitle) {
this.wiki = wiki;
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(" ");
this.tiddlers = {};
this.parentStack = [];
this.sliceTitle = null;
this.slicers = $tw.modules.applyMethods("slicer");
this.anchors = Object.create(null); // Hashmap of HTML anchor ID to tiddler title
}
Slicer.prototype.destroy = function() {
@ -106,6 +106,7 @@ Slicer.prototype.makeUniqueTitle = function(prefix,rawText) {
var self = this,
cleanText;
if(rawText) {
// Replace non alpha characters with spaces
cleanText = rawText.toLowerCase().replace(/[^\s\xA0]/mg,function($0,$1,$2) {
if(($0 >= "a" && $0 <= "z") || ($0 >= "0" && $0 <= "9")) {
return $0;
@ -137,13 +138,16 @@ Slicer.prototype.makeUniqueTitle = function(prefix,rawText) {
return title;
};
Slicer.prototype.registerAnchor = function(id) {
this.anchors[id] = this.currentTiddler;
}
Slicer.prototype.processNodeList = function(domNodeList) {
$tw.utils.each(domNodeList,this.processNode.bind(this));
}
Slicer.prototype.processNode = function(domNode) {
var text = $tw.utils.htmlEncode(domNode.textContent),
nodeType = domNode.nodeType;
var nodeType = domNode.nodeType;
if(nodeType === 1) { // DOM element nodes
var tagName = domNode.tagName.toLowerCase(),
hasProcessed = false;

View File

@ -0,0 +1,26 @@
/*\
title: $:/plugins/tiddlywiki/text-slicer/modules/slicers/anchor.js
type: application/javascript
module-type: slicer
Handle slicing anchor nodes
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.processAnchorNode = function(domNode,tagName) {
if(tagName === "a") {
var id = domNode.getAttribute("id");
if(id) {
this.registerAnchor(id);
}
return true;
}
return false;
};
})();

View File

@ -31,6 +31,7 @@ exports.processHeadingNode = function(domNode,tagName) {
list: [],
tags: tags
})});
this.currentTiddler = title;
return true;
}
}

View File

@ -3,7 +3,7 @@ title: $:/plugins/tiddlywiki/text-slicer/modules/slicers/item.js
type: application/javascript
module-type: slicer
Handle slicing heading nodes
Handle slicing list item nodes
\*/
(function(){
@ -30,6 +30,8 @@ exports.processListItemNode = function(domNode,tagName) {
list: [],
tags: tags
});
this.currentTiddler = title;
this.processNodeList(domNode.childNodes);
return true;
}
}

View File

@ -3,7 +3,7 @@ title: $:/plugins/tiddlywiki/text-slicer/modules/slicers/list.js
type: application/javascript
module-type: slicer
Handle slicing heading nodes
Handle slicing list nodes
\*/
(function(){
@ -30,6 +30,7 @@ exports.processListNode = function(domNode,tagName) {
list: [],
tags: tags
})});
this.currentTiddler = title;
this.processNodeList(domNode.childNodes);
this.parentStack.pop();
return true;

View File

@ -3,7 +3,7 @@ title: $:/plugins/tiddlywiki/text-slicer/modules/slicers/paragraph.js
type: application/javascript
module-type: slicer
Handle slicing heading nodes
Handle slicing paragraph nodes
\*/
(function(){
@ -17,16 +17,18 @@ exports.processParagraphNode = function(domNode,tagName) {
if(tagName === "p") {
if(!this.isBlank(text)) {
var parentTitle = this.parentStack[this.parentStack.length - 1].title,
tags = [];
tags = [],
title = this.makeUniqueTitle("paragraph",text);
if(domNode.className.trim() !== "") {
tags = tags.concat(domNode.className.split(" "));
}
this.addToList(parentTitle,this.addTiddler({
"toc-type": "paragraph",
title: this.makeUniqueTitle("paragraph",text),
title: title,
text: text,
tags: tags
}));
this.currentTiddler = title;
return true;
}
}