Commit 267963a9 by Mike Chen

parsing and converting curly bracket variable names on client side

parent de652ef2
...@@ -22,7 +22,7 @@ KeyValueString ...@@ -22,7 +22,7 @@ KeyValueString
} }
KeyValuePair KeyValuePair
= key:KeyValueString _ ':' _ value:KeyValueString = key:KeyValueString ' '* ':' ' '* value:KeyValueString
{ {
return {'key': key, 'value': value}; return {'key': key, 'value': value};
} }
...@@ -70,7 +70,7 @@ ImageTitle ...@@ -70,7 +70,7 @@ ImageTitle
} }
Image Image
= ImageIdentifier title:ImageTitle? _ '(' _ url:ImageLink ')' ' '* Linebreak = ImageIdentifier title:ImageTitle? ' '* '(' ' '* url:ImageLink ')' ' '* Linebreak
{ {
return {'type': 'image', 'url': url, 'title': title}; return {'type': 'image', 'url': url, 'title': title};
} }
...@@ -91,14 +91,24 @@ CorrectAnswerIdentifier ...@@ -91,14 +91,24 @@ CorrectAnswerIdentifier
/* StringResponse */ /* StringResponse */
QuotedVariableName
= '"' name:VariableName '"' { return name; }
/ '\'' name:VariableName '\'' { return name; }
VariableName
= '{' chars:([a-zA-Z0-9_]+) '}'
{
return '{' + chars.join("") + '}'
}
StringResponse StringResponse
= CorrectAnswerIdentifier _ !'(' value:(QuotedString / Text) Linebreak = CorrectAnswerIdentifier ' '* !'(' value:(QuotedString / QuotedVariableName / Text) Linebreak
{ {
return {'type': 'string', 'answer': $.trim(value)}; return {'type': 'string', 'answer': $.trim(value)};
} }
NumericalOrStringResponse NumericalOrStringResponse
= CorrectAnswerIdentifier _ value:NumericalValue _ tolerance:NumericalTolerance? _ Linebreak = CorrectAnswerIdentifier ' '* value:NumericalValue ' '* tolerance:NumericalTolerance? ' '* Linebreak
{ {
if (tolerance == "") if (tolerance == "")
tolerance = "5%" tolerance = "5%"
...@@ -117,7 +127,7 @@ ChoiceTextLine ...@@ -117,7 +127,7 @@ ChoiceTextLine
Choice Choice
= correct:ChoiceIdentifier lines:(ChoiceTextLine)+ = correct:ChoiceIdentifier lines:(ChoiceTextLine)+
{ {
return {'type': 'choice', 'correct' : correct, 'text': lines.join("")}; return {'type': 'choice', 'correct' : correct, 'text': lines.join("").replace(/[\n\r]$/,"")};
} }
MultipleChoice MultipleChoice
...@@ -156,12 +166,6 @@ Text ...@@ -156,12 +166,6 @@ Text
return chars.join(""); return chars.join("");
} }
_AndLines
= (' ' / '\n' / '\t')*
_
= (' ' / '\t')*
Linebreak Linebreak
= ('\n') = ('\n')
...@@ -308,13 +312,13 @@ NumericalToleranceValueType ...@@ -308,13 +312,13 @@ NumericalToleranceValueType
= decimal / percentage / integer = decimal / percentage / integer
NumericalTolerance NumericalTolerance
= '+-' _ value:NumericalToleranceValueType _ = '+-' ' '* value:NumericalToleranceValueType ' '*
{ {
return value; return value;
} }
NumericalResponse NumericalResponse
= CorrectAnswerIdentifier _ '(' _ value:NumericalValue _ tolerance:NumericalTolerance? ')' _ Linebreak = CorrectAnswerIdentifier ' '* '(' ' '* value:NumericalValue ' '* tolerance:NumericalTolerance? ')' ' '* Linebreak
{ {
if (tolerance == "") if (tolerance == "")
tolerance = "5%" tolerance = "5%"
...@@ -323,6 +327,7 @@ NumericalResponse ...@@ -323,6 +327,7 @@ NumericalResponse
NumericalValue NumericalValue
= additive = additive
/ VariableName
/* Mathematical rules */ /* Mathematical rules */
......
...@@ -68,6 +68,10 @@ class @CapaDescriptor ...@@ -68,6 +68,10 @@ class @CapaDescriptor
el.children().last().remove() el.children().last().remove()
return el return el
variable_name_wrapper = (expression) ->
match = /^\{(.+)\}$/.exec(expression)
return if match then "$" + match[1] else expression
for section in parsed for section in parsed
if section.type == 'text' if section.type == 'text'
newel = create_text_element(section.text) newel = create_text_element(section.text)
...@@ -116,7 +120,7 @@ class @CapaDescriptor ...@@ -116,7 +120,7 @@ class @CapaDescriptor
else if section.type == 'numerical' else if section.type == 'numerical'
newel = $(doc.createElement('numericalresponse')) newel = $(doc.createElement('numericalresponse'))
newel.attr 'answer', section.answer newel.attr 'answer', variable_name_wrapper(section.answer)
tolerance = $(doc.createElement('responseparam')) tolerance = $(doc.createElement('responseparam'))
tolerance.attr 'type', 'tolerance' tolerance.attr 'type', 'tolerance'
...@@ -131,7 +135,7 @@ class @CapaDescriptor ...@@ -131,7 +135,7 @@ class @CapaDescriptor
else if section.type == 'string' else if section.type == 'string'
newel = $(doc.createElement('stringresponse')) newel = $(doc.createElement('stringresponse'))
newel.attr 'answer', section.answer newel.attr 'answer', variable_name_wrapper(section.answer)
newel.append doc.createElement('textline') newel.append doc.createElement('textline')
problem.append(newel) problem.append(newel)
...@@ -139,7 +143,7 @@ class @CapaDescriptor ...@@ -139,7 +143,7 @@ class @CapaDescriptor
else if section.type == 'formula' else if section.type == 'formula'
formularesponse = $(doc.createElement("formularesponse")) formularesponse = $(doc.createElement("formularesponse"))
formularesponse.attr 'samples', section.samples formularesponse.attr 'samples', section.samples
formularesponse.attr 'answer', section.answer formularesponse.attr 'answer', variable_name_wrapper(section.answer)
formularesponse.attr 'type', 'cs' formularesponse.attr 'type', 'cs'
tolerance = $(doc.createElement('responseparam')) tolerance = $(doc.createElement('responseparam'))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment