-
Notifications
You must be signed in to change notification settings - Fork 274
/
upsert_data_by_readme.rb
executable file
·70 lines (60 loc) · 2.28 KB
/
upsert_data_by_readme.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env ruby
require 'kramdown'
require 'sanitize'
lang = ARGV[0] || 'en'
readme = if lang == 'en'
IO.readlines('../README.en.md')
elsif lang == 'ja'
IO.readlines('../README.md')
else
puts "Need to pass [en|ja] to exec this task:"
puts "Ex. $ bundle exec rake upsert_data_by_readme:en"
puts "Ex. $ bundle exec rake upsert_data_by_readme:ja"
puts "Ex. $ bundle exec rake upsert_data_by_readme"
puts " # This generate both data in English and Japanese"
exit
end
# Remove existing files, parse README, and re-generate them
Dir.glob("./#{lang}/_posts/*.md").each { |filename| File.delete(filename) }
start_parsing_flag = false
readme.each_with_index do |line, index|
# Code for operating start-of and end-of parsing table of lines in README.
if start_parsing_flag == false
start_parsing_flag = true if line.start_with? '| ---'
next
end
break if line.start_with? '##' # Stop parsing if reached to next heading.
# Code for generating Markdown files to publish from remotework.jp
next unless line.include? '|'
cells = line.gsub('\|', '|').split '|'
name_and_link = Kramdown::Document.new(cells[1]).root.children[0].children[0]
name = name_and_link.children[0].value.strip
link = name_and_link.attr['href']
id = name.gsub(' ', '_')
.gsub('&', 'and')
.gsub('&', 'and')
.gsub('(', '(')
.gsub(')', ')')
.delete(".,").downcase
full_description = Kramdown::Document.new(cells[2].strip).to_html.strip
is_full_remote = cells[3].include?('ok') ? 'full_remote' : ''
# Generate a corresponding file by parsed-README data
company = <<~COMPANY_PAGE
---
layout: post
lang: #{lang}
permalink: /#{lang}/#{id}
title: #{name}
description: '#{Sanitize.clean(full_description)}'
description_full: '#{full_description}'
categories: #{is_full_remote}
link: #{link}
---
COMPANY_PAGE
#company << "date: 2019-01-01 00:00:00 +0900\n" # Not being used
#company << "by: John Doe\n" # Not being used
#company << "image: ''\n" # Not being used
IO.write("./#{lang}/_posts/2020-02-22-#{id}.md", company)
puts "Upsert: ./#{lang}/_posts/2020-02-22-#{id}.md"
end
puts ''