Skip to content

Instantly share code, notes, and snippets.

@siu-issiki
Last active September 6, 2018 09:49
Show Gist options
  • Save siu-issiki/9586f508de54469d57425fe1cc80e434 to your computer and use it in GitHub Desktop.
Save siu-issiki/9586f508de54469d57425fe1cc80e434 to your computer and use it in GitHub Desktop.
kibela to crowi
require 'rest-client'
require 'json'
require 'yaml'
$tokens = {
}
$base_uri = ''
def get_wiki_list base: "kibela/wikis"
wiki_list = Dir.glob("#{base}/*")
wiki_list.each_with_index do |title, i|
wiki_list[i] = get_wiki_list(base:title) unless title =~ /\d+-/
end
return wiki_list
end
def post_wiki wiki_list
wiki_list.each do |wiki|
if wiki.is_a?(Array)
post_wiki wiki
else
article = parse_md wiki
path = wiki_cast_path wiki
post_article_to_crowi(article[1], path, article[0]["author"][1..-1], dry: true)
end
end
end
def post_blog blog_list
blog_list.each do |user|
user_blogs = Dir.glob("kibela/blogs/#{user}/*")
user_blogs.each do |blog|
article = parse_md blog
path = blog_cast_path blog
post_article_to_crowi(article[1], path, article[0]["author"][1..-1], dry: true)
end
end
end
def post_article_to_crowi(body, path, from, dry: false)
uri = "#{$base_uri}?access_token=#{CGI.escape($tokens[from])}&user=#{from}"
if dry
p uri
p path
p from
else
RestClient.post uri, {body: body, path: path}
end
end
#def post_comment_to_crowi
# uri = "#{$base_uri}?access_token=#{$tokens[from]}&user=#{from}"
# if dry
# p uri
# p path
# p from
# else
# RestClient.post uri, {body: body, path: path}
# end
#end
def blog_cast_path path
path.sub!("kibela/blogs/", "/user/")
path.sub!(/\d+-/, "")
path.sub!(/.md$/, "")
end
def wiki_cast_path path
path.sub!("kibela/wikis/", "/")
path.sub!(/\d+-/, "")
path.sub!(/.md$/, "")
end
def parse_md path
File.open(path) do |file|
article = file.read.split("---")[1..-1]
article[0] = YAML.load(article[0])
return article
end
end
blog_list = Dir.glob("*", base: "kibela/blogs/")
wiki_list = get_wiki_list(base: "kibela/wikis")
post_blog blog_list
post_wiki wiki_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment