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 site_url].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, parent_data: nil) ⇒ Config

Returns a new instance of Config.



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
79
80
81
82
# File 'lib/ligarb/config.rb', line 33

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"]
  @description     = data.fetch("description", nil)
  @site_url        = data.fetch("site_url", nil)
  @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.



28
29
30
# File 'lib/ligarb/config.rb', line 28

def ai_generated
  @ai_generated
end

#authorObject (readonly)

Returns the value of attribute author.



28
29
30
# File 'lib/ligarb/config.rb', line 28

def author
  @author
end

#base_dirObject (readonly)

Returns the value of attribute base_dir.



28
29
30
# File 'lib/ligarb/config.rb', line 28

def base_dir
  @base_dir
end

#bibliographyObject (readonly)

Returns the value of attribute bibliography.



28
29
30
# File 'lib/ligarb/config.rb', line 28

def bibliography
  @bibliography
end

#chapter_numbersObject (readonly)

Returns the value of attribute chapter_numbers.



28
29
30
# File 'lib/ligarb/config.rb', line 28

def chapter_numbers
  @chapter_numbers
end

#descriptionObject (readonly)

Returns the value of attribute description.



28
29
30
# File 'lib/ligarb/config.rb', line 28

def description
  @description
end

Returns the value of attribute footer.



28
29
30
# File 'lib/ligarb/config.rb', line 28

def footer
  @footer
end

#github_reviewObject (readonly)

Returns the value of attribute github_review.



28
29
30
# File 'lib/ligarb/config.rb', line 28

def github_review
  @github_review
end

#languageObject (readonly)

Returns the value of attribute language.



28
29
30
# File 'lib/ligarb/config.rb', line 28

def language
  @language
end

#output_dirObject (readonly)

Returns the value of attribute output_dir.



28
29
30
# File 'lib/ligarb/config.rb', line 28

def output_dir
  @output_dir
end

#repositoryObject (readonly)

Returns the value of attribute repository.



28
29
30
# File 'lib/ligarb/config.rb', line 28

def repository
  @repository
end

#site_urlObject (readonly)

Returns the value of attribute site_url.



28
29
30
# File 'lib/ligarb/config.rb', line 28

def site_url
  @site_url
end

#sourcesObject (readonly)

Returns the value of attribute sources.



28
29
30
# File 'lib/ligarb/config.rb', line 28

def sources
  @sources
end

#structureObject (readonly)

Returns the value of attribute structure.



28
29
30
# File 'lib/ligarb/config.rb', line 28

def structure
  @structure
end

#styleObject (readonly)

Returns the value of attribute style.



28
29
30
# File 'lib/ligarb/config.rb', line 28

def style
  @style
end

#titleObject (readonly)

Returns the value of attribute title.



28
29
30
# File 'lib/ligarb/config.rb', line 28

def title
  @title
end

#translationsObject (readonly)

Returns the value of attribute translations.



28
29
30
# File 'lib/ligarb/config.rb', line 28

def translations
  @translations
end

Instance Method Details

#all_file_pathsObject

Returns all file paths including part title pages



138
139
140
# File 'lib/ligarb/config.rb', line 138

def all_file_paths
  collect_all_paths(@structure)
end

#appendix_labelObject



100
101
102
# File 'lib/ligarb/config.rb', line 100

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

#bibliography_pathObject



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

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)



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

def chapter_paths
  collect_chapter_paths(@structure)
end


121
122
123
124
125
126
127
128
129
130
# File 'lib/ligarb/config.rb', line 121

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)


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

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

#github_review_issue_templateObject



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

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



116
117
118
119
# File 'lib/ligarb/config.rb', line 116

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

#output_pathObject



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

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

#style_pathObject



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

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

#translations_hub?Boolean

Returns:

  • (Boolean)


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

def translations_hub?
  @translations_hub == true
end