Skip to content

Instantly share code, notes, and snippets.

@monkstone
Last active April 3, 2016 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkstone/70072d2951dbb869dff5 to your computer and use it in GitHub Desktop.
Save monkstone/70072d2951dbb869dff5 to your computer and use it in GitHub Desktop.
Conver ruby-processing to propane
# convert.rb
VECLIBR = /\:(vecmath|fastmath)/
sketch = <<CODE
require 'propane'
class %s < Propane::App
%s
end
%s.new title: '%s'
CODE
Dir['/home/tux/test/**/*.rb'].each do |rb_file|
next if File.directory? rb_file
old = IO.read(rb_file)
title_a = File.basename(rb_file, '.rb').split('_').each(&:capitalize!)
title = title_a.join(' ')
classname = title_a.join
new = format(sketch, classname, old, classname, title)
IO.write(rb_file, new)
end
# encoding: utf-8
# frozen_string_literal: false
TAB = ' '.freeze
# class
class Indent
attr_reader :indent_string, :file, :new_lines
def initialize(file)
@file = file
@indent_string = []
@new_lines = []
end
def parse_code
File.open(file, 'r').readlines.each do |line|
new_line = line.strip
case new_line
when /^(end)/
indent_string.shift
new_line.insert(0, indent_string.join)
when /^(class)/
indent_string << TAB
when /^(def)/
new_line.insert(0, indent_string.join)
indent_string << TAB
when /^(if)\s+/
new_line.insert(0, indent_string.join)
indent_string << TAB
when /^(module)/
indent_string << TAB
else
new_line.insert(0, indent_string.join)
end
new_lines << new_line
end
self
end
def write
File.open(file, 'w:UTF-8') do |f|
new_lines.each { |l| f.puts(l) }
end
end
end
Dir['/home/tux/test/**/*.rb'].each do |rb_file|
Indent.new(rb_file)
.parse_code
.write
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment