Sha256: 0f0e021a3c0e18eb1bc6af72ee1672af50de9b0c2a986d46c2ed66050a7bbf1f

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 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 1
                         replace_pattern1(content)
                       when 2
                         replace_pattern2(content)
                       else
                         raise "Invalid option. Please choose 1 or 2."
                       end
    File.write(@file_path, modified_content)
  end

  private

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

  # Replacement for pattern 2
  def replace_pattern2(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 (1 or 2)]"
  exit
end

file_path, option = ARGV
replacer = RegexpReplacer.new(file_path)
begin
  replacer.perform_replacement(option.to_i)
  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.6 lib/regexp_replacer.rb