יחידה:קטע

גרסה מ־07:50, 27 בנובמבר 2026 מאת שיע.ק (שיחה | תרומות) (הוספת פונקציה exclude (חיקוי #בלי קטע))
(הבדל) → הגרסה הקודמת | הגרסה האחרונה (הבדל) | הגרסה הבאה ← (הבדל)

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:קטע/תיעוד

-- חיקוי של הרחבת Labeled Section Transclusion (הפונקציה #קטע) עבור טקסטים שהועתקו מוויקיטקסט.
-- הסימונים בדפים הם הערות HTML בצורה <!--קטע התחלה=שם--> ... <!--קטע סוף=שם-->
local p = {}

local function trim(s)
	return mw.text.trim(s or '')
end

local function cleanTransclusion(text)
	text = mw.ustring.gsub(text, '<noinclude>.-</noinclude>', '')
	text = mw.ustring.gsub(text, '</?includeonly>', '')
	text = mw.ustring.gsub(text, '<onlyinclude>', '')
	text = mw.ustring.gsub(text, '</onlyinclude>', '')
	return text
end

function p.section(frame)
	local args = frame.args
	local pageName = trim(args[1])
	local label = trim(args[2])
	if pageName == '' or label == '' then
		return ''
	end
	local title = mw.title.new(pageName)
	if not title then
		return ''
	end
	local content = title:getContent()
	if not content then
		return ''
	end
	local startMark = '<!--קטע התחלה=' .. label .. '-->'
	local endMark = '<!--קטע סוף=' .. label .. '-->'
	local parts = {}
	local pos = 1
	while true do
		local s, e = string.find(content, startMark, pos, true)
		if not s then
			break
		end
		local s2, e2 = string.find(content, endMark, e + 1, true)
		if s2 then
			table.insert(parts, string.sub(content, e + 1, s2 - 1))
			pos = e2 + 1
		else
			table.insert(parts, string.sub(content, e + 1))
			break
		end
	end
	if #parts == 0 then
		return ''
	end
	return frame:preprocess(cleanTransclusion(table.concat(parts, '\n')))
end

function p.exclude(frame)
	local args = frame.args
	local pageName = trim(args[1])
	local label = trim(args[2])
	if pageName == '' then
		return ''
	end
	local title = mw.title.new(pageName)
	if not title then
		return ''
	end
	local content = title:getContent()
	if not content then
		return ''
	end
	if label ~= '' then
		local startMark = '<!--קטע התחלה=' .. label .. '-->'
		local endMark = '<!--קטע סוף=' .. label .. '-->'
		while true do
			local s, e = string.find(content, startMark, 1, true)
			if not s then
				break
			end
			local s2, e2 = string.find(content, endMark, e + 1, true)
			if s2 then
				content = string.sub(content, 1, s - 1) .. string.sub(content, e2 + 1)
			else
				content = string.sub(content, 1, s - 1)
				break
			end
		end
	end
	return frame:preprocess(cleanTransclusion(content))
end

return p