summaryrefslogtreecommitdiffstats
path: root/_plugins/enunciation.rb
blob: b141ae976bc368d26cf9f96330d51227f229fb83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module Jekyll
  class EnunciationTagBlock < Liquid::Block
    def initialize(tag_name, arg, parse_context)
      super
      @arg = arg.strip
      @slugtemplate = Liquid::Template.parse('{{ string | slugify }}')
    end

    def render(context)
      text = super
      # If the enunciation ends with a KaTeX displayed equation,
      # then we remove the bottom margin.
      if text[-5, 5] == "  $$\n"
        text += '  {: .katex-display .mb-0 }'
      elsif text[-3, 3] == "$$\n"
        text += '{: .katex-display .mb-0 }'
      end
      # Check if a description was given.
      if @arg.empty?
        "{: .#{block_name} }\n #{text.gsub!(/^/, '> ')}"
      else
        if @arg[0] == '*'
          title = @arg.delete_prefix('* ')
        else
          title = "#{block_name.capitalize} (#{@arg})"
        end
        slug = @slugtemplate.render('string' => title)
        "{: .#{block_name}-title }\n> #{title}\n> {: \##{slug} }\n>\n#{text.gsub!(/^/, '> ')}"
      end
    end
  end
end

# TODO: avoid reading configuration twice
Jekyll.configuration({})['callouts'].each do |name, _|
  Liquid::Template.register_tag(name, Jekyll::EnunciationTagBlock)
end