Skip to content

Instantly share code, notes, and snippets.

@delight09
Last active January 19, 2024 14:56
Show Gist options
  • Save delight09/62e86ad5f7c1907572dc9918ee4448bd to your computer and use it in GitHub Desktop.
Save delight09/62e86ad5f7c1907572dc9918ee4448bd to your computer and use it in GitHub Desktop.
selectable SVG image for your github contributions calendar
require "nokogiri"
require "open-uri"
x_weeks_last = params['last-x-weeks'].to_i
width = params['width'].to_i
height = params['height'].to_i
url = "https://github.com/users/#{params['username']}/contributions"
document = Nokogiri::HTML(open(url))
# Remove invalid colums
document.xpath("/html/body/svg/g/g[position() <= #{54 - x_weeks_last}]").remove
# Remove month titles
document.xpath('/html/body/svg/g/text[@class="month"]').remove
outter_tag = document.xpath('/html/body/svg/g').first
outter_tag['transform'] = "translate(16, 0)"
# Month title is hard to control skipping for now...
# document.xpath('/html/body/svg/g/text[@class="month"]').each do |v|
# v['x'] = v['x'].to_i - 240
# end
# Move contribute colums
document.xpath('/html/body/svg/g/g').each do |v|
t = v['transform'].scan(/\((\d+),/).last.first.to_i
v['transform'] = "translate(#{t - ( (54 - x_weeks_last - 3) * 13) + 16}, 0)" # MAGIC 13 from SVG, 3 is MAGIC
end
# Add SVG namespace
outter_tag = document.xpath('/html/body/svg').first
outter_tag['xmlns'] = "http://www.w3.org/2000/svg"
# set SVG width and height attributes
outter_tag = document.xpath('/html/body/svg').first
outter_tag['width'] = width
outter_tag['height'] = height
{
:body => document.xpath('/html/body/svg').first,
:content_type => 'image/svg+xml;charset=utf-8'
}
@delight09
Copy link
Author

Inspired by jcouyang's origin work, add query parameter last-x-weeks for the situations that ajusting the width comes with an ugly output(think a div wrapper with 290x80).

Live demo available at: https://gist.github.com.ru/delight09/62e86ad5f7c1907572dc9918ee4448bd?last-x-weeks=20&height=80&width=280&username=delight09
Demo credit to @jcouyang's github.com.ru(by) service.

@AaronConlon
Copy link

@delight09 but look like,it has some error.

@broisnischal
Copy link

I am quite new to ruby, haven't done this before but i kinda need this feature!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment