Class: ERB::Formatter::CommandLine
- Inherits:
-
Object
- Object
- ERB::Formatter::CommandLine
- Defined in:
- lib/erb/formatter/command_line.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#read_stdin ⇒ Object
readonly
Returns the value of attribute read_stdin.
-
#write ⇒ Object
readonly
Returns the value of attribute write.
Class Method Summary collapse
Instance Method Summary collapse
- #ignore_list ⇒ Object
-
#initialize(argv, stdin: $stdin) ⇒ CommandLine
constructor
A new instance of CommandLine.
- #run ⇒ Object
Constructor Details
#initialize(argv, stdin: $stdin) ⇒ CommandLine
Returns a new instance of CommandLine.
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 71 72 73 74 75 |
# File 'lib/erb/formatter/command_line.rb', line 22 def initialize(argv, stdin: $stdin) @argv = argv.dup @stdin = stdin @write, @filename, @read_stdin, @code, @single_class_per_line = nil OptionParser.new do |parser| parser. = "Usage: #{$0} FILENAME... --write" parser.on("-w", "--[no-]write", "Write file") do |value| @write = value end parser.on("--stdin-filename FILEPATH", "Set the stdin filename (implies --stdin)") do |value| @filename = value @read_stdin = true end parser.on("--[no-]stdin", "Read the file from stdin") do |value| if read_stdin == true && value == false abort "Can't set stdin filename and not use stdin at the same time" end @read_stdin = value @filename ||= '-' end parser.on("--print-width WIDTH", Integer, "Set the formatted output width") do |value| @width = value end parser.on("--single-class-per-line", "Print each class on a separate line") do |value| @single_class_per_line = value end parser.on("--tailwind-output-path PATH", "Set the path to the tailwind output file") do |value| @tailwind_output_path = value end parser.on("--[no-]debug", "Enable debug mode") do |value| $DEBUG = value end parser.on("-h", "--help", "Prints this help") do puts parser exit end parser.on("-v", "--version", "Show ERB::Formatter version number and quit") do puts "ERB::Formatter #{ERB::Formatter::VERSION}" exit end end.parse!(@argv) end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
20 21 22 |
# File 'lib/erb/formatter/command_line.rb', line 20 def filename @filename end |
#read_stdin ⇒ Object (readonly)
Returns the value of attribute read_stdin.
20 21 22 |
# File 'lib/erb/formatter/command_line.rb', line 20 def read_stdin @read_stdin end |
#write ⇒ Object (readonly)
Returns the value of attribute write.
20 21 22 |
# File 'lib/erb/formatter/command_line.rb', line 20 def write @write end |
Class Method Details
.tailwindcss_class_sorter(css_path) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/erb/formatter/command_line.rb', line 6 def self.tailwindcss_class_sorter(css_path) css = File.read(css_path) css = css.tr("\n", " ").gsub(%r{\/\*.*?\*\/},"") # remove comments css = css.gsub(%r<@media.*?\{>, "") # strip media queries css = css.scan(%r<(?:^|\}|\{) *(\S.*?) *\{>).join(" ") # extract selectors classes = css.tr(","," ").split(" ").grep(/\./).uniq.map { _1.split('.').last.gsub("\\", "") } indexed_classes = Hash[classes.zip((0...classes.size).to_a)] ->(class_name) do indexed_classes[class_name] || classes.index { _1.start_with?(class_name) } || -1 end end |
Instance Method Details
#ignore_list ⇒ Object
77 78 79 |
# File 'lib/erb/formatter/command_line.rb', line 77 def ignore_list @ignore_list ||= ERB::Formatter::IgnoreList.new end |
#run ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/erb/formatter/command_line.rb', line 81 def run if read_stdin abort "Can't read both stdin and a list of files" unless @argv.empty? files = [ [@filename, @stdin.read] ] else files = @argv.map do |filename| [filename, File.read(filename)] end end if @tailwind_output_path css_class_sorter = self.class.tailwindcss_class_sorter(@tailwind_output_path) end files.each do |(filename, code)| if ignore_list.should_ignore_file? filename print code unless write else html = ERB::Formatter.new( code, filename: filename, line_width: @width || 80, single_class_per_line: @single_class_per_line, css_class_sorter: css_class_sorter ) if write File.write(filename, html) else puts html end end end end |