Database changes have finished applying - please report any issues you're (still) seeing to support@shoutwiki.com.

Moduuli:Keskustelu

Jedipediasta, vapaasta Tähtien sota-tietosanakirjasta tänään, 18. kesäkuuta 2025
Siirry navigaatioonSiirry hakuun

Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:Keskustelu/ohje

  1 -- <nowiki>
  2 --
  3 -- [[Malline:Keskustelu]]
  4 
  5 local p = {}
  6 
  7 local function makeCategoryLink(cat)
  8 	return string.format('[[Luokka:%s]]', cat)
  9 end
 10 
 11 local function makeWikitextError(msg)
 12 	local ret = ''
 13 	ret = ret .. string.format(
 14 		'<strong class="error">[[Malline:Keskustelu]] virhe: %s.</strong>',
 15 		msg
 16 	)
 17 	if mw.title.getCurrentTitle().namespace == 0 then
 18 		ret = ret .. makeCategoryLink('Sivut, joissa on mallineparametrivirheitä')
 19 	end
 20 	return ret
 21 end
 22 
 23 function p._main(lines, options)
 24 	-- Validate the lines data.
 25 	if #lines < 2 then
 26 		return makeWikitextError('keskustelussa on oltava vähintään kaksi riviä')
 27 	elseif #lines > 10 then
 28 		return makeWikitextError('keskustelussa ei voi olla yli 10 riviä')
 29 	end
 30 	for i, t in ipairs(lines) do
 31 		if not t.speaker then
 32 			return makeWikitextError(string.format(
 33 				'keskustelun rivillä %d ei ole puhujaa',
 34 				i
 35 			))
 36 		elseif not t.text then
 37 			return makeWikitextError(string.format(
 38 				'keskustelun rivillä %d ei ole tekstiä',
 39 				i
 40 			))
 41 		end
 42 	end
 43 
 44 	-- Get the dialogue text.
 45 	local dialogue = {}
 46 	for i, t in ipairs(lines) do
 47 		local text
 48 		if t.format == 'trans' then
 49 			text = string.format("&laquo;''%s''&raquo;", t.text)
 50 		elseif t.format == 'no' then
 51 			text = t.text
 52 		else
 53 			text = string.format("”''%s''”", t.text)
 54 		end
 55 		table.insert(dialogue, string.format(
 56 			":'''%s''': %s", t.speaker, text
 57 		))
 58 	end
 59 	dialogue = table.concat(dialogue, '\n')
 60 
 61 	-- Make the footnote.
 62 	local footnote = {}
 63 	table.insert(footnote, ':&#8213;')
 64 	if options.kuvaus then
 65 		table.insert(footnote, options.kuvaus)
 66 	else
 67 		return makeWikitextError("muuttujaa 'kuvaus' ei annettu")
 68 	end
 69 	if options['ääni'] then
 70 		table.insert(footnote, string.format(
 71 			'<span class="noprint">&nbsp;&mdash; ' ..
 72 			'[[Tiedosto:Gnome-speakernotes.png|20px|(ääni)|link=]]' ..
 73 			'[[Media:%s|Kuuntele]] %s' ..
 74 			'<small>([[:Tiedosto:%s|kuvaus]])</small></span>',
 75 			options['ääni'], options.fi and '' or '<span style="color:#808080">(englanniksi)</span> ', options['ääni']
 76 		))
 77 	end
 78 	local sourceOnHover = false
 79 	if options['lähde'] then
 80 		if string.find( options['lähde'], '-ref-' ) ~= nil then
 81 			table.insert(footnote, options['lähde'])
 82 		else
 83 			local formatString
 84 			if options.url then
 85 				formatString = '<sup class="noprint plainlinks">[%s %s]</sup>'
 86 			else
 87 				formatString = '<sup class="noprint">[[%s|%s]]</sup>'
 88 				sourceOnHover = true
 89 			end
 90 			table.insert(footnote, string.format(
 91 				formatString,
 92 				options['lähde'],
 93 				mw.text.nowiki('[lähde]')
 94 			))
 95 		end
 96 	elseif mw.title.getCurrentTitle().namespace == 0 or mw.title.getCurrentTitle().namespace == 4 then
 97 		table.insert(footnote, makeCategoryLink('Lähteettömät sitaatit'))
 98 	end
 99 	footnote = table.concat(footnote)
100 
101 	-- Return the assembled output.
102 	if sourceOnHover then
103 		return string.format(
104 			'<div class="quote" title="Lähde: %s">\n%s\n%s</div>',
105 			options['lähde'],
106 			dialogue,
107 			footnote
108 		)
109 	else
110 		return string.format(
111 			'<div class="quote">\n%s\n%s</div>',
112 			dialogue,
113 			footnote
114 		)
115 	end
116 end
117 
118 function p.main(frame)
119 	-- Process arguments from the frame object.
120 	local args = {}
121 	for k, v in pairs(frame:getParent().args) do
122 		v = v:match('^%s*(.-)%s*$') -- trim whitespace
123 		if v ~= '' then
124 			args[k] = v
125 		end
126 	end
127 
128 	-- Sort the aguments into a usable format.
129 	local lines, options = {}, {}
130 	local function addLineData(line, lineKey, value)
131 		lines[line] = lines[line] or {}
132 		lines[line][lineKey] = value
133 	end
134 	for k, v in pairs(args) do
135 		if type(k) == 'number' then
136 			local line = math.ceil(k / 2)
137 			local lineKey = k % 2 == 1 and 'speaker' or 'text'
138 			addLineData(line, lineKey, v)
139 		else
140 			-- Assume k is a string.
141 			local line = k:match('^line([1-9][0-9]*)$')
142 			if line then
143 				addLineData(tonumber(line), 'format', v)
144 			else
145 				options[k] = v
146 			end
147 		end
148 	end
149 
150 	-- Remove blanks from the lines data.
151 	local function removeBlanks(t)
152 		local ret, nums = {}, {}
153 		for num in pairs(t) do
154 			nums[#nums + 1] = num
155 		end
156 		for i, num in ipairs(nums) do
157 			ret[i] = t[num]
158 		end
159 		return ret
160 	end
161 	lines = removeBlanks(lines)
162 
163 	return p._main(lines, options)
164 end
165 
166 return p
167 
168 -- </nowiki>