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, extra_args = []) ⇒ DocBuilder

Returns a new instance of DocBuilder.



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

def initialize(options, extra_args = [])
  @pages = {}
  @errors = []
  @dependencies = options.fetch('dependencies', nil) || []
  @index = options['index']
  @base_path = options.fetch('base_path', Dir.pwd)
  @renderer = options.fetch('renderer', MarkdownRenderer)
  @source = Array(options['source'])
  @destination = options['destination']
  @documentation_assets = options['documentation_assets']
  @config_yml = options['config_yml']
  @plugins = Plugins.new(options.fetch('config_yml', {}), extra_args)
  @nav_level = options['nav_level'] || 'page'
  @exit_on_warnings = options['exit_on_warnings']
  @code_example_templates = options['code_example_templates']
  @code_example_renderers = options['code_example_renderers']
  @custom_extensions = Array(options['custom_extensions'])
  @ignore_paths = options.fetch('ignore_paths', [])

  if @exit_on_warnings
    DisplayMessage.exit_on_warnings!
  end
end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



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

def base_path
  @base_path
end

#config_ymlObject

Returns the value of attribute config_yml.



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

def config_yml
  @config_yml
end

#dependenciesObject

Returns the value of attribute dependencies.



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

def dependencies
  @dependencies
end

#destinationObject

Returns the value of attribute destination.



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

def destination
  @destination
end

#doc_assets_dirObject (readonly)

Returns the value of attribute doc_assets_dir.



7
8
9
# File 'lib/hologram/doc_builder.rb', line 7

def doc_assets_dir
  @doc_assets_dir
end

#doc_blocksObject

Returns the value of attribute doc_blocks.



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

def doc_blocks
  @doc_blocks
end

#documentation_assetsObject

Returns the value of attribute documentation_assets.



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

def documentation_assets
  @documentation_assets
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

Returns the value of attribute footer_erb.



7
8
9
# File 'lib/hologram/doc_builder.rb', line 7

def footer_erb
  @footer_erb
end

#header_erbObject (readonly)

Returns the value of attribute header_erb.



7
8
9
# File 'lib/hologram/doc_builder.rb', line 7

def header_erb
  @header_erb
end

#indexObject

Returns the value of attribute index.



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

def index
  @index
end

#input_dirObject (readonly)

Returns the value of attribute input_dir.



7
8
9
# File 'lib/hologram/doc_builder.rb', line 7

def input_dir
  @input_dir
end

#output_dirObject (readonly)

Returns the value of attribute output_dir.



7
8
9
# File 'lib/hologram/doc_builder.rb', line 7

def output_dir
  @output_dir
end

#pagesObject

Returns the value of attribute pages.



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

def pages
  @pages
end

#rendererObject

Returns the value of attribute renderer.



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

def renderer
  @renderer
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

Class Method Details

.from_yaml(yaml_file, extra_args = []) ⇒ Object



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

def self.from_yaml(yaml_file, extra_args = [])

  #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'])
  ), extra_args)

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hologram/doc_builder.rb', line 29

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",
    "code_example_templates/",
    "code_example_templates/markdown_example_template.html.erb",
    "code_example_templates/markdown_table_template.html.erb",
    "code_example_templates/js_example_template.html.erb",
    "code_example_templates/jsx_example_template.html.erb",
  ]
  DisplayMessage.created(new_files)
end

Instance Method Details

#buildObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hologram/doc_builder.rb', line 74

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)


93
94
95
96
97
98
99
100
101
# File 'lib/hologram/doc_builder.rb', line 93

def is_valid?
  errors.clear
  set_dirs
  validate_source
  validate_destination
  validate_document_assets

  errors.empty?
end