Skip to content

Instantly share code, notes, and snippets.

@jcouyang
Last active February 11, 2023 22:32
Show Gist options
  • Save jcouyang/6336168ecbbf4fbdc46e to your computer and use it in GitHub Desktop.
Save jcouyang/6336168ecbbf4fbdc46e to your computer and use it in GitHub Desktop.
SVG image for your github contributions calendar
require "nokogiri"
require "open-uri"
url = "https://github.com/#{params['username']}"
document = Nokogiri::HTML(open(url))
contrib_boxes = document.css('svg.js-calendar-graph-svg')[0]
contrib_boxes['xmlns']="http://www.w3.org/2000/svg"
width = (params['width']||54*13-2).to_i
height = (params['height']||89).to_i
contrib_boxes.css('text').remove
contrib_boxes['width']=(width+11).to_s+'px'
contrib_boxes['height']=(height+11).to_s+'px'
contrib_boxes.at_css('>g')['transform']='translate(0, 0)'
day_boxes = contrib_boxes.css('g>g')
day_boxes.each_with_index{|box, m|
box['transform']="translate(#{m*((width-53*2)/54+2)}, 0)"
box.css('rect.day').each_with_index{|col,n|
col['height']=(height-12)/7
col['width']=(width-53*2)/54
col['y'] = col['y'].to_i-(11-(height-12)/7)*col['y'].to_i/13
}
}
{
:body => contrib_boxes.to_html,
:content_type => 'image/svg+xml;charset=utf-8'
}
@jcouyang
Copy link
Author

SVG screenshot of you Github Contributions Wall

Usage

https://gist.github.com.ru/jcouyang/6336168ecbbf4fbdc46e.svg?username=<your-github-username>&width=400&height=60

you can ommit width and height parameter and get the default size just like it is in github.

example

visit https://gist.github.com.ru/jcouyang/6336168ecbbf4fbdc46e.svg?username=jcouyang&width=400&height=60 will get you the svg file

Now you can paste it in any where you want

<img src="https://gist.github.com.ru/jcouyang/6336168ecbbf4fbdc46e.svg?username=jcouyang&width=400&height=60" />
  1. blog
  2. email signature
  3. forum signature

lookat my awesome email signature for example

@delight09
Copy link

delight09 commented Nov 17, 2017

Hi @jcouyang ,

Good job on the script, it's working smoothly.

You can try this reliable github API to retrive SVG data which would make the script slightly more readable.

FYI, here's my implement with it.


Edit: Provide git blame against the API.

@AaronConlon
Copy link

It's well.I will take it for my blog home page.Thanks!!!

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