Class: Yanagi::DocSync

Inherits:
Object
  • Object
show all
Defined in:
lib/yanagi/doc_sync.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil) ⇒ DocSync

Returns a new instance of DocSync.



18
19
20
# File 'lib/yanagi/doc_sync.rb', line 18

def initialize(path: nil)
  @path = path || self.class.default_path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/yanagi/doc_sync.rb', line 7

def path
  @path
end

Class Method Details

.default_pathObject



9
10
11
12
13
14
15
16
# File 'lib/yanagi/doc_sync.rb', line 9

def self.default_path
  candidates = [
    File.expand_path("~/projects/meijin/shared/transliteration.md"),
    File.expand_path("shared/transliteration.md", Dir.pwd),
    File.expand_path("../../shared/transliteration.md", __dir__)
  ]
  candidates.find { |p| File.exist?(p) } || candidates.first
end

Instance Method Details

#diff(rules = nil) ⇒ Object



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
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
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/yanagi/doc_sync.rb', line 22

def diff(rules = nil)
  rules ||= Rules.mora_map
  return [{ kind: :file_not_found, path: @path }] unless File.exist?(@path)

  content = File.read(@path, encoding: "UTF-8")
  differences = []

  # 1. Parse §1 table
  table_1_entries = parse_section_1_table(content)
  table_1_entries.each do |entry|
    entry[:kanas].each do |kana|
      rule_data = rules[kana.to_sym]
      next unless rule_data

      expected_cyr = rule_data[:cyrillic]
      expected_forb = rule_data[:forbidden] || []

      # Check if doc Cyrillic matches expected (either exact mora, base consonant prefix, or included in listed items)
      matched_cyr = entry[:cyrillic_items].include?(expected_cyr) ||
                    (entry[:base_cyrillic] && expected_cyr.start_with?(entry[:base_cyrillic]))

      unless matched_cyr
        differences << {
          kind: :mora_mismatch,
          section: 1,
          kana: kana,
          doc: entry[:base_cyrillic] || entry[:cyrillic_items].join(", "),
          rules: expected_cyr
        }
      end

      # Check forbidden variants
      if entry[:forbidden_items].any?
        # At least one forbidden form or base prefix in doc should match rule's forbidden
        matched_forb = entry[:forbidden_items].any? do |df|
          expected_forb.any? { |rf| rf == df || rf.start_with?(df) }
        end || (entry[:base_forbidden] && expected_forb.any? { |rf| rf.start_with?(entry[:base_forbidden]) })

        unless matched_forb
          differences << {
            kind: :forbidden_mismatch,
            section: 1,
            kana: kana,
            doc_forbidden: entry[:forbidden_items].join(", "),
            rules_forbidden: expected_forb
          }
        end
      end
    end
  end

  # 2. Parse §2 bullet points (Yōon)
  yoon_entries = parse_section_2_yoon(content)
  yoon_entries.each do |entry|
    entry[:kanas].each_with_index do |kana, idx|
      expected_cyr = rules.dig(kana.to_sym, :cyrillic)
      next unless expected_cyr

      doc_cyr = entry[:cyrillics][idx] || entry[:cyrillics].first
      if doc_cyr && doc_cyr != expected_cyr
        differences << {
          kind: :mora_mismatch,
          section: 2,
          kana: kana,
          doc: doc_cyr,
          rules: expected_cyr
        }
      end
    end
  end

  differences
end

#synced?(rules = nil) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/yanagi/doc_sync.rb', line 96

def synced?(rules = nil)
  diff(rules).empty?
end