[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/subed 8790abf 048/389: Add subed-srt-validate
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/subed 8790abf 048/389: Add subed-srt-validate |
Date: |
Fri, 3 Dec 2021 10:59:54 -0500 (EST) |
branch: elpa/subed
commit 8790abfb54806726270db3d60a5703599bbe6aed
Author: Random User <rndusr@posteo.de>
Commit: Random User <rndusr@posteo.de>
Add subed-srt-validate
Signed-off-by: Random User <rndusr@posteo.de>
---
subed/subed-srt.el | 27 +++++++++++++++++++++++++++
tests/test-subed-srt.el | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 75 insertions(+)
diff --git a/subed/subed-srt.el b/subed/subed-srt.el
index c0b05c9..a4156d3 100644
--- a/subed/subed-srt.el
+++ b/subed/subed-srt.el
@@ -492,6 +492,33 @@ each subtitle."
(when (looking-at "\n*")
(replace-match "\n"))))))
+(defun subed-srt-validate ()
+ "Move point to the first invalid subtitle and report an error."
+ (interactive)
+ (atomic-change-group
+ (save-match-data
+ (let ((orig-point (point)))
+ (goto-char (point-min))
+ (while (re-search-forward (format "\\(%s\\|\\`\\)"
subed-srt--regexp-separator) nil t)
+ (unless (looking-at "^[0-9]+$")
+ (error "Found invalid subtitle ID: %S" (substring (or
(thing-at-point 'line :no-properties) "\n") 0 -1)))
+ (forward-line)
+ ;; This regex is stricter than `subed-srt--regexp-timestamp'
+ (unless (looking-at
"^[0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\},[0-9]\\{3\\}")
+ (error "Found invalid start time: %S" (substring (or
(thing-at-point 'line :no-properties) "\n") 0 -1)))
+ (condition-case nil
+ (forward-char subed-srt--length-timestamp)
+ (error nil))
+ (unless (looking-at " --> ")
+ (error "Found invalid separator between start and stop time: %S"
+ (substring (or (thing-at-point 'line :no-properties) "\n")
0 -1)))
+ (condition-case nil
+ (forward-char 5)
+ (error nil))
+ (unless (looking-at
"[0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\},[0-9]\\{3\\}$")
+ (error "Found invalid stop time: %S" (substring (or
(thing-at-point 'line :no-properties) "\n") 0 -1))))
+ (goto-char orig-point)))))
+
(defun subed-srt-sort ()
"Sanitize, then sort subtitles by start time and re-number them."
(interactive)
diff --git a/tests/test-subed-srt.el b/tests/test-subed-srt.el
index 2eab3c8..ed197f6 100644
--- a/tests/test-subed-srt.el
+++ b/tests/test-subed-srt.el
@@ -844,6 +844,54 @@ Baz.
)
)
+(describe "Validating"
+ (it "works in empty buffer."
+ (with-temp-buffer
+ (expect (subed-srt-validate) :to-throw
+ 'error '("Found invalid subtitle ID: \"\""))))
+ (it "reports invalid IDs."
+ (with-temp-buffer
+ (insert mock-srt-data)
+ (subed-srt-move-to-subtitle-id 1)
+ (insert "x")
+ (expect (subed-srt-validate) :to-throw
+ 'error '("Found invalid subtitle ID: \"x1\""))
+ (expect (point) :to-equal 1)))
+ (it "reports invalid start time."
+ (with-temp-buffer
+ (insert mock-srt-data)
+ (subed-srt-move-to-subtitle-time-start 1)
+ (forward-char 5)
+ (delete-char 1)
+ (expect (subed-srt-validate) :to-throw
+ 'error '("Found invalid start time: \"00:0101,000 -->
00:01:05,123\""))
+ (expect (point) :to-equal 3)))
+ (it "reports invalid stop time."
+ (with-temp-buffer
+ (insert mock-srt-data)
+ (subed-srt-move-to-subtitle-time-stop 1)
+ (forward-char 10)
+ (insert "3")
+ (expect (subed-srt-validate) :to-throw
+ 'error '("Found invalid stop time: \"00:01:01,000 -->
00:01:05,1323\""))
+ (expect (point) :to-equal 20)))
+ (it "reports invalid time separator."
+ (with-temp-buffer
+ (insert mock-srt-data)
+ (subed-srt-move-to-subtitle-time-stop 1)
+ (delete-char -1)
+ (expect (subed-srt-validate) :to-throw
+ 'error '("Found invalid separator between start and
stop time: \"00:01:01,000 -->00:01:05,123\""))
+ (expect (point) :to-equal 15)))
+ (it "preserves point if there is no error."
+ (with-temp-buffer
+ (insert mock-srt-data)
+ (subed-srt-move-to-subtitle-text 2)
+ (forward-char 2)
+ (subed-srt-validate)
+ (expect (point) :to-equal 73)))
+ )
+
(describe "Sanitizing"
(it "removes trailing tabs and spaces from all lines."
(with-temp-buffer
- [nongnu] elpa/subed 6797d10 028/389: Use lexical scoping, (continued)
- [nongnu] elpa/subed 6797d10 028/389: Use lexical scoping, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 7939e0a 022/389: Move function to have same order as tests, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 31f8255 016/389: README: Turns out spaces are not the issue, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 27c8141 014/389: Some work on README, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 6b97e24 023/389: Fix test's description, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 7e04e16 013/389: Use C-u ... when inserting subtitles, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 005c0cc 027/389: Remove TODO for C-u N M-k, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed fd737b7 035/389: Use save-excursion in subed--save-excursion, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 0815e10 047/389: Simplify subed-srt--regexp-duration, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed bbd239a 042/389: subed-srt--regenerate-ids: Wrap function body in atomic-change-group, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 8790abf 048/389: Add subed-srt-validate,
ELPA Syncer <=
- [nongnu] elpa/subed 7d76a35 049/389: Simplify subed-srt-move-to-subtitle-text, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 821b259 045/389: subed-srt--regenerate-ids: Make this a public function, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed d3d2013 050/389: Look for stop time only on the relevant line, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 042941c 051/389: Fix docstrings, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 4e50796 052/389: Fix subed-srt-backward-subtitle-id, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 86914b4 053/389: Add more tests for moving to next/previous subtitle ID, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 6391996 056/389: Kill debugging buffer+window in subed-disable-debugging, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 0fe0c82 059/389: When opening debug window, always split horizontally, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 4e31010 062/389: mpv returns nil as "time-pos" in at least one circumstance, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 1c8e0d1 064/389: Sanitize separator between start and stop time, ELPA Syncer, 2021/12/03