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

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

[nongnu] elpa/subed da55592ee2 3/9: subed-subtitle-comment now returns n


From: ELPA Syncer
Subject: [nongnu] elpa/subed da55592ee2 3/9: subed-subtitle-comment now returns nil when there's no comment
Date: Tue, 19 Dec 2023 13:00:37 -0500 (EST)

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

    subed-subtitle-comment now returns nil when there's no comment
    
    * subed/subed-align.el (subed-align): Add format to docstring. Return
    new filename.
    (subed-align-reinsert-comments): Use subed-set-subtitle-comment.
    * subed/subed-common.el (subtitle-comment): Return nil if none.
    (subed-subtitle-list-text): Add a line after the comment.
    * subed/subed-vtt.el (subed--subtitle-comment): Return nil if none.
    Don't include NOTE or the final newline.
    * tests/test-subed-common.el: Update tests for comments.
    * tests/test-subed-vtt.el: Add tests for creating with comments.
    ("VTT"): Update test to reflect that comments are nil when they don't
    exist, and that comments don't include NOTE.
---
 subed/subed-align.el       | 25 ++++++++++++++-----------
 subed/subed-common.el      |  3 ++-
 subed/subed-vtt.el         |  8 +++++---
 tests/test-subed-common.el |  8 ++++----
 tests/test-subed-vtt.el    | 32 +++++++++++++++++++++-----------
 5 files changed, 46 insertions(+), 30 deletions(-)

diff --git a/subed/subed-align.el b/subed/subed-align.el
index 332d4af348..87f23d8525 100644
--- a/subed/subed-align.el
+++ b/subed/subed-align.el
@@ -43,8 +43,8 @@ will remove silence and other non-speech spans.")
 
 ;;;###autoload
 (defun subed-align (audio-file text-file format)
-  "Align AUDIO-FILE with TEXT-FILE to get timestamps.
-Return a buffer with FORMAT."
+  "Align AUDIO-FILE with TEXT-FILE to get timestamps in FORMAT.
+Return the new filename."
   (interactive
    (list
     (or
@@ -85,14 +85,17 @@ Return a buffer with FORMAT."
                              (if subed-align-options (concat "|" 
subed-align-options) ""))
                      new-file)))
       (when temp-file (delete-file temp-file))
-      (find-file new-file)
-      (when (derived-mode-p 'subed-mode)
-        (subed-trim-overlaps))
-      (when (derived-mode-p 'subed-vtt-mode)
-        (goto-char (point-min))
-        (flush-lines "^[0-9]+$")
-        ;; reinsert comments
-        (subed-align-reinsert-comments subtitles)))))
+      (with-temp-file new-file
+        (insert-file-contents new-file)
+        (subed-guess-format new-file)
+        (when (derived-mode-p 'subed-mode)
+          (subed-trim-overlaps))
+        (when (derived-mode-p 'subed-vtt-mode)
+          (goto-char (point-min))
+          (flush-lines "^[0-9]+$")
+          ;; reinsert comments
+          (subed-align-reinsert-comments subtitles)))
+      new-file)))
 
 (defun subed-align-reinsert-comments (subtitles)
   "Reinsert the comments from SUBTITLES.
@@ -102,7 +105,7 @@ Assume that the subtitles are still in the same sequence."
    (lambda (sub)
      (subed-forward-subtitle-time-start)
      (when (elt sub 4)
-       (insert (elt sub 4))))
+       (subed-set-subtitle-comment (elt sub 4))))
    subtitles))
 
 (provide 'subed-align)
diff --git a/subed/subed-common.el b/subed/subed-common.el
index e0217746ac..453106f3eb 100644
--- a/subed/subed-common.el
+++ b/subed/subed-common.el
@@ -406,7 +406,7 @@ If SUB-ID is not given, set the text of the current 
subtitle."
       (- (point) start-point))))
 
 (subed-define-generic-function subtitle-comment (&optional _)
-  "Return subtitle comment or an empty string."
+  "Return subtitle comment or nil if none."
   nil)
 
 (subed-define-generic-function set-subtitle-comment (comment &optional _)
@@ -676,6 +676,7 @@ before including them."
                  (if (functionp include-comments)
                      (funcall include-comments (elt sub 4))
                    (elt sub 4))
+                 "\n"
                  (elt sub 3) "\n")
        (concat (elt sub 3) "\n")))
    subtitles
diff --git a/subed/subed-vtt.el b/subed/subed-vtt.el
index 45de9b99cd..2c550367d2 100644
--- a/subed/subed-vtt.el
+++ b/subed/subed-vtt.el
@@ -261,14 +261,16 @@ Use the format-specific function for MAJOR-MODE."
 
 (cl-defmethod subed--subtitle-comment (&context (major-mode subed-vtt-mode)
                                                 &optional sub-id)
-  "Return subtitle comment or an empty string.
+  "Return subtitle comment or nil if none.
 If SUB-ID is not given, use the subtitle on point.
 Use the format-specific function for MAJOR-MODE."
   (save-excursion
     (let ((comment-start (subed-jump-to-subtitle-comment sub-id)))
       (if comment-start
-          (buffer-substring comment-start (subed-jump-to-subtitle-id sub-id))
-        ""))))
+          (string-trim
+           (replace-regexp-in-string "^NOTE[ \n]+" ""
+                                     (buffer-substring comment-start 
(subed-jump-to-subtitle-id sub-id))))
+        nil))))
 
 (cl-defmethod subed--set-subtitle-comment (comment &context (major-mode 
subed-vtt-mode)
                                                    &optional sub-id)
diff --git a/tests/test-subed-common.el b/tests/test-subed-common.el
index 878baa04f8..a875f74798 100644
--- a/tests/test-subed-common.el
+++ b/tests/test-subed-common.el
@@ -3455,16 +3455,16 @@ This is another.
       (expect
        (subed-subtitle-list-text
         '((nil 0 99 "Hello")
-          (nil 100 199 "world" "NOTE Comment\n"))
+          (nil 100 199 "world" "Comment"))
         t)
-       :to-equal "Hello\n\nNOTE Comment\nworld\n"))
+       :to-equal "Hello\n\nComment\nworld\n"))
     (it "includes comments transformed by a function."
       (let ((val
              (subed-subtitle-list-text
               '((nil 0 99 "Hello")
-                (nil 100 199 "world" "NOTE Comment\n"))
+                (nil 100 199 "world" "Comment"))
               #'upcase)))
-        (expect val :to-equal "Hello\n\nNOTE COMMENT\nworld\n"))))
+        (expect val :to-equal "Hello\n\nCOMMENT\nworld\n"))))
   (describe "Copying region text"
     (it "works on the whole buffer"
       (with-temp-srt-buffer
diff --git a/tests/test-subed-vtt.el b/tests/test-subed-vtt.el
index 159443b84a..c0193ad3df 100644
--- a/tests/test-subed-vtt.el
+++ b/tests/test-subed-vtt.el
@@ -882,30 +882,40 @@ Subtitle 2")
                                                      "Foo. bar\nbaz.\n"))
            (expect (point) :to-equal 31)))
         )
-      (describe "after"
-        (it "passing nothing."
+      (describe "when appending"
+        (it "creates a subtitle with default arguments."
           (with-temp-vtt-buffer
            (expect (subed-append-subtitle) :to-equal 31)
            (expect (buffer-string) :to-equal (concat "00:00:00.000 --> 
00:00:01.000\n\n"))
            (expect (point) :to-equal 31)))
-        (it "passing start time."
+        (it "creates a subtitle with a start time."
           (with-temp-vtt-buffer
            (expect (subed-append-subtitle nil 60000) :to-equal 31)
            (expect (buffer-string) :to-equal (concat "00:01:00.000 --> 
00:01:01.000\n\n"))
            (expect (point) :to-equal 31)))
-        (it "passing start time and stop time."
+        (it "creates a subtitle with a start time and stop time."
           (with-temp-vtt-buffer
            (expect (subed-append-subtitle nil 60000 65000) :to-equal 31)
            (expect (buffer-string) :to-equal (concat "00:01:00.000 --> 
00:01:05.000\n\n"))
            (expect (point) :to-equal 31)))
-        (it "passing start time, stop time and text."
+        (it "creates a subtitle with a start time, stop time and text."
           (with-temp-vtt-buffer
            (expect (subed-append-subtitle nil 60000 65000 "Foo, bar\nbaz.") 
:to-equal 31)
            (expect (buffer-string) :to-equal (concat "00:01:00.000 --> 
00:01:05.000\n"
                                                      "Foo, bar\nbaz.\n"))
            (expect (point) :to-equal 31)))
-        )
-      )
+        (it "creates a subtitle with a start time, stop time, text, and a 
single-line comment."
+          (with-temp-vtt-buffer
+           (subed-append-subtitle nil 60000 65000 "Foo, bar\nbaz." "Hello")
+           (expect (buffer-string) :to-equal (concat "NOTE 
Hello\n\n00:01:00.000 --> 00:01:05.000\n"
+                                                     "Foo, bar\nbaz.\n"))
+           (expect (looking-at "Foo") :to-be t)))
+        (it "creates a subtitle with a start time, stop time, text, and a 
multi-line comment."
+          (with-temp-vtt-buffer
+           (subed-append-subtitle nil 60000 65000 "Foo, bar\nbaz." 
"Hello\nworld")
+           (expect (buffer-string) :to-equal (concat 
"NOTE\nHello\nworld\n\n00:01:00.000 --> 00:01:05.000\n"
+                                                     "Foo, bar\nbaz.\n"))
+           (expect (looking-at "Foo") :to-be t)))))
     (describe "in a non-empty buffer"
       (describe "before the current subtitle"
         (describe "with point on the first subtitle"
@@ -1552,16 +1562,16 @@ This is another test here.
          (subed-jump-to-subtitle-comment)
          (expect (looking-at "NOTE A comment") :to-be t))))
     (describe "getting the comment"
-      (it "returns an empty string when there is no comment."
+      (it "returns nil when there is no comment."
         (with-temp-vtt-buffer
          (insert mock-vtt-comments-data)
          (re-search-backward "This is a test")
-         (expect (subed-subtitle-comment) :to-equal "")))
+         (expect (subed-subtitle-comment) :to-be nil)))
       (it "returns the comment."
         (with-temp-vtt-buffer
          (insert mock-vtt-comments-data)
          (goto-char (point-max))
-         (expect (subed-subtitle-comment) :to-equal "NOTE A comment can go 
here\nand have more text as needed.\n\n"))))
+         (expect (subed-subtitle-comment) :to-equal "A comment can go 
here\nand have more text as needed."))))
     (describe "setting the comment"
       (it "sets the comment when there isn't one yet."
         (with-temp-vtt-buffer
@@ -1732,7 +1742,7 @@ World
 00:01:21.058 --> 00:01:23.868
 Again")
        (with-current-buffer (subed-convert "TXT" t)
-         (expect (buffer-string) :to-equal "Hello\n\nNOTE 
Comment\n\nWorld\nAgain\n")))))
+         (expect (buffer-string) :to-equal 
"Hello\n\nComment\nWorld\nAgain\n")))))
   (describe "iterating over subtitles"
     (describe "forwards"
       (it "handles headers."



reply via email to

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