emacs-elpa-diffs
[Top][All Lists]
Advanced

[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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]