Merge pull request #346 from arv/monkey-yaml-confused

monkeyYaml: Add support for line folding
This commit is contained in:
Brian Terlson 2015-07-05 23:23:08 -05:00
commit c6ac390868
2 changed files with 63 additions and 9 deletions

View File

@ -15,13 +15,17 @@ mYamlMultilineList = re.compile(r"^ *- (.*)$")
def load(str): def load(str):
dict = None dict = None
key = None
emptyLines = 0
lines = str.splitlines() lines = str.splitlines()
while lines: while lines:
line = lines.pop(0) line = lines.pop(0)
if myIsAllSpaces(line): if myIsAllSpaces(line):
emptyLines += 1
continue continue
result = mYamlKV.match(line) result = mYamlKV.match(line)
if result: if result:
if not dict: if not dict:
dict = {} dict = {}
@ -30,7 +34,12 @@ def load(str):
(lines, value) = myReadValue(lines, value) (lines, value) = myReadValue(lines, value)
dict[key] = value dict[key] = value
else: else:
raise Exception("monkeyYaml is confused at " + line) if dict and key and key in dict:
c = " " if emptyLines == 0 else "\n" * emptyLines
dict[key] += c + line.strip()
else:
raise Exception("monkeyYaml is confused at " + line)
emptyLines = 0
return dict return dict
def myReadValue(lines, value): def myReadValue(lines, value):

View File

@ -111,30 +111,75 @@ class TestMonkeyYAMLParsing(unittest.TestCase):
self.assertEqual(monkeyYaml.load(y), yaml.load(y)) self.assertEqual(monkeyYaml.load(y), yaml.load(y))
def test_oneline_indented(self): def test_oneline_indented(self):
y = " foo: bar\n baz: baf\n" y = " foo: bar\n baz: baf\n"
self.assertEqual(monkeyYaml.load(y), yaml.load(y)) self.assertEqual(monkeyYaml.load(y), yaml.load(y))
def test_indentation_215(self): def test_indentation_215(self):
self.maxDiff = None self.maxDiff = None
y = """ y = """
description: > description: >
The method should exist on the Array prototype, and it should be writable The method should exist on the Array prototype, and it should be writable
and configurable, but not enumerable. and configurable, but not enumerable.
includes: [propertyHelper.js] includes: [propertyHelper.js]
es6id: 22.1.3.13 es6id: 22.1.3.13
""" """
self.assertEqual(monkeyYaml.load(y), yaml.load(y)) self.assertEqual(monkeyYaml.load(y), yaml.load(y))
def test_indentation_215_2(self): def test_indentation_215_2(self):
self.maxDiff = None self.maxDiff = None
y = """ y = """
description: > description: >
The method should exist The method should exist
includes: [propertyHelper.js] includes: [propertyHelper.js]
es6id: 22.1.3.13 es6id: 22.1.3.13
""" """
self.assertEqual(monkeyYaml.load(y), yaml.load(y)) self.assertEqual(monkeyYaml.load(y), yaml.load(y))
def test_line_folding(self):
self.maxDiff = None
y = """
description: aaa
bbb
es6id: 19.1.2.1
"""
self.assertEqual(monkeyYaml.load(y), yaml.load(y))
def test_line_folding_2(self):
self.maxDiff = None
y = """
description: ccc
ddd
es6id: 19.1.2.1
"""
self.assertEqual(monkeyYaml.load(y), yaml.load(y))
def test_line_folding_3(self):
self.maxDiff = None
y = """
description: eee
fff
es6id: 19.1.2.1
"""
self.assertEqual(monkeyYaml.load(y), yaml.load(y))
def test_line_folding_4(self):
self.maxDiff = None
y = """
description: ggg
hhh
iii
jjj
es6id: 19.1.2.1
"""
self.assertEqual(monkeyYaml.load(y), yaml.load(y))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()