gift-parser 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,207 @@
1
+ # Gift format treetop parser
2
+
3
+ #:nodoc: all
4
+ grammar Gift
5
+
6
+ rule gift
7
+ (command* question blank_line)+
8
+ {
9
+ @current_category
10
+
11
+ def questions
12
+ elements.map{|e| e.question}
13
+ end
14
+
15
+ def commands
16
+ elements.map{|e| e.elements[0].elements[0].command_text}
17
+ end
18
+ }
19
+
20
+ end
21
+
22
+ rule command
23
+ '$' (!line_break .)* line_break
24
+ <Command>
25
+ end
26
+
27
+ rule question
28
+ (essay_question / true_false_question / match_question / fill_in_question / short_answer_question / mutiple_choice_question / numeric_question / description_question)
29
+ end
30
+
31
+ # Question Types
32
+ rule essay_question
33
+ comment? title? space? question_text '{' nbsp? '}'
34
+ <EssayQuestion>
35
+ end
36
+
37
+ rule true_false_question
38
+ comment? title? space? question_text '{' space? answer_list:(true_answer / false_answer) space? '}'
39
+ <TrueFalseQuestion>
40
+ end
41
+
42
+ rule short_answer_question
43
+ comment? title? space? question_text '{' space? answer_list:(right_answer)+ space? '}'
44
+ <ShortAnswerQuestion>
45
+ end
46
+
47
+ rule mutiple_choice_question
48
+ comment? title? space? question_text '{' space? answer_list:(wrong_answer / right_answer)+ space? '}'
49
+ <MultipleChoiceQuestion>
50
+ end
51
+
52
+ rule numeric_question
53
+ comment? title? space? question_text '{#' space? answer_list:(numeric_answer)+ '}'
54
+ <NumericQuestion>
55
+ end
56
+
57
+ rule match_question
58
+ comment? title? space? question_text '{' space? answer_list:(match_answer)+ '}'
59
+ <MatchQuestion>
60
+ end
61
+
62
+ rule fill_in_question
63
+ comment? title? space? question_text '{' space? answer_list:(wrong_answer / right_answer)+ space? '}' (![\r\n] (("\\" [=~{}#:])/ .))+
64
+ <FillInQuestion>
65
+ end
66
+
67
+ rule description_question
68
+ comment? title? space? question_text
69
+ <DescriptionQuestion>
70
+ end
71
+
72
+ # Question Components
73
+
74
+ rule number
75
+ [+-]? [\d]+ (!".." "." [\d]*)?
76
+ end
77
+
78
+ rule weight
79
+ "%" [+-]? [1]? [\d]? [\d] "."? [\d]? [\d]? "%"
80
+ end
81
+
82
+ rule numeric_answer
83
+ value:(range / numeric_with_tolerance) space? feedback? space?
84
+ {
85
+ def answer
86
+ {:maximum => value.maximum, :minimum => value.minimum}
87
+ end
88
+ }
89
+ end
90
+
91
+ rule numeric_with_tolerance
92
+ "="? weight? number (':' number)?
93
+ {
94
+ def maximum
95
+ ans = Float(elements[2].text_value)
96
+ ans += Float(elements[3].elements[1].text_value) unless elements[3].text_value.blank?
97
+ return ans
98
+ end
99
+
100
+ def minimum
101
+ ans = Float(elements[2].text_value)
102
+ ans -= Float(elements[3].elements[1].text_value) unless elements[3].text_value.blank?
103
+ return ans
104
+ end
105
+
106
+ }
107
+ end
108
+
109
+ rule range
110
+ number '..' number
111
+ {
112
+ def maximum
113
+ Float(elements[0].text_value)
114
+ end
115
+
116
+ def minimum
117
+ Float(elements[2].text_value)
118
+ end
119
+ }
120
+ end
121
+
122
+ rule wrong_answer
123
+ "~" weight? (![=~}#] (("\\" [=~{}#:])/ .))* space? feedback?
124
+ {
125
+ def answer
126
+ ans = { :value => elements[2].text_value.rstrip, :correct => false, :feedback => elements[4].text_value.rstrip[1..-1] }
127
+ ans[:weight] = Float(elements[1].text_value[1..-2]) if elements[1].text_value != ""
128
+ return ans
129
+ end
130
+ }
131
+ end
132
+
133
+ rule right_answer
134
+ "=" weight? (![=~}#] (("\\" [=~{}#:])/ .))* space? feedback?
135
+ {
136
+ def answer
137
+ ans = { :value => elements[2].text_value.rstrip, :correct => true, :feedback => elements[4].text_value.rstrip[1..-1] }
138
+ ans[:weight] = Float(elements[1].text_value[1..-2]) if elements[1].text_value != ""
139
+ return ans
140
+ end
141
+ }
142
+ end
143
+
144
+ rule match_answer
145
+ "=" (!"->" ![=~}#] (("\\" [=~{}#:])/ .))* "->" (![=~}#] (("\\" [=~{}#:])/ .))* space?
146
+ {
147
+ def answer
148
+ { elements[1].text_value.strip => elements[3].text_value.strip }
149
+ end
150
+ }
151
+ end
152
+
153
+ rule true_answer
154
+ ("TRUE" / "T") space? feedback?
155
+ {
156
+ def answer
157
+ {:value => true, :correct => true, :feedback => elements[2].text_value.rstrip[1..-1]}
158
+ end
159
+ }
160
+ end
161
+
162
+ rule false_answer
163
+ ("FALSE" / "F") space? feedback?
164
+ {
165
+ def answer
166
+ {:value => false, :correct => true, :feedback => elements[2].text_value.rstrip[1..-1]}
167
+ end
168
+ }
169
+ end
170
+
171
+ rule comment
172
+ "//" (!line_break .)* line_break comment?
173
+ end
174
+
175
+ rule title
176
+ '::' (!"::" (("\\" [=~{}#:])/ .))* '::' line_break?
177
+ end
178
+
179
+ rule question_text
180
+ markup:markup? (!"{" !blank_line (("\\" [=~{}#:])/ .))*
181
+ end
182
+
183
+ rule markup
184
+ "[" (!"]" !blank_line (("\\" [=~{}#:])/ .))* "]"
185
+ end
186
+
187
+ rule feedback
188
+ "#" (![=~}#] (("\\" [=~{}#:])/ .))*
189
+ end
190
+
191
+ rule line_break
192
+ "\r\n" / "\n"
193
+ end
194
+
195
+ rule nbsp
196
+ (" " / "\t")+
197
+ end
198
+
199
+ rule space
200
+ (" " / "\t" / "\r" / "\n")+
201
+ end
202
+
203
+ rule blank_line
204
+ line_break nbsp? line_break space?
205
+ end
206
+
207
+ end
@@ -0,0 +1,400 @@
1
+ # Examples of the GIFT fromat from the Moodle Docs.
2
+
3
+ class GiftExamples
4
+
5
+
6
+ def self.examples
7
+
8
+ examples = {}
9
+
10
+ examples[:description] = <<EOS
11
+ // EXAMPLE QUESTIONS for the GIFT import filter
12
+ // by Paul Tsuchido Shew, January 2004.
13
+
14
+ //-----------------------------------------//
15
+ // EXAMPLES FROM DESCRIPTION
16
+ //-----------------------------------------//
17
+
18
+ Who's buried in Grant's tomb?{~Grant ~Jefferson =no one}
19
+
20
+ Grant is {~buried =entombed ~living} in Grant's tomb.
21
+
22
+ Grant is buried in Grant's tomb.{FALSE}
23
+
24
+ Who's buried in Grant's tomb?{=no one =nobody}
25
+
26
+ When was Ulysses S. Grant born?{#1822:1}
27
+ EOS
28
+
29
+ examples[:multi] = <<EOS
30
+ //-----------------------------------------//
31
+ // EXAMPLES FROM DOCUMENTATION
32
+ //-----------------------------------------//
33
+
34
+ // ===Multiple Choice===
35
+
36
+ Who's buried in Grant's tomb?{~Grant ~Jefferson =no one}
37
+
38
+ Grant is {~buried =entombed ~living} in Grant's tomb.
39
+
40
+ The American holiday of Thanksgiving is celebrated on the {
41
+ ~second
42
+ ~third
43
+ =fourth
44
+ } Thursday of November.
45
+
46
+ Japanese characters originally came from what country? {
47
+ ~India
48
+ =China
49
+ ~Korea
50
+ ~Egypt}
51
+ EOS
52
+
53
+ examples[:short] = <<EOS
54
+ // ===Short Answer===
55
+
56
+ Who's buried in Grant's tomb?{=no one =nobody}
57
+
58
+ Two plus two equals {=four =4}.
59
+ EOS
60
+
61
+ examples[:tf] = <<EOS
62
+ // ===True-False===
63
+
64
+ Grant is buried in Grant's tomb.{F}
65
+
66
+ The sun rises in the east.{T}
67
+ EOS
68
+
69
+ examples[:numerical] = <<EOS
70
+ // ===Numerical===
71
+
72
+ Matching Question. {
73
+ =subquestion1 -> subanswer1
74
+ =subquestion2 -> subanswer2
75
+ =subquestion3 -> subanswer3
76
+ }
77
+
78
+ Match the following countries with their corresponding capitals. {
79
+ =Canada -> Ottawa
80
+ =Italy -> Rome
81
+ =Japan -> Tokyo
82
+ =India -> New Delhi
83
+ }
84
+
85
+ // ===Numerical===
86
+
87
+ When was Ulysses S. Grant born? {#1822}
88
+
89
+ What is the value of pi (to 3 decimal places)? {#3.1415:0.0005}
90
+
91
+ What is the value of pi (to 3 decimal places)? {#3.141..3.142}
92
+
93
+ When was Ulysses S. Grant born? {#
94
+ =1822:0
95
+ =%50%1822:2}
96
+ EOS
97
+
98
+ examples[:line_comments] = <<EOS
99
+ // OPTIONS
100
+
101
+ // ===Line Comments===
102
+
103
+ // Subheading: Numerical questions below
104
+ What's 2 plus 2? {#4}
105
+
106
+ EOS
107
+
108
+ examples[:question_name] = <<EOS
109
+ // ===Question Name===
110
+
111
+ ::Kanji Origins::Japanese characters originally came from what country? {=China}
112
+
113
+ ::Thanksgiving Date::The American holiday of Thanksgiving is celebrated on the {~second ~third =fourth} Thursday of November.
114
+ EOS
115
+
116
+ examples[:feedback] = <<EOS
117
+ // ===Feedback===
118
+
119
+ What's the answer to this multiple-choice question?{
120
+ ~wrong answer#feedback comment on the wrong answer
121
+ ~another wrong answer#feedback comment on this wrong answer
122
+ =right answer#Very good!}
123
+
124
+ Who's buried in Grant's tomb?{
125
+ =no one#excellent answer!
126
+ =nobody#excellent answer!}
127
+ EOS
128
+
129
+ examples[:weights] = <<EOS
130
+ // ===Percentage Answer Weights===
131
+ Grant is buried in Grant's tomb.{FALSE#No one is buried in Grant's tomb.}
132
+
133
+ Difficult question.{~wrong answer ~%50%half credit answer =full credit answer}
134
+
135
+ ::Jesus' hometown::Jesus Christ was from {
136
+ ~Jerusalem#This was an important city, but the wrong answer.
137
+ ~%25%Bethlehem#He was born here, but not raised here.
138
+ ~%50%Galilee#You need to be more specific.
139
+ =Nazareth#Yes! That's right!}
140
+
141
+ ::Jesus' hometown:: Jesus Christ was from {
142
+ =Nazareth#Yes! That's right!
143
+ =%75%Nazereth#Right, but misspelled.
144
+ =%25%Bethlehem#He was born here, but not raised here.}
145
+ EOS
146
+
147
+ examples[:multiple] = <<EOS
148
+ // ===Multiple Answers===
149
+
150
+ What two people are entombed in Grant's tomb? {
151
+ ~No one
152
+ ~%50%Grant
153
+ ~%50%Grant's wife
154
+ ~Grant's father }
155
+
156
+ What two people are entombed in Grant's tomb? {
157
+ ~%-50%No one
158
+ ~%50%Grant
159
+ ~%50%Grant's wife
160
+ ~%-50%Grant's father }
161
+ EOS
162
+
163
+ examples[:format] = <<EOS
164
+ //-----------------------------------------//
165
+ // EXAMPLES FROM gift/format.php
166
+ //-----------------------------------------//
167
+
168
+ Who's buried in Grant's tomb?{~Grant ~Jefferson =no one}
169
+
170
+ Grant is {~buried =entombed ~living} in Grant's tomb.
171
+
172
+ Grant is buried in Grant's tomb.{FALSE}
173
+
174
+ Who's buried in Grant's tomb?{=no one =nobody}
175
+
176
+ When was Ulysses S. Grant born?{#1822:5}
177
+
178
+ Match the following countries with their corresponding
179
+ capitals.{=Canada->Ottawa =Italy->Rome =Japan->Tokyo}
180
+ EOS
181
+
182
+ examples[:complicated] = <<EOS
183
+ //-----------------------------------------//
184
+ // MORE COMPLICATED EXAMPLES
185
+ //-----------------------------------------//
186
+
187
+ ::Grant's Tomb::Grant is {
188
+ ~buried#No one is buried there.
189
+ =entombed#Right answer!
190
+ ~living#We hope not!
191
+ } in Grant's tomb.
192
+
193
+ Difficult multiple choice question.{
194
+ ~wrong answer #comment on wrong answer
195
+ ~%50%half credit answer #comment on answer
196
+ =full credit answer #well done!}
197
+
198
+ ::Jesus' hometown (Short answer ex.):: Jesus Christ was from {
199
+ =Nazareth#Yes! That's right!
200
+ =%75%Nazereth#Right, but misspelled.
201
+ =%25%Bethlehem#He was born here, but not raised here.
202
+ }
203
+
204
+ //this comment will be ignored by the filter
205
+ ::Numerical example::
206
+ When was Ulysses S. Grant born? {#
207
+ =1822:0 #Correct! 100% credit
208
+ =%50%1822:2 #He was born in 1822.
209
+ You get 50% credit for being close.
210
+ }
211
+ EOS
212
+
213
+ examples[:all] = <<EOS
214
+ // EXAMPLE QUESTIONS for the GIFT import filter
215
+ // by Paul Tsuchido Shew, January 2004.
216
+
217
+ //-----------------------------------------//
218
+ // EXAMPLES FROM DESCRIPTION
219
+ //-----------------------------------------//
220
+
221
+ Who's buried in Grant's tomb?{~Grant ~Jefferson =no one}
222
+
223
+ Grant is {~buried =entombed ~living} in Grant's tomb.
224
+
225
+ Grant is buried in Grant's tomb.{FALSE}
226
+
227
+ Who's buried in Grant's tomb?{=no one =nobody}
228
+
229
+ When was Ulysses S. Grant born?{#1822:1}
230
+
231
+
232
+ //-----------------------------------------//
233
+ // EXAMPLES FROM DOCUMENTATION
234
+ //-----------------------------------------//
235
+
236
+ // ===Multiple Choice===
237
+
238
+ Who's buried in Grant's tomb?{~Grant ~Jefferson =no one}
239
+
240
+ Grant is {~buried =entombed ~living} in Grant's tomb.
241
+
242
+ The American holiday of Thanksgiving is celebrated on the {
243
+ ~second
244
+ ~third
245
+ =fourth
246
+ } Thursday of November.
247
+
248
+ Japanese characters originally came from what country? {
249
+ ~India
250
+ =China
251
+ ~Korea
252
+ ~Egypt}
253
+
254
+ // ===Short Answer===
255
+
256
+ Who's buried in Grant's tomb?{=no one =nobody}
257
+
258
+ Two plus two equals {=four =4}.
259
+
260
+ // ===True-False===
261
+
262
+ Grant is buried in Grant's tomb.{F}
263
+
264
+ The sun rises in the east.{T}
265
+
266
+ // ===Numerical===
267
+
268
+ Matching Question. {
269
+ =subquestion1 -> subanswer1
270
+ =subquestion2 -> subanswer2
271
+ =subquestion3 -> subanswer3
272
+ }
273
+
274
+ Match the following countries with their corresponding capitals. {
275
+ =Canada -> Ottawa
276
+ =Italy -> Rome
277
+ =Japan -> Tokyo
278
+ =India -> New Delhi
279
+ }
280
+
281
+ // ===Numerical===
282
+
283
+ When was Ulysses S. Grant born? {#1822}
284
+
285
+ What is the value of pi (to 3 decimal places)? {#3.1415:0.0005}
286
+
287
+ What is the value of pi (to 3 decimal places)? {#3.141..3.142}
288
+
289
+ When was Ulysses S. Grant born? {#
290
+ =1822:0
291
+ =%50%1822:2}
292
+
293
+ // OPTIONS
294
+
295
+ // ===Line Comments===
296
+
297
+ // Subheading: Numerical questions below
298
+ What's 2 plus 2? {#4}
299
+
300
+
301
+ // ===Question Name===
302
+
303
+ ::Kanji Origins::Japanese characters originally
304
+ came from what country? {=China}
305
+
306
+ ::Thanksgiving Date::The American holiday of Thanksgiving is
307
+ celebrated on the {~second ~third =fourth} Thursday of November.
308
+
309
+ // ===Feedback===
310
+
311
+ What's the answer to this multiple-choice question?{
312
+ ~wrong answer#feedback comment on the wrong answer
313
+ ~another wrong answer#feedback comment on this wrong answer
314
+ =right answer#Very good!}
315
+
316
+ Who's buried in Grant's tomb?{
317
+ =no one#excellent answer!
318
+ =nobody#excellent answer!}
319
+
320
+ // ===Percentage Answer Weights===
321
+ Grant is buried in Grant's tomb.{FALSE#No one is buried in Grant's tomb.}
322
+
323
+ Difficult question.{~wrong answer ~%50%half credit answer =full credit answer}
324
+
325
+ ::Jesus' hometown::Jesus Christ was from {
326
+ ~Jerusalem#This was an important city, but the wrong answer.
327
+ ~%25%Bethlehem#He was born here, but not raised here.
328
+ ~%50%Galilee#You need to be more specific.
329
+ =Nazareth#Yes! That's right!}.
330
+
331
+ ::Jesus' hometown:: Jesus Christ was from {
332
+ =Nazareth#Yes! That's right!
333
+ =%75%Nazereth#Right, but misspelled.
334
+ =%25%Bethlehem#He was born here, but not raised here.}
335
+
336
+ // ===Multiple Answers===
337
+
338
+ What two people are entombed in Grant's tomb? {
339
+ ~No one
340
+ ~%50%Grant
341
+ ~%50%Grant's wife
342
+ ~Grant's father }
343
+
344
+ What two people are entombed in Grant's tomb? {
345
+ ~%-50%No one
346
+ ~%50%Grant
347
+ ~%50%Grant's wife
348
+ ~%-50%Grant's father }
349
+
350
+ //-----------------------------------------//
351
+ // EXAMPLES FROM gift/format.php
352
+ //-----------------------------------------//
353
+
354
+ Who's buried in Grant's tomb?{~Grant ~Jefferson =no one}
355
+
356
+ Grant is {~buried =entombed ~living} in Grant's tomb.
357
+
358
+ Grant is buried in Grant's tomb.{FALSE}
359
+
360
+ Who's buried in Grant's tomb?{=no one =nobody}
361
+
362
+ When was Ulysses S. Grant born?{#1822:5}
363
+
364
+ Match the following countries with their corresponding
365
+ capitals.{=Canada->Ottawa =Italy->Rome =Japan->Tokyo}
366
+
367
+ //-----------------------------------------//
368
+ // MORE COMPLICATED EXAMPLES
369
+ //-----------------------------------------//
370
+
371
+ ::Grant's Tomb::Grant is {
372
+ ~buried#No one is buried there.
373
+ =entombed#Right answer!
374
+ ~living#We hope not!
375
+ } in Grant's tomb.
376
+
377
+ Difficult multiple choice question.{
378
+ ~wrong answer #comment on wrong answer
379
+ ~%50%half credit answer #comment on answer
380
+ =full credit answer #well done!}
381
+
382
+ ::Jesus' hometown (Short answer ex.):: Jesus Christ was from {
383
+ =Nazareth#Yes! That's right!
384
+ =%75%Nazereth#Right, but misspelled.
385
+ =%25%Bethlehem#He was born here, but not raised here.
386
+ }
387
+
388
+ //this comment will be ignored by the filter
389
+ ::Numerical example::
390
+ When was Ulysses S. Grant born? {#
391
+ =1822:0 #Correct! 100% credit
392
+ =%50%1822:2 #He was born in 1822.
393
+ You get 50% credit for being close.
394
+ }
395
+
396
+ EOS
397
+ return examples
398
+ end
399
+
400
+ end