Commit 10f4a1a7 authored by Guillaume Lebigot's avatar Guillaume Lebigot

Merge remote-tracking branch 'upstream/master'

parents 1c172e7c 831c2a03
Pipeline #8684 passed with stage
in 30 seconds
......@@ -2275,9 +2275,6 @@ let SubtitleManager = (function() {
if (hasLineBreaks || hasProblematicOverride) {
// Split on newlines, then into block-text pairs, then split the pair.
let lines = data.Text.split(/\\[Nn]/g).map(x => combineAdjacentBlocks("{}"+x).split("{").slice(1).map(y => y.split("}")));
// Merge subtitle line pieces into non-problematic strings. Each piece can still have more
// than one block in it, but problematic blocks will start a new piece. For example, a block
// that is scaled differently will start a piece and every other block in that piece must have
......@@ -2288,11 +2285,32 @@ let SubtitleManager = (function() {
// transitions, every block after a transition will have to be split on, even if it doesn't
// itself contain a transition.
let megablock = "{";
for (let line of lines) {
for (let line of data.Text.split(/\\[Nn]/g)) {
// Remove leading and trailing whitespace.
let lastSeenText = '';
line = combineAdjacentBlocks("{}" + line.replace(/([^{]*)(?:{[^\\]*([^}]*)})?/g, (match,text,overrides) => {
// If there is no text or overrides, this is the end of the line.
if (!text && !overrides) return "";
// If we haven't seen any text yet, remove whitespace from the left.
if (!lastSeenText) text = text.replace(/^[^\S\xA0]+/,"");
lastSeenText = text;
// If there was no override block, `overrides` will be undefined.
// Otherwise it will be an empty string.
return text + (overrides === undefined ? "" : "{" + overrides + "}");
}));
// Trim everything to the right of the last text we saw.
if (lastSeenText) {
lastSeenText = lastSeenText.replace(/[^\S\xA0]+$/,"");
let i = line.lastIndexOf(lastSeenText);
line = line.slice(0, i + lastSeenText.length);
}
let currblock = "", pieces = [], currPiece = "", hasTransition = false;
// Loop through line pieces checking which ones need to be separated.
for (let [overrides,text] of line) {
for (let [overrides,text] of line.split("{").slice(1).map(y => y.split("}"))) {
// if we need to start a new piece
if (currPiece && (hasTransition || reProblem.test(overrides))) {
hasTransition = hasTransition || /\\t(?!e)/.test(overrides);
......@@ -2311,9 +2329,6 @@ let SubtitleManager = (function() {
megablock += currblock;
}
// Remove pieces that have no text.
pieces = pieces.filter(piece => piece.replace(/{[^}]*}/g,""));
// Convert piece text into a NewLinePiece.
let pieceNum = 0, newLine = pieces.map(piece => NewLinePiece(this, combineAdjacentBlocks(piece), ++pieceNum));
newLine.shattered = false;
......@@ -2753,16 +2768,18 @@ let SubtitleManager = (function() {
if (map[j] == "Text") new_event.Text = elems.slice(j).join(",").trim();
else continue;
// Remove all overrides from the Text and check if there's anything left.
// If there isn't, there's no reason to add the line.
if (!new_event.Text.replace(/{[^}]*}/g,""))
continue;
// Remove leading and trailing whitespace and fix some issues that could exist in the override blocks.
let last_seen_text = '', pathVal = 0;
new_event.Text = combineAdjacentBlocks(new_event.Text.replace(/([^{]*)(?:{[^\\]*([^}]*)})?/g, (match,text,overrides) => {
// If there is no text or overrides, this is the end of the line.
if (!text && !overrides) return "";
// If we haven't seen any text yet, remove whitespace from the left.
if (!last_seen_text) text = text.replace(/^\s+/,"");
// Fix some issues that could exist in the override blocks.
let pathVal = 0;
new_event.Text = combineAdjacentBlocks(new_event.Text).replace(/([^{]*)(?:{[^\\]*([^}]*)})?/g, (match,text,overrides) => {
// Replace '\h' in the text with the non-breaking space.
text = text.replace(/\\h/g,"\xA0");
last_seen_text = text;
// If there are no overrides, we can return here.
// If there was no override block, `overrides` will be undefined.
......@@ -2796,9 +2813,16 @@ let SubtitleManager = (function() {
if (pathVal) overrides += `\\p${pathVal}`;
return text + "{" + overrides + "}";
});
events.push(new_event);
}));
// Only add the line if we actually saw any text in it.
if (last_seen_text) {
// But first trim everything to the right of the last text we saw.
last_seen_text = last_seen_text.replace(/[^\S\xA0]+$/,"");
let i = new_event.Text.lastIndexOf(last_seen_text);
new_event.Text = new_event.Text.slice(0, i + last_seen_text.length);
events.push(new_event);
}
}
return [events,i-1];
}
......
......@@ -368,6 +368,6 @@ def convert(lines, offset=0):
if __name__ == '__main__':
if len(sys.argv) > 1:
with open(sys.argv[1], encoding='utf8') as file, open(1, 'w', encoding='utf8', closefd=False) as stdout:
with open(sys.argv[1], encoding='utf-8-sig') as file, open(1, 'w', encoding='utf8', closefd=False) as stdout:
for line in convert([line.strip() for line in file if line], int(sys.argv[2]) if len(sys.argv) > 2 else 0):
print(line, file=stdout)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment