Sha256: 141c46c9ea141826e611a2d850a2f5ecac65fd97b446fbe47e49d858c4f738ea

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

# encoding=utf-8

require 'find'

class RegexpReplacer
  # Constructor to initialize with file path
  def initialize(file_path)
    @file_path = file_path
  end

  # Perform the replacement based on the specified option
  def perform_replacement(option)
    content = File.read(@file_path)
    modified_content = case option
                       when 'v'
                         verbose(content)
                       when 'q'
                         quiet(content)
                       else
                         raise "Invalid option. Please choose 'v' or 'q'."
                       end
    File.write(@file_path, modified_content)
  end

  private

  # Replacement for pattern 'v'
  def verbose(content)
    regexp = /^( *)# (&\w+) ('.+)/
    substitution = '\1;;pp [__LINE__,\3] #\2'
    content.gsub(regexp, substitution)
  end

  # Replacement for pattern 'q'
  def quiet(content)
    regexp = /^( *);; ?pp \[__LINE__,(.+)\] #(&\w+)/
    substitution = '\1# \3 \2'
    content.gsub(regexp, substitution)
  end
end

# Running the script with command line arguments
if ARGV.length != 2
  puts "Usage: ruby regexp_replacer.rb [file_path] [option ('v' or 'q')]"
  exit
end

file_path, option = ARGV
replacer = RegexpReplacer.new(file_path)
begin
  replacer.perform_replacement(option)
  puts "Replacement performed successfully."
rescue StandardError => e
  puts "Error: #{e.message}"
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
markdown_exec-1.8.7 lib/regexp_replacer.rb