Module: Asciidoctor::TemplatesCompiler::RubyBeautify

Extended by:
RubyBeautify
Includes:
RubyBeautify
Included in:
RubyBeautify
Defined in:
lib/asciidoctor/templates_compiler/ruby_beautify.rb

Instance Method Summary collapse

Instance Method Details

#pretty_string(code, indent_count: 2) ⇒ Object Also known as: call



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/asciidoctor/templates_compiler/ruby_beautify.rb', line 13

def pretty_string(code, indent_count: 2)
  new_lines_old = NEW_LINES
  NEW_LINES.push(:on_semicolon)  # XXX: sandbox somehow?

  s = "module M_\n#{code}\nend\n".dup
  s.gsub!(/^[ \t]*;/, '')      # remove leading semicolons
  s.gsub!(/;\s*$/, '')         # remove trailing semicolons
  s.replace(pretty_string_orig(s, indent_token: "\1", indent_count: indent_count))
  s.gsub!(";\1", "\n\1")       # remove trailing semicolons after formatting
  s.gsub!(/^#{"\1" * indent_count}/, '')  # remove redundant indentation level
  s.tr!("\1", ' ')             # replace placeholder indent char with space
  s.sub!(/\Amodule M_\n/, '')  # remove wrapper module
  s.sub!(/\nend\n\z/, '')      # remove wrapper module

  NEW_LINES.replace(new_lines_old)  # XXX: not thread-safe
  s
end