my_help 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,205 +1,6 @@
1
- # -*- coding: utf-8 -*-
2
- require "optparse"
3
- require "yaml"
1
+ require 'my_help/version.rb'
2
+ require 'my_help/my_help_controll'
4
3
  require 'my_help/org2yml'
5
- require 'my_help/yml2org'
6
- require "fileutils"
7
- require "my_help/version"
8
- require "systemu"
9
- require "colorize"
10
-
11
- module MyHelp
12
- class Command
13
-
14
- def self.run(argv=[])
15
- new(argv).execute
16
- end
17
-
18
- def initialize(argv=[])
19
- @argv = argv
20
- @template_dir = File.expand_path("../../lib/templates", __FILE__)
21
- @exe_path = File.expand_path("../../exe", __FILE__)
22
- @local_help_dir = File.join(ENV['HOME'],'.my_help')
23
- set_help_dir_if_not_exists
24
- end
25
-
26
- def set_help_dir_if_not_exists
27
- return if File::exists?(@local_help_dir)
28
- FileUtils.mkdir_p(@local_help_dir, :verbose=>true)
29
- Dir.entries(@template_dir).each{|file|
30
- next if file=='template_help.org'
31
- file_path=File.join(@local_help_dir,file)
32
- next if File::exists?(file_path)
33
- FileUtils.cp((File.join(@template_dir,file)),@local_help_dir,:verbose=>true)
34
- }
35
- end
36
-
37
- def execute
38
- @argv << '--help' if @argv.size==0
39
- command_parser = OptionParser.new do |opt|
40
- opt.on('-v', '--version','show program Version.') { |v|
41
- opt.version = MyHelp::VERSION
42
- puts opt.ver
43
- }
44
- opt.on('-l', '--list', 'list specific helps'){list_helps}
45
- opt.on('-e NAME', '--edit NAME', 'edit NAME help(eg test_help)'){|file| edit_help(file)}
46
- opt.on('-i NAME', '--init NAME', 'initialize NAME help(eq test_help)'){|file| init_help(file)}
47
- opt.on('-m', '--make', 'make executables for all helps'){make_help}
48
- opt.on('-c', '--clean', 'clean up exe dir.'){clean_exe_dir}
49
- opt.on('-y', '--yml2org [FILE]', 'convert FILE from yaml to org format'){|file| yml2org(file)}
50
- opt.on('--install_local','install local after edit helps'){install_local}
51
- opt.on('--delete NAME','delete NAME help'){|file| delete_help(file)}
52
- end
53
- begin
54
- command_parser.parse!(@argv)
55
- rescue=> eval
56
- p eval
57
- end
58
- exit
59
- end
60
-
61
- def yml2org(file)
62
- p target = File.join(@local_help_dir,file+'.yml')
63
- cont = YmlToOrg.new(target).contents
64
- dump = file+'.org'
65
- File.open(dump, 'w'){|file| file.print cont }
66
- delete_help(file)
67
- end
68
-
69
- def delete_help(file)
70
- del_files=[]
71
- del_files << File.join(@local_help_dir,file+'.org')
72
- exe_dir=File.join(File.expand_path('../..',@template_dir),'exe')
73
- exe_0_dir= @exe_path
74
- del_files << File.join(exe_dir,file)
75
- del_files << File.join(exe_0_dir,file)
76
- del_files << File.join(exe_dir,short_name(file))
77
- del_files << File.join(exe_0_dir,short_name(file))
78
- del_files.each do |file|
79
- print "Are you sure to delete "+file.blue+"?[Ynq] ".red
80
- case gets.chomp
81
- when 'Y'
82
- begin
83
- FileUtils.rm(file,:verbose=>true)
84
- rescue => error
85
- puts error.to_s.red
86
- end
87
- when 'n' ; next
88
- when 'q' ; exit
89
- end
90
- end
91
- end
92
-
93
- def install_local
94
- status, stdout = systemu 'gem env gemdir'
95
- system_inst_dir = stdout.chomp
96
- local_help_entries.each do |file|
97
- title = file.split('.')[0]
98
- [title, short_name(title)].each do |name|
99
- source = File.join(@exe_path, name)
100
- target = File.join(system_inst_dir, 'bin', name)
101
- FileUtils.cp(source, target, verbose: true)
102
- end
103
- end
104
- end
105
-
106
- def short_name(file)
107
- file_name=file.split('_')
108
- return file_name[0][0]+"_"+file_name[1][0]
109
- end
110
-
111
- def make_help
112
- local_help_entries.each{|file|
113
- title = file.split('.')[0]
114
- exe_cont=<<"EOS"
115
- #!/usr/bin/env ruby
116
- require 'specific_help'
117
- help_file = File.join(ENV['HOME'],'.my_help','#{file}')
118
- SpecificHelp::Command.run(help_file, ARGV)
119
- EOS
120
- [title, short_name(title)].each do |name|
121
- p target=File.join(@exe_path, name)
122
- File.open(target,'w'){|file| file.print exe_cont}
123
- FileUtils.chmod('a+x', target, :verbose => true)
124
- end
125
- }
126
- install_local
127
- end
128
-
129
- def clean_exe_dir
130
- local_help_entries.each{|file|
131
- next if ['emacs_help.org', 'my_help.org',
132
- 'my_todo.org', 'org_help.org'].include?(file)
133
- file = File.basename(file,'.org')
134
- [file, short_name(file)].each{|name|
135
- p target=File.join(@exe_path, name)
136
- begin
137
- FileUtils::Verbose.rm(target)
138
- rescue=> eval
139
- puts eval.to_s.red
140
- end
141
- }
142
- }
143
- end
144
-
145
- def init_help(file)
146
- p target_help=File.join(@local_help_dir,file+'.org')
147
- if File::exists?(target_help)
148
- puts "File exists. --delete it first to initialize it."
149
- exit
150
- end
151
- p template = File.join(@template_dir,'template_help.org')
152
- FileUtils::Verbose.cp(template,target_help)
153
- end
154
-
155
- def edit_help(file)
156
- target_help = File.join(@local_help_dir,file)
157
- ['.yml','.org'].each do |ext|
158
- p target_help += ext if local_help_entries.member?(file+ext)
159
- end
160
- system "emacs #{target_help}"
161
- end
162
-
163
- def local_help_entries
164
- entries= []
165
- Dir.entries(@local_help_dir).each{|file|
166
- next unless file.include?('_')
167
- next if file[0]=='#' or file[-1]=='~' or file[0]=='.'
168
- next if file.match(/(.+)_e\.org/) # OK?
169
- entries << file
170
- }
171
- return entries
172
- end
173
-
174
- def auto_load(file_path)
175
- case File.extname(file_path)
176
- when '.yml'
177
- cont = YAML.load(File.read(file_path))
178
- when '.org'
179
- cont = OrgToYaml.new(file_path).help_cont
180
- else
181
- puts "Not handling file types of #{file}"
182
- end
183
- cont
184
- end
185
-
186
- def list_helps
187
- print "Specific help file:\n"
188
- local_help_entries.each do |file|
189
- file_path=File.join(@local_help_dir,file)
190
- title = file.split('.')[0]
191
- begin
192
- help = auto_load(file_path)
193
- rescue=> eval
194
- p eval.to_s.red
195
- print "\n YAML load error in #{file}.".red
196
- print " Revise it by "+"my_help --edit #{title}\n".red
197
- exit
198
- end
199
- desc = help[:head][:cont].split("\n")[0]
200
- print " #{title}\t: #{desc}\n".blue
201
- end
202
- end
203
-
204
- end
205
- end
4
+ require 'colorize'
5
+ # Add requires for other files you add to your project here, so
6
+ # you just need to require this one file in your bin file
@@ -0,0 +1,139 @@
1
+ module MyHelp
2
+ class Control
3
+ def initialize()
4
+ @template_dir = File.expand_path("../../templates", __FILE__)
5
+ @exe_dir = File.expand_path("../../exe", __FILE__)
6
+ @local_help_dir = File.join(ENV['HOME'],'.my_help')
7
+ set_help_dir_if_not_exists
8
+ end
9
+
10
+ def set_help_dir_if_not_exists
11
+ return if File::exists?(@local_help_dir)
12
+ FileUtils.mkdir_p(@local_help_dir, :verbose=>true)
13
+ Dir.entries(@template_dir).each{|file|
14
+ next if file=='template_help.org'
15
+ file_path=File.join(@local_help_dir,file)
16
+ next if File::exists?(file_path)
17
+ FileUtils.cp((File.join(@template_dir,file)),@local_help_dir,:verbose=>true)
18
+ }
19
+ end
20
+
21
+ def show_item(file, item)
22
+ file_path=File.join(@local_help_dir,file+'.org')
23
+ help = auto_load(file_path)
24
+ select = select_item(help, item)
25
+ print help[:head][:cont]
26
+ puts '-'*5+"\n"+select.to_s.red
27
+ print help[select][:cont]
28
+ end
29
+
30
+ def select_item(help, item)
31
+ o_key = ''
32
+ help.each_pair do |key, cont|
33
+ p key
34
+ next if key==:license or key==:head
35
+ if cont[:opts][:short] == item or cont[:opts][:long] == item
36
+ o_key = key
37
+ break
38
+ end
39
+ end
40
+ o_key
41
+ end
42
+
43
+ def list_help(file)
44
+ file_path=File.join(@local_help_dir,file+'.org')
45
+ help = auto_load(file_path)
46
+ help.each_pair do |key, conts|
47
+ print conts[:cont] if key==:head
48
+ disp_opts( conts[:opts] )
49
+ end
50
+ end
51
+
52
+ def disp_opts( conts )
53
+ col = 0
54
+ conts.each_pair do |key, item|
55
+ col_length = case col
56
+ when 0; print item.rjust(5)+", "
57
+ when 1; print item.ljust(15)+": "
58
+ else; print item
59
+ end
60
+ col += 1
61
+ end
62
+ print("\n")
63
+ end
64
+
65
+ def list_all
66
+ print "List all helps\n"
67
+ local_help_entries.each do |file|
68
+ file_path=File.join(@local_help_dir,file)
69
+ title = file.split('.')[0]
70
+ help = auto_load(file_path)
71
+ next if help.nil?
72
+ desc = help[:head][:cont].split("\n")[0]
73
+ print " #{title}\t: #{desc}\n".blue
74
+ end
75
+ end
76
+
77
+ def edit_help(file)
78
+ target_help = File.join(@local_help_dir,file)
79
+ ['.yml','.org'].each do |ext|
80
+ p target_help += ext if local_help_entries.member?(file+ext)
81
+ end
82
+ system "emacs #{target_help}"
83
+ end
84
+
85
+ def init_help(file)
86
+ if file.nil?
87
+ puts "specify NAME".red
88
+ exit
89
+ end
90
+ p target_help=File.join(@local_help_dir,file+'.org')
91
+ if File::exists?(target_help)
92
+ puts "File exists. --delete it first to initialize it."
93
+ exit
94
+ end
95
+ p template = File.join(@template_dir,'template_help.org')
96
+ FileUtils::Verbose.cp(template,target_help)
97
+ end
98
+
99
+ def delete_help(file)
100
+ file = File.join(@local_help_dir,file+'.org')
101
+ print "Are you sure to delete "+file.blue+"?[Ynq] ".red
102
+ case STDIN.gets.chomp
103
+ when 'Y'
104
+ begin
105
+ FileUtils.rm(file,:verbose=>true)
106
+ rescue => error
107
+ puts error.to_s.red
108
+ end
109
+ when 'n', 'q' ; exit
110
+ end
111
+ end
112
+
113
+ private
114
+ def local_help_entries
115
+ entries= []
116
+ Dir.entries(@local_help_dir).each{|file|
117
+ next unless file.include?('_')
118
+ next if file[0]=='#' or file[-1]=='~' or file[0]=='.'
119
+ next if file.match(/(.+)_e\.org/) # OK?
120
+ next if file.match(/(.+)\.html/)
121
+ entries << file
122
+ }
123
+ return entries
124
+ end
125
+
126
+ def auto_load(file_path)
127
+ case File.extname(file_path)
128
+ when '.yml'
129
+ cont = YAML.load(File.read(file_path))
130
+ when '.org'
131
+ cont = OrgToYaml.new(file_path).help_cont
132
+ else
133
+ puts "Not handling file types of #{file_path}"
134
+ cont = nil
135
+ end
136
+ cont
137
+ end
138
+ end
139
+ end
@@ -9,6 +9,7 @@ class OrgToYaml
9
9
  @help_cont = {} #{ head: [File.basename(file, '.org')] }
10
10
  @head_sym = nil
11
11
  @conts = ''
12
+ @short_stored = []
12
13
  org_to_yaml(File.readlines(file))
13
14
  end
14
15
 
@@ -16,7 +17,12 @@ class OrgToYaml
16
17
  head, desc = line.split(':')
17
18
  desc ||= head.to_s
18
19
  short = "-#{head[0]}"
19
- { short: short, long: "--#{head}", desc: desc }
20
+ if @short_stored.include?(short) or head=='license' or head=='head'
21
+ short = ''
22
+ else
23
+ @short_stored << short
24
+ end
25
+ { short: short, long: "#{head}", desc: desc }
20
26
  end
21
27
 
22
28
  def next_cont(head)
@@ -41,7 +47,7 @@ class OrgToYaml
41
47
  end
42
48
  end
43
49
 
44
- if __FILE__ == $1
50
+ if $PROGRAM_NAME == __FILE__
45
51
  helps = OrgToYaml.new(ARGV[0])
46
52
  pp helps.help_cont
47
53
  end
@@ -1,3 +1,3 @@
1
1
  module MyHelp
2
- VERSION = "0.6.2"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -13,8 +13,6 @@
13
13
  - - でitem追加
14
14
  - * でitem追加
15
15
  - #+STARTUP: indent nolineimages nofold
16
- - #+BEGIN_QUOTE -- #+END_QUOTE
17
- - #+BEGIN_SRC bash -- #+END_SRC
18
16
  - #+TITLE: 格子欠陥について最近の研究から
19
17
  - #+AUTHOR: 関西学院大学・理工学部・情報科学科 西谷滋人 2017
20
18
  * key_bind
@@ -22,32 +20,37 @@
22
20
  - shift-tabで全部に対するtoggle
23
21
  - shift-矢印でkeyを切り替え
24
22
  - M-shift-<left, right>アウトラインのレベルをツリーごと移動
25
- - beamer出力はc-c c-eでlbを選択
26
- * markdown
27
- - in emacs, M-x org-md-export-as-markdown
28
- - pandoc -f markdown -t org -o tmp.md sample.org
29
- - 行の折り返し,M-x toggle-truncate-lines
30
- * format
31
- - #+OPTIONS: ^:{}でsub, superを抑制.
23
+ * convert
24
+ - #+OPTIONS: ^:{} でsub, superを抑制.
25
+ - markdown
26
+ 1. in emacs, M-x org-md-export-as-markdown
27
+ 2. pandoc -f markdown -t org -o tmp.md sample.org
28
+ 3. 行の折り返し,M-x toggle-truncate-lines
29
+ - latex出力
30
+ 1. c-c c-eでllを選択
31
+ 2. 日本語と数式が混じる時には,$の前後に半角スペースを入れる
32
+ 3. [[https://www-he.scphys.kyoto-u.ac.jp/member/shotakaha/dokuwiki/doku.php?id=toolbox:emacs:org:export:latex:start~]]
33
+ 4. https://orgmode.org/worg/org-tutorials/org-latex-export.html
34
+ - beamer出力
35
+ 1. c-c c-eでlbを選択
32
36
  * list
33
37
  - 項目- 子項目1
34
38
  - 番号付き1. 2.
35
39
  - 名前付き - 名前1 :: 内容1
36
40
  * link
37
41
  - [[file:./tmp/tmp.txt][Link to file]]
42
+ - [[link][name]]
38
43
  - to edit, c-c c-l
39
44
  - to open, c-c c-o
40
- * latex
41
- - latex出力はc-c c-eでllを選択
42
- - 日本語と数式が混じる時には,$の前後に半角スペースを入れる
43
- - [[https://www-he.scphys.kyoto-u.ac.jp/member/shotakaha/dokuwiki/doku.php?id=toolbox:emacs:org:export:latex:start~]]
44
- - https://orgmode.org/worg/org-tutorials/org-latex-export.html
45
+ - org-return-follows-linkが設定されていたらRETでも開く
45
46
  * todo
46
47
  - c-c c-tでno_head->TODO->DONEをloop
47
48
  - m-x agenda-modeでschedule操作可能コマンドを表示
48
- * verbose
49
+ * verb_and_source
49
50
  - ': 'を頭に
50
51
  - #+BEGIN_EXAMPLE...#+END_EXAMPLE
52
+ - #+BEGIN_QUOTE -- #+END_QUOTE
53
+ - #+BEGIN_SRC bash -- #+END_SRC
51
54
  * date_and_time
52
55
  - c-c .でカレンダーと日付表示<2018-03-14 Wed>
53
56
  - +7とか-1を入力してreturnするとその日付が出力
@@ -1,34 +1,24 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'my_help/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "my_help"
8
- spec.version = MyHelp::VERSION
9
- spec.authors = ["Shigeto R. Nishitani"]
10
- spec.email = ["shigeto_nishitani@me.com"]
11
-
12
- spec.summary = %q{display emacs key bindings in Japanese.}
13
- spec.description = %q{Emulating CUI(CLI) help, an user makes and displays his own helps.}
14
- spec.homepage = "https://github.com/daddygongon/my_help"
15
- spec.license = "MIT"
16
- spec.metadata["yard.run"] = "yri" # use "yard" to build full HTML docs.
17
- # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
- # delete this section to allow pushing this gem to any host.
19
- # if spec.respond_to?(:metadata)
20
- # spec.metadata['allowed_push_host'] = 'http://rubygems.org'
21
- # else
22
- # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
- # end
24
-
25
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
- spec.bindir = "exe"
27
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
- spec.require_paths = ["lib"]
29
-
30
- spec.add_development_dependency "bundler", "~> 1.11"
31
- spec.add_development_dependency "rake", "~> 10.0"
32
- spec.add_dependency "systemu"
33
- spec.add_dependency "colorize"
1
+ # Ensure we require the local version and not one we might have installed already
2
+ require File.join([File.dirname(__FILE__),'lib','my_help','version.rb'])
3
+ spec = Gem::Specification.new do |s|
4
+ s.name = 'my_help'
5
+ s.version = MyHelp::VERSION
6
+ s.author = 'Your Name Here'
7
+ s.email = 'your@email.address.com'
8
+ s.homepage = 'http://your.website.com'
9
+ s.platform = Gem::Platform::RUBY
10
+ s.summary = 'A description of your project'
11
+ s.files = `git ls-files`.split("
12
+ ")
13
+ s.require_paths << 'lib'
14
+ s.has_rdoc = true
15
+ s.extra_rdoc_files = ['README.rdoc','my_help.rdoc']
16
+ s.rdoc_options << '--title' << 'my_help' << '--main' << 'README.rdoc' << '-ri'
17
+ s.bindir = 'bin'
18
+ s.executables << 'my_help'
19
+ s.add_development_dependency('rake')
20
+ s.add_development_dependency('rdoc')
21
+ s.add_development_dependency('aruba')
22
+ s.add_runtime_dependency('gli','2.17.1')
23
+ s.add_runtime_dependency "colorize"
34
24
  end