From 479d24248a10afddffeb69eff9cf8ca4d47a73f0 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 27 Oct 2019 21:22:16 +0100 Subject: [PATCH] Updating subtitle.js --- Kyoo/ClientApp/src/libraries/subtitles.js | 55 +++++++++++++++++------ Kyoo/Controllers/AdminController.cs | 1 - Kyoo/InternalAPI/Crawler/Crawler.cs | 22 ++++++--- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/Kyoo/ClientApp/src/libraries/subtitles.js b/Kyoo/ClientApp/src/libraries/subtitles.js index 06aeb3fa..e7d48f5d 100644 --- a/Kyoo/ClientApp/src/libraries/subtitles.js +++ b/Kyoo/ClientApp/src/libraries/subtitles.js @@ -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); @@ -2750,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. @@ -2793,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]; } diff --git a/Kyoo/Controllers/AdminController.cs b/Kyoo/Controllers/AdminController.cs index a3f5b8c0..976f9140 100644 --- a/Kyoo/Controllers/AdminController.cs +++ b/Kyoo/Controllers/AdminController.cs @@ -22,7 +22,6 @@ namespace Kyoo.Controllers public IActionResult ScanLibrary(bool watch) { crawler.Start(watch); - return Ok("Scanning"); } } diff --git a/Kyoo/InternalAPI/Crawler/Crawler.cs b/Kyoo/InternalAPI/Crawler/Crawler.cs index cc6f46a3..13eff220 100644 --- a/Kyoo/InternalAPI/Crawler/Crawler.cs +++ b/Kyoo/InternalAPI/Crawler/Crawler.cs @@ -14,6 +14,8 @@ namespace Kyoo.InternalAPI { public class Crawler : ICrawler { + public bool IsScanning = false; + private static ICrawler runningCrawler; private readonly CancellationTokenSource cancellation; @@ -27,19 +29,27 @@ namespace Kyoo.InternalAPI this.libraryManager = libraryManager; this.metadataProvider = metadataProvider; this.transcoder = transcoder; - this.config = configuration; - + config = configuration; cancellation = new CancellationTokenSource(); } - public Task Start(bool watch) + public async Task Start(bool watch) { if (runningCrawler == null) { runningCrawler = this; - return StartAsync(watch, cancellation.Token); + await StartAsync(watch, cancellation.Token); + } + else if (runningCrawler is Crawler) + { + Crawler crawler = (Crawler)runningCrawler; + if (!crawler.IsScanning) + { + await crawler.StopAsync(); + runningCrawler = this; + await StartAsync(watch, cancellation.Token); + } } - return null; } private Task StartAsync(bool watch, CancellationToken cancellationToken) @@ -47,6 +57,7 @@ namespace Kyoo.InternalAPI IEnumerable episodes = libraryManager.GetAllEpisodes(); IEnumerable libraryPaths = libraryManager.GetLibrariesPath(); + IsScanning = true; Debug.WriteLine("&Crawler started"); foreach (Episode episode in episodes) { @@ -62,6 +73,7 @@ namespace Kyoo.InternalAPI Watch(path, cancellationToken); } + IsScanning = false; while (!cancellationToken.IsCancellationRequested); Debug.WriteLine("&Crawler stopped"); runningCrawler = null;