Commit 267963a9 by Mike Chen

parsing and converting curly bracket variable names on client side

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