From d923f047cd55623df2ea73b265bfb15214086a99 Mon Sep 17 00:00:00 2001 From: Guangcong Luo Date: Thu, 25 Jun 2015 18:02:31 -0400 Subject: [PATCH] Improve spoiler tag support Putting parentheses like this: (spoiler: test) now allows you to designate a part of a line to be spoilered, instead of spoilering everything to end-of-line. Yes, it's also possible to have multiple spoilers on a line. As a side effect, this also fixes the issue where spoilers messed up modlogs by restricting them to one line. --- js/battledata.js | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/js/battledata.js b/js/battledata.js index 8d69f0c01..8f8ae6e8a 100644 --- a/js/battledata.js +++ b/js/battledata.js @@ -439,13 +439,37 @@ var Tools = { options.hidebold ? '$1' : '$1'); if (!options.hidespoiler) { - var spoilerIndex = str.toLowerCase().indexOf('spoiler:'); - if (spoilerIndex < 0) spoilerIndex = str.toLowerCase().indexOf('spoilers:'); - if (spoilerIndex >= 0) { - var offset = spoilerIndex+8; - if (str.charAt(offset) === ':') offset++; - if (str.charAt(offset) === ' ') offset++; - str = str.substr(0, offset)+''+str.substr(offset)+''; + var untilIndex = 0; + while (untilIndex < str.length) { + var spoilerIndex = str.toLowerCase().indexOf('spoiler:', untilIndex); + if (spoilerIndex < 0) spoilerIndex = str.toLowerCase().indexOf('spoilers:', untilIndex); + if (spoilerIndex >= 0) { + untilIndex = str.indexOf("\n", spoilerIndex); + if (untilIndex < 0) untilIndex = str.length; + + if (str.charAt(spoilerIndex - 1) === '(') { + var nextLParenIndex = str.indexOf('(', spoilerIndex); + var nextRParenIndex = str.indexOf(')', spoilerIndex); + if (nextRParenIndex < 0 || nextRParenIndex >= untilIndex) { + // no `)`, keep spoilering until next newline + } else if (nextLParenIndex < 0 || nextLParenIndex > nextRParenIndex) { + // no `(` before next `)` - spoiler until next `)` + untilIndex = nextRParenIndex; + } else { + // `(` before next `)` - just spoiler until the last `)` + untilIndex = str.lastIndexOf(')', untilIndex); + if (untilIndex < 0) untilIndex = str.length; // should never happen + } + } + + var offset = spoilerIndex + 8; + if (str.charAt(offset) === ':') offset++; + if (str.charAt(offset) === ' ') offset++; + str = str.slice(0, offset)+''+str.slice(offset, untilIndex)+''+str.slice(untilIndex); + untilIndex += 29; + } else { + break; + } } }