Module: Kettle::Dev::PreReleaseCLI::Markdown
- Defined in:
- lib/kettle/dev/pre_release_cli.rb,
sig/kettle/dev/pre_release_cli.rbs
Overview
Markdown parsing helpers
Constant Summary collapse
- SCRATCH_PATH_PREFIXES =
%w[ tmp/ .git/ ].freeze
Class Method Summary collapse
-
.extract_image_urls_from_files(glob_pattern = nil) ⇒ Array<String>
Extract from files matching glob.
-
.extract_image_urls_from_text(text) ⇒ Array<String>
Extract unique remote HTTP(S) image URLs from markdown or HTML images.
-
.project_markdown_files ⇒ Array<String>
Find Markdown files that are part of the releasable project.
- .scratch_path?(path) ⇒ Boolean
- .tracked_markdown_files ⇒ Array<String>
Class Method Details
.extract_image_urls_from_files(glob_pattern = nil) ⇒ Array<String>
Extract from files matching glob.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/kettle/dev/pre_release_cli.rb', line 247 def extract_image_urls_from_files(glob_pattern = nil) files = if glob_pattern.nil? project_markdown_files elsif glob_pattern.is_a?(String) Dir.glob(glob_pattern) else Array(glob_pattern) end urls = files.flat_map do |f| begin extract_image_urls_from_text(File.read(f)) rescue => e warn("[kettle-pre-release] Could not read #{Kettle::Dev.display_path(f)}: #{e.class}: #{e.}") [] end end urls.uniq end |
.extract_image_urls_from_text(text) ⇒ Array<String>
Extract unique remote HTTP(S) image URLs from markdown or HTML images.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/kettle/dev/pre_release_cli.rb', line 193 def extract_image_urls_from_text(text) urls = [] # Inline image syntax text.scan(/!\[[^\]]*\]\(([^\s)]+)(?:\s+"[^"]*")?\)/) { |m| urls << m[0] } # Reference definitions ref_defs = {} text.scan(/^\s*\[([^\]]+)\]:\s*(\S+)/) { |m| ref_defs[m[0]] = m[1] } # Reference image usage text.scan(/!\[[^\]]*\]\[([^\]]+)\]/) do |m| id = m[0] url = ref_defs[id] urls << url if url end # HTML <img src="..."> text.scan(/<img\b[^>]*\bsrc\s*=\s*"([^"]+)"[^>]*>/i) { |m| urls << m[0] } text.scan(/<img\b[^>]*\bsrc\s*=\s*'([^']+)'[^>]*>/i) { |m| urls << m[0] } urls.reject! { |u| u.nil? || u.strip.empty? } urls.select! { |u| u =~ %r{^https?://}i } urls.uniq end |
.project_markdown_files ⇒ Array<String>
Find Markdown files that are part of the releasable project.
221 222 223 224 225 226 |
# File 'lib/kettle/dev/pre_release_cli.rb', line 221 def project_markdown_files files = tracked_markdown_files return files unless files.empty? Dir.glob(["**/*.md", "**/*.md.example"], File::FNM_DOTMATCH).reject { |path| scratch_path?(path) }.sort end |
.scratch_path?(path) ⇒ Boolean
240 241 242 |
# File 'lib/kettle/dev/pre_release_cli.rb', line 240 def scratch_path?(path) SCRATCH_PATH_PREFIXES.any? { |prefix| path.start_with?(prefix) } end |
.tracked_markdown_files ⇒ Array<String>
229 230 231 232 233 234 235 236 |
# File 'lib/kettle/dev/pre_release_cli.rb', line 229 def tracked_markdown_files output = IO.popen(["git", "ls-files", "-z", "--", "*.md", "*.md.example"], err: File::NULL, &:read) return [] unless $CHILD_STATUS.success? output.split("\0").reject { |path| scratch_path?(path) }.sort rescue Errno::ENOENT [] end |