Class: Marsdawn::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/marsdawn/command.rb

Defined Under Namespace

Classes: ParamError, RuntimeError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Returns a new instance of Command.



22
23
24
25
26
# File 'lib/marsdawn/command.rb', line 22

def initialize
  @editor = 'vim'
  @auto_editor = true
  read_dot_marsdawn
end

Instance Attribute Details

#auto_editorObject

Returns the value of attribute auto_editor.



7
8
9
# File 'lib/marsdawn/command.rb', line 7

def auto_editor
  @auto_editor
end

#editorObject

Returns the value of attribute editor.



7
8
9
# File 'lib/marsdawn/command.rb', line 7

def editor
  @editor
end

Class Method Details

.exec(argv) ⇒ Object

Raises:



12
13
14
15
16
17
18
19
20
# File 'lib/marsdawn/command.rb', line 12

def self.exec argv
  raise ParamError.new("No command is specified.") if argv.empty?
  cmd = new
  command = argv.shift.to_sym
  raise ParamError.new("Unknown command '#{command}'.") unless cmd.respond_to?(command)
  value = (argv.size > 0 ? argv.shift : nil)
  opts = parse_options(argv)
  cmd.__send__ command, value, opts
end

Instance Method Details

#create(title, opts = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/marsdawn/command.rb', line 28

def create title, opts={}
  key = (opts.key?(:file) ? opts[:file] : file_namize(title))
  path = File.expand_path(key)
  raise "The directory '#{doc_name}' already exists." if File.exists?(path)
  Dir.mkdir path
  data = {key: key, title: title, lang: 'en', version: '0.0.1'}
  dot_file = File.join(path, '.marsdawn.yml')
  index_file = File.join(path, '.index.md')
  File.write dot_file, YAML.dump(data)
  create_page index_file, title, 'title' => title
  edit_cmd dot_file, index_file if @auto_editor || opts[:edit]
end

#debug(type, opts = {}) ⇒ Object



71
72
73
74
75
76
# File 'lib/marsdawn/command.rb', line 71

def debug type, opts={} 
  case type
  when 'options'
    opts
  end
end

#dir(title, opts = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/marsdawn/command.rb', line 41

def dir title, opts={}
  dir = (opts.key?(:file) ? opts[:file] : file_namize(title))
  dir = add_num(dir, opts)
  path = File.expand_path(dir)
  Dir.mkdir path
  index_file = File.join(path, '.index.md')
  create_page index_file, title, 'title' => title
  edit_cmd index_file if @auto_editor || opts[:edit]
end

#page(title, opts = {}) ⇒ Object



51
52
53
54
55
56
# File 'lib/marsdawn/command.rb', line 51

def page title, opts={}
  file = (opts.key?(:file) ? "#{opts[:file]}.md" : file_namize(title, '.md'))
  file = File.expand_path(add_num(file, opts))
  create_page file, title, 'title' => title
  edit_cmd file if @auto_editor || opts[:edit]
end

#renum(step, opts = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/marsdawn/command.rb', line 58

def renum step, opts={}
  step = (step.nil? ? 10 : step.to_i)
  num = 0
  list = Dir.glob('*').sort.each_with_object({}) do |item, ret|
    num += step
    ret[item] = add_num(item, num: num, step: step)
  end
  list.each do |src, dest|
    FileUtils.mv src, dest unless src == dest
  end
  'ls -1'
end