Last active
November 16, 2018 12:28
-
-
Save satour/9bca8e076b6819fcd68438ac036f2076 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/usr/bin/ruby | |
| require 'fileutils' | |
| # 引数の有無を検証 | |
| unless ARGV[0] | |
| puts "Please specify file(s)." | |
| exit | |
| end | |
| # 引数の文字列を検証 | |
| ARGV.each{|arg| | |
| unless arg.index(".md") || arg.index(".markdown") | |
| puts "ERR: Bad file extention: #{arg}" | |
| puts "Only *.md or *.markdown file(s) is/are acceptable." | |
| exit | |
| end | |
| } | |
| @level1 = /^[#][^#]*[^}]$/ | |
| @level2 = /^[#][#][^#]*[^}]$/ | |
| @level3 = /^[#][#][#][^#]*[^}]$/ | |
| ARGV.each{|arg| | |
| FileUtils.copy_file(arg, arg + ".bak") | |
| File.open(arg + ".bak", "r") { |original| | |
| File.open(arg, "w") { |dist| | |
| original.each_line { |line| | |
| buffer = line | |
| if @level1.match(buffer.chomp) | |
| # 既存行の末尾に Gitbook書式 の anchor を追加している | |
| new_line = "#{buffer.chomp} {##{buffer.slice(2..-1).chomp}}\n" | |
| dist.write(new_line) | |
| elsif @level2.match(buffer.chomp) | |
| new_line = "#{buffer.chomp} {##{buffer.slice(3..-1).chomp}}\n" | |
| dist.write(new_line) | |
| elsif @level3.match(buffer.chomp) | |
| new_line = "#{buffer.chomp} {##{buffer.slice(4..-1).chomp}}\n" | |
| dist.write(new_line) | |
| else | |
| dist.write(buffer) | |
| end | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment