Module:Discourse
De Movilab
La documentation pour ce module peut être créée à Module:Discourse/doc
local discourse = require( 'mw.ext.discourse' ) -- 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?)" if(json_date == nil) then return 0 end 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 get_url_path( id ) return '/t/' .. id end function get_topic_map( site, id ) -- Load data from de the default Discourse if ( "{{{2}}}" == id ) then id = site site = nil end local baseUrl = discourse.getBaseUrl( site ) local pathUrl = get_url_path( id ) local urlPath = pathUrl .. '.json' local lang = mw.language.new("fr") -- Get the data. local data = discourse.getData( 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' ):attr( 'class', 'discourse') if (data.title) then title = mw.html.create( 'div' ):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" ) )) footer_img = '[[Image:Dialog-information_on.svg.png|50px|alt=|link=]]' footer_text = '[' .. baseUrl .. pathUrl .. ' ' .. 'Échangez sur ce sujet dans la discussion sur le forum' .. ']' footer = mw.html.create( 'div' ):attr( 'class', 'discourse_footer'):wikitext( footer_img .. footer_text ) outHtml:node( title ) outHtml:node( topic_map ) outHtml:node( footer ) end return outHtml end function get_posts( site, id ) local urlPath = get_url_path( id ) .. '.json' -- Get the data. local baseUrl = discourse.getBaseUrl( site ) local data = discourse.getData( site, urlPath ) mw.logObject(data) -- Construct the output wikitext. local outHtml = mw.html.create( 'div' ) outHtml:addClass( 'ext-discourse' ) if ( data.post_stream ) then 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( baseUrl .. "/" ..post.avatar_template:gsub("{size}", img_size) .. '\'\'\'' .. post.username .. '\'\'\'' ) div -- :node( img ) :node( div_name ) :wikitext( post.cooked ) li :node( div ) outHtml:node( li ) end end return outHtml end function get_topics_cat( frame ) local urlPath = '/c/' .. frame.args[1] .. '.json' local outHtml = mw.html.create( 'div' ) -- Get the data. local data = discourse.getData( frame.args.site, urlPath ) mw.logObject(data) for _,topics in pairs(data.topic_list.topics) do topic_map = get_topic_map( frame.args.site, topics.id ) -- posts = get_posts( frame.args.site, topics.id ) outHtml:node( topic_map ) outHtml:node( posts ) end return outHtml end return { posts = function( frame ) return get_posts( frame.args.site, frame.args[1] ) end; topic_map = function( frame ) return get_topic_map( frame.args.site, frame.args[1] ) end; topics_cat = function( frame ) return get_topics_cat( frame ) end; } -- Debugging: -- =p.news({args={site='space'}}) -- =p.news({args={tags='wikisource'}})