Permalink
Browse files

dates that span and apply

  • Loading branch information...
1 parent e1f8046 commit 26a1a3c265f0e5c2c5bc95f2db186d54029152ad @WardCunningham committed Feb 4, 2013
@@ -1,11 +1,67 @@
-emit = (div, item) -> div.append """
- <div style="height: 10px; border-top: 2px solid lightgray; margin-top: 24px; text-align: center; position: relative; clear: both;">
- <span style="position: relative; top: -.8em; background: white; display: inline-block; color: gray; ">
- &nbsp; #{item.text} &nbsp;
- </span>
- </div>
-"""
+
+months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
+spans = ['DECADE', 'EARLY', 'LATE', 'YEAR', 'MONTH', 'DAY']
+
+parse = (text) ->
+ rows = []
+ for line in text.split /\n/
+ result = {}
+ words = line.match /\S+/g
+ for word, i in words
+ if word.match /^\d\d\d\d$/
+ result.year = +word
+ else if m = word.match /^(\d0)S$/
+ result.year = +m[1]+1900
+ result.span ||= 'DECADE'
+ else if (m = spans.indexOf word) >= 0
+ result.span = spans[m]
+ else if (m = months.indexOf word[0..2]) >= 0
+ result.month = m+1
+ else if m = word.match /^([1-3]?[0-9])$/
+ result.day = +m[1]
+ else
+ result.label = words[i..999].join ' '
+ break
+ rows.push result
+ rows
+
+apply = (input, output, date, rows) ->
+ result = []
+ for row in rows
+ if input[row.label]?.date?
+ date = input[row.label].date
+ if output[row.label]?.date?
+ date = output[row.label].date
+ if row.year?
+ date = new Date row.year, 1-1
+ if row.month?
+ date = new Date date.getYear()+1900, row.month-1
+ if row.day?
+ date = new Date date.getYear()+1900, date.getMonth(), row.day
+ if row.label?
+ output[row.label] = {date}
+ output[row.label].span = row.span if row.span?
+ row.date = date
+ result.push row
+ result
+
+format = (rows) ->
+ for row in rows
+ """<tr><td>#{row.date.toDateString()}<td>#{row.label}"""
+
+module.exports = {parse, apply, format} if module?
+
+
+emit = (div, item) ->
+ rows = parse item.text
+ wiki.log 'calendar rows', rows
+ results = apply {}, {}, new Date(), rows
+ wiki.log 'calendar results', results
+ div.append """
+ <table style="width:100%; background:#eee; padding:.8em; margin-bottom:5px;">#{format(results).join ''}</table>
+ """
+
bind = (div, item) ->
div.dblclick -> wiki.textEditor div, item
-window.plugins.calendar = {emit, bind}
+window.plugins.calendar = {emit, bind} if window?

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,59 @@
+report = require './calendar'
+
+describe 'calendar plugin', ->
+
+ describe 'parsing', ->
+
+ it 'recognizes decades', ->
+ expect(report.parse "1960 DECADE").to.eql [{year: 1960, span:'DECADE'}]
+ expect(report.parse "DECADE 1960").to.eql [{year: 1960, span:'DECADE'}]
+ expect(report.parse "60S").to.eql [{year: 1960, span:'DECADE'}]
+
+ it 'recognizes half decades', ->
+ expect(report.parse "60S EARLY").to.eql [{year: 1960, span:'EARLY'}]
+ expect(report.parse "EARLY 60S").to.eql [{year: 1960, span:'EARLY'}]
+ expect(report.parse "LATE 60S").to.eql [{year: 1960, span:'LATE'}]
+
+ it 'recognizes years', ->
+ expect(report.parse "1960").to.eql [{year: 1960}]
+
+ it 'recognizes months', ->
+ expect(report.parse "1960 MAR").to.eql [{year: 1960, month:3}]
+ expect(report.parse "MAR 1960").to.eql [{year: 1960, month:3}]
+ expect(report.parse "MARCH 1960").to.eql [{year: 1960, month:3}]
+
+ it 'recognizes days', ->
+ expect(report.parse "MAR 5 1960").to.eql [{year: 1960, month:3, day: 5}]
+ expect(report.parse "1960 MAR 5").to.eql [{year: 1960, month:3, day: 5}]
+ expect(report.parse "5 MAR 1960").to.eql [{year: 1960, month:3, day: 5}]
+
+ it 'recognizes labels', ->
+ expect(report.parse "Ward's CHM Interview").to.eql [{label: "Ward's CHM Interview"}]
+ expect(report.parse "APRIL 24 2006 Ward's CHM Interview").to.eql [{year: 2006, month:4, day: 24, label: "Ward's CHM Interview"}]
+ expect(report.parse " APRIL 24 2006\tWard's CHM Interview ").to.eql [{year: 2006, month:4, day: 24, label: "Ward's CHM Interview"}]
+
+ describe 'applying', ->
+
+ today = new Date 2013, 2-1, 3
+ interview = new Date 2006, 4-1, 24
+
+ it 'recalls input', ->
+ input = {interview: {date: interview}}
+ output = {}
+ rows = report.parse "interview"
+ expect(report.apply input, output, today, rows).to.eql [{date: interview, label:'interview'}]
+
+ it 'extends today', ->
+ input = {}
+ output = {}
+ rows = report.parse "APRIL April Fools Day"
+ results = report.apply input, output, today, rows
+ expect(results).to.eql [{date: new Date(2013, 4-1), month: 4, label: 'April Fools Day'}]
+ expect(output).to.eql {'April Fools Day': {date: new Date(2013, 4-1)}}
+
+
+
+ # describe 'formatting', ->
+ # it 'returns an array of strings', ->
+ # rows = report.format report.parse ""
+ # expect(rows).to.eql []
Oops, something went wrong.

0 comments on commit 26a1a3c

Please sign in to comment.