Skip to content

Commit

Permalink
refactor: simplify markup logic (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
HakonHarnes authored Mar 24, 2024
1 parent e3032eb commit 21b2c9c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 87 deletions.
127 changes: 49 additions & 78 deletions lua/img-clip/markup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,115 +57,86 @@ M.url_encode = function(str)
return str
end

---@param file_path string the file path or base64 string
---@return boolean
function M.insert_markup(file_path)
local file_name = vim.fn.fnamemodify(file_path, ":t")
local file_name_no_ext = vim.fn.fnamemodify(file_path, ":t:r")
local label = file_name_no_ext:gsub("%s+", "-"):lower()

-- see issue #21
local current_dir_path = vim.fn.expand("%:p:h")
if
config.get_opt("relative_template_path")
and not config.get_opt("use_absolute_path")
and current_dir_path ~= vim.fn.getcwd()
then
file_path = fs.relpath(file_path, current_dir_path)
end

-- pass args to template
---@param input string
---@param is_file_path? boolean
---@return string | nil
function M.get_template(input, is_file_path)
local template_args = {
file_path = file_path,
file_name = file_name,
file_name_no_ext = file_name_no_ext,
file_path = "",
file_name = "",
file_name_no_ext = "",
cursor = "$CURSOR",
label = label,
label = "",
}
local template = config.get_opt("template", template_args)
if not template then
return false

if is_file_path then
template_args.file_path = input
template_args.file_name = vim.fn.fnamemodify(input, ":t")
template_args.file_name_no_ext = vim.fn.fnamemodify(input, ":t:r")
template_args.label = template_args.file_name_no_ext:gsub("%s+", "-"):lower()

-- see issue #21
local current_dir_path = vim.fn.expand("%:p:h")
if
config.get_opt("relative_template_path")
and not config.get_opt("use_absolute_path")
and current_dir_path ~= vim.fn.getcwd()
then
template_args.file_path = fs.relpath(template_args.file_path, current_dir_path)
end

-- url encode path
if config.get_opt("url_encode_path") then
template_args.file_path = M.url_encode(template_args.file_path)
template_args.file_path = template_args.file_path:gsub("%%", "%%%%") -- escape % so we can call gsub again
end
else
template_args.file_path = input
end

-- url encode path
if config.get_opt("url_encode_path") then
file_path = M.url_encode(file_path)
file_path = file_path:gsub("%%", "%%%%") -- escape % so we can call gsub again
local template = config.get_opt("template", template_args)
if not template then
return nil
end

template = template:gsub("$FILE_NAME_NO_EXT", file_name_no_ext)
template = template:gsub("$FILE_NAME", file_name)
template = template:gsub("$FILE_PATH", file_path)
template = template:gsub("$LABEL", label)
template = template:gsub("$FILE_NAME_NO_EXT", template_args.file_name_no_ext)
template = template:gsub("$FILE_NAME", template_args.file_name)
template = template:gsub("$FILE_PATH", template_args.file_path)
template = template:gsub("$LABEL", template_args.label)

if not config.get_opt("use_cursor_in_template") then
template = template:gsub("$CURSOR", "")
end

local lines = M.split_lines(template)

local cur_pos = vim.api.nvim_win_get_cursor(0)
local cur_row = cur_pos[1]

local new_row, line, index = M.get_new_cursor_row(cur_row, lines)
local new_col = M.get_new_cursor_col(line)

lines[index] = line:gsub("$CURSOR", "")

vim.api.nvim_put(lines, "l", true, true)

vim.api.nvim_win_set_cursor(0, { new_row, new_col })

if config.get_opt("insert_mode_after_paste") and vim.api.nvim_get_mode().mode ~= "i" then
if new_col == string.len(line) - 1 then
vim.api.nvim_input("a")
else
vim.api.nvim_input("i")
end
end

return true
return template
end

---@param base64 string the file path or base64 string
---@param input string
---@param is_file_path? boolean
---@return boolean
function M.insert_base64_markup(base64)
-- pass args to template
local template_args = {
file_path = "",
file_name = "",
file_name_no_ext = "",
cursor = "$CURSOR",
label = "",
}
local template = config.get_opt("template", template_args)
function M.insert_markup(input, is_file_path)
local template = M.get_template(input, is_file_path)
if not template then
return false
end

template = template:gsub("$FILE_NAME_NO_EXT", "")
template = template:gsub("$FILE_NAME", "")
template = template:gsub("$FILE_PATH", base64)
template = template:gsub("$LABEL", "")

if not config.get_opt("use_cursor_in_template") then
template = template:gsub("$CURSOR", "")
end

-- get current cursor position
local lines = M.split_lines(template)

local cur_pos = vim.api.nvim_win_get_cursor(0)
local cur_row = cur_pos[1]

-- get new cursor position
local new_row, line, index = M.get_new_cursor_row(cur_row, lines)
local new_col = M.get_new_cursor_col(line)

-- remove cursor placeholder from template
lines[index] = line:gsub("$CURSOR", "")

-- paste lines and place cursor in correct position
vim.api.nvim_put(lines, "l", true, true)

vim.api.nvim_win_set_cursor(0, { new_row, new_col })

-- enter insert mode if configured
if config.get_opt("insert_mode_after_paste") and vim.api.nvim_get_mode().mode ~= "i" then
if new_col == string.len(line) - 1 then
vim.api.nvim_input("a")
Expand Down
10 changes: 5 additions & 5 deletions lua/img-clip/paste.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ M.paste_image_from_url = function(url)
util.warn("Output: " .. output, true)
end

if not markup.insert_markup(file_path) then
if not markup.insert_markup(file_path, true) then
util.error("Could not insert markup code.")
return false
end
Expand All @@ -104,7 +104,7 @@ M.paste_image_from_path = function(src_path)
end

if not config.get_opt("drag_and_drop.copy_images") then
if not markup.insert_markup(src_path) then
if not markup.insert_markup(src_path, true) then
util.error("Could not insert markup code.")
return false
end
Expand Down Expand Up @@ -135,7 +135,7 @@ M.paste_image_from_path = function(src_path)
util.warn("Output: " .. output, true)
end

if not markup.insert_markup(file_path) then
if not markup.insert_markup(file_path, true) then
util.error("Could not insert markup code.")
return false
end
Expand Down Expand Up @@ -175,7 +175,7 @@ M.paste_image_from_clipboard = function()
end
end

if not markup.insert_markup(file_path) then
if not markup.insert_markup(file_path, true) then
util.error("Could not insert markup code.")
return false
end
Expand Down Expand Up @@ -213,7 +213,7 @@ M.embed_image_as_base64 = function(file_path)
end

local prefix = M.get_base64_prefix()
if not markup.insert_base64_markup(prefix .. base64) then
if not markup.insert_markup(prefix .. base64) then
util.error("Could not insert markup code.")
return false
end
Expand Down
8 changes: 4 additions & 4 deletions tests/markup_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe("markup", function()
end)

it("inserts markup into a file", function()
local success = markup.insert_markup("/path/to/file.png")
local success = markup.insert_markup("/path/to/file.png", true)
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)

assert.equal("/path/to/file.png", lines[2])
Expand All @@ -74,7 +74,7 @@ describe("markup", function()
it("inserts markup into a markdown file", function()
vim.bo.filetype = "markdown"

local success = markup.insert_markup("/path/to/file.png")
local success = markup.insert_markup("/path/to/file.png", true)
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)

assert.equal("![](/path/to/file.png)", lines[2])
Expand All @@ -84,7 +84,7 @@ describe("markup", function()
it("inserts markup into a LaTeX file", function()
vim.bo.filetype = "tex"

local success = markup.insert_markup("/path/to/example file.png")
local success = markup.insert_markup("/path/to/example file.png", true)
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)

assert.equal("\\begin{figure}[h]", lines[2])
Expand Down Expand Up @@ -114,7 +114,7 @@ describe("markup", function()
},
})

local success = markup.insert_markup("/path/to/file.png")
local success = markup.insert_markup("/path/to/file.png", true)
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)

assert.equal(" /path/to/file.png file.png file file", lines[2])
Expand Down

0 comments on commit 21b2c9c

Please sign in to comment.