Commit 75eca718 by Mike Chen

added image and moved everything to grammars folder

parent 386aa43e
start start
= sections:(StringResponse / NumericalResponse / MultipleChoice / Paragraph / PreservedLinebreaks)+ = sections:(Image /
NumericalOrStringResponse /
StudentProducedResponseIdentifier NumericalResponse /
= '=' MultipleChoice /
Paragraph /
PreservedLinebreaks)+
/* Image */
ImageLink
= DoubleQuotedString
/ chars:( (!')' [^\n])+)
{
return $.trim(chars.map(function(element) { return element[1]; }).join(""));
}
NumericalToleranceValueType ImageTitle
= decimal / percentage / integer = chars:( !'(' [^\n\[\]] )+
{
return $.trim(chars.map(function(element) { return element[1]; }).join(""));
}
NumericalTolerance Image
= '+-' OptionalSpaces value:NumericalToleranceValueType OptionalSpaces = ImageIdentifier title:ImageTitle? OptionalSpaces '(' OptionalSpaces url:ImageLink ')' OptionalSpaces Linebreak
{ {
return value; return {'type': 'image', 'url': url, 'title': title};
} }
NumericalResponse /* First character in a line as identifier */
= StudentProducedResponseIdentifier OptionalSpaces '(' OptionalSpaces value:NumericalValue OptionalSpaces tolerance:NumericalTolerance? ')' OptionalSpaces Linebreak
ChoiceIdentifier
= '(' parenthesized:[x ]? ')'
{ {
if (tolerance == "") return parenthesized == 'x';
tolerance = "5%"
return {'type': 'numerical', 'answer': value, 'tolerance': tolerance};
} }
ImageIdentifier
= '!'
StudentProducedResponseIdentifier
= '='
/* StringResponse */
StringResponse StringResponse
= StudentProducedResponseIdentifier OptionalSpaces !'(' value:Text Linebreak = StudentProducedResponseIdentifier OptionalSpaces !'(' value:Text Linebreak
{ {
return {'type': 'string', 'answer': $.trim(value)}; return {'type': 'string', 'answer': $.trim(value)};
} }
/* MultipleChoice */ NumericalOrStringResponse
ChoiceIdentifier = StudentProducedResponseIdentifier OptionalSpaces value:NumericalValue OptionalSpaces tolerance:NumericalTolerance? OptionalSpaces Linebreak
= '(' parenthesized:[x ]? ')'
{ {
return parenthesized == 'x'; if (tolerance == "")
tolerance = "5%"
return {'type': 'numerical', 'answer': value, 'tolerance': tolerance};
} }
/ StringResponse
/* MultipleChoice */
ChoiceTextLine ChoiceTextLine
= !ChoiceIdentifier line:Line = !ChoiceIdentifier line:Line
...@@ -52,10 +78,10 @@ MultipleChoice ...@@ -52,10 +78,10 @@ MultipleChoice
return {'type': 'multiple_choice', 'choices': choices}; return {'type': 'multiple_choice', 'choices': choices};
} }
/* Paragraph */ /* Paragraph */
ParagraphTextLine ParagraphTextLine
= !ChoiceIdentifier !StudentProducedResponseIdentifier line:Line = !ImageIdentifier !ChoiceIdentifier !StudentProducedResponseIdentifier line:Line
{ {
return line; return line;
} }
...@@ -67,6 +93,7 @@ Paragraph ...@@ -67,6 +93,7 @@ Paragraph
} }
/* Base symbols */ /* Base symbols */
Line Line
= text:Text terminator:Linebreak = text:Text terminator:Linebreak
{ {
...@@ -79,8 +106,11 @@ Text ...@@ -79,8 +106,11 @@ Text
return chars.join(""); return chars.join("");
} }
OptionalSpacesAndLines
= (' ' / '\n' / '\t')*
OptionalSpaces OptionalSpaces
= ' '* = (' ' / '\t')*
PreservedLinebreaks PreservedLinebreaks
= terminators:('\n')+ = terminators:('\n')+
...@@ -91,10 +121,45 @@ PreservedLinebreaks ...@@ -91,10 +121,45 @@ PreservedLinebreaks
Linebreak Linebreak
= ('\n') = ('\n')
DoubleQuotedString
= "\"\"" { return ""; }
/ "\"" str:(!UnescapedQuote .)* last:UnescapedQuote {
return str.map(function(element) { return element[1]; }).join("") + last;
}
UnescapedQuote
= last:[^\\] "\"" {return last;}
AlphanumericalText
= chars:[a-zA-Z0-9]+
{
return chars.join("");
}
/* NumericalResponse */
NumericalToleranceValueType
= decimal / percentage / integer
NumericalTolerance
= '+-' OptionalSpaces value:NumericalToleranceValueType OptionalSpaces
{
return value;
}
NumericalResponse
= StudentProducedResponseIdentifier OptionalSpaces '(' OptionalSpaces value:NumericalValue OptionalSpaces tolerance:NumericalTolerance? ')' OptionalSpaces Linebreak
{
if (tolerance == "")
tolerance = "5%"
return {'type': 'numerical', 'answer': value, 'tolerance': tolerance};
}
NumericalValue NumericalValue
= additive = additive
/* Mathematical rules */ /* Mathematical rules */
additive additive
= left:multiplicative "+" right:additive { return left + right; } = left:multiplicative "+" right:additive { return left + right; }
/ left:multiplicative "-" right:additive { return left - right; } / left:multiplicative "-" right:additive { return left - right; }
......
/*
1. Stealed from W3Schools XML Validator <www.w3schools.com/xml/xml_validator.asp>
2. Beautified with jsbeautifier.org
3. Feed in XML string and use return instead of alert.
*/
var xt = "",
h3OK = 1;
function checkErrorXML(x) {
xt = "";
h3OK = 1;
checkXML(x);
}
function checkXML(n) {
var l, i, nam;
nam = n.nodeName;
if (nam == "h3") {
if (h3OK == 0) {
return;
}
h3OK = 0
}
if (nam == "#text") {
xt = xt + n.nodeValue + "\n"
}
l = n.childNodes.length
for (i = 0; i < l; i++) {
checkXML(n.childNodes[i])
}
}
function validateXML(txt) {
// code for IE
console.log(txt);
if (window.ActiveXObject) {
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(txt);
if (xmlDoc.parseError.errorCode != 0) {
txt = "Error Code: " + xmlDoc.parseError.errorCode + "\n";
txt = txt + "Error Reason: " + xmlDoc.parseError.reason;
txt = txt + "Error Line: " + xmlDoc.parseError.line;
return false;
} else {
return true;
}
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation.createDocument) {
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(txt, "text/xml");
if (xmlDoc.getElementsByTagName("parsererror").length > 0) {
checkErrorXML(xmlDoc.getElementsByTagName("parsererror")[0]);
return false;
} else {
return true;
}
} else {
}
}
\ No newline at end of file
start
= XMLRootElement
XMLComment
= '<!--' chars:(!'-->' .)+ '-->'
{
return '<!--' + chars.map(function(element) { return element[1]; }).join("") + '-->'
}
XMLAttribute
= attr:AlphanumericalText s1:OptionalSpaces '=' s2:OptionalSpaces value:DoubleQuotedString
{
return attr + '=' + "\"" value.replace("\"", "\\"") + "\""
}
XMLTagOpen
= '<' OptionalSpaces name:AlphanumericalText OptionalSpaces attrs:(XMLAttribute / ' ')* OptionalSpaces '>'
{
tag = '<' + name;
if (attrs.length)
tag += ' ' + attrs.join("");
tag += '>';
return tag;
}
XMLTagClose
= '<' OptionalSpaces '/' OptionalSpaces name:AlphanumericalText OptionalSpaces '>'
{
return '</' + name + '>'
}
XMLTagEmpty
= '<' OptionalSpaces name:AlphanumericalText OptionalSpaces attrs:(XMLAttribute / ' ')* OptionalSpaces '/>'
{
tag = '<' + name;
if (attrs.length)
tag += ' ' + attrs.join("");
tag += '/>';
return tag;
}
XMLElement
= chars:((!'>' !'<' .)+)
{
return chars.map(function(element) { return element[2]; }).join("");
}
/ XMLRootElement
XMLRootElement
= XMLTagEmpty
/ open:XMLTagOpen children:(XMLElement+) close:XMLTagClose
{
return open + children.join("") + close;
}
/ XMLTagOpen XMLTagClose
{
return open + close;
}
/ XMLComment
OptionalSpaces
= ' '*
DoubleQuotedString
= "\"\"" { return ""; }
/ "\"" str:(!UnescapedQuote .)* last:UnescapedQuote {
return str.map(function(element) { return element[1]; }).join("") + last;
}
UnescapedQuote
= last:[^\\] "\"" {return last;}
<section class="capa-edit"> <section class="capa-edit">
<div class="parser-message-box" style="display:none;">Input parsed successfully.</div> <div class="parser-message-box" style="display:none;">Input parsed successfully.</div>
<script type="text/javascript" src="/static/js/vendor/peg-0.7.0.min.js"></script> <script type="text/javascript" src="/static/grammars/xml-validator.js"></script>
<script type="text/javascript" src="/static/grammars/peg-0.7.0.min.js"></script>
<textarea name="" class="edit-box source-box" rows="8" cols="40"></textarea> <textarea name="" class="edit-box source-box" rows="8" cols="40"></textarea>
<textarea name="" class="edit-box capa-box" rows="8" cols="40">${data}</textarea> <textarea name="" class="edit-box capa-box" rows="8" cols="40">${data}</textarea>
</section> </section>
...@@ -1179,10 +1179,10 @@ class FormulaResponse(LoncapaResponse): ...@@ -1179,10 +1179,10 @@ class FormulaResponse(LoncapaResponse):
value = random.uniform(*ranges[var]) value = random.uniform(*ranges[var])
instructor_variables[str(var)] = value instructor_variables[str(var)] = value
student_variables[str(var)] = value student_variables[str(var)] = value
#log.debug('formula: instructor_vars=%s, expected=%s' % (instructor_variables,expected)) log.debug('formula: instructor_vars=%s, expected=%s' % (instructor_variables,expected))
instructor_result = evaluator(instructor_variables, dict(), expected, cs=self.case_sensitive) instructor_result = evaluator(instructor_variables, dict(), expected, cs=self.case_sensitive)
try: try:
#log.debug('formula: student_vars=%s, given=%s' % (student_variables,given)) log.debug('formula: student_vars=%s, given=%s' % (student_variables,given))
student_result = evaluator(student_variables, student_result = evaluator(student_variables,
dict(), dict(),
given, given,
......
...@@ -446,6 +446,8 @@ class CapaModule(XModule): ...@@ -446,6 +446,8 @@ class CapaModule(XModule):
try: try:
old_state = self.lcp.get_state() old_state = self.lcp.get_state()
lcp_id = self.lcp.problem_id lcp_id = self.lcp.problem_id
import pdb
pdb.set_trace()
correct_map = self.lcp.grade_answers(answers) correct_map = self.lcp.grade_answers(answers)
except StudentInputError as inst: except StudentInputError as inst:
# TODO (vshnayder): why is this line here? # TODO (vshnayder): why is this line here?
......
...@@ -10,7 +10,7 @@ class @CapaDescriptor ...@@ -10,7 +10,7 @@ class @CapaDescriptor
save: -> @edit_box.val() save: -> @edit_box.val()
buildParser: -> buildParser: ->
$.get "/static/grammar.jspeg", (data) => $.get "/static/grammars/main.jspeg", (data) =>
@grammar = data @grammar = data
@parser = PEG.buildParser @grammar @parser = PEG.buildParser @grammar
, "text" , "text"
...@@ -56,6 +56,17 @@ class @CapaDescriptor ...@@ -56,6 +56,17 @@ class @CapaDescriptor
newel = create_text_element(section.text) newel = create_text_element(section.text)
problem.append(newel) problem.append(newel)
else if section.type == 'image'
center = $(doc.createElement('center'))
img = $(doc.createElement('img'))
img.attr 'src', section.url
center.append img
if section.title
title = create_text_element(section.title)
center.append doc.createElement('br')
center.append title
problem.append center
else if section.type == 'linebreaks' else if section.type == 'linebreaks'
text = create_text_element('') text = create_text_element('')
for i in [0..section.count] by 1 for i in [0..section.count] by 1
......
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