1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +00:00

Add support for retrieving formatted text content from the fake dom

The idea is to convert HTML block elements into newlines, so that the
output is readable on a terminal.
This commit is contained in:
Jermolene 2014-02-25 14:49:07 +00:00
parent 8ce7c3920c
commit 3636744d4f
3 changed files with 33 additions and 2 deletions

View File

@ -35,5 +35,6 @@ exports.htmlEntities = {quot:34, amp:38, apos:39, lt:60, gt:62, nbsp:160, iexcl:
exports.htmlVoidElements = "area,base,br,col,command,embed,hr,img,input,keygen,link,meta,param,source,track,wbr".split(",");
exports.htmlBlockElements = "address,article,aside,audio,blockquote,canvas,dd,div,dl,fieldset,figcaption,figure,footer,form,h1,h2,h3,h4,h5,h6,header,hgroup,hr,li,noscript,ol,output,p,pre,section,table,tfoot,ul,video".split(",");
})();

View File

@ -26,6 +26,12 @@ var TW_TextNode = function(text) {
this.textContent = text;
};
Object.defineProperty(TW_TextNode.prototype, "formattedTextContent", {
get: function() {
return this.textContent.replace(/(\r?\n)/g,"");
}
});
var TW_Element = function(tag,namespace) {
bumpSequenceNumber(this);
this.isTiddlyWikiFakeDom = true;
@ -176,6 +182,30 @@ Object.defineProperty(TW_Element.prototype, "textContent", {
}
});
Object.defineProperty(TW_Element.prototype, "formattedTextContent", {
get: function() {
if(this.isRaw) {
throw "Cannot get formattedTextContent on a raw TW_Element";
} else {
var b = [],
isBlock = $tw.config.htmlBlockElements.indexOf(this.tag) !== -1;
if(isBlock) {
b.push("\n");
}
if(this.tag === "li") {
b.push("* ")
}
$tw.utils.each(this.children,function(node) {
b.push(node.formattedTextContent);
});
if(isBlock) {
b.push("\n");
}
return b.join("");
}
}
});
var document = {
setSequenceNumber: function(value) {
sequenceNumber = value;

View File

@ -892,11 +892,11 @@ parentWidget: optional parent widget for the root node
*/
exports.renderTiddler = function(outputType,title,options) {
options = options || {};
var parser = this.parseTiddler(title),
var parser = this.parseTiddler(title,options),
widgetNode = this.makeWidget(parser,options);
var container = $tw.fakeDocument.createElement("div");
widgetNode.render(container,null);
return outputType === "text/html" ? container.innerHTML : container.textContent;
return outputType === "text/html" ? container.innerHTML : (outputType === "text/plain-formatted" ? container.formattedTextContent : container.textContent);
};
/*