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

@@ -47,5 +47,15 @@ class PostQuery
select_metatags(*names).first&.value
end
memoize :tags
# Return a new query AST, with aliased tags replaced with real tags.
def replace_aliases
ast.replace_tags(aliases)
end
# A hash mapping aliased tag names to real tag names.
def aliases
TagAlias.aliases_for(tag_names)
end
memoize :tags, :aliases
end

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...`