Module:Library

From Comprehensible Input Wiki

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

local p = {}

function createTable(lang)
    if lang[1] then
        local g = mw.ext.externaldata.getWebData {
            url = 'https://comprehensibleinputwiki.com/library.php?language=' .. lang[1],
            data = {title, channel, id, index, difficulty, difficulty_ordered},
            format = 'JSON'
        }

        local h = mw.ext.externaldata.getWebData {
            url = tostring(mw.uri.fullUrl( 'Data:Mandarin Chinese/Videos' )) .. '?action=raw',
            format = 'CSV with header'
        }

        tableOut = '<table id="library"><tr id="lib-header"><th>Video</th><th>Title</th><th>Difficulty</th><th>Channel</th></tr>'

        for key, value in ipairs(g) do
        	if not value['difficulty'] then
        		value['difficulty'] = "No Difficulty Set Yet"
        	end
        	if not value['difficulty_ordered'] then
        		value['difficulty_ordered'] = "0"
        	end
			if not value['channel'] then
        		value['channel'] = "Missing Channel Name"
        	end
    		
	        tableOut = tableOut .. '<tr data-index="' .. value['main_index'] .. '"><td data-sort="' .. value['main_index'] .. '"> ' .. mw.getCurrentFrame():callParserFunction( '#ev', 'youtube', value['id']) .. '</td><td>' .. value['title'] .. '</td><td data-order="' .. value['difficulty_ordered'] .. '">' .. value['difficulty'] .. '</td><td>' .. value['channel'] .. '</td></tr>'
        end

        tableOut = tableOut .. '</table>'

        return tableOut
    else
        return 'No language selected.'
    end
end

function dump(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '\n['..k..'] = ' .. dump(v) .. ','
      end
      return s .. '} '
   else
      return tostring(o)
   end
end



p.showLibrary = function(frame)
	return createTable(frame:getParent().args)
end


return p