Class: Ligarb::Config

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

Defined Under Namespace

Classes: Source, StructEntry, TranslationEntry

Constant Summary collapse

REQUIRED_KEYS =
%w[title chapters].freeze
INHERITABLE_KEYS =

Keys that can be inherited from a parent (translations hub) config. output_dir is intentionally excluded — it always comes from the config file that was directly passed to ‘ligarb build`.

%w[author language chapter_numbers style
repository ai_generated footer bibliography
github_review].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, parent_data: nil) ⇒ Config

Returns a new instance of Config.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
# File 'lib/ligarb/config.rb', line 31

def initialize(path, parent_data: nil)
  @base_dir = File.dirname(File.expand_path(path))
  data = YAML.safe_load_file(path)

  # If this is a translations hub (has translations but no chapters), skip normal validation
  if data.is_a?(Hash) && data.key?("translations") && !data.key?("chapters")
    @translations_hub = true
    @translations_data = data
    load_translations_hub(data)
    return
  end

  # Load inherit file if specified (for standalone builds of child configs)
  if !parent_data && data.is_a?(Hash) && data.key?("inherit")
    inherit_path = File.expand_path(data["inherit"], @base_dir)
    if File.exist?(inherit_path)
      parent_data = YAML.safe_load_file(inherit_path)
    else
      abort "Error: inherit config not found: #{inherit_path}"
    end
  end

  # Merge parent (inheritable) keys as defaults
  if parent_data
    INHERITABLE_KEYS.each do |key|
      data[key] = parent_data[key] if parent_data.key?(key) && !data.key?(key)
    end
  end

  validate!(data)

  @title           = data["title"]
  @author          = data.fetch("author", "")
  @language        = data.fetch("language", "en")
  @output_dir      = data.fetch("output_dir", "build")
  @chapter_numbers = data.fetch("chapter_numbers", true)
  @style           = data.fetch("style", nil)
  @repository      = data.fetch("repository", nil)
  @ai_generated    = data.fetch("ai_generated", false)
  @footer          = data.fetch("footer", nil)
  @bibliography    = data.fetch("bibliography", nil)
  @github_review   = data.fetch("github_review", nil)
  @sources         = parse_sources(data.fetch("sources", []))
  @structure       = parse_structure(data["chapters"])
  @translations    = []

  load_translations(data, path) if data.key?("translations")
end

Instance Attribute Details

#ai_generatedObject (readonly)

Returns the value of attribute ai_generated.



26
27
28
# File 'lib/ligarb/config.rb', line 26

def ai_generated
  @ai_generated
end

#authorObject (readonly)

Returns the value of attribute author.



26
27
28
# File 'lib/ligarb/config.rb', line 26

def author
  @author
end

#base_dirObject (readonly)

Returns the value of attribute base_dir.



26
27
28
# File 'lib/ligarb/config.rb', line 26

def base_dir
  @base_dir
end

#bibliographyObject (readonly)

Returns the value of attribute bibliography.



26
27
28
# File 'lib/ligarb/config.rb', line 26

def bibliography
  @bibliography
end

#chapter_numbersObject (readonly)

Returns the value of attribute chapter_numbers.



26
27
28
# File 'lib/ligarb/config.rb', line 26

def chapter_numbers
  @chapter_numbers
end

Returns the value of attribute footer.



26
27
28
# File 'lib/ligarb/config.rb', line 26

def footer
  @footer
end

#github_reviewObject (readonly)

Returns the value of attribute github_review.



26
27
28
# File 'lib/ligarb/config.rb', line 26

def github_review
  @github_review
end

#languageObject (readonly)

Returns the value of attribute language.



26
27
28
# File 'lib/ligarb/config.rb', line 26

def language
  @language
end

#output_dirObject (readonly)

Returns the value of attribute output_dir.



26
27
28
# File 'lib/ligarb/config.rb', line 26

def output_dir
  @output_dir
end

#repositoryObject (readonly)

Returns the value of attribute repository.



26
27
28
# File 'lib/ligarb/config.rb', line 26

def repository
  @repository
end

#sourcesObject (readonly)

Returns the value of attribute sources.



26
27
28
# File 'lib/ligarb/config.rb', line 26

def sources
  @sources
end

#structureObject (readonly)

Returns the value of attribute structure.



26
27
28
# File 'lib/ligarb/config.rb', line 26

def structure
  @structure
end

#styleObject (readonly)

Returns the value of attribute style.



26
27
28
# File 'lib/ligarb/config.rb', line 26

def style
  @style
end

#titleObject (readonly)

Returns the value of attribute title.



26
27
28
# File 'lib/ligarb/config.rb', line 26

def title
  @title
end

#translationsObject (readonly)

Returns the value of attribute translations.



26
27
28
# File 'lib/ligarb/config.rb', line 26

def translations
  @translations
end

Instance Method Details

#all_file_pathsObject

Returns all file paths including part title pages



134
135
136
# File 'lib/ligarb/config.rb', line 134

def all_file_paths
  collect_all_paths(@structure)
end

#appendix_labelObject



96
97
98
# File 'lib/ligarb/config.rb', line 96

def appendix_label
  @language == "ja" ? "付録" : "Appendix"
end

#bibliography_pathObject



92
93
94
# File 'lib/ligarb/config.rb', line 92

def bibliography_path
  @bibliography ? File.join(@base_dir, @bibliography) : nil
end

#chapter_pathsObject

Returns a flat list of all chapter file paths (excluding part title pages)



129
130
131
# File 'lib/ligarb/config.rb', line 129

def chapter_paths
  collect_chapter_paths(@structure)
end


117
118
119
120
121
122
123
124
125
126
# File 'lib/ligarb/config.rb', line 117

def effective_footer
  return @footer if @footer
  return nil unless @ai_generated

  if @language == "ja"
    "この章の内容は AI によって生成されました。正確性は保証されません。"
  else
    "This chapter was generated by AI. Accuracy is not guaranteed."
  end
end

#github_review_enabled?Boolean

Whether the reader’s “Report as issue” feedback UI is requested in book.yml. This reflects intent only; actual injection also requires ‘repository` (the issues/new base) — the builder warns and skips when it is missing.

Returns:

  • (Boolean)


103
104
105
# File 'lib/ligarb/config.rb', line 103

def github_review_enabled?
  @github_review.is_a?(Hash) && @github_review.fetch("enabled", false) == true
end

#github_review_issue_templateObject



107
108
109
110
# File 'lib/ligarb/config.rb', line 107

def github_review_issue_template
  tmpl = @github_review.is_a?(Hash) ? @github_review["issue_template"] : nil
  tmpl && !tmpl.to_s.empty? ? tmpl.to_s : "book-feedback.yml"
end

#github_review_labelsObject



112
113
114
115
# File 'lib/ligarb/config.rb', line 112

def github_review_labels
  labels = @github_review.is_a?(Hash) ? @github_review["labels"] : nil
  Array(labels || ["feedback"]).map(&:to_s)
end

#output_pathObject



84
85
86
# File 'lib/ligarb/config.rb', line 84

def output_path
  File.join(@base_dir, @output_dir)
end

#style_pathObject



88
89
90
# File 'lib/ligarb/config.rb', line 88

def style_path
  @style ? File.join(@base_dir, @style) : nil
end

#translations_hub?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/ligarb/config.rb', line 80

def translations_hub?
  @translations_hub == true
end