Bläddra i källkod

Handle parse pages and config JSON + Add config var "page_title" and "error"

Gabriele Lauricella 6 år sedan
förälder
incheckning
cd0df14e0a
4 ändrade filer med 47 tillägg och 12 borttagningar
  1. 5 1
      assets/template.ejs
  2. 23 8
      bin/generator.js
  3. 12 2
      config-dist.json
  4. 7 1
      lib/json-reader.js

+ 5 - 1
assets/template.ejs

@@ -3,11 +3,15 @@
 <head>
     <!-- Simple HttpErrorPages | MIT License | https://github.com/HttpErrorPages -->
     <meta charset="utf-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1" />
+<% if (locals.page_title) { %>
+    <title><%= page_title %></title>
+<% } else { %>
     <title>We've got some trouble | <%= code %> - <%= title %></title>
+<% } %>
     <style type="text/css"><%- inlinecss %></style>
 </head>
 <body>
-    <div class="cover"><h1><%= title %> <small>Error <%= code %></small></h1><p class="lead"><%= message %></p></div>
+    <div class="cover"><h1><%= title %> <small><%= error %></small></h1><p class="lead"><%= message %></p></div>
     <% if (footer){ %><footer><p><%- footer %></p></footer><% } %>
 </body>
 </html>

+ 23 - 8
bin/generator.js

@@ -38,28 +38,42 @@ async function generator(configFilename, pageDefinitionFile, distPath){
     console.log('Generating static pages');
 
     // for each errorcode generate custom page
-    await Promise.all(Object.keys(pages).map(async function(p){
+    await Promise.all(Object.keys(pages).map(async function(code){
         // page config
-        const pconf = pages[p];
+        const pconf = pages[code];
 
         // inject errorcode
-        pconf.code = p;
+        pconf.code = code;
+
+        // inject lang
+        pconf.lang = lang || 'en';
+
+        // inject page title
+        if(config && config.page_title) {
+            pconf.page_title = config.page_title.replace('%code%', code);
+            pconf.page_title = pconf.page_title.replace('%title%', pconf.title);
+        }
+
+        // inject error
+        pconf.error = 'Error '+ pconf.code 
+        if(config && config.error) {
+            pconf.error = config.error.replace('%code%', code);
+        }
 
         // inject footer
         pconf.footer = pconf.footer || config.footer;
 
-        // inject footer
-        pconf.lang = lang || 'en';
+        
 
         // render page
         const content = await _pageRenderer(tpl, css, pconf);
 
         // generate filename
-        let filename = 'HTTP' + p + '.html';
+        let filename = 'HTTP' + code + '.html';
 
         // check filename schema
-        if(config && config.scheme && config.scheme.indexOf("%d") !== -1) {
-            filename =  config.scheme.replace('%d', p);
+        if(config && config.scheme && config.scheme.indexOf("%code%") !== -1) {
+            filename =  config.scheme.replace('%code%', code);
         }
         
         // write content to file
@@ -122,6 +136,7 @@ _cli
             })
             .catch(function(e){
                 console.error(e);
+                console.log('\nStatic files not generated\n');
             });
     });
 

+ 12 - 2
config-dist.json

@@ -1,6 +1,16 @@
 {
-    //  Output Filename Scheme - eg. HTTP500.html
-    "scheme": "HTTP%d.html",
+    // Output Filename Scheme - eg. HTTP500.html
+    // %code% - HTTP error code eg. 400
+    "scheme": "HTTP%code%.html",
+
+    // Page title (HTML not allowed)
+    // %code% - HTTP error code eg. 400
+    // %title% - HTTP error eg. Bad Request
+    "page_title": "We've got some trouble | %code% - %title%",
+
+    // Error (HTML not allowed)
+    // %code% - HTTP error code eg. 400
+    "error": "Error %code%",
 
     // Footer content (HTML Allowed)
     "footer": null

+ 7 - 1
lib/json-reader.js

@@ -9,7 +9,13 @@ async function readJSONFile(filename){
     raw = raw.replace(/^\s*\/\/.*$/gm, '');
 
     // parse text
-    return JSON.parse(raw);
+    try {
+        return JSON.parse(raw);
+    } catch(e) {
+        console.log("Error parsing JSON file: "+filename+"\n");
+        throw e;
+    }
+    //return JSON.parse(raw);
 }
 
 module.exports = readJSONFile;