Module: Atom

Defined in:
lib/atom/plugin.rb,
lib/atom/helpers.rb,
lib/atom/plugger.rb,
lib/atom_version.rb,
lib/atom/generate.rb,
lib/atom/scaffold.rb

Defined Under Namespace

Classes: Generate, Plugger, Plugin, Scaffold

Constant Summary collapse

VERSION =
'0.1.4'

Class Method Summary collapse

Class Method Details

.get_src_file_by_title(title, src_dir) ⇒ Object



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

def self.get_src_file_by_title(title, src_dir)
  file_title = title.downcase.split.join('_')
  result = Dir.glob("#{Atom::PATH}/source/#{src_dir}/?_#{file_title}.*")
  if result.size == 1
    result.first
  elsif result.size == 0
    raise "Title '#{title}' not found"
  else 
    raise "Ambiguous title '#{title}'"
  end
end

.load_configObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/atom/helpers.rb', line 2

def self.load_config
  const_set(:PATH, Dir.pwd)
  const_set(:CONFIG, YAML.load_file("#{PATH}/config/atom.yml"))
  const_set(:PRE_PLUGINS, CONFIG['plugins']['pre'])
  const_set(:POST_PLUGINS, CONFIG['plugins']['post'])
  const_set(:PLUGINS, PRE_PLUGINS | POST_PLUGINS)
  
  unless CONFIG[:author]
    begin
      CONFIG[:author] = `git config --get user.name`.chomp
    rescue
      CONFIG[:author] = 'unknown'
    end
  end
end

.name(type, title) ⇒ Object



70
71
72
73
# File 'lib/atom/helpers.rb', line 70

def self.name(type, title)
  # TODO abastract file extension to accomodate markup choice
  "#{type.split('').first}_#{title.chomp.downcase.split.join('_')}.textile"
end

.read_yaml(content) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/atom/helpers.rb', line 18

def self.read_yaml(content)
  begin
    if content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
      content = $'
      data = YAML.load($1)
    end
  rescue => e
    $stderr.puts "YAML Exception: #{e.message}"
  end

  data ||= {}
  return [data, content]
end

.sub_topics(file) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/atom/helpers.rb', line 32

def self.sub_topics(file)
  map_data, content = Atom::read_yaml(File.read(file))
  new_file = ''

  content.split("\n").each do |line|
    if line.match(/^=/)
      depth, title = line.match(/^=(\d)? (.*)/)[1..2]
      depth ||= 0
      data, content = Atom::read_yaml(
        File.read(
          Atom::get_src_file_by_title(title, 'topics')
        ).gsub(/^h(\d)(.*)/) { "h#{($1.to_i + depth.to_i).to_s}#{$2}" }
      )
      
      new_file += "<section class=\"#{data['class']}\" author=\"#{data['author']}\">\n"
      new_file += "\n#{content}\n"
      new_file += "</section>"
      new_file += "\n"
    else
      new_file += "#{line}\n"
    end
  end
    
  return new_file
end

.write_file(path, content) ⇒ Object

Ensure files are written in a consistant manner



76
77
78
79
80
81
# File 'lib/atom/helpers.rb', line 76

def self.write_file(path, content)
  file = File.open(path, "w")
  file.write(content)
  file.flush
  file.close
end