User:Gary/categorize promoted featured items.coffee

CATEGORIZE PROMOTED FEATURED ITEMS

Description: At Featured articles promoted in 2010, shows the section from Featured articles under where each featured article is placed. class CategorizePromoted @categorizePromotedCallback: (obj) => # Get the first, and should be only, article from the results pages = obj['query']['pages']

for page of pages article = pages[page] break

pageType = @categorizePromotedPageType content = article['revisions'][0]['*']

# split "content" by section first sections = {}

# Go through WP:FA to find sections and their content while content # 0: whole thing; 1: section title; 2: articles only matches = content.match(/\==+(.*?)==+\n([\s\S]*?)\n\n/)

heading = matches[1] section = matches[2]

sections[heading] = section content = $.trim(content.replace(matches[0], ''))

break unless content.match('==')

$('#bodyContent').find('.wikitable').each (index, element) => $table = $(element) tbody = $table.children(':first')

# add a new column at the end of the table tbody.children(':first').append($(' Category '))

tbody.children.each (index, element) => row = $(element)

# Only continue if the node is an element and it contains something unless row[0].nodeType is 1 and row.children(':first') .children(':first').contents.length return true

for heading of sections articlePattern = new RegExp('\\[\\[' + @escapeRegExp(row .children(':first').text) + '(\\|(.*?))?\\]\\]')

match = sections[heading].match(articlePattern)

if match # Make article title appear based on formatting on WP:FA if match[2] row.find('a:first').contents.eq(0).replaceWith(match[2]               .replace(/(.*?)/g, $1))

row.append($(' ' + heading + ' '))

break

@categorizePromotedPageType: -> featuredTypes = { 'articles': { 'all': 'Wikipedia:Featured articles' 'promoted': 'Wikipedia:Featured articles promoted in YEAR' }   }

for type of featuredTypes conditions = featuredTypes[type]['promoted'] and window.mw.config.get('wgPageName').replace(/_/g, ' ') .indexOf(featuredTypes[type]['promoted'].replace('YEAR', '')) is 0

if conditions pageType = featuredTypes[type] break

pageType

@escapeRegExp: (str) -> str.replace(/[-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')

@init: => return false unless window.mw.config.get('wgPageName')

pageType = @categorizePromotedPageType return false unless pageType

# Fix the width of each column window.mw.util.addCSS('.wikitable th:nth-child(1) { width: 25%; }     .wikitable th:nth-child(2) { width: 15%; }      .wikitable th:nth-child(3) { width: 30%; }      .wikitable th:nth-child(4) { width: 30%; }    ')

parameters = { action: 'query', prop: 'revisions', rvdir: 'older', rvlimit: 1, rvprop: 'content', titles: pageType['all'], format: 'json' }

$.get(window.mw.config.get('wgScriptPath') + '/api.php', parameters,     CategorizePromoted.categorizePromotedCallback)

$ -> CategorizePromoted.init