class MemoRack::MdMenu
Constants
- DEFAULT_FORMATS
- URI_UNSAFE
Attributes
files[R]
Public Class Methods
new(config)
click to toggle source
# File lib/memorack/mdmenu.rb, line 23 def initialize(config) @config = config @file = config[:file] @links = analyze(@file) @files = [] @extentions = {} regFormats(config[:formats] || DEFAULT_FORMATS) end
Public Instance Methods
analyze(path)
click to toggle source
Markdownファイルを解析してリンクを取出す
# File lib/memorack/mdmenu.rb, line 48 def analyze(path) links = [] if path && File.exist?(path) then open(path) { |f| while line = f.gets() case line when /\[.+\]\s*\(([^()]+)\)/ # インライン・リンク links << $1 when /\[.+\]:\s*([^\s]+)/ # 参照リンク links << $1 end end } end return links end
collection(dir)
click to toggle source
追加されたファイルを集める
# File lib/memorack/mdmenu.rb, line 70 def collection(dir) # 拡張子 exts = @extentions.keys.join(',') # 検索パターン pattern = File.join(dir, "**/*.{#{exts}}"); pattern.gsub!(/^\.\//, '') Dir.glob(pattern) { |path| link = @config[:prefix].to_s + (@config[:uri_escape] ? URI.escape(path, URI_UNSAFE) : path) link = link.sub(/\.[^.]*$/, '') + @config[:suffix] if @config[:suffix] @files << {:link => link, :path => path} if ! @links.member?(link) } end
each_subdir(d, dir = nil, dirs = []) { |name, i| ... }
click to toggle source
ディレクトリが違うときにサブディレクトリを引数に実行する
# File lib/memorack/mdmenu.rb, line 104 def each_subdir(d, dir = nil, dirs = []) return [dir, dirs] if d == dir prefix = @config[:prefix] d = File.join(d, '') d.gsub!(/^#{prefix}/, '') if prefix ds = d.scan(/[^\/]+/) ds.delete('.') ds.each_with_index { |name, i| next if name == dirs[i] name = URI.unescape(name) if @config[:uri_escape] yield(name, i) } [d, ds] end
generate(outfile = @file, &block)
click to toggle source
新規リンクを追加する
# File lib/memorack/mdmenu.rb, line 125 def generate(outfile = @file, &block) len = @files.length if 0 < len outfile = nil if outfile && outfile.kind_of?(String) && File.exist?(outfile) && ! @config[:update] block = lambda { |path| File.basename(path, '.*') } unless block stdout(outfile, 'a') { prefix = @config[:prefix] dir = nil dirs = [] @files.each { |item| title = block.call(item[:path]) link = item[:link] dir, dirs = each_subdir(File.dirname(link), dir, dirs) { |name, i| print ' ' * i + "- #{name}\n" } print ' ' * dirs.size + "- [#{title}](#{link})\n" } } if outfile then # update message = "append #{len} links" else # dry-run message = "found #{len} links (can update with -u option)" end else message = "not found" end $stderr.print message, "\n" if message && @config[:verbose] outfile end
regFormat(name, extentions)
click to toggle source
フォーマットとファイル拡張子を登録する
# File lib/memorack/mdmenu.rb, line 41 def regFormat(name, extentions) extentions.each { |value| @extentions[value] = name } end
regFormats(formats)
click to toggle source
フォーマット・ハッシュからファイル拡張子を登録する
# File lib/memorack/mdmenu.rb, line 34 def regFormats(formats) formats.each { |name, extentions| regFormat(name, extentions) } end
stdout(path, mode = 'r', perm = 0666) { || ... }
click to toggle source
標準出力を切換える
# File lib/memorack/mdmenu.rb, line 85 def stdout(path, mode = 'r', perm = 0666) curr = $stdout f = nil begin if path.kind_of?(String) $stdout = f = File.open(path, mode, perm) elsif path $stdout = path end yield ensure f.close if f $stdout = curr end end