Модуль:Текст

Материал из свободной русской энциклопедии «Традиция»
Перейти к: навигация, поиск

Для документации этого модуля может быть создана страница Модуль:Текст/doc

local args_module = require 'Module:SummaryII/args'
local get_args, parse_args = args_module.get, args_module.parse

local make_title = mw.title.new
local wikilink = rex_pcre ().new '^\\[\\[(?<page>.+?)(?:\\|(?<alias>.*?))?\\]\\]$'

local function parse_part (part)
	local page, alias
	
	local start, _, captures = wikilink:tfind (part)
	if start then
		page, alias = captures.page, captures.alias or captures.page
	else
		local title = make_title (part)
		if title then
			page, alias = title.fullText, title.subpageText
		else
			alias = part
		end
	end
	return page, alias
end	-- local function parse_part (part)

local function item (page, alias, toc)

	local link = page and '[[' .. page .. '|' .. alias .. ']]' or alias
	
	return '\n<li>' .. link .. (toc
	   and '\n<div class="tocinsummary" style="padding-left: 0.5em; font-size: smaller; font-style: italic">__TOC__</div>'
	   or '') .. '</li>'
	   
end	-- local function item (page, alias, toc)

local function implement_contents (parts, nocontent, current, currentno)
	if not parts then
		return nil
	end
	local lines = ''
	-- TOC heading:
	if type (parts) == 'table' and #parts > 0 then
		lines = lines .. '<strong class="summarySection">Содержание</strong>'
	end
	-- Preface:
	local page, alias, toc
	if parts [0] then
		page, alias = parse_part (parts [0])
		toc = not nocontent and (page == current or alias == current or currentno == 0)
		lines = lines .. '\n<ul>' .. item (page, alias, toc) .. '</ul>'
		parts [0] = nil
	end
	-- Chapters:
	if #parts > 0 then
		lines = lines .. '\n<ol>'
		for no, part in ipairs (parts) do
			page, alias = parse_part (part)
			toc = not nocontent and (page == current or alias == current or currentno == no)
			lines = lines .. item (page, alias, toc)
		end
		lines = lines .. '\n</ol>'
	end
	-- Table of contents, if not set yet:
	if not nocontent and not toc then
		lines = lines .. '\n<div class="tocinsummary">__TOC__</div>'
	end
	return lines
end	-- local function implement_contents (parts, nocontent, current, currentno)

local function chapter_params (frame)
	local regex = rex_pcre ().new
	local args = parse_args (get_args (frame), {
		['parts / \\1']	= regex '^раздел(\\d+)$'
	  , nocontent		= regex '^безсодержания$'
	  , current			= regex '^раздел$'
	  , currentno		= regex '^№раздела$'
	})
	local parts = args.parts
	local title = mw.title.getCurrentTitle ()
	if not parts or not next (parts) then
		parts = {}
		for i, row in ipairs (mw.smw.ask {'[[-Подстраница::' .. title.baseText .. ']]'
										, '?#-=subpage'
										, '?Номер раздела=no'
										, sort = 'Номер раздела'} or {}) do
			parts [row.no or i] = row.subpage
		end
	end	-- if parts == {}
	local nocontent = (args.nocontent or '') ~= ''
	local current = args.current
				 or (title.isSubpage and title.fullText)
	local currentno = tonumber (args.currentno)
	if currentno and not current then
		current = parts [currentno]
	end
	if not currentno then
		for i, row in pairs (parts or {}) do
			if row == current then
				currentno = tonumber (i)
				break
			end
		end	-- for i, row in pairs (parts)
	end	-- if not currentno
	return parts, nocontent, current, currentno
end	-- 	local function chapter_params (frame)
	
local function contents (frame)
	local parts, nocontent, current, currentno = chapter_params (frame)
	return implement_contents (parts, nocontent, current, currentno)
end	-- local function contents (frame)

local function chapter (frame)
	local parts, nocontent, current, currentno = chapter_params (frame)
	if current then
		local title = make_title (current)
		return title and title.isSubpage and title.subpageText or current
	else
		return ''
	end
end	-- local function chapter (frame)

local function set (frame)
	local parts, nocontent, current, currentno = chapter_params (frame)
	if currentno then
		mw.smw.set {['Номер раздела']	= currentno}
	end
end	-- local function set (frame)
		
return {
	['содержание']	= contents
  , ['раздел']		= chapter
  , ['задать']		= set
}