Модуль:SummaryII/types/quantity

Материал из свободной русской энциклопедии «Традиция»
Перейти к навигации Перейти к поиску
<<=re~	param			<- ( quoted / plain / tail ) !.
	quoted			<- item ( separator? item )* tail?
	item			<- {| prefix? link suffix? |}
	link			<- open !screen %s* value_quoted ( %s* pipe %s* alias )? %s* close
	atom			<- screened / ( !open . )
	prefix			<- {:prefix: atom+ :}
	alias			<- {:alias: ( !close . )+ :}
	suffix			<- {:suffix: ( !separator atom )+ :}
	tail			<- {:tail: atom+ :}
	plain			<- {| value_plain |} ( separator {| value_plain |} )* tail?
	screened		<- open screen (!close .)* close
	value_plain		<- {:sign: sign :} %s* ( float_comma / float_dot ) %s* exp? %s* unit?
	value_quoted	<- {:sign: sign :} %s* ( float_comma / float_dot ) %s* exp? %s* unit?
	open			<- "[["
	screen			<- ":"
	pipe			<- "|"
	close			<- "]]"
	separator		<- {:separator: ( ( "<br" ( %s* "/" )? ">" ) / [,;%nl] %s* ) %s* :}
	    sign			<- [+-]?
	    float_comma		<- int_dot ( {:decimal: comma :} frac )? !( dot %d )
	    int_dot			<- senior ( {:class_sep: %s+ / dot :} class )*
	    float_dot		<- int_comma ({:decimal: dot :} frac)?
	    int_comma		<- senior ( {:class_sep: %s+ / comma :} class )*
	    senior			<- { %d+ }
	    class			<- { %d^3 }
	    frac			<- {:frac: %d+ :}
	    exp				<- exp_e / exp_10caron / exp_10sup
	    exp_e			<- [eE] %s* {:exp: sign? %s* %d+ :}
	    exp_10caron		<- mutiply %s* "10" %s* "^" %s* {:exp: sign? %s* %d+ :}
	    exp_10sup		<- mutiply %s* "10" %s* "<sup>" %s* {:exp: sign? %s* %d+ :} %s* "</sup>"
	    mutiply			<- [*•⋅]
	    dot				<- "."
	    comma			<- ","
    		unit	<- .*
    ~|<<|<<#|<<?prefix>>[[Has property::<<?sign>><<#|<<>>>><<|,<<frac>>|>><<|e<<exp>>|>><<unit| <<>>|>><<|\|<<alias>>|\|<<?sign>><<#|<<>><<,|<<class_sep| >>>>>><<|<<decimal>><<frac>>|>><<|·10<sup><<exp>></sup>|>><<unit| <<>>|>>>>]]<<?suffix>><<,|<<?separator>>>>|>>>><<?tail>>>>
Plain dot-separated float with spaces 1 004.5 m [[Has property::1004,5|1 004.5]]
Plain comma-separated float with dots 1.004,5 m [[Has property::1004,5|1 004,5]]
Plain one-digit integer 5 m [[Has property::5|5]]
Plain comma-separated float 4,5 m [[Has property::4,5|4,5]]
No unit 5 [[Has property::5|5]]
Plain comma-separated float with exponent e 1,5e2 m [[Has property::1,5e2|1,5·10<sup>2</sup>]]
Plain two-digits integer 45m [[Has property::45|45]]
Plain dot-separated float 4.5 m [[Has property::4,5|4.5]]
Plain dot-separated float with commas 1,004.5 m [[Has property::1004,5|1 004.5]]
Plain comma-separated float with spaces 1 004,5 m [[Has property::1004,5|1 004,5]]

local dependencies = require 'Module:SummaryII/dependencies'
local p = require 'Module:SummaryII/types/number'

p.plain = p.plain .. ' %s* unit?'
p.quoted = plain

local value = p.value
-- @TODO: allowed units, default unit.
function p.value (value_plain, value_quoted, separator, namespace)
	local flavour, widget = value (p.plain, p.quoted, separator, namespace)
	widget = widget .. [==[
		unit	<- .*
    ]==]
	return flavour, widget
end
-- @TODO: p.value with named arguments.

function p.format (property, default_ns)
	local decimal = ',' -- @TODO.
	local unit = '<<unit| <<>>|>>'
	local value = '<<?sign>><<#|<<>>>><<|' .. decimal .. '<<frac>>|>><<|e<<exp>>|>>'
			   .. '<<unit| <<>>|>>'
	local alias = '<<?sign>><<#|<<>><<,|<<class_sep| >>>>>><<|<<decimal>><<frac>>|>><<|&middot;10<sup><<exp>></sup>|>>'
			   .. '<<unit|&nbsp;<<>>|>>'
	return '<<|<<#|'
		.. '<<?prefix>>'
		.. p.open .. (property and property .. p.four_dots or '')
		.. value
		.. '<<|' .. '\\' .. p.pipe .. '<<alias>>|\\' .. p.pipe .. alias .. '>>'
		.. p.close
		.. '<<?suffix>><<,|<<?separator>>>>'
		.. '|>>>><<?tail>>'
end

local concat = dependencies.join
local gsub = dependencies.gsub

function p.test (frame)
	local tokens = dependencies.tokens
	local open, pipe, close, equals, delim = tokens.open, tokens.pipe, tokens.close, tokens.equals, tokens.delim

	local flavour, regex = p.value (p.plain, p.quoted)
	local format = open
				.. equals .. flavour .. delim .. regex .. delim
				.. pipe .. p.format 'Has property'
				.. close
	local formatter = dependencies.formatter (format)

	local function preprocess (wikitext)
		return frame and frame:preprocess (wikitext) or wikitext
	end

	local result = { preprocess ( '<pre><nowiki>' .. format .. '</nowiki></pre>') }

	result [#result + 1] = '{| class="wikitable"', '! Case !! Wikitext !! Formatted'

	local cases = {
		['No unit']										= '5',
		['Plain one-digit integer']						= '5 m',
		['Plain two-digits integer']					= '45m',
		['Plain comma-separated float']					= '4,5 m',
		['Plain dot-separated float']					= '4.5 m',
		['Plain comma-separated float with spaces']		= '1 004,5 m',
		['Plain comma-separated float with dots']		= '1.004,5 m',
		['Plain dot-separated float with spaces']		= '1 004.5 m',
		['Plain dot-separated float with commas']		= '1,004.5 m',
		['Plain comma-separated float with exponent e']	= '1,5e2 m',
		['Plain comma-separated float with exponent e']	= '1,5e2 m',		
	}

	for title, wikitext in pairs (cases) do
		local tbl = { param = wikitext }
		result [#result + 1] = '|-'
		result [#result + 1] = '| ' .. title
		result [#result + 1] = '| ' .. wikitext
		result [#result + 1] = '| ' .. preprocess ('<code><nowiki>'.. (type (formatter) == 'function'
			and formatter (tbl)
			or tostring (formatter)
		) .. '</nowiki></code>')
	end
	result [#result + 1] = '|}'
	return table.concat (result, '\n')
end

return p