Skip to content

Instantly share code, notes, and snippets.

@haya14busa
Last active August 29, 2015 14:10
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
#
# USAGE:
# - Just remove read vimrc
# ruby update_wiki.rb
# - Add vimrc request
# ruby update_wiki.rb https://github.com/haya14busa/dotfiles/blob/master/.vimrc
#
require 'net/http'
require 'uri'
require 'json'
# Json Data
uri = URI.parse('http://vim-jp.org/reading-vimrc/json/archives.json')
json = Net::HTTP.get(uri)
result = JSON.parse(json)
last_author_name = result[-1]['author']['name']
# Target File
request_file = File::expand_path("#{File::dirname(__FILE__)}/Request.md")
# Add request
request_vimrcs = ARGV.map {|url|
url_path_split = url.split('/')
raw_url = (url_path_split[0..1] +
['raw.githubusercontent.com'] +
url_path_split[3..4] +
url_path_split[6..-1]
).join('/')
{
url: url,
author_name: url_path_split[3],
author_url: url_path_split[0..3].join('/'),
raw_url: raw_url,
length: Net::HTTP.get(URI.parse(raw_url)).split("\n").length,
}
}
requester_name = `git config --get github.user`[0...-1]
new_content = []
# Get contents
open(request_file, "r") {|f|
f.each{ |line|
if /^#{last_author_name}/ !~ line
if /^\.\.\./ =~ line
request_vimrcs.each{ |vimrc|
new_content.push(
"#{vimrc[:author_name]} | #{vimrc[:length]} | #{requester_name} | | [リンク](#{vimrc[:url]})"
)
}
end
new_content.push(line)
end
}
}
# Update contents
open(request_file, "w") {|f| new_content.each {|line| f.puts(line)}}
# Finally
puts 'Successfully updating Request.md'
puts 'Make sure the update is correct with git diff or whatever :)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment