Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ The aliases are defined in [English.rb](lib/English.rb), and are as follows, fir
### Builtin to Alias Table

| Builtin | Alias |
| :-------: | ---------------------- |
| :-----: | ------------------------ |
| $! | $ERROR_INFO |
| $$ | $PID |
| $$ | $PROCESS_ID |
Expand Down
4 changes: 4 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
require "bundler/gem_tasks"

file "README.md" => "lib/English.rb" do
ruby "bin/alias_markdown_table_generator", "README.md"
end
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,44 @@ require 'stringio'

# Parses the English.rb file for aliases into an array of alias/builtin tuples.
class FileParser
ALIAS_SOURCE_FILESPEC = File.absolute_path(File.join(File.basename(__FILE__), '../lib/English.rb'))
ALIAS_SOURCE_FILESPEC = File.join(__dir__, '../lib/English.rb')

def self.parse(code_text_lines=File.readlines(ALIAS_SOURCE_FILESPEC))
lines = code_text_lines \
.map(&:strip) \
.reject { |line| line.start_with?('#') } \
.select { |line| line.match(/^alias/) }
tuples = lines.each_with_object([]) do |line, tuples|
tuples << line.split[1..2]
end
tuples.sort
code_text_lines.grep(/^\s*alias\s+(\S+)\s+(\S+)/) {$~.captures}
end
end


class MarkdownTableGenerator
# Returns a string containing a markdown table of the aliases, with headings "Alias" and "Built-In".
# @param [Array] tuples array of alias/builtin tuples
# @param [Boolean] alias_to_builtin true if alias to builtin, false if builtin to alias
def self.generate(tuples_orig, alias_to_builtin=true)
col1_label, col2_label = alias_to_builtin ? ['Builtin', 'Alias'] : ['Alias', 'Builtin']

# Sort by the first column, whatever that is.
tuples = alias_to_builtin ? tuples_orig.map(&:reverse).sort.map(&:reverse) : tuples_orig
# @param [Boolean] builtin_to_alias true if builtin to alias, false if alias to builtin
def self.generate(tuples, builtin_to_alias=true)

tuples = tuples.map(&:reverse) if alias_to_builtin
col1_label, col2_label = 'Alias', 'Builtin'
col1_length = [tuples.map { |(a, _b)| a.length }.max, col1_label.length].max
col2_length = [tuples.map { |(_a, b)| b.length }.max, col2_label.length].max

col1_heading_line = alias_to_builtin ? ":#{'-' * col1_length}:" : ('-' * col1_length)
col2_heading_line = alias_to_builtin ? '-' * (col2_length - 2) : ":#{'-' * (col2_length - 2)}:"
col1_heading_line = ('-' * col1_length)
col2_heading_line = ":#{'-' * (col2_length - 2)}:"

format_line = ->(item1, item2) do
if alias_to_builtin
if builtin_to_alias
tuples = tuples.map(&:reverse)
col1_label, col2_label = col2_label, col1_label
col1_length, col2_length = col2_length, col1_length
col1_heading_line, col2_heading_line = col2_heading_line, col1_heading_line
format_line = ->(item1, item2) do
"| #{item1.center(col1_length)} | #{item2.ljust(col2_length)} |"
else
end
else
format_line = ->(item1, item2) do
"| #{item1.ljust(col1_length)} | #{item2.center(col2_length)} |"
end
end

# Sort by the first column, whatever that is.
tuples.sort!

markdown_string = StringIO.new
markdown_string << format_line.call(col1_label, col2_label) << "\n"
markdown_string << format_line.call(col1_heading_line, col2_heading_line) << "\n"
Expand All @@ -74,6 +72,14 @@ end

if __FILE__ == $0
alias_tuples = FileParser.parse
replace = !ARGV.empty?
if replace
ARGF.inplace_mode = "~"
ARGF.each do |line|
break if /^### Builtin to Alias Table/ =~ line
puts line
end
end
puts <<~OUTPUT
### Builtin to Alias Table

Expand All @@ -83,4 +89,18 @@ if __FILE__ == $0

#{MarkdownTableGenerator.generate(alias_tuples, false)}
OUTPUT
if replace
puts
ARGF.each do |line|
if replace
case line
when /^##?\s/, /^### (?!Alias to Builtin Table)/
puts line
replace = false
end
else
puts line
end
end
end
end