Class: Hologram::DocBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/hologram/doc_builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DocBuilder

Returns a new instance of DocBuilder.


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hologram/doc_builder.rb', line 38

def initialize(options)
  @pages = {}
  @errors = []
  @dependencies = options.fetch('dependencies', [])
  @index = options['index']
  @base_path = options.fetch('base_path', Dir.pwd)
  @renderer = options.fetch('renderer', MarkdownRenderer)
  @source = options['source']
  @destination = options['destination']
  @documentation_assets = options['documentation_assets']
  @config_yml = options['config_yml']
end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path


3
4
5
# File 'lib/hologram/doc_builder.rb', line 3

def base_path
  @base_path
end

#config_ymlObject

Returns the value of attribute config_yml


3
4
5
# File 'lib/hologram/doc_builder.rb', line 3

def config_yml
  @config_yml
end

#dependenciesObject

Returns the value of attribute dependencies


3
4
5
# File 'lib/hologram/doc_builder.rb', line 3

def dependencies
  @dependencies
end

#destinationObject

Returns the value of attribute destination


3
4
5
# File 'lib/hologram/doc_builder.rb', line 3

def destination
  @destination
end

#doc_assets_dirObject (readonly)

Returns the value of attribute doc_assets_dir


5
6
7
# File 'lib/hologram/doc_builder.rb', line 5

def doc_assets_dir
  @doc_assets_dir
end

#doc_blocksObject

Returns the value of attribute doc_blocks


3
4
5
# File 'lib/hologram/doc_builder.rb', line 3

def doc_blocks
  @doc_blocks
end

#documentation_assetsObject

Returns the value of attribute documentation_assets


3
4
5
# File 'lib/hologram/doc_builder.rb', line 3

def documentation_assets
  @documentation_assets
end

#errorsObject (readonly)

Returns the value of attribute errors


4
5
6
# File 'lib/hologram/doc_builder.rb', line 4

def errors
  @errors
end

Returns the value of attribute footer_erb


5
6
7
# File 'lib/hologram/doc_builder.rb', line 5

def footer_erb
  @footer_erb
end

#header_erbObject (readonly)

Returns the value of attribute header_erb


5
6
7
# File 'lib/hologram/doc_builder.rb', line 5

def header_erb
  @header_erb
end

#indexObject

Returns the value of attribute index


3
4
5
# File 'lib/hologram/doc_builder.rb', line 3

def index
  @index
end

#input_dirObject (readonly)

Returns the value of attribute input_dir


5
6
7
# File 'lib/hologram/doc_builder.rb', line 5

def input_dir
  @input_dir
end

#output_dirObject (readonly)

Returns the value of attribute output_dir


5
6
7
# File 'lib/hologram/doc_builder.rb', line 5

def output_dir
  @output_dir
end

#pagesObject

Returns the value of attribute pages


3
4
5
# File 'lib/hologram/doc_builder.rb', line 3

def pages
  @pages
end

#rendererObject

Returns the value of attribute renderer


3
4
5
# File 'lib/hologram/doc_builder.rb', line 3

def renderer
  @renderer
end

#sourceObject

Returns the value of attribute source


3
4
5
# File 'lib/hologram/doc_builder.rb', line 3

def source
  @source
end

Class Method Details

.from_yaml(yaml_file) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hologram/doc_builder.rb', line 7

def self.from_yaml(yaml_file)

  #Change dir so that our paths are relative to the config file
  base_path = Pathname.new(yaml_file)
  yaml_file = base_path.realpath.to_s
  Dir.chdir(base_path.dirname)

  config = YAML::load_file(yaml_file)
  raise SyntaxError if !config.is_a? Hash

  new(config.merge(
    'config_yml' => config,
    'base_path' => Pathname.new(yaml_file).dirname,
    'renderer' => Utils.get_markdown_renderer(config['custom_markdown'])
  ))

rescue SyntaxError, ArgumentError, Psych::SyntaxError
  raise SyntaxError, "Could not load config file, check the syntax or try 'hologram init' to get started"
end

.setup_dirObject


27
28
29
30
31
32
33
34
35
36
# File 'lib/hologram/doc_builder.rb', line 27

def self.setup_dir
  if File.exists?("hologram_config.yml")
    DisplayMessage.warning("Cowardly refusing to overwrite existing hologram_config.yml")
    return
  end

  FileUtils.cp_r INIT_TEMPLATE_FILES, Dir.pwd
  new_files = ["hologram_config.yml", "doc_assets/", "doc_assets/_header.html", "doc_assets/_footer.html"]
  DisplayMessage.created(new_files)
end

Instance Method Details

#buildObject


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hologram/doc_builder.rb', line 51

def build
  set_dirs
  return false if !is_valid?

  set_header_footer
  current_path = Dir.pwd
  Dir.chdir(base_path)
  # Create the output directory if it doesn't exist
  if !output_dir
    FileUtils.mkdir_p(destination)
    set_dirs #need to reset output_dir post-creation for build_docs.
  end
  # the real work happens here.
  build_docs
  Dir.chdir(current_path)
  DisplayMessage.success("Build completed. (-: ")
  true
end

#is_valid?Boolean

Returns:

  • (Boolean)

70
71
72
73
74
75
76
77
78
# File 'lib/hologram/doc_builder.rb', line 70

def is_valid?
  errors.clear
  set_dirs
  errors << "No source directory specified in the config file" if !source
  errors << "No destination directory specified in the config" if !destination
  errors << "No documentation assets directory specified" if !documentation_assets
  errors << "Can not read source directory (#{source}), does it exist?" if source && !input_dir
  errors.empty?
end