1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-08-08 06:43:49 +00:00

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

View File

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

View File

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

View File

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