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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/subed 949a66ae6b 3/5: Move validation and sanitizing funct


From: ELPA Syncer
Subject: [nongnu] elpa/subed 949a66ae6b 3/5: Move validation and sanitizing functions to hooks
Date: Thu, 30 Dec 2021 03:58:47 -0500 (EST)

branch: elpa/subed
commit 949a66ae6b943ad39abdd86fc5c2755ff79a5777
Author: Sacha Chua <sacha@sachachua.com>
Commit: Sacha Chua <sacha@sachachua.com>

    Move validation and sanitizing functions to hooks
    
    * subed/subed-common.el (subed-sanitize-functions): New hook.
    (subed-validate-functions): New hook.
    (subed-prepare-to-save): New function for use in before-save-hook.
    (subed-sort): Extracted into its own generic function.
    * subed/subed-srt.el (subed-srt--init): Add validation and sanitizing
    functions. Use specific functions so that they're easier to override,
    starting the migration towards non-defalias-based mode-specific
    functions.
    * subed/subed-vtt.el (subed-vtt--init): Add validation and sanitizing
    functions. Use specific functions so that they're easier to override,
    starting the migration towards non-defalias-based mode-specific
    functions.
    * subed/subed.el (subed-mode): Use subed-prepare-for-save in the 
before-save-hook.
    * tests/test-subed-srt.el ("Validating"): Test that validation runs
    before save.
    ("Sanitizing"): Test that sanitizing runs before save.
    ("Renumbering"): Test that IDs are regenerated before save.
    * tests/test-subed-vtt.el ("VTT"): Test that validation and
    sanitizing runs before save.
---
 subed/subed-common.el   | 32 ++++++++++++++++++++++++++++++++
 subed/subed-srt.el      |  5 ++++-
 subed/subed-vtt.el      |  4 +++-
 subed/subed.el          |  2 +-
 tests/test-subed-srt.el | 33 +++++++++++++++++++++++++++++++--
 tests/test-subed-vtt.el | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 118 insertions(+), 5 deletions(-)

diff --git a/subed/subed-common.el b/subed/subed-common.el
index b08ea66bba..f1640117b1 100644
--- a/subed/subed-common.el
+++ b/subed/subed-common.el
@@ -1313,5 +1313,37 @@ be run in `after-change-functions'."
          'after-string
          (propertize (format " %.1f CPS" cps) 'face 'shadow 'display '(height 
0.9)))))))
 
+;;; Sorting and sanitizing
+
+(defvar-local subed-sanitize-functions nil
+  "Functions to sanitize this buffer.
+Functions can clean up whitespace, rearrange subtitles, etc.")
+
+(defvar-local subed-validate-functions nil
+  "Functions to validate this buffer.
+Validation functions should throw an error or prompt the user for
+action.")
+
+(defun subed-prepare-to-save ()
+  "Sanitize and validate this buffer."
+  (interactive)
+  (atomic-change-group
+    (run-hooks 'subed-sanitize-functions)
+    (run-hooks 'subed-validate-functions)))
+
+(defun subed-sort ()
+  "Sort subtitles."
+  (subed-save-excursion
+   (goto-char (point-min))
+   (sort-subr nil
+              ;; nextrecfun (move to next record/subtitle or to end-of-buffer
+              ;; if there are no more records)
+              (lambda () (unless (subed-forward-subtitle-id)
+                           (goto-char (point-max))))
+              ;; endrecfun (move to end of current record/subtitle)
+              #'subed-jump-to-subtitle-end
+              ;; startkeyfun (return sort value of current record/subtitle)
+              #'subed-subtitle-msecs-start)))
+
 (provide 'subed-common)
 ;;; subed-common.el ends here
diff --git a/subed/subed-srt.el b/subed/subed-srt.el
index 0341a92fb9..3a7989c735 100644
--- a/subed/subed-srt.el
+++ b/subed/subed-srt.el
@@ -588,7 +588,10 @@ scheduled call is canceled and another call is scheduled in
                                         ;; a "-" at the start of the line.
                                         (mapconcat 'identity '("^-"
                                                                
"[[:graph:]]*$") "\\|")
-                                        "\\)"))))
+                                        "\\)")))
+  (add-hook 'subed-sanitize-functions #'subed-srt--sort nil t)
+  (add-hook 'subed-sanitize-functions #'subed-srt--regenerate-ids t t)
+  (add-hook 'subed-validate-functions #'subed-srt--validate t t))
 
 (provide 'subed-srt)
 ;;; subed-srt.el ends here
diff --git a/subed/subed-vtt.el b/subed/subed-vtt.el
index 64848864cc..d87fcd09aa 100644
--- a/subed/subed-vtt.el
+++ b/subed/subed-vtt.el
@@ -557,7 +557,9 @@ Update the end timestamp accordingly."
                                         ;; a "-" at the start of the line.
                                         (mapconcat 'identity '("^-"
                                                                
"[[:graph:]]*$") "\\|")
-                                        "\\)"))))
+                                        "\\)"))
+    (add-hook 'subed-sanitize-functions #'subed-vtt--sort nil t)
+    (add-hook 'subed-validate-functions #'subed-vtt--validate t t)))
 
 (provide 'subed-vtt)
 ;;; subed-vtt.el ends here
diff --git a/subed/subed.el b/subed/subed.el
index cdb9842920..119c4249c2 100644
--- a/subed/subed.el
+++ b/subed/subed.el
@@ -193,7 +193,7 @@ Key bindings:
   :group 'subed
   (subed--init)
   (add-hook 'post-command-hook #'subed--post-command-handler :append :local)
-  (add-hook 'before-save-hook #'subed-sort :append :local)
+  (add-hook 'before-save-hook #'subed-prepare-for-save :append :local)
   (add-hook 'after-save-hook #'subed-mpv-reload-subtitles :append :local)
   (add-hook 'kill-buffer-hook #'subed-mpv-kill :append :local)
   (add-hook 'kill-emacs-hook #'subed-mpv-kill :append :local)
diff --git a/tests/test-subed-srt.el b/tests/test-subed-srt.el
index e6447219d2..63a9158ddc 100644
--- a/tests/test-subed-srt.el
+++ b/tests/test-subed-srt.el
@@ -1263,7 +1263,15 @@ Baz.
       (forward-char 2)
       (subed-srt--validate)
       (expect (point) :to-equal 73)))
-  )
+  (it "runs before saving."
+    (with-temp-srt-buffer
+     (insert mock-srt-data)
+     (subed-srt--jump-to-subtitle-time-start 3)
+     (forward-char 3)
+     (insert "##")
+     (expect (subed-prepare-to-save) :to-throw
+             'error '("Found invalid start time: \"00:##03:03,45 --> 
00:03:15,5\""))
+     (expect (point) :to-equal 79))))
 
 (describe "Sanitizing"
   (it "removes trailing tabs and spaces from all lines."
@@ -1400,7 +1408,19 @@ Baz.
       (expect (buffer-string) :to-equal "")
       (subed-srt--sanitize)
       (expect (buffer-string) :to-equal "")))
-  )
+  (it "runs before saving."
+    (with-temp-srt-buffer
+     (insert mock-srt-data)
+     (goto-char (point-min))
+     (re-search-forward " --> ")
+     (replace-match "  --> ")
+     (re-search-forward " --> ")
+     (replace-match " -->  ")
+     (re-search-forward " --> ")
+     (replace-match "-->")
+     (expect (buffer-string) :not :to-equal mock-srt-data)
+     (subed-prepare-to-save)
+     (expect (buffer-string) :to-equal mock-srt-data))))
 
 (describe "Renumbering"
   (it "ensures consecutive subtitle IDs."
@@ -1412,6 +1432,15 @@ Baz.
       (expect (buffer-string) :not :to-equal mock-srt-data)
       (subed-srt--regenerate-ids)
       (expect (buffer-string) :to-equal mock-srt-data)))
+  (it "runs before saving."
+    (with-temp-srt-buffer
+      (insert mock-srt-data)
+      (goto-char (point-min))
+      (while (looking-at "^[0-9]$")
+        (replace-match "123"))
+      (expect (buffer-string) :not :to-equal mock-srt-data)
+      (subed-prepare-to-save)
+      (expect (buffer-string) :to-equal mock-srt-data)))
   (it "does not modify the kill-ring."
     (with-temp-srt-buffer
       (insert mock-srt-data)
diff --git a/tests/test-subed-vtt.el b/tests/test-subed-vtt.el
index 7c7f9628d8..a044b8dafa 100644
--- a/tests/test-subed-vtt.el
+++ b/tests/test-subed-vtt.el
@@ -955,6 +955,15 @@ Baz.
        (expect (subed-vtt--validate) :to-throw
                'error '("Found invalid stop time: \"00:01:01.000 --> 
00:01:05.1323\""))
        (expect (point) :to-equal 26)))
+    (it "runs before saving."
+      (with-temp-vtt-buffer
+       (insert mock-vtt-data)
+       (subed-vtt--jump-to-subtitle-time-stop "00:01:01.000")
+       (forward-char 10)
+       (insert "3")
+       (expect (subed-prepare-to-save) :to-throw
+               'error '("Found invalid stop time: \"00:01:01.000 --> 
00:01:05.1323\""))
+       (expect (point) :to-equal 26)))
     (it "reports invalid time separator."
       (with-temp-vtt-buffer
        (insert mock-vtt-data)
@@ -1095,6 +1104,19 @@ Baz.
        (expect (buffer-string) :not :to-equal mock-vtt-data)
        (subed-vtt--sanitize)
        (expect (buffer-string) :to-equal mock-vtt-data)))
+    (it "runs before saving."
+      (with-temp-vtt-buffer
+       (insert mock-vtt-data)
+       (goto-char (point-min))
+       (re-search-forward " --> ")
+       (replace-match "  --> ")
+       (re-search-forward " --> ")
+       (replace-match " -->  ")
+       (re-search-forward " --> ")
+       (replace-match "-->")
+       (expect (buffer-string) :not :to-equal mock-vtt-data)
+       (subed-prepare-to-save)
+       (expect (buffer-string) :to-equal mock-vtt-data)))
     (it "does not insert newline in empty buffer."
       (with-temp-vtt-buffer
        (expect (buffer-string) :to-equal "")
@@ -1129,6 +1151,31 @@ Baz.
                 "\n"
                 "00:12:01.000 --> 00:01:05.123\n"
                 "Foo.\n"))))
+    (it "runs before saving."
+      (with-temp-vtt-buffer
+       (insert mock-vtt-data)
+       (goto-char (point-min))
+       (re-search-forward "01:01")
+       (replace-match "12:01")
+       (goto-char (point-min))
+       (re-search-forward "02:02")
+       (replace-match "10:02")
+       (goto-char (point-min))
+       (re-search-forward "03:03")
+       (replace-match "11:03")
+       (subed-prepare-to-save)
+       (expect (buffer-string) :to-equal
+               (concat
+                "WEBVTT\n"
+                "\n"
+                "00:10:02.234 --> 00:02:10.345\n"
+                "Bar.\n"
+                "\n"
+                "00:11:03.45 --> 00:03:15.5\n"
+                "Baz.\n"
+                "\n"
+                "00:12:01.000 --> 00:01:05.123\n"
+                "Foo.\n"))))
     (describe "preserves point in the current subtitle"
       (it "when subtitle text is non-empty."
         (with-temp-vtt-buffer



reply via email to

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