Class: Cosensee::HtmlBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/cosensee/html_builder.rb

Overview

generate HTML files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, output_dir: nil, css_dir: nil, base_url: nil) ⇒ HtmlBuilder

Returns a new instance of HtmlBuilder.



11
12
13
14
15
16
17
18
# File 'lib/cosensee/html_builder.rb', line 11

def initialize(project, output_dir: nil, css_dir: nil, base_url: nil)
  @project = project
  @templates_dir = File.join(__dir__, '../../templates')
  @output_dir = output_dir || File.join(Dir.pwd, Cosensee::DEFAULT_OUTPUT_DIR)
  @css_dir = css_dir || Cosensee::DEFAULT_CSS_DIR
  @base_url = base_url
  FileUtils.mkdir_p(@output_dir)
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



20
21
22
# File 'lib/cosensee/html_builder.rb', line 20

def base_url
  @base_url
end

#css_dirObject (readonly)

Returns the value of attribute css_dir.



20
21
22
# File 'lib/cosensee/html_builder.rb', line 20

def css_dir
  @css_dir
end

#output_dirObject (readonly)

Returns the value of attribute output_dir.



20
21
22
# File 'lib/cosensee/html_builder.rb', line 20

def output_dir
  @output_dir
end

#projectObject (readonly)

Returns the value of attribute project.



20
21
22
# File 'lib/cosensee/html_builder.rb', line 20

def project
  @project
end

#templates_dirObject (readonly)

Returns the value of attribute templates_dir.



20
21
22
# File 'lib/cosensee/html_builder.rb', line 20

def templates_dir
  @templates_dir
end

Instance Method Details

#build_all(clean: true) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cosensee/html_builder.rb', line 22

def build_all(clean: true)
  purge_files if clean

  build_index(project)

  # build all pages
  project.pages.each do |page|
    build_page(page)
  end

  # build all orphan (title only) pages
  project.orphan_page_titles.each do |title|
    build_page_only_title(title)
  end
end

#build_index(project) ⇒ Object



38
39
40
# File 'lib/cosensee/html_builder.rb', line 38

def build_index(project)
  render_html(src: 'index.html.erb', to: 'index.html', project:, css_dir:, base_url:)
end

#build_page(page) ⇒ Object



42
43
44
# File 'lib/cosensee/html_builder.rb', line 42

def build_page(page)
  render_html(src: 'page.html.erb', to: page.link_path, project:, css_dir:, page:, title: page.title, base_url:)
end

#build_page_only_title(title) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cosensee/html_builder.rb', line 46

def build_page_only_title(title)
  path = File.join(output_dir, "#{title.gsub(/ /, '_').gsub('/', '%2F')}.html")
  return if File.exist?(path)

  # make a dummy page
  page = Page.new(title:, id: 0, created: Time.now, updated: Time.now, views: 0, lines: [])

  template = Tilt::ErubiTemplate.new(File.join(templates_dir, 'page.html.erb'), escape_html: true)
  output = template.render(nil, project:, page:, title:, css_dir:, base_url:)
  File.write(path, output)
end

#purge_filesObject



58
59
60
# File 'lib/cosensee/html_builder.rb', line 58

def purge_files
  FileUtils.rm_rf(Dir.glob("#{output_dir}/*.html"))
end

#render_html(src:, to:, **args) ⇒ Object



62
63
64
65
66
# File 'lib/cosensee/html_builder.rb', line 62

def render_html(src:, to:, **args)
  template = Tilt::ErubiTemplate.new(File.join(templates_dir, src), escape_html: true)
  output = template.render(nil, **args)
  File.write(File.join(output_dir, to), output)
end