Class: CoachZed::Catalog::Loader
- Inherits:
-
Object
- Object
- CoachZed::Catalog::Loader
- Defined in:
- lib/coach_zed/catalog.rb,
sig/coach_zed.rbs
Constant Summary collapse
- IGNORED_BASENAMES =
%w[INDEX.md TEMPLATE.md].freeze
Instance Attribute Summary collapse
-
#root ⇒ Pathname
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #ignored?(path) ⇒ Boolean
-
#initialize(root) ⇒ Loader
constructor
A new instance of Loader.
- #load ⇒ Array[Entry]
- #normalize_bullets(lines) ⇒ Array[String]
- #normalize_key(key) ⇒ String
- #normalize_section_name(name) ⇒ String
- #normalize_urls(lines) ⇒ Array[String]
- #parse_entry(path) ⇒ Entry?
- #parse_sections(lines) ⇒ Hash[untyped, Array[String]]
- #strip_ticks(value) ⇒ String
Constructor Details
Instance Attribute Details
#root ⇒ Pathname (readonly)
Returns the value of attribute root.
99 100 101 |
# File 'lib/coach_zed/catalog.rb', line 99 def root @root end |
Instance Method Details
#ignored?(path) ⇒ Boolean
101 102 103 |
# File 'lib/coach_zed/catalog.rb', line 101 def ignored?(path) IGNORED_BASENAMES.include?(path.basename.to_s) end |
#load ⇒ Array[Entry]
88 89 90 91 92 93 94 95 |
# File 'lib/coach_zed/catalog.rb', line 88 def load Dir.glob(@root.join("**/*.md")).sort.filter_map do |file| path = Pathname(file) next if ignored?(path) parse_entry(path) end end |
#normalize_bullets(lines) ⇒ Array[String]
183 184 185 186 187 188 189 |
# File 'lib/coach_zed/catalog.rb', line 183 def normalize_bullets(lines) lines.filter_map do |line| next if line.strip.empty? line.sub(/^- /, "").strip end end |
#normalize_key(key) ⇒ String
175 176 177 |
# File 'lib/coach_zed/catalog.rb', line 175 def normalize_key(key) key.downcase.tr(" -", "_") end |
#normalize_section_name(name) ⇒ String
171 172 173 |
# File 'lib/coach_zed/catalog.rb', line 171 def normalize_section_name(name) name.downcase.strip end |
#normalize_urls(lines) ⇒ Array[String]
191 192 193 194 195 196 197 |
# File 'lib/coach_zed/catalog.rb', line 191 def normalize_urls(lines) lines.filter_map do |line| next unless line.match?(%r{\A-\s+https?://}) line.sub(/^- /, "") end end |
#parse_entry(path) ⇒ Entry?
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/coach_zed/catalog.rb', line 105 def parse_entry(path) relative_path = path.relative_path_from(root).to_s lines = path.read.lines(chomp: true) title = lines.find { |line| line.start_with?("# ") }&.sub("# ", "") return if title.nil? # @type var metadata: Hash[String, String] = {} index = 1 index += 1 while index < lines.length && lines[index].strip.empty? while index < lines.length line = lines[index] break if line.nil? || line.empty? break unless line.start_with?("- ") if (match = line.match(/^- ([^:]+):\s*(.*)$/)) key = normalize_key(match[1].to_s) [key] = strip_ticks(match[2].to_s) end index += 1 end index += 1 while index < lines.length && lines[index].strip.empty? sections = parse_sections(lines[index..] || []) Entry.new( path: path, relative_path: relative_path, title: title, domain: ["domain"], session_duration: ["session_duration"], precedence: ["precedence"], frequency: ["frequency"], program: ["program"], format: ["format"], equipment: ["equipment"], summary: sections.fetch("summary", []).join(" ").strip, work_items: normalize_bullets(sections.fetch("work", [])), notes: normalize_bullets(sections.fetch("notes", [])), source_urls: normalize_urls(sections.fetch("source", [])) ) end |
#parse_sections(lines) ⇒ Hash[untyped, Array[String]]
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/coach_zed/catalog.rb', line 150 def parse_sections(lines) # @type var sections: Hash[String, Array[String]] sections = {} current = nil lines.each do |line| if (match = line.match(/^##\s+(.+)$/)) current = normalize_section_name(match[1].to_s) next end next unless current section_name = current.to_s sections[section_name] ||= [] sections[section_name] << line end sections end |
#strip_ticks(value) ⇒ String
179 180 181 |
# File 'lib/coach_zed/catalog.rb', line 179 def strip_ticks(value) value.to_s.delete_prefix("`").delete_suffix("`") end |