Index: trunk/extensions/ParserPlayground/tests/parserTests.js |
— | — | @@ -128,6 +128,8 @@ |
129 | 129 | if (err) { |
130 | 130 | console.log('RENDER FAIL', err); |
131 | 131 | } else { |
| 132 | + console.log('INPUT:'); |
| 133 | + console.log(item.input + "\n"); |
132 | 134 | console.log('EXPECTED:'); |
133 | 135 | console.log(item.result + "\n"); |
134 | 136 | |
Index: trunk/extensions/ParserPlayground/modules/pegParser.pegjs.txt |
— | — | @@ -1,7 +1,61 @@ |
2 | 2 | /* Produces output more or less compatible with FakeParser; plug it into FP's output and see */ |
3 | 3 | |
| 4 | +{ |
| 5 | + |
| 6 | + /* Temporary debugging help */ |
| 7 | + var print_r = function (arr, level) { |
| 8 | + |
| 9 | + var dumped_text = ""; |
| 10 | + if (!level) level = 0; |
| 11 | + |
| 12 | + //The padding given at the beginning of the line. |
| 13 | + var level_padding = ""; |
| 14 | + var bracket_level_padding = ""; |
| 15 | + |
| 16 | + for (var j = 0; j < level + 1; j++) level_padding += " "; |
| 17 | + for (var b = 0; b < level; b++) bracket_level_padding += " "; |
| 18 | + |
| 19 | + if (typeof(arr) == 'object') { //Array/Hashes/Objects |
| 20 | + dumped_text += "Array\n"; |
| 21 | + dumped_text += bracket_level_padding + "(\n"; |
| 22 | + for (var item in arr) { |
| 23 | + |
| 24 | + var value = arr[item]; |
| 25 | + |
| 26 | + if (typeof(value) == 'object') { //If it is an array, |
| 27 | + dumped_text += level_padding + "[" + item + "] => "; |
| 28 | + dumped_text += print_r(value, level + 2); |
| 29 | + } else { |
| 30 | + dumped_text += level_padding + "[" + item + "] => " + value + "\n"; |
| 31 | + } |
| 32 | + |
| 33 | + } |
| 34 | + dumped_text += bracket_level_padding + ")\n\n"; |
| 35 | + } else { //Stings/Chars/Numbers etc. |
| 36 | + dumped_text = "===>" + arr + "<===(" + typeof(arr) + ")"; |
| 37 | + } |
| 38 | + |
| 39 | + return dumped_text; |
| 40 | + |
| 41 | + } |
| 42 | +} |
| 43 | + |
4 | 44 | start |
5 | | - = e:block* { return {type: 'page', content: e } } |
| 45 | + = e:block* { |
| 46 | + var es = []; |
| 47 | + // flatten sub-arrays, as a list block can contain multiple lists |
| 48 | + $.each(e, function(i, ei) { |
| 49 | + if (ei.constructor == Array) |
| 50 | + es = es.concat(ei); |
| 51 | + else |
| 52 | + es.push(ei); |
| 53 | + }); |
| 54 | + //console.log(print_r(es, 10)); |
| 55 | + return { |
| 56 | + type: 'page', |
| 57 | + content: es |
| 58 | + } |
| 59 | + } |
6 | 60 | |
7 | 61 | anything |
8 | 62 | = a:[A-Za-z0-9,._ -]+ { return a.join('') } / [^\n] |
— | — | @@ -15,7 +69,7 @@ |
16 | 70 | block |
17 | 71 | = br |
18 | 72 | / h |
19 | | - / li |
| 73 | + / lists |
20 | 74 | / para |
21 | 75 | |
22 | 76 | h = h1 / h2 / h3 / h4 / h5 / h6 |
— | — | @@ -82,7 +136,7 @@ |
83 | 137 | c:anything |
84 | 138 | |
85 | 139 | inline |
86 | | - = c:inline_element+ { |
| 140 | + = c:(inline_element / anything)+ { |
87 | 141 | var out = []; |
88 | 142 | var text = ''; |
89 | 143 | for (var i = 0; i < c.length; i++) { |
— | — | @@ -116,7 +170,6 @@ |
117 | 171 | / link |
118 | 172 | / bold |
119 | 173 | / italic |
120 | | - / anything |
121 | 174 | |
122 | 175 | comment |
123 | 176 | = '<!--' c:comment_chars+ '-->' { |
— | — | @@ -293,7 +346,7 @@ |
294 | 347 | } |
295 | 348 | |
296 | 349 | ref_content |
297 | | - = !ref_end a:inline_element { |
| 350 | + = !ref_end a:(inline_element / anything) { |
298 | 351 | return a; |
299 | 352 | } |
300 | 353 | |
— | — | @@ -337,7 +390,7 @@ |
338 | 391 | } |
339 | 392 | |
340 | 393 | references_content |
341 | | - = !references_end a:inline_element { |
| 394 | + = !references_end a:(inline_element / anything) { |
342 | 395 | return a; |
343 | 396 | } |
344 | 397 | |
— | — | @@ -358,20 +411,120 @@ |
359 | 412 | / "'" t:[^'>]+ "'" { return { quote: "'", text: t.join('') } } |
360 | 413 | / '"' t:[^">]+ '"' { return { quote: '"', text: t.join('') } } |
361 | 414 | |
| 415 | +lists = es:(dtdd / li)+ |
| 416 | +{ |
| 417 | + var out = [], // List of list nodes |
| 418 | + bstack = "", // Bullet stack, previous element's listStyle |
| 419 | + bnext = "", // Next element's listStyle |
| 420 | + nodes = []; // Stack of currently active, nested list nodes |
| 421 | + |
| 422 | + var commonPrefixLength = function (x, y) { |
| 423 | + var minLength = Math.min(x.length, y.length); |
| 424 | + for(var i = 0; i < minLength; i++) { |
| 425 | + if (x[i] != y[i]) |
| 426 | + break; |
| 427 | + } |
| 428 | + return i; |
| 429 | + } |
| 430 | + |
| 431 | + var pushN = function ( n ) { |
| 432 | + if (nodes.length > 0) { |
| 433 | + nodes[nodes.length - 1].content.push(n); |
| 434 | + } else { |
| 435 | + out.push(n); |
| 436 | + nodes.push(n); |
| 437 | + } |
362 | 438 | |
| 439 | + } |
| 440 | + |
| 441 | + var openLists = function ( bs, bn ) { |
| 442 | + var prefix = commonPrefixLength (bs, bn); |
| 443 | + nodes = nodes.slice(0, prefix); |
| 444 | + $.each(bn.slice(prefix, bn.length), function (i, c) { |
| 445 | + switch (c) { |
| 446 | + case '*': |
| 447 | + pushN({type: 'ul', content: []}); |
| 448 | + break; |
| 449 | + case '#': |
| 450 | + pushN({type: 'ol', content: []}); |
| 451 | + break; |
| 452 | + case ';': |
| 453 | + case ':': |
| 454 | + pushN({type: 'dl', content: []}); |
| 455 | + break; |
| 456 | + default: |
| 457 | + throw("Unknown node prefix " + c); |
| 458 | + } |
| 459 | + }); |
| 460 | + } |
| 461 | + |
| 462 | + |
| 463 | + $.each(es, function(i, e) { |
| 464 | + if (e.type == 'dtdd') { |
| 465 | + bnext = e.content[0].listStyle; |
| 466 | + lnode = openLists( bstack, bnext ); |
| 467 | + |
| 468 | + nodes[nodes.length - 1].content = |
| 469 | + nodes[nodes.length - 1].content.concat(e.content); |
| 470 | + } else { |
| 471 | + bnext = e.listStyle; |
| 472 | + openLists( bstack, bnext, nodes ); |
| 473 | + nodes[nodes.length - 1].content.push(e); |
| 474 | + } |
| 475 | + bstack = bnext; |
| 476 | + }); |
| 477 | + //console.log("out: " + print_r(out, 5)); |
| 478 | + return out; |
| 479 | + |
| 480 | + |
| 481 | + |
| 482 | +} |
| 483 | + |
363 | 484 | li = bullets:list_char+ |
364 | 485 | c:(inline / anything) |
365 | 486 | newline |
366 | 487 | { |
| 488 | + var type; |
| 489 | + switch (bullets[bullets.length - 1]) { |
| 490 | + case '#': |
| 491 | + case '*': |
| 492 | + type = 'li'; break; |
| 493 | + case ';': type = 'dt'; break; |
| 494 | + case ':': type = 'dd'; break; |
| 495 | + } |
367 | 496 | return { |
368 | | - type: 'li', |
| 497 | + type: type, |
369 | 498 | listStyle: bullets, |
370 | 499 | content: c |
371 | 500 | }; |
372 | 501 | } |
373 | 502 | |
374 | | -list_char = |
375 | | - '*' / |
376 | | - '#' / |
377 | | - ':' / |
378 | | - ';' |
| 503 | +dtdd = bullets:list_char+ |
| 504 | + c:(inline_element / [^:\n])+ |
| 505 | + ":" |
| 506 | + d:(inline / anything) |
| 507 | + newline |
| 508 | +{ |
| 509 | + // reject rule if bullets do not end in colon |
| 510 | + if (bullets[bullets.length - 1] != ';') |
| 511 | + return null; |
| 512 | + else |
| 513 | + return { type: 'dtdd', |
| 514 | + content: [ |
| 515 | + { |
| 516 | + type: 'dt', |
| 517 | + listStyle: bullets, |
| 518 | + content: c |
| 519 | + }, |
| 520 | + { |
| 521 | + type: 'dd', |
| 522 | + listStyle: bullets.slice(0, bullets.length - 1) + ':', |
| 523 | + content: d |
| 524 | + } |
| 525 | + |
| 526 | + ] |
| 527 | + }; |
| 528 | +} |
| 529 | + |
| 530 | + |
| 531 | +list_char = [*#:;] |
Index: trunk/extensions/ParserPlayground/modules/ext.parserPlayground.renderer.js |
— | — | @@ -123,6 +123,9 @@ |
124 | 124 | case 'span': |
125 | 125 | case 'ol': |
126 | 126 | case 'ul': |
| 127 | + case 'dl': |
| 128 | + case 'dt': |
| 129 | + case 'dd': |
127 | 130 | case 'li': |
128 | 131 | var $span = $('<' + tree.type + '>'); |
129 | 132 | if ('attrs' in tree) { |
Index: trunk/extensions/ParserPlayground/modules/ext.parserPlayground.serializer.js |
— | — | @@ -119,7 +119,14 @@ |
120 | 120 | // @fixme validate that text doesn't contain '-->' |
121 | 121 | src = '<!--' + tree.text + '-->'; |
122 | 122 | break; |
| 123 | + case 'ul': |
| 124 | + case 'ol': |
| 125 | + case 'dl': |
| 126 | + src = subParseArray(tree.content); |
| 127 | + break; |
123 | 128 | case 'li': |
| 129 | + case 'dt': |
| 130 | + case 'dd': |
124 | 131 | src = tree.listStyle.join(''); |
125 | 132 | src += subParseArray(tree.content) + '\n'; |
126 | 133 | break; |