class ReVIEW::Unfold

Constants

ZSPACE

Public Class Methods

new(indent_paragraph = false) click to toggle source
# File lib/review/unfold.rb, line 30
def initialize(indent_paragraph = false)
  @indent_paragraph = indent_paragraph
end
unfold(f) click to toggle source
# File lib/review/unfold.rb, line 26
def Unfold.unfold(f)
  new().unfold(f)
end
unfold_author_source(s) click to toggle source

unfold paragraphs and strip preprocessor tags.

# File lib/review/unfold.rb, line 22
def Unfold.unfold_author_source(s)
  unfold(Preprocessor::Strip.new(StringIO.new(s)))
end

Public Instance Methods

unfold(input, output = nil) click to toggle source

unfold(f) -> String unfold(input, output) -> nil

# File lib/review/unfold.rb, line 36
def unfold(input, output = nil)
  if output
    @output = output
    do_unfold input
    nil
  else
    @output = StringIO.new
    do_unfold input
    @output.string
  end
end

Private Instance Methods

blank() click to toggle source
# File lib/review/unfold.rb, line 120
def blank
  @blank_needed = true
end
do_unfold(input) click to toggle source
# File lib/review/unfold.rb, line 52
def do_unfold(input)
  @blank_needed = false
  first = true
  indent = @indent_paragraph ? ZSPACE : ''
  f = LineInput.new(input)
  while line = f.gets
    case line
    when /\A\#@/
      raise "must not happen: input includes preproc directive: #{line.inspect}"
    when /\A=/
      if first
        first = false
      else
        blank
      end
      println line
      # blank
    when /\A\s+\*/
      blank
      println line
      skip_block f, /\A\s+\*|\A\s+\S/
      blank
    when /\A\s+\d+\./
      blank
      println line
      skip_block f, /\A\s+\d+\.|\A\s+\S/
      blank
    when /\A:/
      blank
      println line
      skip_block f, /\A:|\A\s+\S/
      blank
    when %r<\A//\w.*\{\s*\z>
      blank
      println line
      f.until_terminator(%r<\A//\}>) do |s|
        println s
      end
      println '//}'
      blank
    when %r<\A//\w>
      blank
      println line
      blank
    when /\A\S/
      if %r<\A//\[> =~ line
        $stderr.puts "warning: #{f.path}:#{f.lineno}: paragraph begin with `//['; missing ReVIEW directive name?"
      end
      flush_blank
      @output.print indent + line.rstrip
      f.until_match(%r<\A\s*\z|\A//\w>) do |s|
        @output.print s.rstrip
      end
      @output.puts
    else
      unless line.strip.empty?
        raise WrongInput, "#{f.path}:#{f.lineno}: wrong input: #{line.inspect}"
      end
    end
  end
end
flush_blank() click to toggle source
# File lib/review/unfold.rb, line 129
def flush_blank
  if @blank_needed
    @output.puts
    @blank_needed = false
  end
end
println(s) click to toggle source
# File lib/review/unfold.rb, line 124
def println(s)
  flush_blank
  @output.puts s.rstrip
end
skip_block(f, re) click to toggle source
# File lib/review/unfold.rb, line 114
def skip_block(f, re)
  f.while_match(re) do |line|
    @output.puts line.rstrip
  end
end