Skip to content

Commit

Permalink
fix(cicero) Nested clause issue in markdown_cicero accordproject#323
Browse files Browse the repository at this point in the history
Signed-off-by: Jerome Simeon <jeromesimeon@me.com>
  • Loading branch information
jeromesimeon committed Sep 11, 2020
1 parent b6495e9 commit 436aad4
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 18 deletions.
33 changes: 19 additions & 14 deletions packages/markdown-it-cicero/lib/cicero_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@

'use strict';

const OPEN_BLOCK_RE = require('./cicero_re').OPEN_BLOCK_RE;
const CLOSE_BLOCK_RE = require('./cicero_re').CLOSE_BLOCK_RE;
const getBlockAttributes = require('./cicero_re').getBlockAttributes;
const matchOpenBlock = require('./cicero_re').matchOpenBlock;
const matchCloseBlock = require('./cicero_re').matchCloseBlock;

function cicero_block(state, startLine, endLine, silent) {
let block_name,
Expand All @@ -38,14 +37,17 @@ function cicero_block(state, startLine, endLine, silent) {
if (0x7B/* { */ !== state.src.charCodeAt(start+1)) { return false; }
if (0x23/* # */ !== state.src.charCodeAt(start+2)) { return false; }

match = state.src.slice(start).match(OPEN_BLOCK_RE);
match = matchOpenBlock(state.src.slice(start),stack);
if (!match) { return false; }

block_open = match[1];
// make sure tail has spaces only
pos = start + match.matched[0].length;
pos = state.skipSpaces(pos);

attrs = getBlockAttributes(match);
if (pos < max) { return false; }

if (block_open !== 'clause') { return false; }
block_open = match.tag;
attrs = match.attrs;

block_name = block_open;
markup = '';
Expand All @@ -70,7 +72,7 @@ function cicero_block(state, startLine, endLine, silent) {
max = state.eMarks[nextLine];

if (start < max && state.sCount[nextLine] < state.blkIndent) {
// non-empty line with negative indent should stop the list:
// non-empty line with negative indent should stop the block:
// - ```
// test
break;
Expand All @@ -81,21 +83,24 @@ function cicero_block(state, startLine, endLine, silent) {
//
if (0x7B/* { */ !== state.src.charCodeAt(start)) { continue; }
if (0x7B/* { */ !== state.src.charCodeAt(start+1)) { continue; }
if (0x2F/* / */ !== state.src.charCodeAt(start+2)) { continue; }
if (0x2F/* / */ !== state.src.charCodeAt(start+2) && 0x23/* # */ !== state.src.charCodeAt(start+2)) { continue; }

// Handles nested blocks
if (0x23/* # */ === state.src.charCodeAt(start+2)) {
match = matchOpenBlock(state.src.slice(start),stack);
continue;
}

if (state.sCount[nextLine] - state.blkIndent >= 4) {
// closing fence should be indented less than 4 spaces
continue;
}

match = state.src.slice(start).match(CLOSE_BLOCK_RE);
match = matchCloseBlock(state.src.slice(start),block_open,stack);
if (!match) { continue; }

const block_close = match[1];
if (block_close !== block_open) { continue; }

// make sure tail has spaces only
pos = start + match[0].length;
pos = start + match.matched[0].length;
pos = state.skipSpaces(pos);

if (pos < max) { continue; }
Expand Down
43 changes: 42 additions & 1 deletion packages/markdown-it-cicero/lib/cicero_re.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,49 @@ function getBlockAttributes(match) {
return result;
}

/**
* Match opening blocks
* @param {string} text - the text
* @param {Array<string>} stack - the block stack
* @return {*} open tag
*/
function matchOpenBlock(text,stack) {
var match = text.match(OPEN_BLOCK_RE);
if (!match) { return null; }
var block_open = match[1];
if (block_open !== 'clause') { return null; }
stack.unshift(block_open);
return { tag: block_open, attrs: getBlockAttributes(match), matched: match };
}
/**
* Match closing blocks
* @param {string} text - the text
* @param {string} block_open - the opening block name
* @param {Array<string>} stack - the block stack
* @return {*} close tag
*/
function matchCloseBlock(text,block_open,stack) {
var match = text.match(CLOSE_BLOCK_RE);
if (!match) {
return null;
}
var block_close = match[1];
// Handle proper nesting
if (stack[0] === block_close) {
stack.shift()
}
// Handle stack depleted
if (stack.length > 0) {
return null;
} else {
return { tag: block_close, matched: match };
}
}

module.exports.VARIABLE_RE = VARIABLE_RE;
module.exports.OPEN_BLOCK_RE = OPEN_BLOCK_RE;
module.exports.CLOSE_BLOCK_RE = CLOSE_BLOCK_RE;
module.exports.FORMULA_RE = FORMULA_RE;
module.exports.getBlockAttributes = getBlockAttributes;
module.exports.matchOpenBlock = matchOpenBlock;
module.exports.matchCloseBlock = matchCloseBlock;
module.exports.getBlockAttributes = getBlockAttributes;
2 changes: 1 addition & 1 deletion packages/markdown-it-cicero/test/data/autoclose1.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"type":"paragraph_open","tag":"p","attrs":null,"map":[0,1],"nesting":1,"level":0,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"inline","tag":"","attrs":null,"map":[0,1],"nesting":0,"level":1,"children":[{"type":"text","tag":"","attrs":null,"map":null,"nesting":0,"level":0,"children":null,"content":"Copyright Notices. Licensee shall ensure that its use of the Work is marked with the appropriate copyright notices specified by Licensor in a reasonably prominent position in the order and manner provided by Licensor. Licensee shall abide by the copyright laws and what are considered to be sound practices for copyright notice provisions in the Territory. Licensee shall not use any copyright notices that conflict with, confuse, or negate the notices Licensor provides and requires hereunder.","markup":"","info":"","meta":null,"block":false,"hidden":false}],"content":"Copyright Notices. Licensee shall ensure that its use of the Work is marked with the appropriate copyright notices specified by Licensor in a reasonably prominent position in the order and manner provided by Licensor. Licensee shall abide by the copyright laws and what are considered to be sound practices for copyright notice provisions in the Territory. Licensee shall not use any copyright notices that conflict with, confuse, or negate the notices Licensor provides and requires hereunder.","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"paragraph_close","tag":"p","attrs":null,"map":null,"nesting":-1,"level":0,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"blockquote_open","tag":"blockquote","attrs":null,"map":[2,4],"nesting":1,"level":0,"children":null,"content":"","markup":">","info":"","meta":null,"block":true,"hidden":false},{"type":"block_clause_open","tag":"div","attrs":[["name","payment"]],"map":[2,4],"nesting":1,"level":1,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"paragraph_open","tag":"p","attrs":null,"map":[3,4],"nesting":1,"level":2,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"inline","tag":"","attrs":null,"map":[3,4],"nesting":0,"level":3,"children":[{"type":"text","tag":"","attrs":null,"map":null,"nesting":0,"level":0,"children":null,"content":"Payment. As consideration in full for the rights granted herein, Licensee shall pay Licensor a one-time fee in the amount of \"forty two\" (42) upon execution of this Agreement, payable as follows: \"wire transfer\".","markup":"","info":"","meta":null,"block":false,"hidden":false}],"content":"Payment. As consideration in full for the rights granted herein, Licensee shall pay Licensor a one-time fee in the amount of \"forty two\" (42) upon execution of this Agreement, payable as follows: \"wire transfer\".","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"paragraph_close","tag":"p","attrs":null,"map":null,"nesting":-1,"level":2,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"block_clause_close","tag":"div","attrs":null,"map":null,"nesting":-1,"level":1,"children":null,"content":"","markup":"{{/clause}}\n\nGeneral.\n\n","info":"","meta":null,"block":true,"hidden":false},{"type":"blockquote_close","tag":"blockquote","attrs":null,"map":null,"nesting":-1,"level":0,"children":null,"content":"","markup":">","info":"","meta":null,"block":true,"hidden":false},{"type":"paragraph_open","tag":"p","attrs":null,"map":[4,5],"nesting":1,"level":0,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"inline","tag":"","attrs":null,"map":[4,5],"nesting":0,"level":1,"children":[{"type":"text","tag":"","attrs":null,"map":null,"nesting":0,"level":0,"children":null,"content":"{{/clause}}","markup":"","info":"","meta":null,"block":false,"hidden":false}],"content":"{{/clause}}","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"paragraph_close","tag":"p","attrs":null,"map":null,"nesting":-1,"level":0,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"paragraph_open","tag":"p","attrs":null,"map":[6,7],"nesting":1,"level":0,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"inline","tag":"","attrs":null,"map":[6,7],"nesting":0,"level":1,"children":[{"type":"text","tag":"","attrs":null,"map":null,"nesting":0,"level":0,"children":null,"content":"General.","markup":"","info":"","meta":null,"block":false,"hidden":false}],"content":"General.","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"paragraph_close","tag":"p","attrs":null,"map":null,"nesting":-1,"level":0,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false}]
[{"type":"paragraph_open","tag":"p","attrs":null,"map":[0,1],"nesting":1,"level":0,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"inline","tag":"","attrs":null,"map":[0,1],"nesting":0,"level":1,"children":[{"type":"text","tag":"","attrs":null,"map":null,"nesting":0,"level":0,"children":null,"content":"Copyright Notices. Licensee shall ensure that its use of the Work is marked with the appropriate copyright notices specified by Licensor in a reasonably prominent position in the order and manner provided by Licensor. Licensee shall abide by the copyright laws and what are considered to be sound practices for copyright notice provisions in the Territory. Licensee shall not use any copyright notices that conflict with, confuse, or negate the notices Licensor provides and requires hereunder.","markup":"","info":"","meta":null,"block":false,"hidden":false}],"content":"Copyright Notices. Licensee shall ensure that its use of the Work is marked with the appropriate copyright notices specified by Licensor in a reasonably prominent position in the order and manner provided by Licensor. Licensee shall abide by the copyright laws and what are considered to be sound practices for copyright notice provisions in the Territory. Licensee shall not use any copyright notices that conflict with, confuse, or negate the notices Licensor provides and requires hereunder.","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"paragraph_close","tag":"p","attrs":null,"map":null,"nesting":-1,"level":0,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"blockquote_open","tag":"blockquote","attrs":null,"map":[2,4],"nesting":1,"level":0,"children":null,"content":"","markup":">","info":"","meta":null,"block":true,"hidden":false},{"type":"block_clause_open","tag":"div","attrs":[["name","payment"]],"map":[2,4],"nesting":1,"level":1,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"paragraph_open","tag":"p","attrs":null,"map":[3,4],"nesting":1,"level":2,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"inline","tag":"","attrs":null,"map":[3,4],"nesting":0,"level":3,"children":[{"type":"text","tag":"","attrs":null,"map":null,"nesting":0,"level":0,"children":null,"content":"Payment. As consideration in full for the rights granted herein, Licensee shall pay Licensor a one-time fee in the amount of \"forty two\" (42) upon execution of this Agreement, payable as follows: \"wire transfer\".","markup":"","info":"","meta":null,"block":false,"hidden":false}],"content":"Payment. As consideration in full for the rights granted herein, Licensee shall pay Licensor a one-time fee in the amount of \"forty two\" (42) upon execution of this Agreement, payable as follows: \"wire transfer\".","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"paragraph_close","tag":"p","attrs":null,"map":null,"nesting":-1,"level":2,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"block_clause_close","tag":"div","attrs":null,"map":null,"nesting":-1,"level":1,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"blockquote_close","tag":"blockquote","attrs":null,"map":null,"nesting":-1,"level":0,"children":null,"content":"","markup":">","info":"","meta":null,"block":true,"hidden":false},{"type":"paragraph_open","tag":"p","attrs":null,"map":[4,5],"nesting":1,"level":0,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"inline","tag":"","attrs":null,"map":[4,5],"nesting":0,"level":1,"children":[{"type":"text","tag":"","attrs":null,"map":null,"nesting":0,"level":0,"children":null,"content":"{{/clause}}","markup":"","info":"","meta":null,"block":false,"hidden":false}],"content":"{{/clause}}","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"paragraph_close","tag":"p","attrs":null,"map":null,"nesting":-1,"level":0,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"paragraph_open","tag":"p","attrs":null,"map":[6,7],"nesting":1,"level":0,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"inline","tag":"","attrs":null,"map":[6,7],"nesting":0,"level":1,"children":[{"type":"text","tag":"","attrs":null,"map":null,"nesting":0,"level":0,"children":null,"content":"General.","markup":"","info":"","meta":null,"block":false,"hidden":false}],"content":"General.","markup":"","info":"","meta":null,"block":true,"hidden":false},{"type":"paragraph_close","tag":"p","attrs":null,"map":null,"nesting":-1,"level":0,"children":null,"content":"","markup":"","info":"","meta":null,"block":true,"hidden":false}]
Loading

0 comments on commit 436aad4

Please sign in to comment.