2013-10-15 15 views
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無法正確在一個textarea紅寶石+屈+ Haml的

顯示結果給軌道列表進行轉換。放置點在正確的地方,括號中的文字和特定的順序

幫助我,請

+0

測試文本(在屏幕截圖中)可以在這裏找到http://www.jame-world.com/us/database-items-35208-beautiful-deformity-limited-edition-.html –

回答

0

你沒有從out方法返回正確的結果(S)。試試這個:

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中,最後將所有行都加入到應該在文本框中看到的一個字符串中。

+0

Спасибозапомощь, надеюсьвдальнейшемябудуучитьсявнимательнее –

+0

沒問題。祝你好運 ;) –