<commit_before># MathJax Block
# Roughly based on Jekyll's HighlightBlock
module Jekyll

  class MathJaxBlock < Liquid::Block
    include Liquid::StandardFilters

    # Detect if we need to use block display style
    SYNTAX = /(display)?/

    def initialize(tag_name, markup, tokens)
      super
      if markup =~ SYNTAX
        if defined? $1
          @inline = false
        else
          @inline = true
        end
      else
        raise SyntaxError.new("Syntax Error in 'math' - Valid syntax: math [display]")
      end
    end

    def render(context)
      if @inline
        render_inline(context, super.join)
      else
        render_display(context, super.join)
      end
    end

    def render_inline(context, math)
      # Render inline - we cannot generate a CDATA elementa here because rdiscount would
      # screw up the whole paragraph flow.
      "<span class=\"MathJax_Preview\">#{h(math).strip}</span><script type=\"math/tex\">#{math.strip}</script>"
    end

    def render_display(context, math)
      # Since this is a block style formula, wrap it in a paragraph
      # Also, wrap the source into a CDATA element to prevent mangling by HTMLTidy
      <<-HTML
<p style="text-align: center">
  <span class="MathJax_Preview">#{h(math).strip}</span><script type="math/tex; mode=display">
%<![CDATA[
  #{math.strip}
%]]>
  </script>
</p>
      HTML
    end
  end

  # Ugly monkeypatch for inline formula post-processing
  class Site
    alias_method :original_write, :write
    def write
      # Before writing, post-process posts and pages
      self.posts.each do |p|
        fix_inline_math(p)
      end
      self.pages.each do |p|
        fix_inline_math(p)
      end
      # Now write everything
      original_write
    end

    def fix_inline_math(page)
      # Inject a CDATA element with appropriate LaTeX comment delimiters inside inline formulas
      page.output.gsub!(/(<script type="math\/tex">)(.*?)(<\/script>)/m, "\\1\n%<![CDATA[\n\\2\n%]]>\n\\3")
    end
  end
end

Liquid::Template.register_tag('math', Jekyll::MathJaxBlock)<commit_msg>Ugly workaround to get (almost) valid XHTML 1.0<commit_after># MathJax Block
# Roughly based on Jekyll's HighlightBlock
module Jekyll

  class MathJaxBlock < Liquid::Block
    include Liquid::StandardFilters

    # Detect if we need to use block display style
    SYNTAX = /(display)?/

    def initialize(tag_name, markup, tokens)
      super
      if markup =~ SYNTAX
        if defined? $1
          @inline = false
        else
          @inline = true
        end
      else
        raise SyntaxError.new("Syntax Error in 'math' - Valid syntax: math [display]")
      end
    end

    def render(context)
      if @inline
        render_inline(context, super.join)
      else
        render_display(context, super.join)
      end
    end

    def render_inline(context, math)
      "<script type=\"math/tex\">#{h(math).strip}</script>"
    end

    def render_display(context, math)
      <<-HTML
<p style="text-align: center">
<script type="math/tex; mode=display">
#{h(math).strip}
</script>
</p>
      HTML
    end
  end

  # Ugly monkeypatch for inline formula post-processing
  class Site
    alias_method :original_render, :render
    def render
      original_render

      # Post-process posts and pages
      self.posts.each do |post|
        fix_math(post)
      end

      self.pages.each do |page|
        fix_math(page)
      end
    end

    def fix_math(p)
      # Get rid of the <script> tags and add proper MathJax delimiters
      # Inline math
      p.output.gsub!(/(<script type="math\/tex">)(.*?)(<\/script>)/m, '\( \2 \)')
      # Display-style math
      p.output.gsub!(/(<script type="math\/tex; mode=display">\n)(.*?)(<\/script>)/m, "$$\n\\2$$")
    end
  end
end

Liquid::Template.register_tag('math', Jekyll::MathJaxBlock)
{'fraction_non_alphanumeric': 0.11858222569715923, 'fraction_numerical': 0.0023455824863174357, 'mean_word_length': 2.543859649122807, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6230728', 'n_tokens_mistral': 1237, 'n_tokens_neox': 1197, 'n_words': 343}