You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2019/07/26 09:27:55 UTC

[sling-whiteboard] 02/02: Factor our the default renderers

This is an automated email from the ASF dual-hosted git repository.

bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit d6a6ccd11367ef6102d003c088badac8821f6f14
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Fri Jul 26 11:27:38 2019 +0200

    Factor our the default renderers
---
 serverless-microsling/lib/default-renderers.js | 61 ++++++++++++++++++++++++++
 serverless-microsling/lib/render.js            | 55 +++--------------------
 2 files changed, 68 insertions(+), 48 deletions(-)

diff --git a/serverless-microsling/lib/default-renderers.js b/serverless-microsling/lib/default-renderers.js
new file mode 100644
index 0000000..dca7c27
--- /dev/null
+++ b/serverless-microsling/lib/default-renderers.js
@@ -0,0 +1,61 @@
+/* 
+ * Copyright 2019 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* eslint-disable no-console */
+'use strict';
+
+module.exports.renderers = {
+   text: {
+    contentType: 'text/plain',
+    getRendererInfo : (resourceType, extension) => {
+      return extension == 'txt';
+    },
+    render : (resource) => {
+      return { output: `${resource.title}\n${resource.body}\n` };
+    },
+  },
+  json: {
+    contentType: 'application/json',
+    getRendererInfo : (resourceType, extension) => {
+      return extension == 'json';
+    },
+    render : (resource) => {
+      return { output: JSON.stringify(resource, 2, null) };
+    },
+  },
+  html: {
+    contentType: 'text/html',
+    getRendererInfo : (resourceType, extension) => {
+      return extension == 'html';
+    },
+    render : (resource) => {
+      return { output: `
+      <html>
+      <head>
+      <title>${resource.title}</title>
+      </head>
+      <body>
+      <h1>
+          ${resource.title}
+      </h1>
+      <div>${resource.body}</div>
+      <div style="color:blue">This is the default HTML rendering</div>
+      </body>
+      </html>
+    `};
+    },
+  },
+};
\ No newline at end of file
diff --git a/serverless-microsling/lib/render.js b/serverless-microsling/lib/render.js
index 73df80b..5ceaa25 100644
--- a/serverless-microsling/lib/render.js
+++ b/serverless-microsling/lib/render.js
@@ -14,59 +14,18 @@
  * limitations under the License.
  */
 
-'use strict';
-
- const { openWhiskRenderer } = require('./openwhisk-renderer');
-
  /* eslint-disable no-console */
+ 'use strict';
 
-const defaultTextRenderer = {
-  contentType: 'text/plain',
-  getRendererInfo : (resourceType, extension) => {
-    return extension == 'txt';
-  },
-  render : (resource) => {
-    return { output: `${resource.title}\n${resource.body}\n` };
-  },
-}
-
-const defaultJsonRenderer = {
-  contentType: 'application/json',
-  getRendererInfo : (resourceType, extension) => {
-    return extension == 'json';
-  },
-  render : (resource) => {
-    return { output: JSON.stringify(resource, 2, null) };
-  },
-}
-
-const defaultHtmlRenderer = {
-  contentType: 'text/html',
-  getRendererInfo : (resourceType, extension) => {
-    return extension == 'html';
-  },
-  render : (resource) => {
-    return { output: `
-    <html>
-    <head>
-    <title>${resource.title}</title>
-    </head>
-    <body>
-    <h1>
-        ${resource.title}
-    </h1>
-    <div>${resource.body}</div>
-    </body>
-    </html>
-  `};
-  },
-}
+const { openWhiskRenderer } = require('./openwhisk-renderer');
+const defaultRenderers = require('./default-renderers');
 
+// Ordered list of renderers, first ones get preference
 const renderers = [
   openWhiskRenderer,
-  defaultTextRenderer,
-  defaultHtmlRenderer,
-  defaultJsonRenderer
+  defaultRenderers.renderers.json,
+  defaultRenderers.renderers.html,
+  defaultRenderers.renderers.text,
 ];
 
 async function selectRendererInfo(resourceType, extension) {