Class: Docwright::Merger

Inherits:
Object
  • Object
show all
Defined in:
lib/docwright/merger.rb

Constant Summary collapse

AUTO_START =
"<!-- DOCWRIGHT:AUTO -->"
AUTO_END =
"<!-- DOCWRIGHT:END -->"

Class Method Summary collapse

Class Method Details

.fresh(path, auto_content) ⇒ Object



24
25
26
27
# File 'lib/docwright/merger.rb', line 24

def self.fresh(path, auto_content)
  content = "#{AUTO_START}\n#{auto_content}\n#{AUTO_END}"
  File.write(path, content)
end

.fresh_named(path, name, auto_content, notes_placeholder) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/docwright/merger.rb', line 29

def self.fresh_named(path, name, auto_content, notes_placeholder)
  content = ""
  content += "<!-- DOCWRIGHT:AUTO:#{name} -->\n"
  content += "#{auto_content}\n"
  content += "<!-- DOCWRIGHT:END:#{name} -->\n"
  content += "#{notes_placeholder}\n"
  File.write(path, content)
end

.merge(path, auto_content) ⇒ Object



38
39
40
41
42
43
# File 'lib/docwright/merger.rb', line 38

def self.merge(path, auto_content)
  existing = File.read(path)
  replacement = "#{AUTO_START}\n#{auto_content}\n#{AUTO_END}"
  updated = existing.gsub(/#{Regexp.escape(AUTO_START)}.*?#{Regexp.escape(AUTO_END)}/m, replacement)
  File.write(path, updated)
end

.merge_named(path, name, auto_content, notes_placeholder) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/docwright/merger.rb', line 45

def self.merge_named(path, name, auto_content, notes_placeholder)
  existing = File.read(path)
  start_marker = "<!-- DOCWRIGHT:AUTO:#{name} -->"
  end_marker = "<!-- DOCWRIGHT:END:#{name} -->"
  replacement = "#{start_marker}\n#{auto_content}\n#{end_marker}"

  updated = if existing.include?(start_marker)
              existing.gsub(/#{Regexp.escape(start_marker)}.*?#{Regexp.escape(end_marker)}/m, replacement)
            else
              existing + "\n#{replacement}\n#{notes_placeholder}\n"
            end
  File.write(path, updated)
end

.write(path, auto_content) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/docwright/merger.rb', line 8

def self.write(path, auto_content)
  if File.exist?(path)
    merge(path, auto_content)
  else
    fresh(path, auto_content)
  end
end

.write_named(path, name, auto_content, notes_placeholder) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/docwright/merger.rb', line 16

def self.write_named(path, name, auto_content, notes_placeholder)
  if File.exist?(path)
    merge_named(path, name, auto_content, notes_placeholder)
  else
    fresh_named(path, name, auto_content, notes_placeholder)
  end
end