A Wiki in the Desert
Log in

Difference between revisions of "Module:HerbGallery"

From A Wiki in the Desert
 
(8 intermediate revisions by the same user not shown)
Line 10: Line 10:
 
     end
 
     end
 
      
 
      
     local queryResult = mw.smw.getQueryResult( '[[Herb/'..mw.text.trim(frame:getParent().args[1])..']]|?Is similar to.Has image=image' )
+
     local queryResult = mw.smw.getQueryResult( '[[Herb/'..mw.text.trim(frame:getParent().args[1])..']]|?Is similar to=name|?Is similar to.Has image=image' )
  
 
     if queryResult == nil then
 
     if queryResult == nil then
Line 16: Line 16:
 
     end
 
     end
  
     local currentIndex = 1
+
     local imageIndex = 1
 +
    local textIndex = 2
 
     local galleryArgs = {
 
     local galleryArgs = {
 
     width="200",  
 
     width="200",  
Line 25: Line 26:
 
     if type( queryResult ) == "table" then
 
     if type( queryResult ) == "table" then
 
         for k,v in pairs( queryResult.results ) do
 
         for k,v in pairs( queryResult.results ) do
         if type( v ) == "table" and type(v.printouts) == "table" and not v.printouts.image == nil then
+
         if type( v ) == "table" and type(v.printouts) == "table" then
 
             for kR,vR in pairs( v.printouts.image ) do
 
             for kR,vR in pairs( v.printouts.image ) do
             galleryArgs[currentIndex] = vR.fulltext
+
             galleryArgs[imageIndex] = vR.fulltext
             currentIndex = currentIndex + 1
+
             imageIndex = imageIndex + 2
 +
            end
 +
           
 +
            for kR,vR in pairs( v.printouts.name ) do
 +
            galleryArgs[textIndex] = '[['..vR.fulltext..'|'..mw.text.split(vR.fulltext, "\/")[2]..']]'
 +
            textIndex = textIndex + 2
 
             end
 
             end
            else
 
            local myResult = ""
 
        for kR,vR in pairs( v.printouts ) do
 
        myResult = myResult .. ' ' .. kR .. '\n'
 
        end
 
            return 'input:'..myResult
 
 
             end
 
             end
 
         end
 
         end
 
          
 
          
         local myResult = ""
+
         mw.logObject(galleryArgs)
        for k,v in pairs( galleryArgs ) do
+
        return frame:expandTemplate{ title = 'Gallery', args = galleryArgs }
        myResult = myResult .. ' ' .. k .. ' ' .. v .. '\n'
 
        end
 
        return myResult
 
 
     end
 
     end
  

Latest revision as of 03:48, 15 May 2021

Documentation for this module may be created at Module:HerbGallery/doc

local p = {} --p stands for package

function p.gallery( frame )
	if not mw.smw then
        return "mw.smw module not found"
	end

    if frame:getParent().args[1] == nil then
        return "no parameter found"
    end
    
    local queryResult = mw.smw.getQueryResult( '[[Herb/'..mw.text.trim(frame:getParent().args[1])..']]|?Is similar to=name|?Is similar to.Has image=image' )

    if queryResult == nil then
        return "(no values)"
    end

    local imageIndex = 1
    local textIndex = 2
    local galleryArgs = {
    	width="200", 
    	height="200", 
    	mode="packed"
    }
    
    if type( queryResult ) == "table" then
        for k,v in pairs( queryResult.results ) do
        	if type( v ) == "table" and type(v.printouts) == "table" then
            	for kR,vR in pairs( v.printouts.image ) do
            		galleryArgs[imageIndex] = vR.fulltext
            		imageIndex = imageIndex + 2
            	end
            	
            	for kR,vR in pairs( v.printouts.name ) do
            		galleryArgs[textIndex] = '[['..vR.fulltext..'|'..mw.text.split(vR.fulltext, "\/")[2]..']]'
            		textIndex = textIndex + 2
            	end
            end
        end
        
        mw.logObject(galleryArgs)
        return frame:expandTemplate{ title = 'Gallery', args = galleryArgs }
    end

    return ""
end

return p