Class: Rawfeed::Layout

Inherits:
Object
  • Object
show all
Defined in:
lib/rawfeed/layout.rb

Constant Summary collapse

CONFIG_PATH =
Rawfeed::CONFIG['CONFIG_YML']
BACKUP_PATH =
"_config.yml.bak"

Class Method Summary collapse

Class Method Details

.blog_index(value) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/rawfeed/layout.rb', line 122

def self.blog_index(value)
  file = "blog/index.md"
  content = File.read(file)
  updated_published = content.gsub(/^published:\s*\S+$/, "published: #{value}")
  updated = updated_published.gsub(/^  enabled:\s*\S+$/, "  enabled: #{value}")
  File.write(file, updated)
end

.change_yml(section, key, new_value, path_match = nil) ⇒ Object



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rawfeed/layout.rb', line 30

def self.change_yml(section, key, new_value, path_match = nil)
  raise "File #{CONFIG_PATH} not found" unless File.exist?(CONFIG_PATH)

  yaml_value = case new_value
              when true then "true"
              when false then "false"
              else
                s = new_value.to_s
                s =~ /\A[-+]?\d+(\.\d+)?\z/ ? s : "\"#{s.gsub('"', '\"')}\""
              end

  lines = File.readlines(CONFIG_PATH)
  modified = false

  # === CASE 1: defaults → scope.path ===
  if section == "defaults"
    path_indices = lines.each_index.select { |i| lines[i] =~ /^\s*path:\s*["']?#{Regexp.escape(path_match)}["']?/ }
    path_indices.each do |path_i|
      start_i = (0..path_i).to_a.reverse.find { |j| lines[j] =~ /^\s*-\s*scope:/ }
      next unless start_i

      values_i = ((start_i + 1)..[lines.length - 1, start_i + 40].min)
                  .find { |k| lines[k] =~ /^\s*values:\s*$/ }
      next unless values_i

      dash_indent = lines[start_i][/^\s*/].size
      published_i = nil
      m = values_i + 1
      while m < lines.length
        line = lines[m]
        break if line =~ /^\s*-\s*scope:/ && line[/^\s*/].size == dash_indent
        break if line =~ /^\S/ && line[/^\s*/].size <= dash_indent
        if line =~ /^\s*#{key}\s*:/
          published_i = m
          break
        end
        m += 1
      end

      if published_i
        indent = lines[published_i][/^\s*/]
        end_comment = lines[published_i][/#.*/] ? " " + lines[published_i][/#.*/] : ""
        lines[published_i] = "#{indent}#{key}: #{yaml_value}#{end_comment}\n"
        modified = true
      else
        values_indent = lines[values_i][/^\s*/]
        insert_indent = values_indent + "  "
        lines.insert(values_i + 1, "#{insert_indent}#{key}: #{yaml_value}\n")
        modified = true
      end
    end

  # === CASE 2: Simple section, e.g. pagination.enabled ===
  else
    section_i = lines.find_index { |l| l =~ /^#{section}:\s*$/ }
    if section_i
      indent_section = lines[section_i][/^\s*/]
      key_indent = indent_section + "  "
      key_i = nil
      m = section_i + 1
      while m < lines.length
        line = lines[m]
        break if line =~ /^\S/ # new top-level section
        if line =~ /^\s*#{key}\s*:/
          key_i = m
          break
        end
        m += 1
      end

      if key_i
        indent = lines[key_i][/^\s*/]
        end_comment = lines[key_i][/#.*/] ? " " + lines[key_i][/#.*/] : ""
        lines[key_i] = "#{indent}#{key}: #{yaml_value}#{end_comment}\n"
        modified = true
      else
        lines.insert(section_i + 1, "#{key_indent}#{key}: #{yaml_value}\n")
        modified = true
      end
    end
  end

  unless modified
    return { changed: false, message: "Nothing changed (structure not found)" }
  end

  File.write(BACKUP_PATH, File.read(CONFIG_PATH))
  File.open(CONFIG_PATH, "w") { |f| f.write(lines.join) }

  { changed: true, backup: BACKUP_PATH, path: CONFIG_PATH }
end

.home_about(verbose) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/rawfeed/layout.rb', line 11

def self.home_about(verbose)
  file = "index.md"
  content = File.read(file)
  updated = content.gsub(/^layout:\s*\S+$/, "layout: home")
  File.write(file, updated)

  if verbose
    puts "Home page changed to 'about' successfully!".green
  end
end

.home_blogObject



21
22
23
24
25
26
27
28
# File 'lib/rawfeed/layout.rb', line 21

def self.home_blog
  file = "index.md"
  content = File.read(file)
  updated = content.gsub(/^layout:\s*\S+$/, "layout: blog")
  File.write(file, updated)

   puts "Home page changed to 'blog' successfully!".green
end

.pixels_index(value) ⇒ Object



130
131
132
133
134
135
# File 'lib/rawfeed/layout.rb', line 130

def self.pixels_index(value)
  file = "pixels/index.md"
  content = File.read(file)
  updated = content.gsub(/^published:\s*\S+$/, "published: #{value}")
  File.write(file, updated)
end

.tags_index(value) ⇒ Object



137
138
139
140
141
142
# File 'lib/rawfeed/layout.rb', line 137

def self.tags_index(value)
  file = "blog/tags/index.md"
  content = File.read(file)
  updated = content.gsub(/^published:\s*\S+$/, "published: #{value}")
  File.write(file, updated)
end