mpv2oboeru

mpv helpers to create flashcards from movies and TV shows
git clone anongit@rnpnr.xyz:mpv2oboeru.git
Log | Files | Refs | Feed | README | LICENSE

Commit: 3ebed4bc3e7b551e3f6415e490b8297cb88a9879
Parent: 76aaf96bb63cd8f5f71d0785d0a72add22721610
Author: Marko Leinikka
Date:   Wed, 30 Mar 2022 00:10:47 +0300

change the way helpers are imported

To be consistent with other imports.

Diffstat:
Mconfig.lua | 7+++----
Mhelpers.lua | 13++++++++-----
Msubs2srs.lua | 35+++++++++++++++++------------------
Mtest.lua | 4++--
4 files changed, 30 insertions(+), 29 deletions(-)

diff --git a/config.lua b/config.lua @@ -1,6 +1,5 @@ -require('helpers') - local mpopt = require('mp.options') +local helpers = require('helpers') local initial_config = {} local default_profile_filename = 'subs2srs' local profiles_filename = 'subs2srs_profiles' @@ -55,9 +54,9 @@ local function validate_config() end local function load_profile(profile_name) - if Helpers:is_empty(profile_name) then + if helpers.is_empty(profile_name) then profile_name = profiles.active - if Helpers:is_empty(profile_name) then + if helpers.is_empty(profile_name) then profile_name = default_profile_filename end end diff --git a/helpers.lua b/helpers.lua @@ -1,10 +1,8 @@ -Helpers = {} - -function Helpers:is_empty(var) +local function is_empty(var) return var == nil or var == '' or (type(var) == 'table' and next(var) == nil) end -function Helpers:get_episode_number(filename) +local function get_episode_number(filename) -- Reverses the filename to start the search from the end as the media title might contain similar numbers. local filename_reversed = filename:reverse() @@ -22,8 +20,13 @@ function Helpers:get_episode_number(filename) local s, e, episode_num for _, pattern in pairs(ep_num_patterns) do s, e, episode_num = string.find(filename_reversed, pattern) - if not Helpers:is_empty(episode_num) then + if not is_empty(episode_num) then return #filename - e, #filename - s, episode_num:reverse() end end end + +return { + is_empty = is_empty, + get_episode_number = get_episode_number, +} diff --git a/subs2srs.lua b/subs2srs.lua @@ -100,14 +100,13 @@ local profiles = { active = "subs2srs", } -require('helpers') - local mp = require('mp') local utils = require('mp.utils') local msg = require('mp.msg') local OSD = require('osd_styler') local config_manager = require('config') local encoder = require('encoder') +local helpers = require('helpers') -- namespaces local subs @@ -266,7 +265,7 @@ local function trim(str) end local function copy_to_clipboard(_, text) - if not Helpers:is_empty(text) then + if not helpers.is_empty(text) then text = config.clipboard_trim_enabled and trim(text) or remove_newlines(text) platform.copy_to_clipboard(text) end @@ -365,9 +364,9 @@ local function tag_format(filename) filename = remove_extension(filename) filename = remove_common_resolutions(filename) - local s, e, episode_num = Helpers:get_episode_number(filename) + local s, e, episode_num = helpers.get_episode_number(filename) - if config.tag_del_episode_num == true and not Helpers:is_empty(s) then + if config.tag_del_episode_num == true and not helpers.is_empty(s) then if config.tag_del_after_episode_num == true then -- Removing everything (e.g. episode name) after the episode number including itself. filename = filename:sub(1, s) @@ -453,10 +452,10 @@ local function update_sentence(new_data, stored_data) -- https://tatsumoto-ren.github.io/blog/discussing-various-card-templates.html#targeted-sentence-cards-or-mpvacious-cards -- if the target word was marked by yomichan, this function makes sure that the highlighting doesn't get erased. - if Helpers:is_empty(stored_data[config.sentence_field]) then + if helpers.is_empty(stored_data[config.sentence_field]) then -- sentence field is empty. can't continue. return new_data - elseif Helpers:is_empty(new_data[config.sentence_field]) then + elseif helpers.is_empty(new_data[config.sentence_field]) then -- *new* sentence field is empty, but old one contains data. don't delete the existing sentence. new_data[config.sentence_field] = stored_data[config.sentence_field] return new_data @@ -647,7 +646,7 @@ local function export_to_anki(gui) return end - if not gui and Helpers:is_empty(sub['text']) then + if not gui and helpers.is_empty(sub['text']) then sub['text'] = string.format("mpvacious wasn't able to grab subtitles (%s)", os.time()) end @@ -670,7 +669,7 @@ local function update_last_note(overwrite) if sub == nil then notify("Nothing to export. Have you set the timings?", "warn", 2) return - elseif Helpers:is_empty(sub['text']) then + elseif helpers.is_empty(sub['text']) then -- In this case, don't modify whatever existing text there is and just -- modify the other fields we can. The user might be trying to add -- audio to a card which they've manually transcribed (either the video @@ -708,7 +707,7 @@ local function update_last_note(overwrite) -- If the text is still empty, put some dummy text to let the user know why -- there's no text in the sentence field. - if Helpers:is_empty(new_data[config.sentence_field]) then + if helpers.is_empty(new_data[config.sentence_field]) then new_data[config.sentence_field] = string.format("mpvacious wasn't able to grab subtitles (%s)", os.time()) end @@ -956,7 +955,7 @@ do local function get_forvo_pronunciation(word) local audio_url = get_pronunciation_url(word) - if Helpers:is_empty(audio_url) then + if helpers.is_empty(audio_url) then msg.warn(string.format("Seems like Forvo doesn't have audio for word %s.", word)) return end @@ -986,14 +985,14 @@ do return new_data end - if Helpers:is_empty(stored_data[config.vocab_field]) then + if helpers.is_empty(stored_data[config.vocab_field]) then -- target word field is empty. can't continue. return new_data end - if config.use_forvo == 'always' or Helpers:is_empty(stored_data[config.vocab_audio_field]) then + if config.use_forvo == 'always' or helpers.is_empty(stored_data[config.vocab_audio_field]) then local forvo_pronunciation = get_forvo_pronunciation(stored_data[config.vocab_field]) - if not Helpers:is_empty(forvo_pronunciation) then + if not helpers.is_empty(forvo_pronunciation) then if config.vocab_audio_field == config.audio_field then -- improperly configured fields. don't lose sentence audio new_data[config.audio_field] = forvo_pronunciation .. new_data[config.audio_field] @@ -1092,7 +1091,7 @@ end ankiconnect.add_note = function(note_fields, gui) local action = gui and 'guiAddCards' or 'addNote' - local tags = Helpers:is_empty(config.note_tag) and {} or { substitute_fmt(config.note_tag) } + local tags = helpers.is_empty(config.note_tag) and {} or { substitute_fmt(config.note_tag) } local args = { action = action, version = 6, @@ -1131,7 +1130,7 @@ ankiconnect.get_last_note_id = function() local note_ids, _ = ankiconnect.parse_result(ret) - if not Helpers:is_empty(note_ids) then + if not helpers.is_empty(note_ids) then return table.max_num(note_ids) else return -1 @@ -1174,7 +1173,7 @@ ankiconnect.gui_browse = function(query) end ankiconnect.add_tag = function(note_id, tag) - if not Helpers:is_empty(tag) then + if not helpers.is_empty(tag) then tag = substitute_fmt(tag) ankiconnect.execute { action = 'addTags', @@ -1259,7 +1258,7 @@ subs.get = function() if sub['start'] > sub['end'] then sub['start'], sub['end'] = sub['end'], sub['start'] end - if not Helpers:is_empty(sub['text']) then + if not helpers.is_empty(sub['text']) then sub['text'] = trim(sub['text']) sub['text'] = escape_special_characters(sub['text']) end diff --git a/test.lua b/test.lua @@ -1,4 +1,4 @@ -require('helpers') +local helpers = require('helpers') local lu = require('luaunit') @@ -17,7 +17,7 @@ function test_get_episode_number() } for _, case in pairs(test_cases) do - local _, _, episode_num = Helpers:get_episode_number(case[2]) + local _, _, episode_num = helpers.get_episode_number(case[2]) lu.assertEquals(episode_num, case[1]) end end