Commit 97c3a1ff by Mike Chen

save wiki source into comment section in XML.

parent 267963a9
...@@ -4,6 +4,7 @@ class @CapaDescriptor ...@@ -4,6 +4,7 @@ class @CapaDescriptor
@edit_box = $(".edit-box.capa-box", @element) @edit_box = $(".edit-box.capa-box", @element)
@source_box = $(".edit-box.source-box", @element) @source_box = $(".edit-box.source-box", @element)
@message_box = $(".parser-message-box", @element) @message_box = $(".parser-message-box", @element)
@loadSource()
@buildParser() @buildParser()
@throttledAutoSave = _.throttle(@autoSave, 0); @throttledAutoSave = _.throttle(@autoSave, 0);
@source_box.keyup => @source_box.keyup =>
...@@ -11,6 +12,14 @@ class @CapaDescriptor ...@@ -11,6 +12,14 @@ class @CapaDescriptor
save: -> @edit_box.val() save: -> @edit_box.val()
loadSource: ->
parser = new DOMParser()
doc = parser.parseFromString @edit_box.val(), "text/xml"
nodes = doc.childNodes
for node in nodes
if node.nodeType == 8 # nodeType = 8 --> #comment
@source_box.val node.nodeValue
buildParser: -> buildParser: ->
$.get "/static/grammars/main.jspeg", (data) => $.get "/static/grammars/main.jspeg", (data) =>
@grammar = data @grammar = data
...@@ -40,22 +49,22 @@ class @CapaDescriptor ...@@ -40,22 +49,22 @@ class @CapaDescriptor
source = @source_box.val() + "\n" source = @source_box.val() + "\n"
result = @parser.parse (source) result = @parser.parse (source)
console.log result console.log result
@outputXML(result) @outputXML(result, source)
@message_box.css {"display":"none"} @message_box.css {"display":"none"}
catch e catch e
console.log @buildErrorMessage(e) console.log @buildErrorMessage(e)
@message_box.html @buildErrorMessage(e) @message_box.html @buildErrorMessage(e)
@message_box.css {"display":"block"} @message_box.css {"display":"block"}
outputXML: (parsed) -> outputXML: (parsed, source) ->
@edit_box.val @buildXML(parsed) @edit_box.val @buildXML(parsed, source)
@checkAutoSave() @checkAutoSave()
dom2capa: (node) -> dom2capa: (node) ->
serializer = new XMLSerializer() serializer = new XMLSerializer()
capa = serializer.serializeToString(node) capa = serializer.serializeToString(node)
buildXML: (parsed) -> buildXML: (parsed, source) ->
dom_parser = new DOMParser() dom_parser = new DOMParser()
doc = dom_parser.parseFromString("<problem />", "text/xml"); doc = dom_parser.parseFromString("<problem />", "text/xml");
problem = $(doc).find('problem') problem = $(doc).find('problem')
...@@ -161,6 +170,7 @@ class @CapaDescriptor ...@@ -161,6 +170,7 @@ class @CapaDescriptor
else else
throw new SyntaxError("unexpected section type " + section.type) throw new SyntaxError("unexpected section type " + section.type)
doc.appendChild doc.createComment(source)
capa = @dom2capa(doc) capa = @dom2capa(doc)
return capa return capa
\ No newline at end of file
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