From 372063295f54990c0bfcb9a29483e92961ae274b Mon Sep 17 00:00:00 2001 From: John McLear Date: Sat, 24 Jan 2015 04:09:13 +0000 Subject: [PATCH 1/5] beginning of a hook, needs docs etc --- src/node/utils/ExportHtml.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/node/utils/ExportHtml.js b/src/node/utils/ExportHtml.js index 0bb93326b..81fc0be9d 100644 --- a/src/node/utils/ExportHtml.js +++ b/src/node/utils/ExportHtml.js @@ -411,6 +411,18 @@ function getHTMLFromAtext(pad, atext, authorColors) } lists = [] + hooks.aCallAll("asyncLineHTMLForExport", { + line: line, + lineContent: lineContent, + apool: apool, + attribLine: attribLines[i], + text: textLines[i] + }, function(err, newLineContent){ + if(newLineContent.length !== 0) lineContent = newLineContent[0]; + // modified lineContent here + }); + + // Old hook probably not to be used.. var lineContentFromHook = hooks.callAllStr("getLineHTMLForExport", { line: line, @@ -419,6 +431,7 @@ function getHTMLFromAtext(pad, atext, authorColors) attribLine: attribLines[i], text: textLines[i] }, " ", " ", ""); + if (lineContentFromHook) { pieces.push(lineContentFromHook, ''); From 9abb85799c70fb66efbf0828de50869d625a1225 Mon Sep 17 00:00:00 2001 From: John McLear Date: Sat, 24 Jan 2015 04:31:50 +0000 Subject: [PATCH 2/5] realization how aCallAll works --- src/node/utils/ExportHtml.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/node/utils/ExportHtml.js b/src/node/utils/ExportHtml.js index 81fc0be9d..50ff11add 100644 --- a/src/node/utils/ExportHtml.js +++ b/src/node/utils/ExportHtml.js @@ -418,7 +418,12 @@ function getHTMLFromAtext(pad, atext, authorColors) attribLine: attribLines[i], text: textLines[i] }, function(err, newLineContent){ - if(newLineContent.length !== 0) lineContent = newLineContent[0]; +//new Line Content is an array of each of the responses from aCallAll.. We should return a function to it +console.error(newLineContent); + if(newLineContent.length !== 0){ +console.error("lC", newLineContent[0]); + lineContent = newLineContent[0]; + } // modified lineContent here }); From 1890ba39766ab6146683a43c05ae64077272e3d0 Mon Sep 17 00:00:00 2001 From: John McLear Date: Sat, 24 Jan 2015 05:14:38 +0000 Subject: [PATCH 3/5] working, might need polish its pretty late --- src/node/utils/ExportHtml.js | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/node/utils/ExportHtml.js b/src/node/utils/ExportHtml.js index 50ff11add..10c2fe5ff 100644 --- a/src/node/utils/ExportHtml.js +++ b/src/node/utils/ExportHtml.js @@ -411,31 +411,26 @@ function getHTMLFromAtext(pad, atext, authorColors) } lists = [] - hooks.aCallAll("asyncLineHTMLForExport", { + var context = { line: line, lineContent: lineContent, apool: apool, attribLine: attribLines[i], text: textLines[i] - }, function(err, newLineContent){ -//new Line Content is an array of each of the responses from aCallAll.. We should return a function to it -console.error(newLineContent); - if(newLineContent.length !== 0){ -console.error("lC", newLineContent[0]); - lineContent = newLineContent[0]; - } - // modified lineContent here + } + + // first context below seems superfluos + hooks.aCallAll("asyncLineHTMLForExport", context, function(err, newLineFunction){ + newLineFunction.forEach(function(fn){ + context.lineContent = fn(context); // note the fn + }); + //new Line Content is an array of each of the responses from aCallAll.. + // We should return a function to it + lineContent = context.lineContent; // modified lineContent here }); // Old hook probably not to be used.. - var lineContentFromHook = hooks.callAllStr("getLineHTMLForExport", - { - line: line, - lineContent: lineContent, - apool: apool, - attribLine: attribLines[i], - text: textLines[i] - }, " ", " ", ""); + var lineContentFromHook = hooks.callAllStr("getLineHTMLForExport", context, " ", " ", ""); if (lineContentFromHook) { From 378ed022698f044c662f6e1310d161d52c3d7df9 Mon Sep 17 00:00:00 2001 From: John McLear Date: Sat, 24 Jan 2015 13:30:03 +0000 Subject: [PATCH 4/5] docs --- doc/api/hooks_server-side.md | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/doc/api/hooks_server-side.md b/doc/api/hooks_server-side.md index c7e7a43af..75c3902bb 100644 --- a/doc/api/hooks_server-side.md +++ b/doc/api/hooks_server-side.md @@ -245,7 +245,7 @@ Things in context: 2. attribLine - line attributes 3. text - line text -This hook will allow a plug-in developer to re-write each line when exporting to HTML. +This hook will allow a plug-in developer to re-write each line when exporting to HTML. Note that you problably don't want to use this plugin and will probably get better results from `asyncLineHTMLForExport` Example: ``` @@ -271,6 +271,39 @@ function _analyzeLine(alineAttrs, apool) { } ``` +## asyncLineHTMLForExport +Called from: src/node/utils/ExportHtml.js + +Things in context: + +1. The context of the line +2. lineContents - The HTML of the line +3. Attribute pool +4. Attribute line +5. Line Text + +This hook will allow functions to be returned to modify the HTML. + +Example: + +``` +exports.asyncLineHTMLForExport = function (hook, context, cb) { + cb(rewriteLine); +} + +function rewriteLine(context){ + var lineContent = context.lineContent; + sizes.forEach(function(size){ + size = size.replace("fs",""); + if(lineContent){ + lineContent = lineContent.replace(" Date: Sat, 24 Jan 2015 13:39:45 +0000 Subject: [PATCH 5/5] boop --- src/node/utils/ExportHtml.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/node/utils/ExportHtml.js b/src/node/utils/ExportHtml.js index 10c2fe5ff..962f69939 100644 --- a/src/node/utils/ExportHtml.js +++ b/src/node/utils/ExportHtml.js @@ -419,13 +419,14 @@ function getHTMLFromAtext(pad, atext, authorColors) text: textLines[i] } - // first context below seems superfluos + // See https://github.com/ether/etherpad-lite/issues/2486 hooks.aCallAll("asyncLineHTMLForExport", context, function(err, newLineFunction){ + // For each function returned by the hook call + // Process the text based on the function newLineFunction.forEach(function(fn){ context.lineContent = fn(context); // note the fn }); - //new Line Content is an array of each of the responses from aCallAll.. - // We should return a function to it + // We now have a line that has been processed by each hook function lineContent = context.lineContent; // modified lineContent here });