Mô đun:Cite doi

Bách khoa toàn thư mở Wikipedia
Tài liệu mô đun[xem] [sửa] [lịch sử] [làm mới]

Mô đun này thực hiện các hàm hỗ trợ Bản mẫu:Chú thích DOI.

Cách sử dụng[sửa mã nguồn]

{{#gọi:Cite doi|anchordecode}}

Giải mã một chuỗi đã được mã hóa cho neo URL, đại khái lùi lại anchorencode: cho chuỗi đó.

Hàm cú pháp anchorencode: có thể mất dữ liệu. Thí dụ "/" và ".2F" đều được mã hóa thành ".2F" và đều được hàm này chuyển đổi lại thành "/".

Hàm này có mục đích thay thế sự chuyển đổi từ |doix= thành |doi=Citation bot thực hiện. [1][2]

{{#gọi:Cite doi|currentdoi}}

Rút DOI ra khỏi tên trang hiện tại. Hãy dùng trong các trang con của Bản mẫu:Chú thích DOI được tạo ra dùng Bản mẫu:Chú thích DOI/preload.

Xem thêm[sửa mã nguồn]

-- Tools for [[Bản mẫu:Chú thích DOI]].
local p = {}

---Returns the ASCII character represented by the given hexadecimal string.
-- This function returns no value when the hexadecimal string represents a
-- control character.
function charFromHexStr(hexStr)
	local hex = tonumber(hexStr, 16)
	local char = hex and string.char(hex)
	if not char:match("%c") then return char end
end

---Decodes the given anchor-encoded string, more or less reversing the effect of
-- anchorencode: on a string.
-- The anchorencode: parser function is lossy. That is, "/" and ".2F" are both
-- encoded as ".2F", which this function converts to "/" regardless.
-- This function is intended as a drop-in replacement for the conversion from
-- doix to doi that [[:en:User:Citation bot]] performs.
-- <https://code.google.com/p/citation-bot/source/browse/trunk/objects.php#1782>
-- <https://code.google.com/p/citation-bot/source/browse/trunk/citewatch.php#14>
function p.anchordecode(frame)
	local anchor = (frame.args and frame.args[1]) or frame
	return (anchor:gsub("%.(%x%x)", charFromHexStr):gsub("_", " "))
end

---Returns the DOI at the end of the current page's title.
function p.currentdoi(frame)
	local title = mw.title.getCurrentTitle()
	local doix = mw.ustring.gsub(title.text, "^" .. title.rootText .. "/", "", 1)
	return p.anchordecode(doix)
end

return p