Просмотр исходного кода

Merge pull request #455 from weseek/imprv/auto-template-placeholder

Imprv/auto template placeholder
Yuki Takei 7 лет назад
Родитель
Сommit
68a11e1ca7
2 измененных файлов с 38 добавлено и 5 удалено
  1. 26 5
      lib/routes/page.js
  2. 12 0
      lib/util/getToday.js

+ 26 - 5
lib/routes/page.js

@@ -13,6 +13,8 @@ module.exports = function(crowi, app) {
     , ApiResponse = require('../util/apiResponse')
     , interceptorManager = crowi.getInterceptorManager()
     , pagePathUtil = require('../util/pagePathUtil')
+    , swig = require('swig-templates')
+    , getToday = require('../util/getToday')
 
     , actions = {};
 
@@ -257,7 +259,6 @@ module.exports = function(crowi, app) {
     var pageTeamplate = 'customlayout-selector/page';
 
     var isRedirect = false;
-    var originalPath = path;
     Page.findPage(path, req.user, req.query.revision)
     .then(function(page) {
       debug('Page found', page._id, page.path);
@@ -281,7 +282,7 @@ module.exports = function(crowi, app) {
           renderVars.tree = tree;
         })
         .then(function() {
-          return Page.checkIfTemplatesExist(originalPath);
+          return Page.checkIfTemplatesExist(path);
         })
         .then(() => {
           return PageGroupRelation.findByPage(renderVars.page);
@@ -327,8 +328,12 @@ module.exports = function(crowi, app) {
     .catch(function(err) {
       pageTeamplate = 'customlayout-selector/not_found';
 
-      return Page.findTemplate(originalPath)
+      return Page.findTemplate(path)
         .then(template => {
+          if (template) {
+            template = replacePlaceholders(template, req);
+          }
+
           renderVars.template = template;
         });
     })
@@ -365,6 +370,17 @@ module.exports = function(crowi, app) {
     });
   };
 
+  const replacePlaceholders = (template, req) => {
+    const definitions = {
+      pagepath: getPathFromRequest(req),
+      username: req.user.name,
+      today: getToday(),
+    };
+    const compiledTemplate = swig.compile(template);
+
+    return compiledTemplate(definitions);
+  };
+
   actions.deletedPageListShow = function(req, res) {
     var path = '/trash' + getPathFromRequest(req);
     var limit = 50;
@@ -437,8 +453,13 @@ module.exports = function(crowi, app) {
   function renderPage(pageData, req, res) {
     // create page
     if (!pageData) {
-      return Page.findTemplate(getPathFromRequest(req))
-        .then((template) => {
+      const path = getPathFromRequest(req);
+      return Page.findTemplate(path)
+        .then(template => {
+          if (template) {
+            template = replacePlaceholders(template, req);
+          }
+
           return res.render('customlayout-selector/not_found', {
             author: {},
             page: false,

+ 12 - 0
lib/util/getToday.js

@@ -0,0 +1,12 @@
+/**
+ * getToday
+ */
+
+module.exports = function() {
+  const today = new Date();
+  const month = ('0' + (today.getMonth() + 1)).slice(-2);
+  const day = ('0' + today.getDate()).slice(-2);
+  const dateString = today.getFullYear() + '/' + month + '/' + day;
+
+  return dateString;
+};