diff --git a/src/diagrams/gantt/gantt.spec.js b/src/diagrams/gantt/gantt.spec.js index a170be6e1..54b74edc4 100644 --- a/src/diagrams/gantt/gantt.spec.js +++ b/src/diagrams/gantt/gantt.spec.js @@ -17,10 +17,16 @@ describe('when parsing a gantt diagram it', function () { parser.parse(str) }) + it('should handle an excludes definition', function () { + const str = 'gantt\ndateFormat yyyy-mm-dd\ntitle Adding gantt diagram functionality to mermaid\nexcludes weekdays 2019-02-01' + + parser.parse(str) + }) it('should handle a section definition', function () { const str = 'gantt\n' + 'dateFormat yyyy-mm-dd\n' + 'title Adding gantt diagram functionality to mermaid\n' + + 'excludes weekdays 2019-02-01\n' + 'section Documentation' parser.parse(str) diff --git a/src/diagrams/gantt/ganttDb.js b/src/diagrams/gantt/ganttDb.js index b4e4fda76..357efd439 100644 --- a/src/diagrams/gantt/ganttDb.js +++ b/src/diagrams/gantt/ganttDb.js @@ -3,6 +3,7 @@ import { logger } from '../../logger' let dateFormat = '' let axisFormat = '' +let excludes = [] let title = '' let sections = [] let tasks = [] @@ -31,6 +32,10 @@ export const setDateFormat = function (txt) { dateFormat = txt } +export const setExcludes = function (txt) { + excludes = txt.split(' ') +} + export const setTitle = function (txt) { title = txt } @@ -58,7 +63,17 @@ export const getTasks = function () { return tasks } -const getStartDate = function (prevTime, dateFormat, str) { +const getNextValidDate = function (date, dateFormat, excludes) { + const excludeWeekends = excludes.indexOf('weekend') >= 0 || excludes.indexOf('weekends') >= 0; + const trimmedDateFormat = dateFormat.trim() + let mDate = moment.isMoment(date) ? date : (moment.isDate(date) ? moment(date) : moment(date, dateFormat, true)); + while ((excludeWeekends && mDate.isoWeekday() >= 6) || (excludes.indexOf(mDate.format(trimmedDateFormat)) >= 0)) { + mDate = mDate.add(1, 'd') + } + return mDate.toDate(); +} + +const getStartDate = function (prevTime, dateFormat, excludes, str) { str = str.trim() // Test for after @@ -71,29 +86,34 @@ const getStartDate = function (prevTime, dateFormat, str) { if (typeof task === 'undefined') { const dt = new Date() dt.setHours(0, 0, 0, 0) + //return getNextValidDate(dt, dateFormat, excludes) return dt } + //return getNextValidDate(task.endTime, dateFormat, excludes) return task.endTime } // Check for actual date set - if (moment(str, dateFormat.trim(), true).isValid()) { - return moment(str, dateFormat.trim(), true).toDate() + let mDate = moment(str, dateFormat.trim(), true); + if (mDate.isValid()) { + return getNextValidDate(mDate, dateFormat, excludes) } else { logger.debug('Invalid date:' + str) logger.debug('With date format:' + dateFormat.trim()) } // Default date - now - return new Date() + return getNextValidDate(new Date(), dateFormat, excludes) } -const getEndDate = function (prevTime, dateFormat, str) { +const getEndDate = function (prevTime, dateFormat, excludes, str) { str = str.trim() // Check for actual date - if (moment(str, dateFormat.trim(), true).isValid()) { - return moment(str, dateFormat.trim()).toDate() + let mDate = moment(str, dateFormat.trim(), true); + if (mDate.isValid()) { + //return getNextValidDate(mDate, dateFormat, excludes) + return mDate.toDate() } const d = moment(prevTime) @@ -119,10 +139,9 @@ const getEndDate = function (prevTime, dateFormat, str) { d.add(durationStatement[1], 'weeks') break } - return d.toDate() } // Default date - now - return d.toDate() + return getNextValidDate(d, dateFormat, excludes) } let taskCnt = 0 @@ -185,17 +204,17 @@ const compileData = function (prevTask, dataStr) { case 1: task.id = parseId() task.startTime = prevTask.endTime - task.endTime = getEndDate(task.startTime, dateFormat, data[0]) + task.endTime = getEndDate(task.startTime, dateFormat, excludes, data[0]) break case 2: task.id = parseId() - task.startTime = getStartDate(undefined, dateFormat, data[0]) - task.endTime = getEndDate(task.startTime, dateFormat, data[1]) + task.startTime = getStartDate(undefined, dateFormat, excludes, data[0]) + task.endTime = getEndDate(task.startTime, dateFormat, excludes, data[1]) break case 3: task.id = parseId(data[0]) - task.startTime = getStartDate(undefined, dateFormat, data[1]) - task.endTime = getEndDate(task.startTime, dateFormat, data[2]) + task.startTime = getStartDate(undefined, dateFormat, excludes, data[1]) + task.endTime = getEndDate(task.startTime, dateFormat, excludes, data[2]) break default: } @@ -322,7 +341,7 @@ const compileTasks = function () { task.startTime = prevTask.endTime break case 'getStartDate': - startTime = getStartDate(undefined, dateFormat, rawTasks[pos].raw.startTime.startData) + startTime = getStartDate(undefined, dateFormat, excludes, rawTasks[pos].raw.startTime.startData) if (startTime) { rawTasks[pos].startTime = startTime } @@ -330,7 +349,7 @@ const compileTasks = function () { } if (rawTasks[pos].startTime) { - rawTasks[pos].endTime = getEndDate(rawTasks[pos].startTime, dateFormat, rawTasks[pos].raw.endTime.data) + rawTasks[pos].endTime = getEndDate(rawTasks[pos].startTime, dateFormat, excludes, rawTasks[pos].raw.endTime.data) if (rawTasks[pos].endTime) { rawTasks[pos].processed = true } @@ -359,5 +378,6 @@ export default { getTasks, addTask, findTaskById, - addTaskOrg + addTaskOrg, + setExcludes } diff --git a/src/diagrams/gantt/ganttDb.spec.js b/src/diagrams/gantt/ganttDb.spec.js index a956e3605..aef2cd92e 100644 --- a/src/diagrams/gantt/ganttDb.spec.js +++ b/src/diagrams/gantt/ganttDb.spec.js @@ -172,4 +172,18 @@ describe('when using the ganttDb', function () { expect(tasks[2].startTime).toEqual(moment('2013-01-15', 'YYYY-MM-DD').toDate()) expect(tasks[2].endTime).toEqual(moment('2013-01-17', 'YYYY-MM-DD').toDate()) }) + it('should ignore weekends', function () { + ganttDb.setDateFormat('YYYY-MM-DD') + ganttDb.setExcludes('weekends 2019-02-06') + ganttDb.addSection('testa1') + ganttDb.addTask('test1', 'id1,2019-02-01,1d') + ganttDb.addTask('test2', 'id2,after id1,3d') + + const tasks = ganttDb.getTasks() + + expect(tasks[1].startTime).toEqual(moment('2019-02-04', 'YYYY-MM-DD').toDate()) + expect(tasks[1].endTime).toEqual(moment('2019-02-07', 'YYYY-MM-DD').toDate()) + expect(tasks[1].id).toEqual('id2') + expect(tasks[1].task).toEqual('test2') + }) }) diff --git a/src/diagrams/gantt/parser/gantt.jison b/src/diagrams/gantt/parser/gantt.jison index 49ab3ad47..734087a2c 100644 --- a/src/diagrams/gantt/parser/gantt.jison +++ b/src/diagrams/gantt/parser/gantt.jison @@ -20,14 +20,15 @@ "gantt" return 'gantt'; "dateFormat"\s[^#\n;]+ return 'dateFormat'; "axisFormat"\s[^#\n;]+ return 'axisFormat'; +"excludes"\s[^#\n;]+ return 'excludes'; \d\d\d\d"-"\d\d"-"\d\d return 'date'; "title"\s[^#\n;]+ return 'title'; "section"\s[^#:\n;]+ return 'section'; [^#:\n;]+ return 'taskTxt'; ":"[^#\n;]+ return 'taskData'; -":" return ':'; -<> return 'EOF'; -. return 'INVALID'; +":" return ':'; +<> return 'EOF'; +. return 'INVALID'; /lex @@ -56,6 +57,7 @@ line statement : 'dateFormat' {yy.setDateFormat($1.substr(11));$$=$1.substr(11);} | 'axisFormat' {yy.setAxisFormat($1.substr(11));$$=$1.substr(11);} + | 'excludes' {yy.setExcludes($1.substr(9));$$=$1.substr(9);} | title {yy.setTitle($1.substr(6));$$=$1.substr(6);} | section {yy.addSection($1.substr(8));$$=$1.substr(8);} | taskTxt taskData {yy.addTask($1,$2);$$='task';} diff --git a/src/diagrams/gantt/parser/gantt.js b/src/diagrams/gantt/parser/gantt.js index c72af3eb9..cd3384a2d 100644 --- a/src/diagrams/gantt/parser/gantt.js +++ b/src/diagrams/gantt/parser/gantt.js @@ -72,12 +72,12 @@ } */ var parser = (function(){ -var o=function(k,v,o,l){for(o=o||{},l=k.length;l--;o[k[l]]=v);return o},$V0=[6,8,10,11,12,13,14,15],$V1=[1,9],$V2=[1,10],$V3=[1,11],$V4=[1,12],$V5=[1,13]; -var parser = {trace: function trace () { }, +var o=function(k,v,o,l){for(o=o||{},l=k.length;l--;o[k[l]]=v);return o},$V0=[6,8,10,11,12,13,14,15,16],$V1=[1,9],$V2=[1,10],$V3=[1,11],$V4=[1,12],$V5=[1,13],$V6=[1,14]; +var parser = {trace: function trace() { }, yy: {}, -symbols_: {"error":2,"start":3,"gantt":4,"document":5,"EOF":6,"line":7,"SPACE":8,"statement":9,"NL":10,"dateFormat":11,"axisFormat":12,"title":13,"section":14,"taskTxt":15,"taskData":16,"$accept":0,"$end":1}, -terminals_: {2:"error",4:"gantt",6:"EOF",8:"SPACE",10:"NL",11:"dateFormat",12:"axisFormat",13:"title",14:"section",15:"taskTxt",16:"taskData"}, -productions_: [0,[3,3],[5,0],[5,2],[7,2],[7,1],[7,1],[7,1],[9,1],[9,1],[9,1],[9,1],[9,2]], +symbols_: {"error":2,"start":3,"gantt":4,"document":5,"EOF":6,"line":7,"SPACE":8,"statement":9,"NL":10,"dateFormat":11,"axisFormat":12,"excludes":13,"title":14,"section":15,"taskTxt":16,"taskData":17,"$accept":0,"$end":1}, +terminals_: {2:"error",4:"gantt",6:"EOF",8:"SPACE",10:"NL",11:"dateFormat",12:"axisFormat",13:"excludes",14:"title",15:"section",16:"taskTxt",17:"taskData"}, +productions_: [0,[3,3],[5,0],[5,2],[7,2],[7,1],[7,1],[7,1],[9,1],[9,1],[9,1],[9,1],[9,1],[9,2]], performAction: function anonymous(yytext, yyleng, yylineno, yy, yystate /* action[1] */, $$ /* vstack */, _$ /* lstack */) { /* this == yyval */ @@ -105,19 +105,22 @@ case 9: yy.setAxisFormat($$[$0].substr(11));this.$=$$[$0].substr(11); break; case 10: -yy.setTitle($$[$0].substr(6));this.$=$$[$0].substr(6); +yy.setExcludes($$[$0].substr(9));this.$=$$[$0].substr(9); break; case 11: -yy.addSection($$[$0].substr(8));this.$=$$[$0].substr(8); +yy.setTitle($$[$0].substr(6));this.$=$$[$0].substr(6); break; case 12: +yy.addSection($$[$0].substr(8));this.$=$$[$0].substr(8); +break; +case 13: yy.addTask($$[$0-1],$$[$0]);this.$='task'; break; } }, -table: [{3:1,4:[1,2]},{1:[3]},o($V0,[2,2],{5:3}),{6:[1,4],7:5,8:[1,6],9:7,10:[1,8],11:$V1,12:$V2,13:$V3,14:$V4,15:$V5},o($V0,[2,7],{1:[2,1]}),o($V0,[2,3]),{9:14,11:$V1,12:$V2,13:$V3,14:$V4,15:$V5},o($V0,[2,5]),o($V0,[2,6]),o($V0,[2,8]),o($V0,[2,9]),o($V0,[2,10]),o($V0,[2,11]),{16:[1,15]},o($V0,[2,4]),o($V0,[2,12])], +table: [{3:1,4:[1,2]},{1:[3]},o($V0,[2,2],{5:3}),{6:[1,4],7:5,8:[1,6],9:7,10:[1,8],11:$V1,12:$V2,13:$V3,14:$V4,15:$V5,16:$V6},o($V0,[2,7],{1:[2,1]}),o($V0,[2,3]),{9:15,11:$V1,12:$V2,13:$V3,14:$V4,15:$V5,16:$V6},o($V0,[2,5]),o($V0,[2,6]),o($V0,[2,8]),o($V0,[2,9]),o($V0,[2,10]),o($V0,[2,11]),o($V0,[2,12]),{17:[1,16]},o($V0,[2,4]),o($V0,[2,13])], defaultActions: {}, -parseError: function parseError (str, hash) { +parseError: function parseError(str, hash) { if (hash.recoverable) { this.trace(str); } else { @@ -409,7 +412,7 @@ showPosition:function () { }, // test the lexed token: return FALSE when not a match, otherwise return token -test_match:function(match, indexed_rule) { +test_match:function (match, indexed_rule) { var token, lines, backup; @@ -539,7 +542,7 @@ next:function () { }, // return next match that has a token -lex:function lex () { +lex:function lex() { var r = this.next(); if (r) { return r; @@ -549,12 +552,12 @@ lex:function lex () { }, // activates a new lexer condition state (pushes the new lexer condition state onto the condition stack) -begin:function begin (condition) { +begin:function begin(condition) { this.conditionStack.push(condition); }, // pop the previously active lexer condition state off the condition stack -popState:function popState () { +popState:function popState() { var n = this.conditionStack.length - 1; if (n > 0) { return this.conditionStack.pop(); @@ -564,7 +567,7 @@ popState:function popState () { }, // produce the lexer rule set which is active for the currently active lexer condition state -_currentRules:function _currentRules () { +_currentRules:function _currentRules() { if (this.conditionStack.length && this.conditionStack[this.conditionStack.length - 1]) { return this.conditions[this.conditionStack[this.conditionStack.length - 1]].rules; } else { @@ -573,7 +576,7 @@ _currentRules:function _currentRules () { }, // return the currently active lexer condition state; when an index argument is provided it produces the N-th previous condition state, if available -topState:function topState (n) { +topState:function topState(n) { n = this.conditionStack.length - 1 - Math.abs(n || 0); if (n >= 0) { return this.conditionStack[n]; @@ -583,7 +586,7 @@ topState:function topState (n) { }, // alias for begin(condition) -pushState:function pushState (condition) { +pushState:function pushState(condition) { this.begin(condition); }, @@ -611,9 +614,9 @@ case 5:return 11; break; case 6:return 12; break; -case 7:return 'date'; +case 7:return 13; break; -case 8:return 13; +case 8:return 'date'; break; case 9:return 14; break; @@ -621,16 +624,18 @@ case 10:return 15; break; case 11:return 16; break; -case 12:return ':'; +case 12:return 17; break; -case 13:return 6; +case 13:return ':'; break; -case 14:return 'INVALID'; +case 14:return 6; +break; +case 15:return 'INVALID'; break; } }, -rules: [/^(?:[\n]+)/i,/^(?:\s+)/i,/^(?:#[^\n]*)/i,/^(?:%[^\n]*)/i,/^(?:gantt\b)/i,/^(?:dateFormat\s[^#\n;]+)/i,/^(?:axisFormat\s[^#\n;]+)/i,/^(?:\d\d\d\d-\d\d-\d\d\b)/i,/^(?:title\s[^#\n;]+)/i,/^(?:section\s[^#:\n;]+)/i,/^(?:[^#:\n;]+)/i,/^(?::[^#\n;]+)/i,/^(?::)/i,/^(?:$)/i,/^(?:.)/i], -conditions: {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14],"inclusive":true}} +rules: [/^(?:[\n]+)/i,/^(?:\s+)/i,/^(?:#[^\n]*)/i,/^(?:%[^\n]*)/i,/^(?:gantt\b)/i,/^(?:dateFormat\s[^#\n;]+)/i,/^(?:axisFormat\s[^#\n;]+)/i,/^(?:excludes\s[^#\n;]+)/i,/^(?:\d\d\d\d-\d\d-\d\d\b)/i,/^(?:title\s[^#\n;]+)/i,/^(?:section\s[^#:\n;]+)/i,/^(?:[^#:\n;]+)/i,/^(?::[^#\n;]+)/i,/^(?::)/i,/^(?:$)/i,/^(?:.)/i], +conditions: {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],"inclusive":true}} }); return lexer; })(); @@ -647,7 +652,7 @@ if (typeof require !== 'undefined' && typeof exports !== 'undefined') { exports.parser = parser; exports.Parser = parser.Parser; exports.parse = function () { return parser.parse.apply(parser, arguments); }; -exports.main = function commonjsMain (args) { +exports.main = function commonjsMain(args) { if (!args[1]) { console.log('Usage: '+args[0]+' FILE'); process.exit(1);