Last active
May 6, 2024 08:19
-
-
Save YujiSoftware/420641a79d12c074a96210e644953038 to your computer and use it in GitHub Desktop.
Migrate CompatibilityTable to Compat macro on MDN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
base = ARGV[0] | |
en_base = ARGV[1] | |
regexp = /\{\{\s*Compat\s*\(?.*\}\}/i | |
Dir.glob("**/*.html", base: base).each do |path| | |
file = File.join(base, path) | |
next if not FileTest.file?(file) | |
content = File.read(file) | |
next if not content.include?("CompatibilityTable") | |
en_file = File.join(en_base, path) | |
if not File.exist?(en_file) | |
p "English file not found. (#{path})" | |
next | |
end | |
match = File.read(en_file).scan(regexp) | |
if match.length == 0 | |
# p "Macro not found in english file. (#{path})" | |
# next | |
end | |
if match.length > 1 | |
p "Contains multiple Compat macro (#{path})" if match.length > 1 | |
next | |
end | |
lines = Array.new | |
skip = false | |
content.each_line(chomp: true) do |c| | |
if c.include?("CompatibilityTable") | |
if match.length == 0 | |
p "Macro not found in english file. (#{path})" | |
while not lines.pop.strip.start_with?("<h2") | |
end | |
end | |
for index in 0...match.length do | |
lines.push("<p>#{match[index]}</p>"); | |
lines.push("") | |
end | |
skip = true | |
end | |
if c.strip.start_with?("<h2") | |
skip = false | |
end | |
p "Contains h3 tag: #{c} (#{path})" if skip && c.strip.start_with?("<h3") | |
lines.push(c) if !skip | |
end | |
File.open(file, mode_enc = "w") do |w| | |
w.puts(lines.join("\n")) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment