MarkdownをHTMLに変換するエディタ

Ruby/TKの練習。

Markdownで記述したテキストをHTMLに変換するエディタ。

#!/usr/bin/env ruby
$KCODE = ‘SJIS’
require ‘tk’
require ‘bluecloth’

class MarkdownEditor
TITLE = ‘Markdown Editor’

def initialize

Tk.root.title(TITLE)
f = TkFrame.new.pack(:fill => :x)
@button = TkButton.new(f,
‘text’=>’Change Mode’,
‘command’ => proc{swich_mode()}
).pack(‘side’ => ‘left’)
@label = TkLabel.new(f).pack(:side => :left)

scr_x = TkScrollbar.new.pack(:fill=>:x, :side=>:bottom)
scr_y = TkScrollbar.new.pack(:fill=>:y, :side=>:right)
@editor = TkText.new {
xscrollbar(scr_x)
yscrollbar(scr_y)
}.pack(:fill => :both, :expand => true)

to_text()
Tk.mainloop
end

def to_html
return if @state == :html
begin
Tk.root.cursor “watch”
Tk.root.update
@state = :html
@label.text(‘HTML Mode’)
@source = @editor.value
@editor.value = BlueCloth::new(@source).to_html
ensure
Tk.root.cursor “”
Tk.root.update
end
end

def to_text
return if @state == :text
@state = :text
@label.text(‘Text Mode’)
@editor.value = @source
end

# モードを切り替える
def swich_mode
case @state
when :text
to_html()
when :html
to_text()
end
end
end

if __FILE__ == $0
MarkdownEditor.new
end

追記
コメントで教えていただいた箇所を修正しました。

[続く](http://www.gesource.jp/weblog/archives/2005/10/markdownhtml2.html)

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください