post queries: add #replace_aliases method.

This commit is contained in:
evazion
2022-04-04 02:14:23 -05:00
parent bf7c721815
commit 1957cb354e
3 changed files with 36 additions and 5 deletions

View File

@@ -254,6 +254,20 @@ class PostQuery
end
end
# Replace tags according to a hash mapping old tag names to new tag names.
#
# @param replacements [Hash<String, String>] A hash mapping old tag names to new tag names.
# @return [AST] A new AST with the tags replaced.
def replace_tags(replacements)
rewrite do |node|
if node.tag? && replacements.has_key?(node.name)
node(:tag, replacements[node.name])
else
node
end
end
end
# Call the block on the AST repeatedly until the output stops changing.
#
# `ast.repeat_until_unchanged(&:trim)` is like doing `ast.trim.trim.trim...`