Skip to content

Instantly share code, notes, and snippets.

@syou6162
Created September 8, 2012 04:51
Show Gist options
  • Save syou6162/3671912 to your computer and use it in GitHub Desktop.
Save syou6162/3671912 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
class Entry
attr_accessor :name
def initialize(name)
@name = name
end
def display(indent=0)
end
def make
end
end
class Text < Entry
attr_accessor :base_path
def display(indent=0)
puts " " + @name
end
def make
input = base_path + "/" + @name + ".txt"
output = "#{Dir.pwd}/#{@name}.html"
system "ruby /tmp/aaa.rb -i \"#{input}\" -o \"#{output}\""
end
end
class Directory < Entry
attr_accessor :texts
def initialize(name)
@name = name
@texts = []
end
def push(text)
@texts.push text
end
def display(indent=0)
puts ("\s" * indent)+ @name
@texts.each{|text|
text.display(indent+1)
}
end
def make
current = Dir.pwd
Dir.mkdir(@name) unless File.exist?(@name)
Dir.chdir(@name)
puts "mkdir " + Dir.pwd
@texts.each{|text|
text.make
}
Dir.chdir(current)
end
end
def convert_file_to_dirs(file,base_path)
dirs = []
prev_item = nil
# 直前のアイテム。DirectoryかTextを判別する用
prev_dir = nil
# 直前のディレクトリを覚えておくためのもの
up_dir = nil
# 上のディレクトリを覚えておくためのもの
num_of_prev_dir_asterisk = nil
# 直前のディレクトリを覚えておくためのもの
File.open(file,"r").each{|line|
if line =~ /^(\*+)([^\*]*?)$/
d = Directory.new($2)
if $1.length == 1
# *Rのようなもの
dirs.push d
up_dir = d
elsif $1.length == num_of_prev_dir_asterisk
# 同レベルのものを追加
up_dir.push d
else
# 下位レベルのものを追加
prev_dir.push d
end
num_of_prev_dir_asterisk = $1.length
prev_item = d
prev_dir = d
elsif line =~ /-\[\[(.*?)\]\]/
t = Text.new($1)
t.base_path = base_path
prev_dir.push t
prev_item = t
elsif line =~ /^\n$/
#改行飮みは飛ばす
next
end
}
return dirs
end
Dir.glob(File.expand_path("~") + "/www/*.txt").reverse.each{|f|
path = File.expand_path("~") + "/www/" + File.basename(f,".txt") + ".html"
system "ruby ~/bin/convert_from_hatena_to_html.rb -i \"#{f}\" -o \"#{path}\""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment