Last active
November 12, 2018 21:13
-
-
Save AlexLarra/0ac306bfae319a940909 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prepare_files | |
unless File.exists?('original') && File.file?('original') && File.readable?('original') | |
abort('There is not `original` file in current directory.') | |
end | |
File.delete('yaml') if File.exists?('yaml') | |
[File.open('original'), File.new('yaml', 'w+')] | |
end | |
def translate_to_yml(original, yaml) | |
original.each_line do |line| | |
next if line == ("\n") | |
translate_line(yaml, line) | |
end | |
end | |
def translate_line(yaml, line) | |
if /##/ =~ line | |
yaml.puts(line.gsub(' ', '_').gsub('##_', ' ').gsub(/\n/, "").downcase + ':') | |
elsif /#/ =~ line | |
yaml.puts(line.gsub(' ', '_').gsub('#_', '').gsub(/\n/, "").downcase + ':') | |
elsif / \* \[/ =~ line | |
translate_link(yaml, line, ' ') | |
elsif /\* \[/ =~ line | |
translate_link(yaml, line, '') | |
elsif /\*/ =~ line | |
yaml.puts(line.gsub(' ', '_').gsub('*_', ' ').gsub(/:\n/, ":").gsub(/\n/, "").downcase) | |
else | |
puts("#{line} cant`t be identified.") | |
end | |
end | |
def translate_link(yaml, line, tab) | |
name_beginning, name_ending = [line =~ /\[/, line =~ /\]/] | |
yaml.puts("#{tab} - name: " + line.slice(name_beginning + 1...name_ending)) | |
link_beginning, link_ending = [line =~ /\(/, line =~ /\)/] | |
yaml.puts("#{tab} link: " + line.slice(link_beginning + 1...link_ending)) | |
description_beginning, description_ending = [line =~ /- /, line =~ /\n/] | |
yaml.puts("#{tab} description: " + line.slice(description_beginning + 1...description_ending)) | |
end | |
begin | |
original, yaml = prepare_files | |
translate_to_yml(original, yaml) | |
yaml.close | |
original.close | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment