0
#encoding:utf-8
#! /usr/local/bin/ruby

require "sinatra"
require "haml"

Encoding.default_internal = Encoding.default_external = "UTF-8"

get "/*" do
  haml :index
end

post '/' do
  @track_in = params[:track_in]
  @track_out = out(@track_in)
  haml :index
end

def out(trackin)
  if trackin != nil
    track = trackin.lines.to_a
    for i in 0 .. track.size
      if track[i] != nil
        tr = track[i].chomp.split(/\s\t/)
        n,r,k = tr[0],tr[1],tr[2]
        t = "#{n}.#{k}(#{r})" if k != nil
        t = "#{n}.#{r}" if k == nil or track[i] == /([0-9]{2})\s\t(.+)\r/
        puts "#{t}\n"
      end
    end
  end
end

__END__

@@ index

!!!
%html
  %head
    %meta#_moz_html_fragment/
    %title
  %body
    #main_wrapper
      #page
        #editing_header
        .clear
        #preview{style: "border: 5px solid rgb(204, 204, 204); margin: 1em 0pt; padding: 5px; background: rgb(255, 255, 255) none repeat scroll 0% 50%; display: none; width: 75%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"}
        %form#form{action: "", method: "post"}
          %table{border: "0", cellpadding: "0", cellspacing: "0", style: "width: 0:px;"}
            %thead
            %tbody
            %input{name: "Submit", type: "submit"}/
          %textarea#track_in{cols: "100", rows: "15", name: "track_in"}= @track_in
          %textarea#track_out{cols: "100", rows: "15", name: "track_out"}= @track_out

输出 http://i.stack.imgur.com/nhplC.jpg 想要的结果 http://i.stack.imgur.com/T0xtJ.jpg

转换曲目列表。将点放在正确的位置,括号内的文本并按特定顺序

请帮帮我

4

1 回答 1

0

您没有从out方法返回正确的结果。试试这个:

def out(trackin)
  res = []
  if trackin != nil
    track = trackin.lines.to_a
    for i in 0 .. track.size
      if track[i] != nil
        tr = track[i].chomp.split(/\s\t/)
        n,r,k = tr[0],tr[1],tr[2]
        t = "#{n}.#{k}(#{r})" if k != nil
        t = "#{n}.#{r}" if k == nil or track[i] == /([0-9]{2})\s\t(.+)\r/
#       puts "#{t}\n"
        res << "#{t}\n"
      end
    end
  end
  res.join
end

现在您out返回所需结果的字符串。方法将字符串收集到数组res中,最后将所有行连接成一个字符串,您应该在文本框中看到该字符串。

于 2013-10-15T10:42:52.147 回答