Skip to content

Instantly share code, notes, and snippets.

@YujiSoftware
Created November 23, 2022 07:27
Show Gist options
  • Save YujiSoftware/6ca47a35df8097c15dd5c1c4f7f23c3c to your computer and use it in GitHub Desktop.
Save YujiSoftware/6ca47a35df8097c15dd5c1c4f7f23c3c to your computer and use it in GitHub Desktop.
Removed old spec macros and insert specifications macro. For https://github.com/mdn/translated-content/issues/5618
#!/bin/env ruby
content_dir = File.expand_path(ARGV[0])
translated_dir = File.expand_path(ARGV[1])
Dir.glob("**/*.md", base: translated_dir).each do |path|
content_file = "#{content_dir}/#{path}"
translated_file = "#{translated_dir}/#{path}"
has = false
if File.exist?(content_file)
has = File.read(content_file).include?("{{Specifications}}")
end
content = File.read(translated_file)
header = -1
found = false
content.each_line.with_index do |line, index|
header = index if line.match?(/^#[# ]/)
if line.match?(/\{\{\s*SpecName/i) || line.match?(/\{\{\s*Spec2/i)
found = true
break
end
end
if !found
if has
# puts "Missmatch: #{content_file}"
end
next
end
if found && !has
puts "Missmatch: #{translated_file}"
next
end
new_content = ""
skip = false
content.each_line.with_index do |line, index|
if index == header
new_content += line
new_content += "\n"
new_content += "{{Specifications}}\n"
new_content += "\n"
skip = true
else
skip = false if line.start_with?("#")
new_content += line if !skip
end
end
File.write(translated_file, new_content)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment