samhuri.net/pressa/lib/utils/frontmatter_converter.rb

71 lines
1.7 KiB
Ruby

module Pressa
module Utils
class FrontmatterConverter
FIELD_PATTERN = /^([A-Z][a-z]+):\s*(.+)$/
def self.convert_file(input_path, output_path = nil)
content = File.read(input_path)
converted = convert_content(content)
if output_path
File.write(output_path, converted)
else
File.write(input_path, converted)
end
end
def self.convert_content(content)
unless content.start_with?("---\n")
raise "File does not start with front-matter delimiter"
end
parts = content.split(/^---\n/, 3)
if parts.length < 3
raise "Could not find end of front-matter"
end
frontmatter = parts[1]
body = parts[2]
yaml_frontmatter = convert_frontmatter_to_yaml(frontmatter)
"---\n#{yaml_frontmatter}---\n#{body}"
end
def self.convert_frontmatter_to_yaml(frontmatter)
fields = {}
frontmatter.each_line do |line|
line = line.strip
next if line.empty?
if line =~ FIELD_PATTERN
field_name = $1
field_value = $2.strip
fields[field_name] = field_value
end
end
yaml_lines = []
fields.each do |name, value|
yaml_lines << format_yaml_field(name, value)
end
yaml_lines.join("\n") + "\n"
end
private_class_method def self.format_yaml_field(name, value)
needs_quoting = (value.include?(':') && !value.start_with?('http')) ||
value.include?(',') ||
name == 'Date'
if needs_quoting
"#{name}: \"#{value}\""
else
"#{name}: #{value}"
end
end
end
end
end