Class: Docco::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/docco/writer.rb

Overview

Writes documents to the file system.

The Writer class handles writing page content to disk with proper directory creation, path handling, and optional overwrite protection. It transforms logical page paths into actual file system paths, appending ‘index.html’ to directory paths as needed.

The above example writes the following files:

output/index.html
output/docs/index.html
output/styles.css

Examples:

Writing pages to output directory

pages = {
  '' => '<html>Home</html>',
  '/docs' => '<html>Docs</html>',
  '/styles.css' => 'body { color: red; }'
}
writer = Docco::Writer.new(pages, output_dir: 'output', overwrite: false)
report = writer.write

Instance Method Summary collapse

Constructor Details

#initialize(pages, output_dir:, overwrite: false) ⇒ Writer

Initializes a new Writer instance with pages and output configuration.

Transforms page paths by:

  • Converting relative paths to absolute paths within output_dir

  • Appending ‘index.html’ to paths without file extensions (directory paths)

  • Creating Pathname objects for each path

Examples:

writer = Docco::Writer.new(
  { '' => 'Home', '/docs' => 'Documentation' },
  output_dir: 'output',
  overwrite: false
)

Parameters:

  • pages (Hash<String, String>)

    Hash of page paths to content strings. Keys are logical page paths (e.g., ”, ‘/docs’, ‘/assets/style.css’). Values are the content to write to those files.

  • output_dir (String)

    The root directory where pages will be written. All page paths will be relative to this directory.

  • overwrite (Boolean) (defaults to: false)

    Whether to overwrite existing files. If false (default), existing files will not be modified. If true, existing files will be overwritten with new content.



51
52
53
54
55
56
57
58
59
# File 'lib/docco/writer.rb', line 51

def initialize(pages, output_dir:, overwrite: false)
  @pages = pages.transform_keys do |path|
    path = Pathname.new(File.join(output_dir, path))
    path += 'index.html' if path.extname.empty?
    path
  end

  @overwrite = overwrite
end

Instance Method Details

#writeObject



61
62
63
64
65
# File 'lib/docco/writer.rb', line 61

def write
  @pages.each.with_object({}) do |(path, content), memo|
    memo[path.to_s] = write_page(path, content)
  end
end