Module: ScribeDown

Defined in:
lib/section.rb,
lib/scribedown.rb,
lib/res/resources.rb,
lib/generate/helpers.rb,
lib/generate/renderer.rb

Defined Under Namespace

Modules: Renderer, Res Classes: Section

Class Method Summary collapse

Class Method Details

.generate(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
57
58
# File 'lib/scribedown.rb', line 8

def self.generate(options={})
  begin
    settings = Res.yaml_contents(Res.read_file('scribe.yml', in_fs: true))
  rescue Exception => e
    abort "FATAL: Not a scribedown directory. Unable to read scribe.yml.\n" + e.message
  end
  sections_yaml = settings[:sections]
  sections = Array.new
  
  lookup = lookup_files()
  failed = Array.new
  
  glob_default = Res.yaml_contents(Res.read_file('default.yml'))[:default]
  defaults = settings[:default] || {}
  
  if glob_default
    defaults.merge! glob_default
  end
  Res.symbolize(defaults)
  # Extra styles don't override the defaults
  defaults[:styles] += defaults[:extra_styles] || []
  
  sections_yaml.each do |section|
    name = section
    ops = defaults.clone
    if section.is_a? Hash
      name = section.select {|k, v| v == nil }.first.first
      ops = ops.merge(section)
      ops.delete(name)
    end
    path = lookup[name.downcase]
    if path
      ops[:path] = path
      ops[:name] = name
      sections.push Section.new(ops)
    else
      failed.push section
    end
  end

  output = defaults[:output]
  type = output['default']
  html = Renderer.to_html sections, defaults
  if type == 'all' || type == 'html'
    Res.create_file(defaults[:output]['html'], html)
  end
  if type == 'all' || type == 'pdf'
    pdf = Renderer.to_pdf html
    Res.create_file(defaults[:output]['pdf'], pdf)
  end
end

.initObject



69
70
71
72
73
74
75
76
# File 'lib/scribedown.rb', line 69

def self.init
  if File.exist? 'scribe.yml'
    abort 'ScribeDown already initialised: scribe.yml exists'
  end
  FileUtils.cp_r(Res.root('resources/init/.'), '.')
  # Res.create_file('scribe.yml', Res.read_res('scribe.yml'))
  # Res.create_file('sections/default_section.md', Res.read_res('default_section.md'))
end

.lookup_filesObject



60
61
62
63
64
65
66
67
# File 'lib/scribedown.rb', line 60

def self.lookup_files
  file_lookup = Hash.new
  Dir['**/*'].each do |file|
    name = File.basename(file).split('.').first.downcase
    file_lookup[name] = file
  end
  file_lookup
end

.new_section(name) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/scribedown.rb', line 78

def self.new_section(name)
  begin
    settings = Res.yaml_contents(Res.read_file('scribe.yml', in_fs: true))
  rescue
    abort 'FATAL: Not a scribedown directory. Unable to read scribe.yml.'
  end
  new_name = name.split('.').first
  if new_name == name
    name = name + '.md'
  end
  settings[:sections].push new_name
  Res.create_file('sections/' + name, '# ' + new_name)
  Res.create_file('scribe.yml', settings.to_yaml)
end