Module: Asciidoctor::TemplatesCompiler::RubyBeautify

Extended by:
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



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/asciidoctor/templates_compiler/ruby_beautify.rb', line 8

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

  s = "def __\n#{code}\nend\n"
  s.gsub! /^;/, ''         # remove leading semicolons
  s.gsub! /;\s*$/, ''      # remove trailing semicolons
  s.replace ::RubyBeautify.pretty_string(s, indent_token: "\1", indent_count: indent_count)
  s.gsub! ";\1", "\n\1"    # remove trailing semicolons after formatting
  s.gsub! "\1", ' '        # replace placeholder indent char with space
  s.sub! /\Adef __\n/, ''  # remove wrapper method
  s.sub! /\nend\n\z/, ''   # remove wrapper method

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