link-target-up.rb 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env ruby
  2. # https://cirosantilli.com/linux-kernel-module-cheat#asciidoctor-link-target-up-rb
  3. require 'asciidoctor'
  4. require 'asciidoctor/extensions'
  5. class LinkTargetUp < Asciidoctor::Extensions::InlineMacroProcessor
  6. use_dsl
  7. named :link
  8. ExternalLinkRegex = /^https?:\/\//
  9. def target_base
  10. # Return an absolute path here because you will
  11. # often want your out/ to be a symlink to your hard
  12. # disk, and doing just '..' in that case breaks.
  13. #
  14. # The downside is that you can't move the repo around
  15. # and still have the HTML work, but other Buildroot things
  16. # will likely break anyways in that case, so this is
  17. # the least of worries.
  18. File.expand_path('..', __dir__)
  19. end
  20. def process parent, target, attrs
  21. text = attrs[1]
  22. if text.nil? || text.empty?
  23. text = target
  24. end
  25. if !ExternalLinkRegex.match?(target)
  26. target = File.join(target_base, target)
  27. end
  28. create_anchor parent, text, type: :link, target: target
  29. end
  30. end
  31. Asciidoctor::Extensions.register do
  32. inline_macro LinkTargetUp
  33. end