2016-12-09 00:15:08 +00:00
|
|
|
require 'uri'
|
|
|
|
|
|
|
|
module Banzai
|
|
|
|
module Filter
|
|
|
|
# HTML filter that adds class="code math" and removes the dollar sign in $`2+2`$.
|
|
|
|
#
|
|
|
|
class MathFilter < HTML::Pipeline::Filter
|
|
|
|
# Attribute indicating inline or display math.
|
|
|
|
STYLE_ATTRIBUTE = 'data-math-style'.freeze
|
|
|
|
|
|
|
|
# Class used for tagging elements that should be rendered
|
|
|
|
TAG_CLASS = 'js-render-math'.freeze
|
|
|
|
|
|
|
|
INLINE_CLASSES = "code math #{TAG_CLASS}".freeze
|
|
|
|
|
|
|
|
DOLLAR_SIGN = '$'.freeze
|
|
|
|
|
|
|
|
def call
|
2016-12-22 13:16:31 +00:00
|
|
|
doc.css('code').each do |code|
|
2016-12-09 00:15:08 +00:00
|
|
|
closing = code.next
|
|
|
|
opening = code.previous
|
|
|
|
|
|
|
|
# We need a sibling before and after.
|
|
|
|
# They should end and start with $ respectively.
|
|
|
|
if closing && opening &&
|
2016-12-22 13:16:31 +00:00
|
|
|
closing.text? && opening.text? &&
|
2016-12-09 00:15:08 +00:00
|
|
|
closing.content.first == DOLLAR_SIGN &&
|
|
|
|
opening.content.last == DOLLAR_SIGN
|
|
|
|
|
|
|
|
code[:class] = INLINE_CLASSES
|
|
|
|
code[STYLE_ATTRIBUTE] = 'inline'
|
|
|
|
closing.content = closing.content[1..-1]
|
|
|
|
opening.content = opening.content[0..-2]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-22 13:16:31 +00:00
|
|
|
doc.css('pre.code.math').each do |el|
|
2016-12-09 00:15:08 +00:00
|
|
|
el[STYLE_ATTRIBUTE] = 'display'
|
|
|
|
el[:class] += " #{TAG_CLASS}"
|
|
|
|
end
|
|
|
|
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|