diff --git a/src/js/crossword_parser.js b/src/js/crossword_parser.js index fc9c508..2cc20c9 100644 --- a/src/js/crossword_parser.js +++ b/src/js/crossword_parser.js @@ -232,11 +232,11 @@ var knownIds = {}; crossword.knownIds = knownIds; var maxId = 0; - var answers = { + + crossword.answers = { across : [], down : [] }; - crossword.answers = answers; for(let grouping of ['across', 'down']){ let prev = groupingPrev[grouping]; @@ -287,6 +287,18 @@ // check answer within bounds // and unpack the answerCSV + // convert "ANSWER,PARTS-INTO,NUMBERS" into number csv e.g. "6,5-4,6" (etc) + if ( /^[A-Z,\-]+$/.test(clue.answerCSV) ) { + clue.numericCSV = clue.answerCSV.replace(/[A-Z]+/g, match => {return match.length.toString() } ); + } else { + clue.numericCSV = clue.answerCSV; + } + + // and if the answer is solely Xs, replace that with the number csv + if ( /^[X,\-]+$/.test(clue.answerCSV) ) { + clue.answerCSV = clue.numericCSV; + } + let answerPieces = clue.answerCSV.split(/[,-]/); let words = answerPieces.map(p => { if (/^[0-9]+$/.test(p)) { @@ -308,7 +320,7 @@ clueError("answer too long for crossword"); break; } - answers[grouping].push(wordsString); + crossword.answers[grouping].push(wordsString); clue.wordsLengths = words.map(function(w){ return w.length; @@ -494,13 +506,23 @@ crossword[grouping].forEach( function(clue) { let item = [ parseInt(clue.id), - clue.body, + clue.body + ' (' + clue.numericCSV + ')', clue.wordsLengths, ]; spec.clues[grouping].push(item); }); }); + { + // if the answers are just placeholders (lots of Xs) + // assume they are not to be displayed, + // so delete them from the spec + let concatAllAnswerWordsStrings = spec.answers.across.join('') + spec.answers.down.join(''); + if ( /^X+$/.test(concatAllAnswerWordsStrings) ) { + delete spec['answers']; + } + } + return spec; } diff --git a/src/js/oCrossword.js b/src/js/oCrossword.js index 934298e..4ac1576 100644 --- a/src/js/oCrossword.js +++ b/src/js/oCrossword.js @@ -64,7 +64,7 @@ function buildGrid( const tempLi = document.createElement('li'); const tempSpan = document.createElement('span'); const answerLength = across[2].filter(isFinite).filter(isFinite).reduce((a,b)=>a+b,0); - tempSpan.textContent = across[0] + '. ' + across[1] + ` (${across[2].filter(isFinite).join(', ')})`; + tempSpan.textContent = across[0] + '. ' + across[1]; tempLi.dataset.oCrosswordNumber = across[0]; tempLi.dataset.oCrosswordAnswerLength = answerLength; tempLi.dataset.oCrosswordDirection = 'across'; @@ -76,7 +76,7 @@ function buildGrid( const tempLi = document.createElement('li'); const tempSpan = document.createElement('span'); const answerLength = down[2].filter(isFinite).filter(isFinite).reduce((a,b)=>a+b,0); - tempSpan.textContent = down[0] + '. ' + down[1] + ` (${down[2].filter(isFinite).join(', ')})`; + tempSpan.textContent = down[0] + '. ' + down[1]; tempLi.dataset.oCrosswordNumber = down[0]; tempLi.dataset.oCrosswordAnswerLength = answerLength; tempLi.dataset.oCrosswordDirection = 'down';