Sha256: a0b08a86e02ee9ae92f1189bbaa736cdc716ddfc90a50971f28f1b9152c9528a

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

require 'json'
require 'erubi'
require 'tilt'
require 'fileutils'

module Cosensee
  # generate HTML files
  class HtmlBuilder
    def initialize(project, root_dir: nil)
      @project = project
      @templates_dir = File.join(__dir__, '../../templates')
      @root_dir = root_dir || File.join(Dir.pwd, '.out')
      FileUtils.mkdir_p(@root_dir)
    end

    attr_reader :project, :root_dir, :templates_dir

    def build_all
      build_index(project)

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

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

    def build_index(project)
      template = Tilt::ErubiTemplate.new(File.join(templates_dir, 'index.html.erb'))
      output = template.render(nil, project:)
      File.write(File.join(root_dir, 'index.html'), output)
    end

    def build_page(page)
      template = Tilt::ErubiTemplate.new(File.join(templates_dir, 'page.html.erb'))
      output = template.render(nil, project:, page:, title: page.title)
      File.write(File.join(root_dir, page.link_path), output)
    end

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

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

    def page_title(page)
      "#{page.title} | #{project.title}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cosensee-0.6.0 lib/cosensee/html_builder.rb