Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Markdown) link attributes on links, indirect images etc. #5

Open
Omikhleia opened this issue Nov 9, 2022 · 6 comments
Open

(Markdown) link attributes on links, indirect images etc. #5

Omikhleia opened this issue Nov 9, 2022 · 6 comments
Labels
enhancement New feature or request good first issue Good for newcomers
Milestone

Comments

@Omikhleia
Copy link
Owner

Lunamark only implements a subset of extension link_attributes (and friends).

Leveraging Lunamark for full support would allow more things than currently supported (e.g. links to images, etc.)

@Omikhleia Omikhleia added the enhancement New feature or request label Nov 10, 2022
@Omikhleia
Copy link
Owner Author

See jgm/lunamark#43

@Omikhleia Omikhleia changed the title Link attributes on images etc. Link attributes on links, indirect images etc. Nov 11, 2022
@Omikhleia Omikhleia added this to the 2.0 milestone Nov 13, 2022
@Omikhleia Omikhleia modified the milestones: 2.0, 1.1 Dec 3, 2022
@Omikhleia
Copy link
Owner Author

Needed for proper handling of #36 (see #8)

@Omikhleia
Copy link
Owner Author

A "minimal" ad-hoc implementation of link attributes on direct links was added in 8a15a6f in order to proceed with #36 (so that the latter can make it with both the markdown and the pandocast packages) -> There is (slightly) less urge for a proper implementation. Rescheduling to a latter milestone.

@Omikhleia Omikhleia modified the milestones: 1.1, 2.0 Jan 1, 2023
@Omikhleia Omikhleia modified the milestones: 1.2.0, 1.3.0 Feb 27, 2023
@no-vici
Copy link

no-vici commented May 4, 2023

See jgm/lunamark#43

Having read the above, I've managed my dirty solution in the meantime. Hope something more integrated will be introduced for seamless workflows.

#!/usr/bin/env lua

--[[
This function creates a new subdirectory within the working one. It then parses the source file for any image URLs,
and downloads the images to the newly created subdirectory. To use this function, you must have 'aria2' installed on
your system, a download utility that enables fast and efficient image downloads.
-- ]]
local function download_images(source, dir)
	local count = 0
	local f = assert(io.open(source, "r"), "Error opening source file.")
	for line in f:lines() do
		local url = line:match("%[!%b[]%b()%]%((.-)%)")
		if url then
			-- aria2 will handle the downloading erorrs, if any.
			os.execute("aria2c " .. url .. " -d " .. dir)
			count = count + 1
		end
	end
	f:close()

	print(string.format("\n%d links to the images used in '%s' have been downloaded to '%s'", count, source, dir))
end

-- Once all the images have been downloaded to a subdirectory, this function will help you reformat all the image links
-- in your markdown and divert them to the local storage instead of their remote servers.
local function reformat_img_links(source)
	local count, subs = 0, 0
	local lines = {}
	local file
	local success, err = pcall(function()
		file = io.open(source, "r")
		for line in file:lines() do
			if line:match("%[!%[.-%]%(.-%)%]%(.-%)") then
				count = count + 1
				-- Capture what inside [![(1)]]((2)) and transfer them into ![%1](%2)
				line, sub = line:gsub("%[!%[(.-)%]%(.-%)%]%((.-)%)", "![%1](%2){width=10cm}")
				-- Use a regular expression to replace the URL with "images/"
				line = line:gsub("https?://.+/", "images/")
				if sub then
					subs = subs + 1
				end
				lines[#lines + 1] = line
			else
				lines[#lines + 1] = line
			end
		end
	end)
	if not success then
		io.stderr:write("Failed to read file: " .. err)
		return
	end
	file:close()

	--- Use a temporary file to avoid overwriting the original file in case of an error.
	local temp = os.tmpname()
	local file
	local success, err = pcall(function()
		file = io.open(temp, "w")
		for n, line in ipairs(lines) do
			file:write(line, "\n")
		end
	end)

	if not success then
		io.stderr:write("Failed to write to temporary file: " .. err)
		return
	end

	file:close()
	-- For more info: https://zzzcode.ai/answer-question?id=7aee8473-b29d-436f-9326-b81666bdf8e0
	local function getFullPath(filename)
		local info = debug.getinfo(1, "S")
		local path = info.source:sub(2):gsub("[^/\\]+", "")
		return path .. filename
	end

	-- Get the full path to the source file.
	-- local source_path = getFullPath(source)
	-- Get the full path to the temporary file.
	local temp_path = getFullPath(temp)

	-- Move the temp file from /tmp to the working directory.
	-- local success = os.execute("mv " .. temp_path .. " " .. source_path)
	local success = os.execute("mv " .. temp_path .. " $(pwd)")

	if success then
		print("\nFile moved successfully!\n")
	else
		print("\nFile moving failed.\n")
	end

	-- Print the number of patterns found and replacements.
	print("Found " .. count .. " patterns and " .. subs .. " replacements\n")

	-- We can rename the tempfile to the source's one, but to be sure, manual renaming plays safe here.
	print(
		string.format(
			'All the links in "%s" file haven replaced, check the result in the file "%s", \nwhich resides in your working directory, before renaming it manually in case of something might have gone wrong in \nthe replacement process.',
			source,
			temp:gsub("/tmp/", "")
		)
	)
end

-- Real action.
-- download_images(arg[1], "images_test")
reformat_img_links(arg[1])

@Omikhleia
Copy link
Owner Author

Omikhleia commented May 4, 2023

Ah, @no-vici - reading your code snippet, I think it is something else.

This issue was about supporting extended (Pandoc) attributes on images and links.
For instance a direct image in Markdown is just ![caption](url), but Pandoc alllows, for instance:

![caption](url){ #id .class width=xxx }

(with an attribute containing an identifier to allow internal PDF links, an explicit width, etc.).

As noted above, there's some support already for this in our version of the lunamark parsing library.
But Markdown also has an indirect image syntax (and the same concept exists for links too), such as:

![caption][ref]
...
[ref]: url

This syntax is perhaps less known, but has the advantages that it allows using references in documents, and likely group them somewhere so that all actual URLs appear together, rather than being scattered.

The support for attributes in that case is however still missing (and so it's kind of useless for now).
That's the point of this very issue: it needs to be added to the lunamark parsing code, and then markdown.sile has to take advantage of it too.

In other terms, it's just a syntax thing and it is wholly unrelated to the question of remote URLs being downloaded.

@no-vici
Copy link

no-vici commented May 4, 2023

Oh, thank you. That was a very clear explanation. I wanted to ask about downloading images automatically like what Pandoc already does but SILE not yet does. Going through the discussion briefly and I thought the discussion was about the feature that I was after ;)

@Omikhleia Omikhleia added the good first issue Good for newcomers label May 22, 2023
@Omikhleia Omikhleia changed the title Link attributes on links, indirect images etc. (Markdown) link attributes on links, indirect images etc. Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants