diff --git a/Readme.md b/Readme.md index ec126d7..685d694 100644 --- a/Readme.md +++ b/Readme.md @@ -64,6 +64,10 @@ Display a short help message. Listen on rather than 8888. The default port can be changed from 8888 by setting the PORT environment variable. +`-r` +`--readonly` +Disable read/write operations from the web interface + ###Examples `nodewiki` diff --git a/lib/getDir.js b/lib/getDir.js index 6181574..e86fd21 100644 --- a/lib/getDir.js +++ b/lib/getDir.js @@ -1,5 +1,6 @@ var fs = require("fs"); var allowedExtensions = require("./allowedExtensions"); +var nodewiki = require("../nodewiki"); // get contents of current directory function getDir(directory){ @@ -44,6 +45,19 @@ function parseLinks(dir, dirDepth){ var mdLinks = ""; + mdLinks += '
'; + if (nodewiki.readonly == false) + mdLinks += 'New File'; + if (dirDepth > 0){ + mdLinks += 'Go Back'; + } else { + mdLinks += 'Go Back'; + } + if (nodewiki.readonly == false) + mdLinks += 'New Folder'; + mdLinks += '
'; + mdLinks += '
'; + dir.forEach(function(fileName){ if (fileName.markdown == true){ mdLinks += '' + fileName.name + '\n'; @@ -54,16 +68,6 @@ function parseLinks(dir, dirDepth){ } }); - mdLinks += '
' - mdLinks += 'New File'; - if (dirDepth > 0){ - mdLinks += 'Go Back'; - } else { - mdLinks += 'Go Back'; - } - mdLinks += 'New Folder'; - mdLinks += '
'; - return mdLinks; } catch (err) { diff --git a/lib/mdserver.js b/lib/mdserver.js index 71ef32f..f2ad62b 100644 --- a/lib/mdserver.js +++ b/lib/mdserver.js @@ -32,7 +32,7 @@ function sendFile(file, directory, socket){ } function saveFile(file, directory, socket){ - if (allowedExtensions.checkExtension(file.name) == true && typeof file.content != 'undefined'){ + if (allowedExtensions.checkExtension(file.name) == true && typeof file.content != 'undefined' && nodewiki.readonly == false){ // only allow markdown files to be saved console.log('saving file'); fs.writeFile(directory + file.name, file.content, function(err, data){ diff --git a/nodewiki.js b/nodewiki.js index 814c88f..d9ab8ab 100755 --- a/nodewiki.js +++ b/nodewiki.js @@ -13,12 +13,13 @@ var getDir = require("./lib/getDir"); var portNumberDefault = process.env.PORT || 8888; var listenAddr = process.env.NW_ADDR || ""; // "" ==> INADDR_ANY exports.gitMode = false; // exported for lib/mdserver.js +exports.readonly = false; var portNumber = portNumberDefault; // Process command line var parser, option; -parser = new mod_getopt.BasicParser('a:(addr)g(git)h(help)l(local)p:(port)', process.argv); +parser = new mod_getopt.BasicParser('a:(addr)g(git)h(help)l(local)p:(port)r(readonly)', process.argv); while ((option = parser.getopt()) !== undefined) { @@ -64,6 +65,10 @@ while ((option = parser.getopt()) !== undefined) { } break; + case 'r': + exports.readonly = true; + break; + default: /* error message already emitted by getopt() */ console.assert('?' == option.option); @@ -129,7 +134,9 @@ io = socketio.listen(server); io.set('log level', 2); io.sockets.on('connection', function (socket){ - var currentPath = process.cwd() + '/'; + socket.emit('config', exports); + + var currentPath = './'; var dir = getDir.getDir(currentPath); var links = getDir.parseLinks(dir); var directoryDepth = 0; diff --git a/static/socketio.js b/static/socketio.js index 7d256a9..306d11b 100644 --- a/static/socketio.js +++ b/static/socketio.js @@ -3,9 +3,22 @@ $(document).ready(function(){ var rawMd, fileName; var editingAllowed = false; //dont allow editing until something is loaded + var config; + window.location.hash = window.location.hash || '/'; var socket = io.connect(); socket.on('connect', function(){ + // open the hash given by the browser + var parts = window.location.hash.match(/[^\/]+\/?/g); + parts.shift(); + parts.forEach(function(part) { + socket.emit('readFile', {name: part}); + }); + + socket.on('config', function (data){ + config = data; + }); + socket.on('navLinks', function (data){ $('#navigation').html(data.links); changeContentHeight(); @@ -19,6 +32,10 @@ $(document).ready(function(){ if (canSendReadFile == true){ canSendReadFile = false; socket.emit('readFile', {name: $(a.currentTarget).text()}); + if (!window.location.hash.match(/\/$/)) { + window.location.hash = dirname(window.location.hash) + '/'; + } + window.location.hash += $(a.currentTarget).text(); $('#navigation').children().attr('class', 'link'); $('#content #markdown_content').html('Loading...'); $('#content_header h1').html('Node Wiki'); @@ -29,6 +46,7 @@ $(document).ready(function(){ cancelNewFile(); } } + return false; }); socket.on('readFileReply', function(data){ @@ -65,6 +83,7 @@ $(document).ready(function(){ $(document).on('click', '#navigation a#go_back', function(){ socket.emit('goBackFolder'); + window.location.hash = dirname(window.location.hash) + '/'; $('#content #markdown_content').html(''); $('#content_header h1').html('Node Wiki'); //editingAllowed = true; @@ -250,7 +269,7 @@ $(document).ready(function(){ function showButtons(show, newFile){ var buttons = 'Edit\nSave'; - if (show){ + if (show && !config.readonly){ if (newFile){ $('#edit_save_buttons').html('Cancel\nSave'); } else { @@ -275,5 +294,8 @@ $(document).ready(function(){ } } + function dirname(path) { + return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');; + } });