Module:Discourse

De Movilab
Aller à :navigation, rechercher

La documentation pour ce module peut être créée à Module:Discourse/doc

local discourse = require( 'mw.ext.discourse' )

function news( frame )
    return discourse.news( frame )
end

function events( frame )
    return discourse.events( frame )
end

-- from https://gist.github.com/zwh8800/9b0442efadc97408ffff248bc8573064
function parse_json_date(json_date)
    local pattern = "(%d+)%-(%d+)%-(%d+)%a(%d+)%:(%d+)%:([%d%.]+)([Z%+%-])(%d?%d?)%:?(%d?%d?)"
    local year, month, day, hour, minute, 
        seconds, offsetsign, offsethour, offsetmin = json_date:match(pattern)
    local timestamp = os.time{year = year, month = month, 
        day = day, hour = hour, min = minute, sec = seconds}
    local offset = 0
    if offsetsign ~= 'Z' then
      offset = tonumber(offsethour) * 60 + tonumber(offsetmin)
      if xoffset == "-" then offset = offset * -1 end
    end
    
    return timestamp + offset
end

function clean_post( post )
	return post:gsub('<a\\b[^<]*>[^<]*(?:<(?!/a>)[^<]*)*</a>"', '')	
end

function topic_map ( frame )
	local id = frame.args[1]
	local baseUrl = discourse.getBaseUrl( frame.args.site )
	local urlPath = '/t/' .. id .. '.json'
	local lang = mw.language.new("fr")
	
    -- Get the data.
    local baseUrl = discourse.getBaseUrl( frame.args.site )
	local data = discourse.getData( frame.args.site, urlPath )
	mw.logObject(data)

	local ts_created_at = parse_json_date(data.created_at)
	local ts_last_posted_at = parse_json_date(data.last_posted_at)
	local head_level = 'p'
	local class_topic = "discourse_topic"

	-- Construct the output wikitext.
	
	local outHtml = mw.html.create( 'div' )
	
	title = mw.html.create( 'h3' ):attr( 'class', 'discourse_title'):wikitext( data.title )
	
	topic_map = mw.html.create( 'ul' ):attr( 'class', 'topic_map')

	topic_map:node(mw.html.create( 'li' )
		:node( mw.html.create( head_level ):attr( 'class', class_topic):wikitext( "Créé le" ) )
		:node( mw.html.create( 'span' ):wikitext( lang:formatDate("j F y", data.created_at) ) )	 )

	topic_map:node(mw.html.create( 'li' )
		:node( mw.html.create( head_level ):attr( 'class', class_topic):wikitext( "Dernier post" ) )
		:node( mw.html.create( 'span' ):wikitext( lang:formatDate("j F y", data.last_posted_at) ) ))

	topic_map:node(mw.html.create( 'li' )
		:node( mw.html.create( 'span' ):wikitext( #data.post_stream.posts ) )
		:node( mw.html.create( head_level ):attr( 'class', class_topic):wikitext( "réponses" ) ))

	topic_map:node(mw.html.create( 'li' )
		:node( mw.html.create( 'span' ):wikitext( data.views ) )
		:node( mw.html.create( head_level ):attr( 'class', class_topic):wikitext( "vues" ) )	)

	topic_map:node(mw.html.create( 'li' )
		:node( mw.html.create( 'span' ):wikitext( #data.details.participants ))
		:node( mw.html.create( head_level ):attr( 'class', class_topic):wikitext( "participants" ) ))

	topic_map:node(mw.html.create( 'li' )
		:node( mw.html.create( 'span' ):wikitext( data.like_count ))	 
		:node( mw.html.create( head_level ):attr( 'class', class_topic):wikitext( "J'aime" ) ))
	
	outHtml:node( title )
	outHtml:node( topic_map )

	return outHtml
end

function posts ( frame )
	local id = frame.args[1]
	local baseUrl = discourse.getBaseUrl( frame.args.site )
	local urlPath = '/t/' .. id .. '.json'
	
    -- Get the data.
	local baseUrl = discourse.getBaseUrl( frame.args.site )
	local data = discourse.getData( frame.args.site, urlPath )
	mw.logObject(data)

	-- Construct the output wikitext.
	local outHtml = mw.html.create( 'div' )
	outHtml:addClass( 'ext-discourse' )
	for _,post in pairs(data.post_stream.posts) do
		local li = mw.html.create( 'div' )
		local div = mw.html.create( 'div' )	
		local div_name = mw.html.create( 'div' )	
		local img = mw.html.create( 'img' )
		local img_size = "45"
		img
    		:attr( 'src', baseUrl .. "/" ..post.avatar_template:gsub("{size}", img_size)  )
    		:attr( 'width', img_size)
    		:attr( 'height', img_size)
  
    		
  
    	div_name
    		:wikitext( '\'\'\'' .. post.username .. '\'\'\'' )
    	div 
    		:node( img )
    		:node( div_name )
    		:wikitext( post.cooked )
		li
			:node( div )
			
		outHtml:node( li )
	end
	return outHtml

end

return {
    news = function( frame ) return news( frame ) end;
    events = function( frame ) return events( frame ) end;
    posts = function( frame ) return posts( frame ) end;
    topic_map = function( frame ) return topic_map( frame ) end;
}

-- Debugging:
-- =p.news({args={site='space'}})
-- =p.news({args={tags='wikisource'}})