Class: Ucode::Site::Generator

Inherits:
Object
  • Object
show all
Includes:
Repo::AtomicWrites
Defined in:
lib/ucode/site/generator.rb

Overview

Orchestrates ‘ucode site init` and `ucode site build`.

init copies the static Vitepress template from ‘lib/ucode/site/template/` into the user’s ‘site/`. The template ships package.json, theme, and the dynamic route components (`char/.vue`, `block/.vue`, `plane/.md` stub).

build regenerates only the parts that depend on the dataset:

- `.vitepress/config.ts`           — ConfigEmitter
- `public/data/`                   — symlinked or copied from `output/`
- `public/data/index/search.json`  — SearchIndex
- `plane/<n>.md`                   — one thin stub per plane (frontmatter)
- `block/<id>.md`                  — one thin stub per block

Static pages are markdown stubs that mount a Vue component; the component fetches the JSON for that plane/block at runtime. This keeps the generator cheap (~363 small writes) and the per-character route dynamic (~160k static pages is infeasible).

Idempotent: every write goes through AtomicWrites.

Instance Method Summary collapse

Methods included from Repo::AtomicWrites

#same_content?, #to_pretty_json, #write_atomic

Constructor Details

#initialize(output_root:, site_root:) ⇒ Generator

Returns a new instance of Generator.

Parameters:

  • output_root (String, Pathname)

    dataset root (read)

  • site_root (String, Pathname)

    Vitepress project root (write)



42
43
44
45
# File 'lib/ucode/site/generator.rb', line 42

def initialize(output_root:, site_root:)
  @output_root = Pathname.new(output_root)
  @site_root = Pathname.new(site_root)
end

Instance Method Details

#buildHash{Symbol => Integer}

Regenerate config + pages + search index from the current ‘output/` tree. Returns a tally of what changed.

Returns:

  • (Hash{Symbol => Integer})


62
63
64
65
66
67
68
69
70
71
# File 'lib/ucode/site/generator.rb', line 62

def build
  tally = { config: 0, pages: 0, search: 0, data_link: 0 }

  tally[:config] = config_emitter.emit ? 1 : 0
  tally[:pages] = write_pages
  tally[:search] = search_index.build ? 1 : 0
  tally[:data_link] = link_data_dir ? 1 : 0

  tally
end

#initInteger

Copy the static template into ‘site_root`. No-op for any file that already exists with identical content (AtomicWrites).

Returns:

  • (Integer)

    number of files written



50
51
52
53
54
55
56
57
# File 'lib/ucode/site/generator.rb', line 50

def init
  count = 0
  each_template_file do |src, rel|
    dst = @site_root.join(rel)
    count += 1 if write_atomic(dst, src.read)
  end
  count
end