extension.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'
  2. include Asciidoctor
  3. class EmojiInlineMacro < Extensions::InlineMacroProcessor
  4. use_dsl
  5. named :emoji
  6. name_positional_attributes 'size'
  7. SIZE_MAP = {'1x' => 17, 'lg' => 24, '2x' => 34, '3x' => 50, '4x' => 68, '5x' => 85}
  8. SIZE_MAP.default = 24
  9. def process parent, target, attributes
  10. doc = parent.document
  11. if doc.attributes['emoji'] == 'tortue'
  12. slash = (doc.attr? 'htmlsyntax', 'xml') ? '/' : nil
  13. size = SIZE_MAP[attributes['size']]
  14. cdn = (attributes.key? 'cdn') ? attributes['cdn'] : (doc.attr 'emoji-cdn', 'http://www.tortue.me/emoji/')
  15. qtarget = %(#{cdn}#{target}.png)
  16. %(<img src="#{parent.image_uri qtarget, nil}" height="#{size}" width="#{size}"#{slash}>)
  17. # Use twemoji by default
  18. else
  19. size_class = (size = attributes['size']) ? %( twa-#{size}) : nil
  20. emoji_name = target.tr '_', '-'
  21. %(<i class="twa#{size_class} twa-#{emoji_name}"></i>)
  22. end
  23. end
  24. end
  25. class EmojiAssetsDocinfoProcessor < Extensions::DocinfoProcessor
  26. use_dsl
  27. #at_location :head
  28. def process doc
  29. unless doc.attributes['emoji'] == 'tortue'
  30. extdir = ::File.join(::File.dirname __FILE__)
  31. stylesheet_name = 'twemoji-awesome.css'
  32. if doc.attr? 'linkcss'
  33. stylesheet_href = handle_stylesheet doc, extdir, stylesheet_name
  34. %(<link rel="stylesheet" href="#{stylesheet_href}">)
  35. else
  36. content = doc.read_asset %(#{extdir}/#{stylesheet_name})
  37. ['<style>', content.chomp, '</style>'] * "\n"
  38. end
  39. end
  40. end
  41. def handle_stylesheet doc, extdir, stylesheet_name
  42. outdir = (doc.attr? 'outdir') ? (doc.attr 'outdir') : (doc.attr 'docdir')
  43. stylesoutdir = doc.normalize_system_path((doc.attr 'stylesdir'), outdir, (doc.safe >= SafeMode::SAFE ? outdir : nil))
  44. if stylesoutdir != extdir && doc.safe < SafeMode::SECURE && (doc.attr? 'copycss')
  45. destination = doc.normalize_system_path stylesheet_name, stylesoutdir, (doc.safe >= SafeMode::SAFE ? outdir : nil)
  46. content = doc.read_asset %(#{extdir}/#{stylesheet_name})
  47. ::File.open(destination, 'w') {|f|
  48. f.write content
  49. }
  50. destination
  51. else
  52. %(./#{stylesheet_name})
  53. end
  54. end
  55. end