Last active
April 3, 2016 06:09
-
-
Save monkstone/70072d2951dbb869dff5 to your computer and use it in GitHub Desktop.
Conver ruby-processing to propane
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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