ang-st authored on 06/06/2015 02:12:32
Showing 7 changed files
... ...
@@ -76,8 +76,8 @@ App.configure('production', function(){
76 76
 App.get('/', Routes.index);
77 77
 App.get('/signin', Routes.signIn);
78 78
 App.post('/signin', Routes.signInPost);
79
-//App.get('/signup', Routes.signUp);
80
-//App.post('/signup', Routes.signUpPost);
79
+App.get('/signup', Routes.signUp);
80
+App.post('/signup', Routes.signUpPost);
81 81
 App.get('/signout', Routes.signout)
82 82
 //App.get('/:name', Routes.view);
83 83
 App.get('/:name/edit', Routes.edit);
... ...
@@ -7,6 +7,7 @@ var Bookshelf = require('bookshelf');
7 7
 var async = require('async');
8 8
 var textile = require('textile-js')
9 9
 
10
+var nodegit = require("nodegit")
10 11
 
11 12
 var Markdown =  textile //Markdown = require('js-markdown-extra').Markdown
12 13
 
... ...
@@ -14,6 +15,7 @@ function orderize(files){
14 14
    var pageObjs = []
15 15
    for (var i = 0; i<files.length; i++){
16 16
      var file = files[i].name
17
+     console.log(file)
17 18
      var order = parseInt(file.split("_")[0])
18 19
      if(!isNaN(order)) {
19 20
       var divId = file.split("_")[1].split(".markdown")[0]
... ...
@@ -53,6 +55,7 @@ function fullPath(files) {
53 53
 // Load a file, parse the title and generate the HTML
54 54
 exports.loadPage = function (name, callback) {
55 55
   var path = pathFromNameMd(name);
56
+  console.log(path)
56 57
 //  if (name != "home"){
57 58
 //    return callback(null,{exists: false})
58 59
 
... ...
@@ -60,19 +63,30 @@ exports.loadPage = function (name, callback) {
60 60
   
61 61
 
62 62
   FS.readdir(pathFromDir(), function(err,files){
63
-      var pathfiles=fullPath(files)
64 63
       var fileObj = []
64
+      var file_ext=".markdown"
65
+      var clean_list = []
66
+      for( var  i = 0; i < files.length; i++){
67
+          var f=files[i]
68
+          if (f.slice( - file_ext.length, f.length ) === file_ext) {
69
+            clean_list.push(f)
70
+          }
71
+       }
72
+      files = clean_list
73
+      
74
+      var pathfiles=fullPath(files) 
65 75
       async.map(pathfiles, FS.readFile, function(err, data){
66 76
           for( var  i = 0; i < files.length; i++){
67
-            //console.log(files[i])
68
-            try{
69
-              var html = Markdown(data[i].toString().replace(/\r/g,""))
70
-              fileObj.push({name : files[i], markdown:data[i].toString(), html:html} )
71
-            }
72
-            catch (err){ }
73
-            
74
-          }
77
+              try{
78
+                console.log(f)
79
+                var html = Markdown(data[i].toString().replace(/\r/g,""))
80
+                fileObj.push({name : files[i], markdown:data[i].toString(), html:html} )
81
+              }
82
+               catch (err){ }
83
+                  console.log(err)
84
+              }
75 85
           var torender = orderize(fileObj)
86
+          
76 87
           callback(null,{exists:true, torender:torender})
77 88
           
78 89
       })  
... ...
@@ -127,8 +141,61 @@ exports.editPage = function (name, callback) {
127 127
 };
128 128
 // Saving is simple.  Just put the markdown in the file
129 129
 exports.savePage = function (name, value, callback) {
130
-  var path = pathFromNameMd(name);
131
-  FS.writeFile(path, value.replace(/\r/g,""), callback);
130
+  var pathFile = pathFromNameMd(name);
131
+  FS.writeFile(pathFile, value.replace(/\r/g,""), function (){
132
+              
133
+              
134
+              
135
+             
136
+  
137
+  var repo;
138
+  var index;
139
+  var oid;
140
+
141
+  nodegit.Repository.open(Path.resolve(__dirname, "pages/.git"))
142
+    .then(function(repoResult) {
143
+        repo = repoResult;
144
+    })
145
+    .then(function (){ 
146
+      return repo.openIndex();
147
+    })
148
+    .then(function(indexResult) {
149
+     index = indexResult;
150
+    return index.read(1);
151
+    })
152
+    .then(function() {
153
+     // this file is in the root of the directory and doesn't need a full path
154
+      return index.addByPath(name);
155
+    })
156
+    .then(function() {
157
+  // this will write both files to the index
158
+      return index.write();
159
+    }) 
160
+    .then(function() {
161
+      return index.writeTree();
162
+  })
163
+  .then(function(oidResult) {
164
+    oid = oidResult;
165
+    return nodegit.Reference.nameToId(repo, "HEAD");
166
+  })
167
+  .then(function(head) {
168
+    return repo.getCommit(head);
169
+  })
170
+  .then(function(parent) {
171
+    var author = nodegit.Signature.create("BOB",
172
+      "BOBHASHH", 123456789, 60);
173
+    var committer = nodegit.Signature.create("ALICE COMMIT",
174
+      "alicehash", 987654321, 90);
175
+
176
+      return repo.createCommit("HEAD", author, committer, "message", oid, [parent]);
177
+    })
178
+    .done(function(commitId) {
179
+      console.log("New Commit: ", commitId);
180
+      callback()
181
+    });
182
+
183
+  })
184
+
132 185
 };
133 186
 
134 187
 var dbFile = Path.join(__dirname, 'app.db');
135 188
new file mode 100644
... ...
@@ -0,0 +1,193 @@
0
+var FS = require('fs');
1
+var Path = require('path');
2
+//var Markdown = require("marked");
3
+//var MdRenderer=require("./md-override")
4
+var knex = require("knex");
5
+var Bookshelf = require('bookshelf');
6
+var async = require('async');
7
+var textile = require('textile-js')
8
+
9
+
10
+var Markdown =  textile //Markdown = require('js-markdown-extra').Markdown
11
+
12
+function orderize(files){
13
+   var pageObjs = []
14
+   for (var i = 0; i<files.length; i++){
15
+     var file = files[i].name
16
+     var order = parseInt(file.split("_")[0])
17
+     if(!isNaN(order)) {
18
+      var divId = file.split("_")[1].split(".markdown")[0]
19
+      var md = files[i].markdown
20
+      var html = files[i].html
21
+      pageObjs.push ({name: file.split(".markdown")[0], order:order, divId:divId, markdown:md, html:html })
22
+    }
23
+   }
24
+  return pageObjs.sort(function(a,b){ return a.order - b.order });
25
+ }
26
+
27
+
28
+// This function is used to map wiki page names to files
29
+// on the real filesystem.
30
+function pathFromNameMd(name) {
31
+  return Path.join(__dirname, "pages", name + ".markdown");
32
+}
33
+
34
+function pathFromName(name) {
35
+  return Path.join(__dirname, "pages", name);
36
+}
37
+
38
+
39
+function pathFromDir() {
40
+  return Path.join(__dirname, "pages");
41
+}
42
+
43
+
44
+function fullPath(files) {
45
+  var paths = []
46
+  for (var i= 0; i < files.length; i++)
47
+    paths.push(pathFromName(files[i]))
48
+
49
+  return paths 
50
+}
51
+
52
+// Load a file, parse the title and generate the HTML
53
+exports.loadPage = function (name, callback) {
54
+  var path = pathFromNameMd(name);
55
+//  if (name != "home"){
56
+//    return callback(null,{exists: false})
57
+
58
+//  }
59
+  
60
+
61
+  FS.readdir(pathFromDir(), function(err,files){
62
+      var pathfiles=fullPath(files)
63
+      var fileObj = []
64
+      async.map(pathfiles, FS.readFile, function(err, data){
65
+          for( var  i = 0; i < files.length; i++){
66
+            //console.log(files[i])
67
+            try{
68
+              var html = Markdown(data[i].toString().replace(/\r/g,""))
69
+              fileObj.push({name : files[i], markdown:data[i].toString(), html:html} )
70
+            }
71
+            catch (err){ }
72
+            
73
+          }
74
+          var torender = orderize(fileObj)
75
+          callback(null,{exists:true, torender:torender})
76
+          
77
+      })  
78
+
79
+  })
80
+
81
+};
82
+
83
+exports.editPage = function (name, callback) {
84
+  var path = pathFromNameMd(name);
85
+
86
+  
87
+  FS.readFile(path, 'utf8', function (err, markdown) {
88
+
89
+    var exists = true;
90
+    if (err) {
91
+      if (err.code === "ENOENT") {
92
+        // Generate a placeholder body.
93
+        markdown = "# " + name.replace(/_/g, " ") +
94
+          "\n\n" + "This page does not exist yet.";
95
+        exists = false;
96
+      } else {
97
+        // Forward on all other errors.
98
+        return callback(err);
99
+      }
100
+    }
101
+
102
+    // Parse and render the markdown.
103
+    /*var tree = Markdown.parse(markdown);
104
+    var title = name;
105
+    for (var i = 1, l = tree.length; i < l; i++) {
106
+      if (tree[i] && tree[i][0] === "header") {
107
+        title = tree[i][2];
108
+        tree.splice(i, 1);
109
+        break;
110
+      }
111
+    }*/
112
+
113
+  var html = Markdown(markdown);
114
+    
115
+    callback(null, {
116
+      name: name,
117
+      title: null,
118
+      exists: exists,
119
+      markdown: unescape(markdown),
120
+      html: html,
121
+    });
122
+   
123
+
124
+  });
125
+ 
126
+};
127
+// Saving is simple.  Just put the markdown in the file
128
+exports.savePage = function (name, value, callback) {
129
+  var path = pathFromNameMd(name);
130
+  FS.writeFile(path, value.replace(/\r/g,""), callback);
131
+  nodegit.Repository.open("pages")
132
+    .then(function (){ 
133
+      return repo.openIndex();
134
+    })
135
+    .then(function(indexResult) {
136
+     index = indexResult;
137
+    return index.read(1);
138
+    })
139
+    .then(function() {
140
+     // this file is in the root of the directory and doesn't need a full path
141
+      return index.addByPath(name);
142
+    })
143
+    .then(function() {
144
+  // this will write both files to the index
145
+      return index.write();
146
+    }) 
147
+    .then(function() {
148
+      return index.writeTree();
149
+  })
150
+  .then(function(oidResult) {
151
+    oid = oidResult;
152
+    return nodegit.Reference.nameToId(repo, "HEAD");
153
+  })
154
+  .then(function(head) {
155
+    return repo.getCommit(head);
156
+  })
157
+  .then(function(parent) {
158
+    var author = nodegit.Signature.create("BOB",
159
+      "BOBHASHH", 123456789, 60);
160
+    var committer = nodegit.Signature.create("ALICE COMMIT",
161
+      "alicehash", 987654321, 90);
162
+
163
+      return repo.createCommit("HEAD", author, committer, "message", oid, [parent]);
164
+    })
165
+    .done(function(commitId) {
166
+      console.log("New Commit: ", commitId);
167
+    });
168
+
169
+};
170
+
171
+var dbFile = Path.join(__dirname, 'app.db');
172
+var DB = Bookshelf(knex({
173
+   client: 'sqlite3', 
174
+   connection: { filename: dbFile }
175
+}));
176
+
177
+FS.exists(dbFile, function(exists) {
178
+    if (!exists) {
179
+        console.log("create a new DB")
180
+        DB.knex.schema.createTable('Users', function(table) {
181
+            table.increments("id")
182
+            table.string('username')
183
+            table.string('password')
184
+        }).then( function(){ console.log("DB created") })
185
+    }
186
+})
187
+
188
+exports.DB = DB;
189
+
190
+
191
+
192
+
... ...
@@ -1,3 +1,3 @@
1
-h3. [THSF]:/less/is/more 
1
+h3. [THSF]:/less/is/more/test/test/test
2 2
 
3 3
 h4. 14-17/05/2015 Toulouse France
... ...
@@ -1,4 +1,4 @@
1
-h3. Conférences !/images/LOGO_THSF_128.png!
1
+h3. ConférenceZ !/images/LOGO_THSF_128.png!
2 2
 
3 3
 | Célia IZOARD ("Revue Z":http://www.zite.fr/-Le-Journal)| L'informatique peut-elle casser des briques?| jeudi à partir de 16h|
4 4
 | Bernard STIEGLER ("Philosophe":https://fr.wikipedia.org/wiki/Bernard_Stiegler)|Un changement possible? | jeudi (suite)|
... ...
@@ -22,3 +22,4 @@ h3=. Datas
22 22
 * Photos
23 23
 "Photomaton du Paulla":http://photomaton.thsf.net/photomaton
24 24
 
25
+h3=. Heloo
25 26
\ No newline at end of file
... ...
@@ -11,6 +11,7 @@ var bcrypt = require('bcrypt-nodejs');
11 11
 // Load a page from the database and render as html
12 12
 exports.index = function (req, res, next) {
13 13
   DB.loadPage(req.params.name, function (err, page) {
14
+    console.log("___" + page)
14 15
     if (err) return next(err);
15 16
     if (page.exists==false && !req.isAuthenticated())
16 17
       res.redirect('/')