Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredmcateer/9604e71e3eb3b39cd3285b67fb3578c3 to your computer and use it in GitHub Desktop.
Save jaredmcateer/9604e71e3eb3b39cd3285b67fb3578c3 to your computer and use it in GitHub Desktop.
An Asciidoctor treeprocessor that wraps text content in Angular's localize filter.
require 'asciidoctor/extensions'
class AngularLocalizerTreeprocessor < Asciidoctor::Extensions::Treeprocessor
def process document
document.blocks? ? (localize_blocks document) : nil
end
def localize_blocks node
node.find_by do |b|
if b.content_model == :simple
b.lines[0] = %({{"#{b.lines[0]})
b.lines[-1] = %(#{b.lines[-1]}" | localize}})
elsif b.context == :list_item
replace_instance_text b
elsif b.context == :table
replace_text = lambda do |r|
r.each do |c|
replace_instance_text c
end
end
b.rows.head.each(&replace_text)
b.rows.body.each(&replace_text)
b.rows.foot.each(&replace_text)
end
# Required for asciidoctor <= 1.5.5
if b.instance_variable_defined?(:@subbed_title)
b.remove_instance_variable :@subbed_title
end
replace_instance_text b, :@title
true
end
nil
end
def replace_instance_text node, prop=:@text
text = node.instance_variable_get prop
node.instance_variable_set prop, (localize_text text) unless text.to_s.empty?
end
def localize_text text
%({{"#{text}"| localize}})
end
end
Asciidoctor::Extensions.register do
treeprocessor AngularLocalizerTreeprocessor
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment