おおいしつかさ


旅行とバイクとドライブと料理と宇宙が好き。
Ubie Discoveryのプログラマ。
Share:  このエントリーをはてなブックマークに追加

I had gone to Samezu where the people in Tokyo renew their licenses.

 今日の一言に、その日のできごとを英語で書いてみることにしました。間違っていたらびしばしつっこんでください。

  2006年12月20日の日記で書いていたかえる記法のパーサですが、以下のようになりました。

  def view_text(diary)  
    html = ''  
    lines = diary.text.chomp.gsub(/\r(?=\n)/,'').gsub(/\r/,"\n").concat("\n").scan(/.*\n/)  
 
    until lines.empty?  
      case  
      when lines[0] == ">||\n"  
        lines.shift  
        buffer = ''  
        until lines.empty?  
          if lines[0] == "||<\n"  
            lines.shift  
            break  
          end  
          buffer << lines.shift  
        end  
        html << exchange_pre(buffer)  
      when lines[0] =~ /^\[amazon:(.+?):detail\]$/  
        lines.shift  
        html << exchange_amazon_detail(Regexp.last_match[1], diary.amazon_lists)  
      else  
        buffer = ''  
        until lines.empty?  
          break if /^((>\|\|)|(\[amazon:(.+?):detail\]))$/ =~ lines[0]  
          break if !buffer.empty? && (/^((\[p:(\d+?)(|:none)\])|(\[amazon:(.+?):image\]))$/ =~ lines[0])  
          buffer << lines.shift  
          break if buffer[-2..-1] =~ /\n\n/  
        end  
        html << exchange_paragraph(buffer.chomp, diary.amazon_lists)  
      end  
    end  
 
    return html  
  end  

 仕様が変わったりしていますが(引数がStringからDiaryクラスに)、やっていることはほぼ同じです。けっこうすっきりしました。tDiaryのパーサみたいに一行ずつ処理するやり方です。でもまだちょっと不満はあります。正規表現のあたりとか……。Rubyで初めて正規表現に触れたし、なおかつほとんど触っていなかったのであまりまだ考え方がよくわかっていないのです。