|
| 1 | +using System.Text.Json; |
| 2 | +using System.Text.Json.Serialization; |
| 3 | +using System.Text.RegularExpressions; |
| 4 | +using SubtitleEdit.Plugins.Haxor; |
| 5 | + |
| 6 | +// A Subtitle Edit 5 plugin is just an executable: |
| 7 | +// 1. read the request file (its path is the first command-line argument), |
| 8 | +// 2. transform the subtitle, |
| 9 | +// 3. write the response file (path is given in the request), |
| 10 | +// 4. exit with code 0. |
| 11 | + |
| 12 | +if (args.Length < 1) |
| 13 | +{ |
| 14 | + Console.Error.WriteLine("Usage: Haxor <requestFilePath>"); |
| 15 | + return 1; |
| 16 | +} |
| 17 | + |
| 18 | +var jsonOptions = new JsonSerializerOptions |
| 19 | +{ |
| 20 | + PropertyNameCaseInsensitive = true, |
| 21 | + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| 22 | + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| 23 | + WriteIndented = true, |
| 24 | +}; |
| 25 | + |
| 26 | +PluginRequest? request; |
| 27 | +try |
| 28 | +{ |
| 29 | + request = JsonSerializer.Deserialize<PluginRequest>(File.ReadAllText(args[0]), jsonOptions); |
| 30 | +} |
| 31 | +catch (Exception exception) |
| 32 | +{ |
| 33 | + Console.Error.WriteLine("Could not read request: " + exception.Message); |
| 34 | + return 1; |
| 35 | +} |
| 36 | + |
| 37 | +if (request is null || string.IsNullOrEmpty(request.ResponseFilePath)) |
| 38 | +{ |
| 39 | + Console.Error.WriteLine("Invalid request."); |
| 40 | + return 1; |
| 41 | +} |
| 42 | + |
| 43 | +// Work on the SubRip representation - it is always provided in the request. |
| 44 | +var srt = request.Subtitle.SubRip; |
| 45 | +var selected = new HashSet<int>(request.SelectedIndices); |
| 46 | + |
| 47 | +var count = 0; |
| 48 | +var blocks = Regex.Split(srt.Replace("\r\n", "\n").Trim('\n'), @"\n[ \t]*\n"); |
| 49 | +for (var i = 0; i < blocks.Length; i++) |
| 50 | +{ |
| 51 | + // An empty SelectedIndices means "apply to every line". |
| 52 | + if (selected.Count > 0 && !selected.Contains(i)) |
| 53 | + { |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + // A SubRip block is: number line, timecode line, then one or more text lines. |
| 58 | + var lines = blocks[i].Split('\n'); |
| 59 | + if (lines.Length < 3) |
| 60 | + { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + for (var t = 2; t < lines.Length; t++) |
| 65 | + { |
| 66 | + lines[t] = HaxorTranslator.Translate(lines[t]); |
| 67 | + } |
| 68 | + |
| 69 | + blocks[i] = string.Join('\n', lines); |
| 70 | + count++; |
| 71 | +} |
| 72 | + |
| 73 | +var response = new PluginResponse |
| 74 | +{ |
| 75 | + Status = "ok", |
| 76 | + Message = count == 0 ? "No lines changed." : $"Translated {count} line(s) to haxor.", |
| 77 | + UndoDescription = "Haxor 1.0.0", |
| 78 | + Subtitle = new PluginSubtitle |
| 79 | + { |
| 80 | + Format = "SubRip", |
| 81 | + Native = (string.Join("\n\n", blocks) + "\n").Replace("\n", "\r\n"), |
| 82 | + }, |
| 83 | +}; |
| 84 | + |
| 85 | +File.WriteAllText(request.ResponseFilePath, JsonSerializer.Serialize(response, jsonOptions)); |
| 86 | +return 0; |
0 commit comments