Class: Ligarb::Writer

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

Defined Under Namespace

Classes: WriterError

Constant Summary collapse

BRIEF_FIELDS_FOR_BOOK_YML =
%w[author output_dir chapter_numbers style repository].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(brief_path, no_build: false) ⇒ Writer

Returns a new instance of Writer.



13
14
15
16
# File 'lib/ligarb/writer.rb', line 13

def initialize(brief_path, no_build: false)
  @brief_path = File.expand_path(brief_path)
  @no_build = no_build
end

Class Method Details

.init_brief(directory = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ligarb/writer.rb', line 49

def self.init_brief(directory = nil)
  dir = directory || "."
  target = File.expand_path(dir)
  path = File.join(target, "brief.yml")

  if File.exist?(path)
    raise WriterError, "#{path} already exists."
  end

  FileUtils.mkdir_p(target)

  File.write(path, <<~YAML)
    # brief.yml - Book brief for ligarb write
    title: "My Book"
    language: ja
    audience: ""
    notes: |
      5章くらいで。
  YAML

  claude_md = File.join(target, "CLAUDE.md")
  created_claude_md = false
  unless File.exist?(claude_md)
    File.write(claude_md, <<~MD)
      # ligarb book project

      This is a book project using [ligarb](https://github.com/ko1/ligarb).

      ## Commands

      - `ligarb build` — Build the book (generates build/index.html)
      - `ligarb help` — Show full specification (Markdown syntax, config options, etc.)

      ## Key rules

      - All chapter files are Markdown (.md), listed in book.yml
      - The first h1 in each file is the chapter title
      - Use ```mermaid, ```math, admonitions (> [!NOTE]), etc. as needed
      - Run `ligarb build` after changes to verify the output
    MD
    created_claude_md = true
  end

  puts "Created #{path}"
  puts "Created #{claude_md}" if created_claude_md
  brief_arg = directory ? " #{path}" : ""
  unless system("git", "rev-parse", "--git-dir", out: File::NULL, err: File::NULL, chdir: target)
    gitignore = File.join(target, ".gitignore")
    unless File.exist?(gitignore)
      File.write(gitignore, "# Generated by ligarb - remove lines as needed\nbuild/\n.ligarb/\n")
    end
    system("git", "init", chdir: target)
    puts "Initialized git repository"
  end

  puts "Edit brief.yml, then run 'ligarb write#{brief_arg}' to generate the book."
end

Instance Method Details

#runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ligarb/writer.rb', line 18

def run
  check_claude_installed!
  brief = load_brief
  output_dir = output_dir_for(brief)
  book_yml_path = File.join(output_dir, "book.yml")

  if File.exist?(book_yml_path)
    raise WriterError, "#{book_yml_path} already exists. Remove it first to regenerate."
  end

  FileUtils.mkdir_p(output_dir)
  prompt = build_prompt(brief, output_dir)
  run_claude(prompt)

  unless File.exist?(book_yml_path)
    raise WriterError, "Claude did not generate book.yml in #{output_dir}"
  end

  puts "Book files generated in #{output_dir}"

  unless @no_build
    puts "Building..."
    require_relative "builder"
    Builder.new(book_yml_path).build
  end

  git_commit_initial(output_dir, brief["title"]) if git_available?(output_dir)

  book_yml_path
end