לדלג לתוכן

יחידה:תבנית מידע: הבדלים בין גרסאות בדף

מתוך צפונות ויקי
מאין תקציר עריכה
מ תיעוד קוד, הסתרת כותרות מיותרות
שורה 2: שורה 2:
local root
local root
local args={}
local args={}
local templateStructure = {}
local templateStructure = {}


 
--[[
Renders the infobox title
]]
local function renderTitle()
local function renderTitle()
-- renders the title
-- renders the title
שורה 15: שורה 16:
end
end


--[[
Renders the right/left images (possibly 1/2/none of them available) and the main image
]]
local function renderImages()
local function renderImages()
-- render right an left images  
-- render right an left images  
שורה 58: שורה 62:
end
end


--[[
Adds edit links for easier editing of the template. If the template use data from wikidata it also adds edit link in Wikidata
]]
local function renderEditLinks()
local function renderEditLinks()
local localEdit = mw.title.getCurrentTitle():canonicalUrl({action='edit',section='0'})
local localEdit = mw.title.getCurrentTitle():canonicalUrl({action='edit',section='0'})
שורה 74: שורה 81:
end
end


--[[
Adds a styled row to the table
]]
local function addRow(rowArgs)
local function addRow(rowArgs)
     -- Adds a row to the infobox, with either a header cell
     -- Adds a row to the infobox, with either a header cell
שורה 122: שורה 132:
end
end
   
   
--[[
This function removes redundent keys from templateStructure: nil entries and header entries with no data
]]
function removeEmptyHeaders()
local j=0
local lastHeaderIndex=nil
for i=1,#templateStructure do
if templateStructure[i]~=nil then
if templateStructure[i].header then
lastHeaderIndex = i
else
if lastHeaderIndex then
j=j+1
templateStructure[j]=templateStructure[lastHeaderIndex]
lastHeaderIndex = nil
end
j=j+1
templateStructure[j]=templateStructure[i]
end
end
end
for i=#templateStructure,j+1,-1 do
table.remove(templateStructure, j)
end
end
--[[
This function builds the infobox table using structure templateStructure and args
]]
function _infobox()
function _infobox()
removeEmptyHeaders()
root = mw.html.create('table')
root = mw.html.create('table')
root:addClass('infobox'):css('width', '22em')
root:addClass('infobox'):css('width', '22em')
שורה 137: שורה 179:
end
end


--[[
This function adds image using valueArg, or if not specified by the user tries to get image from wikidata using property
]]
function getValueOrWikidataImage(valueArg, wikidataArg,width)
return (#valueArg>0 and valueArg) or (wikidataArg and propertyLink.getImageLink(wikidataArg, width))
end


function infobox(frame)
function infobox(frame)
local DEFAULT_IMAGE_PROPERTY = 'P18'
local templateParams =  frame.args
local templateParams =  frame.args
local templateArgs = frame:getParent().args
local templateArgs = frame:getParent().args
args.title = frame.args['כותרת תבנית']
args.title = frame.args['כותרת תבנית']
args.rightImage = (#frame.args['תמונה ימין']>0 and frame.args['תמונה ימין']) or (frame.args['תמונה ימין-ויקינתונים'] and propertyLink.getImageLink(frame.args['תמונה ימין-ויקינתונים'], '100'))
args.rightImage = getValueOrWikidataImage(frame.args['תמונה ימין'], frame.args['תמונה ימין-ויקינתונים'], '100')
args.leftImage = (#frame.args['תמונה שמאל']>0 and frame.args['תמונה שמאל']) or (frame.args['תמונה שמאל-ויקינתונים'] and propertyLink.getImageLink(frame.args['תמונה שמאל-ויקינתונים'], '100'))
args.leftImage = getValueOrWikidataImage(frame.args['תמונה שמאל'], frame.args['תמונה שמאל-ויקינתונים'], '100')
args.image = frame.args['תמונה']
args.image = getValueOrWikidataImage(frame.args['תמונה'], frame.args['תמונה-ויקינתונים'] or 'P18', '250')
args.imageDesc = frame.args['כתובית']
args.imageDesc = frame.args['כתובית']
args.headerclass= frame.args['כותרת-מחלקה']
args.headerclass= frame.args['כותרת-מחלקה']
    args.headerstyle= frame.args['כותרת-עיצוב']
args.headerstyle= frame.args['כותרת-עיצוב']
      
      
local labelPrefix = 'תווית'
local labelPrefix = 'תווית'
שורה 159: שורה 207:
while found do
while found do
local subHeaderName = templateParams[subheader..i]
local subHeaderName = templateParams[subheader..i]
if subHeaderName then
if subHeaderName then --subheader type
table.insert(templateStructure, {header=subHeaderName})
table.insert(templateStructure, {header=subHeaderName})
else
else
שורה 165: שורה 213:
local dataTemplate = templateParams[dataPrefix..i]
local dataTemplate = templateParams[dataPrefix..i]
if labelName and dataTemplate then
if labelName and dataTemplate then
--label: data type
table.insert(templateStructure, {label=labelName, data=dataTemplate})
table.insert(templateStructure, {label=labelName, data=dataTemplate})
elseif labelName and not dataTemplate then
elseif labelName and not dataTemplate then
--skip it
--skip it. no only label [use subheader]
elseif not labelName and dataTemplate then
elseif not labelName and dataTemplate then
--only data type
table.insert(templateStructure, {data=dataTemplate})
table.insert(templateStructure, {data=dataTemplate})
else
else
שורה 189: שורה 239:
if v.data then
if v.data then
local m = mw.ustring.match(v.data, '\{\{\{(.-)\}\}\}')
local m = mw.ustring.match(v.data, '\{\{\{(.-)\}\}\}')
-- template isn't full - try to use wikidata to fill it
if m then
if m then
if templateParams[m..'-ויקינתונים'] then
if templateParams[m..'-ויקינתונים'] then
שורה 199: שורה 250:
v.data = frame:preprocess(mw.ustring.gsub(v.data, '\{\{\{('..m..')\}\}\}', wikidataParamValue))
v.data = frame:preprocess(mw.ustring.gsub(v.data, '\{\{\{('..m..')\}\}\}', wikidataParamValue))
args.usingWikidata = args.usingWikidata or (v.data and 1)
args.usingWikidata = args.usingWikidata or (v.data and 1)
else
v.data=nil
end
end
else
end
-- if data isn't available remove it
-- if data isn't available remove it
v.data=nil
if not args.usingWikidata then
templateStructure[k]=nil
end
end
end
end

גרסה מ־21:33, 16 בינואר 2015

תבנית מידע היא יחידה המשמשת ליצירת תבניות מידע (infobox) בצורה קלה והמקושרות לוויקינתונים.

אופן השימוש

להלן תיעוד קצר של הפרמטרים הנתמכים על ידי היחידה. ניתן להיעזר גם ב#דוגמאות שימוש.

הגדרת פרמטרים

  • היחידה תומכת בקבלת פרמטרים ממסופרים כאשר המספר מגדיר יחס סדר בין הפרמטרים
  • סוגי הפרמטרים הנתמכים (יש להחליף את X במספר)
    • תוויתX - תווית עבור פרמטר מספר X (לא יוצג ללא מילוי מידעX)
    • מידעX - תוכן עבור פרמטר X
      • אם מופיע פרמטר תוויתX, יופיע משמאל לתווית המתאימה שהוגדרה
      • אם לא מופיע פרמטר תוויתX, יוצג בשתי עמודות
      • תווית-מידעX - קיצור דרך כדי לציין פרמטר שהתווית שלו זהה לשם הפרמטר (מקביל ל "תוויתX=שם הפרמטר" בצירוף "מידעX={{{שם הפרמטר|}}}". התווית יכולה להכיל קישור, אשר מוסר משם הפרמטר.
    • כותרתX - כותרת שתופיע לפני מקבץ של פרמטרים (הפרמטרים X+1, X+2 וכו'). הכותרת תופיע רק אם הפרמטרים שמופיעים בינה לבין הכותרת הבאה אחריה מלאים. (ראו removeEmptyHeaders)
    • תמונה וכיתוב - הגדרת התוכן שיופיע כתמונה, והתוכן שיופיע ככיתוב
    • תמונה ימין ותמונה שמאל - תוכן שיופיע עבור תמונה שתופיע מימין/שמאל (בתבנית שתומכות בתמונה ימנית ושמאלית)
    • כותרת תבנית - מציין את הכותרת הראשית לתבנית. אם לא מוגדר בתבנית, כותרת ברירת המחדל היא פרמטר שם ולאחריו שם בשפת המקור.

תמיכה בוויקינתונים

התבנית תומכת במילוי אוטומטי של מידע מוויקינתונים כאשר לא מוזן מידע על ידי המשתמשים:

  • עבור {{{שם הפרמטר}}} היחידה מחפשת הגדרה של "שם הפרמטר-ויקינתונים" המגדיר את שם התכונה שיש למשוך מוויקינתונים במקרה שהמידע לא הוזן על ידי המשתמש. לשימושים מתקדמים:
    • שם הפרמטר-ויקינתונים מגדיר משיכה חכמה של פרמטרים מוויקינתונים, שיודעת להוסיף קישורים מתאימים לערכים, להגדיר תמונות , ולהוסיף "נכון ל" עבור ערכים מספריים (ראו עוד ביחידה:PropertyLink)
    • שם הפרמטר-ויקינתונים-פשוט - מגדיר משיכה לא חכמה של פרמטרים מוויקינתונים
    • שם הפרמטר-ויקינתונים-מרובה - מגדיר שאפשר למשוך מספר ערכים מוויקינתונים, כשכל ערך מופרד בפסיק.

פרמטרים מיוחדים:

  • קואורדינטות - עבור פרמטר שנקרא קואורידנטות התבנית מוסיפה בצורה אוטומטית {{קואורדינטות מוויקינתונים}}. אין צורך להגדיר בצורה מיוחדת, אך יש להימנע ממתן שם "קואורדינטות" לפרמטר שלא מיועד לקואורדינטות גאוגרפיות
  • מפה-זום - עבור תבניות שמצפות למפה (פרמטר בשם מפה), ניתן לציין פרמטר זה להוספה אוטומטית של מפה עם זום ברירת מחדל במידה והעורכים לא ציינו מפה ספציפית בערך.
  • qid - ניתן לציין את שם היישות שממנה התבנית תשלוף מידע, לאו דווקא היישות של הערך (אפשרות זו לא נתמכת עבור קואורדינטות ועבור מפה)

הגדרות מתקדמות נוספות לשליטה על אופן התצוגה של ערכים מיובאים מוויקינתונים:

  • שם הפרמטר-ויקינתונים-מרובה-הפרדה - מאפשר להגדיר את התו המפריד בין ערכים שונים. ברירת המחדל היא פסיק (,). בשדות שעשויות להכיל תוכן רב, מומלץ להגדיר את ההפרדה כשבירת שורה (<br>)
  • שם הפרמטר-ויקינתונים-פרטים - מאפשר להגדיר פרט אופציונלי נוסף שיופיע בסוגריים ליד הנתון. הפרט הנוסף צריך להיות שם של מבחין (Qualifier).
  • שם הפרמטר-ויקינתונים-מגדר - יש לציין "כן" אם ערכי הפרמטר תלויים במגדר. למשל בפרמטר של "עיסוק" נרצה "סופר" או "סופרת" בהתאם למין.

עיצוב

הפרמטרים הבאים מאפשרים שליטה על עיצוב התבנית:

  • עיצוב-שורהX - שליטה על עיצוב שורה
  • תוויתX-עיצוב - שליטה על עיצוב תוויתX
  • מידעX-עיצוב - שליטה על עיצוב מידעX
  • כותרתX-עיצוב - שליטה על עיצוב כותרתX
  • כותרת תבנית-עיצוב - הגדרת עיצוב לכותרת התבנית
  • כותרת תבנית-מחלקה - הגדרת מחלקת עיצוב לכותרת התבנית
  • טבלה-עיצוב - מגדירה את עיצוב הטבלה
  • טבלה-מחלקה - מגדירה מחלקה לעיצוב הטבלה. דוגמאות למחלקות שימושיות:
    • infobox-nowrap-labels - מגדירה ללא שבירות שורה בתוויות
    • bordered - מוסיפה מסגרות

הסתרת פרמטר

כאשר מעוניינים להסתיר פרמטר שהתבנית מקבלת, ניתן להגדיר את הערך שלו כ"-".

דוגמאות שימוש

ניתן להיעזר בתבניות אחרות שמשתמשות ביחידה זו כדוגמאות (מיוחד:דפים המקושרים לכאן/יחידה:תבנית מידע)


local propertyLink = require('Module:PropertyLink')
local root
local args={}
local templateStructure = {}

--[[ 
Renders the infobox title
]]
local function renderTitle()
	-- renders the title
	return 	root
        :tag('caption')
            :addClass(args.titleclass)
            :cssText(args.titlestyle)
            :wikitext(args.title)
end

--[[ 
Renders the right/left images (possibly 1/2/none of them available) and the main image
]]
local function renderImages()
	-- render right an left images 
	if args.rightImage or args.leftImage then
		local row = root:tag('tr')
						:tag('td')
						:attr('colspan', 2)
						:css('text-align','center')
		if args.rightImage and args.leftImage then
			row:tag('table')
				:css('width','100%')
				:tag('tr')
					:tag('td')
						:css('width','50%')
						:css('vertical-align','middle')
						:wikitext(args.rightImage)
					:tag('td')
						:css('width','50%')
						:css('vertical-align','middle')
						:wikitext(args.leftImage)
					:done()
		else
			row:wikitext(args.leftImage or args.rightImage)
		end
	end
	-- render the main image
	if args.image then
		root:tag('tr')
			:tag('td')
			:attr('colspan', 2)
			:css('text-align','center')
			:wikitext(args.image)
		if args.imageDesc then
			root:tag('tr')
			:tag('td')
			:attr('colspan', 2)
			:addClass('thumbcaption')
			:css('text-align','center')
			:wikitext(args.imageDesc)
		end
		
	end
end

--[[ 
Adds edit links for easier editing of the template. If the template use data from wikidata it also adds edit link in Wikidata
]]
local function renderEditLinks()
	local localEdit = mw.title.getCurrentTitle():canonicalUrl({action='edit',section='0'})
	localEdit = '[[File:VisualEditor - Icon - Code.svg|25px|link='..localEdit..'|עריכת התבנית]]' -- link to edit template
	local wbEdit='' -- link to wikidata
	if args.usingWikidata then
		local entity = mw.wikibase.getEntityObject()
		wbEdit = '//www.wikidata.org/wiki/'..entity.id
		wbEdit = '[[File:VisualEditor - Icon - Alienextension.svg|25px|link='..wbEdit..'|עריכת מידע בוויקינתונים]]'
	end
	root:tag('tr')
			:tag('td')
			:attr('colspan', 2)
			:css('text-align','left')
			:wikitext(localEdit..wbEdit)
end

--[[ 
Adds a styled row to the table
]]
local function addRow(rowArgs)
    -- Adds a row to the infobox, with either a header cell
    -- or a label/data cell combination.
    if rowArgs.header then
        root
            :tag('tr')
                :addClass(rowArgs.rowclass)
                :cssText(rowArgs.rowstyle)
                :attr('id', rowArgs.rowid)
                :tag('th')
                    :attr('colspan', 2)
                    :attr('id', rowArgs.headerid)
                    :addClass(rowArgs.class)
                    :addClass(args.headerclass)
                    :css('text-align', 'center')
                    :cssText(args.headerstyle)
                    :wikitext(rowArgs.header)
    elseif rowArgs.data then
        local row = root:tag('tr')
        row:addClass(rowArgs.rowclass)
        row:cssText(rowArgs.rowstyle)
        row:attr('id', rowArgs.rowid)
        if rowArgs.label then
            row
                :tag('th')
                    :attr('scope', 'row')
                    :attr('id', rowArgs.labelid)
                    :css('text-align', 'right')
                    :cssText(args.labelstyle)
                    :wikitext(rowArgs.label)
                    :done()
        end
 
        local dataCell = row:tag('td')
        if not rowArgs.label then 
            dataCell
                :attr('colspan', 2)
                :css('text-align', 'center') 
        end
        dataCell
            :attr('id', rowArgs.dataid)
            :addClass(rowArgs.class)
            :cssText(rowArgs.datastyle)
            :newline()
            :wikitext(rowArgs.data)
    end
end
 
 --[[ 
This function removes redundent keys from templateStructure: nil entries and header entries with no data
]]
function removeEmptyHeaders()
 	local j=0
 	local lastHeaderIndex=nil
 	for i=1,#templateStructure do
 		if templateStructure[i]~=nil then
 			if templateStructure[i].header then
 				lastHeaderIndex = i
 			else
 				if lastHeaderIndex then
 					j=j+1
 					templateStructure[j]=templateStructure[lastHeaderIndex]
 					lastHeaderIndex = nil
 				end
 				j=j+1
 				templateStructure[j]=templateStructure[i]	
 			end
 		end
 end
 
 for i=#templateStructure,j+1,-1 do
 	table.remove(templateStructure, j)
 end
 
end
 
--[[ 
This function builds the infobox table using structure templateStructure and args
]]
function _infobox()
	removeEmptyHeaders()
	root = mw.html.create('table')
	root:addClass('infobox'):css('width', '22em')
	renderTitle()
	renderImages()

	
	local infobox_rows={}
	for k,v in pairs(templateStructure) do
		addRow(v)
	end
	renderEditLinks()
	return tostring(root)
end

--[[ 
This function adds image using valueArg, or if not specified by the user tries to get image from wikidata using property
]]
function getValueOrWikidataImage(valueArg, wikidataArg,width)
	return (#valueArg>0 and valueArg) or (wikidataArg and propertyLink.getImageLink(wikidataArg, width))
end

function infobox(frame)
	local DEFAULT_IMAGE_PROPERTY = 'P18'
	local templateParams =  frame.args
	local templateArgs = frame:getParent().args
	args.title = frame.args['כותרת תבנית']
	args.rightImage = getValueOrWikidataImage(frame.args['תמונה ימין'], frame.args['תמונה ימין-ויקינתונים'], '100')
	args.leftImage = getValueOrWikidataImage(frame.args['תמונה שמאל'], frame.args['תמונה שמאל-ויקינתונים'], '100')
	args.image = getValueOrWikidataImage(frame.args['תמונה'], frame.args['תמונה-ויקינתונים'] or 'P18', '250')
	args.imageDesc = frame.args['כתובית']
	args.headerclass= frame.args['כותרת-מחלקה']
	args.headerstyle= frame.args['כותרת-עיצוב']
    
	local labelPrefix = 'תווית'
	local dataPrefix = 'מידע'
	local subheader = 'כותרת'
	local labelDataPrefix = 'תווית-מידע'
	local k=0
	local i=1
	local found = true
	while found do
		local subHeaderName = templateParams[subheader..i]
		if subHeaderName then --subheader type
			table.insert(templateStructure, {header=subHeaderName})
		else
			local labelName = templateParams[labelPrefix..i]
			local dataTemplate = templateParams[dataPrefix..i]
			if labelName and dataTemplate then
				--label: data type
				table.insert(templateStructure, {label=labelName, data=dataTemplate})
			elseif labelName and not dataTemplate then
				--skip it. no only label [use subheader]
			elseif not labelName and dataTemplate then
				--only data type
				table.insert(templateStructure, {data=dataTemplate})
			else
				 local label_data_names = templateParams[labelDataPrefix..i]
				 if label_data_names then
				 	labelName = label_data_names
				 	dataTemplate = templateArgs[label_data_names] or '{{{' ..label_data_names.. '}}}'
				 else
				 	found=false
				 	break
			 	end
			 	table.insert(templateStructure, {label=labelName, data=dataTemplate})
			end
		end
		i=i+1
	end
	
	-- use wikidata / remove unused parameters
	for k,v in pairs(templateStructure) do
		if v.data then
			local m = mw.ustring.match(v.data, '\{\{\{(.-)\}\}\}')
			-- template isn't full - try to use wikidata to fill it
			if m then
				if templateParams[m..'-ויקינתונים'] then
					v.data = propertyLink.getProperty(templateParams[m..'-ויקינתונים']) or nil
					args.usingWikidata = args.usingWikidata or (v.data and 1)
				elseif templateParams[m..'-ויקינתונים-פשוט'] then
					-- use data should be taken from wikidata and link must not be added
					local wikidataParamValue = propertyLink.getLabel(templateParams[m..'-ויקינתונים-פשוט'])
					if wikidataParamValue then
						v.data = frame:preprocess(mw.ustring.gsub(v.data, '\{\{\{('..m..')\}\}\}', wikidataParamValue))
						args.usingWikidata = args.usingWikidata or (v.data and 1)
					end
				end
				-- if data isn't available remove it
				if not args.usingWikidata then
					templateStructure[k]=nil
				end
			end
		end
	end

	return _infobox()
end


return 	{
	['מידע']=infobox
}