From 3636744d4f00a2ff27d3907d4fb5f9382ead7f13 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Tue, 25 Feb 2014 14:49:07 +0000 Subject: [PATCH] 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. --- core/modules/config.js | 1 + core/modules/utils/fakedom.js | 30 ++++++++++++++++++++++++++++++ core/modules/wiki.js | 4 ++-- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/core/modules/config.js b/core/modules/config.js index d94a03407..c82ece7a0 100644 --- a/core/modules/config.js +++ b/core/modules/config.js @@ -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(","); })(); diff --git a/core/modules/utils/fakedom.js b/core/modules/utils/fakedom.js index 18f9ffe5b..16181fee5 100755 --- a/core/modules/utils/fakedom.js +++ b/core/modules/utils/fakedom.js @@ -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; diff --git a/core/modules/wiki.js b/core/modules/wiki.js index 51486e185..7a4c15b3c 100755 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -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); }; /*