Class: MemoRack::MdMenu
- Inherits:
-
Object
- Object
- MemoRack::MdMenu
- Defined in:
- lib/memorack/mdmenu.rb
Constant Summary collapse
- URI_UNSAFE =
/[^\-_.!~*'a-zA-Z\d;\/?:@&=+$,\[\]]/
- DEFAULT_FORMATS =
{ 'markdown' => ['txt', 'md', 'markdown'], 'textile' => ['tt'], 'wiki' => ['wiki'], 'json' => ['json'], 'html' => ['html', 'htm'] }
Instance Attribute Summary collapse
-
#files ⇒ Object
readonly
Returns the value of attribute files.
Instance Method Summary collapse
-
#analyze(path) ⇒ Object
Markdownファイルを解析してリンクを取出す.
-
#collection(dir) ⇒ Object
追加されたファイルを集める.
-
#each_subdir(d, dir = nil, dirs = []) ⇒ Object
ディレクトリが違うときにサブディレクトリを引数に実行する.
-
#generate(outfile = @file, &block) ⇒ Object
新規リンクを追加する.
-
#initialize(config) ⇒ MdMenu
constructor
A new instance of MdMenu.
-
#regFormat(name, extentions) ⇒ Object
フォーマットとファイル拡張子を登録する.
-
#regFormats(formats) ⇒ Object
フォーマット・ハッシュからファイル拡張子を登録する.
-
#stdout(path, mode = 'r', perm = 0666) ⇒ Object
標準出力を切換える.
Constructor Details
#initialize(config) ⇒ MdMenu
Returns a new instance of MdMenu.
23 24 25 26 27 28 29 30 31 |
# 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 |
Instance Attribute Details
#files ⇒ Object (readonly)
Returns the value of attribute files.
21 22 23 |
# File 'lib/memorack/mdmenu.rb', line 21 def files @files end |
Instance Method Details
#analyze(path) ⇒ Object
Markdownファイルを解析してリンクを取出す
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# 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) ⇒ Object
追加されたファイルを集める
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# 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 = []) ⇒ Object
ディレクトリが違うときにサブディレクトリを引数に実行する
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# 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) ⇒ Object
新規リンクを追加する
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# 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 = "append #{len} links" else # dry-run = "found #{len} links (can update with -u option)" end else = "not found" end $stderr.print , "\n" if && @config[:verbose] outfile end |
#regFormat(name, extentions) ⇒ Object
フォーマットとファイル拡張子を登録する
41 42 43 44 45 |
# File 'lib/memorack/mdmenu.rb', line 41 def regFormat(name, extentions) extentions.each { |value| @extentions[value] = name } end |
#regFormats(formats) ⇒ Object
フォーマット・ハッシュからファイル拡張子を登録する
34 35 36 37 38 |
# File 'lib/memorack/mdmenu.rb', line 34 def regFormats(formats) formats.each { |name, extentions| regFormat(name, extentions) } end |
#stdout(path, mode = 'r', perm = 0666) ⇒ Object
標準出力を切換える
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# 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 |