模块:Su

来自决策链云智库

This module implements the {{su}} template. It is used to create two smaller lines of text on one actual line.

Usage from wikitext

This module cannot be used directly from wikitext. It can only be used through a template, usually the {{su}} template. Please see the template page for documentation.

Usage from Lua modules

To use this module from other Lua modules, first load the module.

local mSu = require('Module:Su')

You can then generate the su links by using the _main function.

mSu._main(sup, sub, options)

sup is the contents of the top line, and sub is the contents of the bottom line. options is a table that can contain the following fields:

  • align - this can be set to "r" or "right" for right-alignment, and "c" or "center" for center-alignment. Anything else will make the output left-aligned. Must be a string value.
  • fontSize - the font size of the text, e.g. "90%". If set to "f" or "fixed", the module will output a fixed-width font at 80%. Must be a string value.
  • lineHeight - the distance from top to bottom (including top character). Default is 1.2em. Must be a string value.
  • verticalAlign - The base alignment from the bottom. Default depends on presence of sub; -0.4em if present, 0.8em if not. Must be a string value.

All arguments are optional.

Examples

Code Result
mSu._main('top-line text', 'bottom-line text') top-line text
bottom-line text
mSu._main('top-line text', 'bottom-line text', {fontSize = '100%'}) top-line text
bottom-line text
mSu._main('top-line text', 'bottom-line text', {fontSize = 'f'}) top-line text
bottom-line text
mSu._main('top-line text', 'bottom-line text', {align = 'r'}) top-line text
bottom-line text
mSu._main('top-line text', 'bottom-line text', {align = 'c'}) top-line text
bottom-line text
mSu._main('12', '8', {align = 'c', lineHeight = '0.8em'}) 12
8
mSu._main('top-line text') top-line text
mSu._main(nil, 'bottom-line text')
bottom-line text



-- This module implements {{su}}.

local p = {}

function p.main(frame)
	-- Use arguments from the parent frame only, and remove any blank arguments.
	-- We don't need to trim whitespace from any arguments, as this module only
	-- uses named arguments, and whitespace is trimmed from them automatically. 
	local origArgs = frame:getParent().args
	local args = {}
	for k, v in pairs(origArgs) do
		if v ~= '' then
			args[k] = v
		end
	end

	-- Define the variables to pass to luaMain.
	local sup = args.p
	local sub = args.b
	local options = {
		align = args.a,
		fontSize = args.w,
		lineHeight = args.lh,
		verticalAlign = args.va
	}
	return p._main(sup, sub, options)
end

function p.invoke_main(frame)
	-- entry point for invocation using frame arguments
	
	local origArgs = frame.args
	local args = {}
	for k, v in pairs(origArgs) do
		if v ~= '' then
			args[k] = v
		end
	end

	-- Define the variables to pass to luaMain.
	local sup = args.p
	local sub = args.b
	local options = {
		align = args.a,
		fontSize = args.w,
		lineHeight = args.lh,
		verticalAlign = args.va
	}
	return p._main(sup, sub, options)
end

function p._main(sup, sub, options)
	options = options or {}
	local span = mw.html.create('span')

	-- Set the styles.
	span:css{
		['display']        = 'inline-block',
		['margin-bottom']  = '-0.3em',
		['vertical-align'] = options.verticalAlign or sub and '-0.4em' or '0.8em',
		['line-height']    = options.lineHeight or '1.2em'
	}
	if options.fontSize == 'f' or options.fontSize == 'fixed' then
		span:css{
			['font-family'] = 'monospace',
			['font-size']   = '80%'
		}
	else
		span:css('font-size', options.fontSize or '80%')
	end
	if options.align == 'r' or options.align == 'right' then
		span:css('text-align', 'right')
	elseif options.align == 'c' or options.align == 'center' then
		span:css('text-align', 'center')
	else
		span:css('text-align', 'left')
	end

	-- Add the wikitext.
	span
		:tag('sup')
			:css('font-size', 'inherit')
			:css('line-height', 'inherit')
			:css('vertical-align', 'baseline')
			:wikitext(sup)
			:done()
		:tag('br', {selfClosing = true}):done()
		:tag('sub')
			:css('font-size', 'inherit')
			:css('line-height', 'inherit')
			:css('vertical-align', 'baseline')
			:wikitext(sub)
	
	return tostring(span)
end

return p