Guest User

Untitled

a guest
Feb 19th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. #!/usr/bin/ruby -w
  2.  
  3. # Ruby beautifier, version 1.3, 04/03/2006
  4. # Copyright (c) 2006, P. Lutus
  5. # Released under the GPL
  6.  
  7. $tabSize = 3
  8. $tabStr = " "
  9.  
  10. # indent regexp tests
  11.  
  12. $indentExp = [
  13. /^module\b/,
  14. /(=\s*|^)if\b/,
  15. /(=\s*|^)until\b/,
  16. /(=\s*|^)for\b/,
  17. /(=\s*|^)unless\b/,
  18. /(=\s*|^)while\b/,
  19. /(=\s*|^)begin\b/,
  20. /(=\s*|^)case\b/,
  21. /\bthen\b/,
  22. /^class\b/,
  23. /^rescue\b/,
  24. /^def\b/,
  25. /\bdo\b/,
  26. /^else\b/,
  27. /^elsif\b/,
  28. /^ensure\b/,
  29. /\bwhen\b/,
  30. /\{[^\}]*$/,
  31. /\[[^\]]*$/
  32. ]
  33.  
  34. # outdent regexp tests
  35.  
  36. $outdentExp = [
  37. /^rescue\b/,
  38. /^ensure\b/,
  39. /^elsif\b/,
  40. /^end\b/,
  41. /^else\b/,
  42. /\bwhen\b/,
  43. /^[^\{]*\}/,
  44. /^[^\[]*\]/
  45. ]
  46.  
  47. def makeTab(tab)
  48. return (tab < 0)?"":$tabStr * $tabSize * tab
  49. end
  50.  
  51. def addLine(line,tab)
  52. line.strip!
  53. line = makeTab(tab)+line if line.length > 0
  54. return line + "\n"
  55. end
  56.  
  57. def beautifyRuby(path)
  58. commentBlock = false
  59. multiLineArray = Array.new
  60. multiLineStr = ""
  61. tab = 0
  62. source = File.read(path)
  63. dest = ""
  64. source.split("\n").each do |line|
  65. # combine continuing lines
  66. if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/)
  67. multiLineArray.push line
  68. multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")
  69. next
  70. end
  71.  
  72. # add final line
  73. if(multiLineStr.length > 0)
  74. multiLineArray.push line
  75. multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")
  76. end
  77.  
  78. tline = ((multiLineStr.length > 0)?multiLineStr:line).strip
  79. if(tline =~ /^=begin/)
  80. commentBlock = true
  81. end
  82. if(commentBlock)
  83. # add the line unchanged
  84. dest += line + "\n"
  85. else
  86. commentLine = (tline =~ /^#/)
  87. if(!commentLine)
  88. # throw out sequences that will
  89. # only sow confusion
  90. tline.gsub!(/\/.*?\//,"")
  91. tline.gsub!(/%r\{.*?\}/,"")
  92. tline.gsub!(/%r(.).*?\1/,"")
  93. tline.gsub!(/\\\"/,"'")
  94. tline.gsub!(/".*?"/,"\"\"")
  95. tline.gsub!(/'.*?'/,"''")
  96. tline.gsub!(/#\{.*?\}/,"")
  97. $outdentExp.each do |re|
  98. if(tline =~ re)
  99. tab -= 1
  100. break
  101. end
  102. end
  103. end
  104. if (multiLineArray.length > 0)
  105. multiLineArray.each do |ml|
  106. dest += addLine(ml,tab)
  107. end
  108. multiLineArray.clear
  109. multiLineStr = ""
  110. else
  111. dest += addLine(line,tab)
  112. end
  113. if(!commentLine)
  114. $indentExp.each do |re|
  115. if(tline =~ re && !(tline =~ /\s+end\s*$/))
  116. tab += 1
  117. break
  118. end
  119. end
  120. end
  121. end
  122. if(tline =~ /^=end/)
  123. commentBlock = false
  124. end
  125. end
  126. if(source != dest)
  127. # make a backup copy
  128. File.open(path + "~","w") { |f| f.write(source) }
  129. # overwrite the original
  130. File.open(path,"w") { |f| f.write(dest) }
  131. end
  132. if(tab != 0)
  133. STDERR.puts "#{path}: Indentation error: #{tab}"
  134. end
  135. end
  136.  
  137. if(!ARGV[0])
  138. STDERR.puts "usage: Ruby filenames to beautify."
  139. exit 0
  140. end
  141.  
  142. ARGV.each do |path|
  143. beautifyRuby(path)
  144. end
Add Comment
Please, Sign In to add comment