לדלג לתוכן

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

מתוך צפונות ויקי
מאין תקציר עריכה
מאין תקציר עריכה
שורה 195: שורה 195:
args.leftImage = 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.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['כותרת-עיצוב']

גרסה מ־18:13, 6 באפריל 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|20px|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|20px|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 lastHeaderIndex=nil
 	local tempTemplateStructure = {}
 	for i,v in pairs(templateStructure) do
  		if v~=nil then
 			if v.header then
 				lastHeaderIndex = i
 			else
 				if lastHeaderIndex then
 					table.insert(tempTemplateStructure, templateStructure[lastHeaderIndex])
 					lastHeaderIndex = nil
 				end
 				table.insert(tempTemplateStructure, v)
 			end
 		end
 	end
 	templateStructure=tempTemplateStructure
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', '18em')
	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)
	if valueArg and #valueArg>0 then return valueArg end
	if wikidataArg then 
		return propertyLink.getImageLink(wikidataArg, width)
	end
	return ''
	
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
				 	if templateArgs[label_data_names] and #templateArgs[label_data_names]>0 then
				 		dataTemplate = templateArgs[label_data_names]
			 		else
			 			dataTemplate = '{{{' ..label_data_names.. '}}}'
				 	end
				 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
				local inWikidata = false
				if templateParams[m..'-ויקינתונים'] then
					v.data = propertyLink.getProperty(templateParams[m..'-ויקינתונים']) or nil
					inWikidata = v.data and #(v.data)>0
				elseif templateParams[m..'-ויקינתונים-מרובה'] then
					v.data = propertyLink.getProperty(templateParams[m..'-ויקינתונים-מרובה'], true) or nil
					inWikidata = v.data and #(v.data)>0
				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 and #wikidataParamValue>0 then
						v.data = frame:preprocess(mw.ustring.gsub(v.data, '\{\{\{('..m..')\}\}\}', wikidataParamValue))
						inWikidata = true
					end
				end
				-- if data isn't available remove it
				if inWikidata then
					args.usingWikidata = true
				else
					templateStructure[k]=nil
				end
			end
		end
	end
	-- use wikidata in title
	local m = mw.ustring.match(args.title, '\{\{\{(.-)\|?\}\}\}')
	if m then
		local inWikidata = false
		if templateParams[m..'-ויקינתונים'] then
			local wikidataParamValue = propertyLink.getProperty(templateParams[m..'-ויקינתונים']) or nil
			if wikidataParamValue then
				args.title = wikidataParamValue
			end
		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
				args.title = frame:preprocess(mw.ustring.gsub(args.title, '\{\{\{('..m..')\|?\}\}\}', wikidataParamValue))
			end
		end
	end

	return _infobox()
end


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