<commit_before>module Asciidoctor
class BaseTemplate

  # create template matter to insert a style class from the role attribute if specified
  def role_class
    attrvalue(:role)
  end

  # create template matter to insert a style class from the style attribute if specified
  def style_class(sibling = true)
    attrvalue(:style, sibling)
  end

  def title_div(opts = {})
    %(<% if title? %><div class="title">#{opts.has_key?(:caption) ? '<%= @caption %>' : ''}<%= title %></div><% end %>)
  end
end

module HTML5
class DocumentTemplate < BaseTemplate
  def self.outline(node, to_depth = 2)
    toc_level = nil
    sections = node.sections
    unless sections.empty?
      toc_level, indent = ''
      nested = true
      unless node.is_a?(Document)
        if node.document.doctype == 'book'
          indent = '    ' * node.level unless node.level == 0
          nested = node.level > 0
        else
          indent = '    ' * node.level
        end
      end
      toc_level << "#{indent}<ol>\n" if nested
      sections.each do |section|
        toc_level << "#{indent}  <li><a href=\"##{section.id}\">#{!section.special && section.level > 0 ? "#{section.sectnum} " : ''}#{section.attr('caption')}#{section.title}</a></li>\n"
        if section.level < to_depth && (child_toc_level = outline(section, to_depth))
          if section.document.doctype != 'book' || section.level > 0
            toc_level << "#{indent}  <li>\n#{child_toc_level}\n#{indent}  </li>\n"
          else
            toc_level << "#{indent}#{child_toc_level}\n"
          end
        end
      end
      toc_level << "#{indent}</ol>" if nested
    end
    toc_level
  end

  # Internal: Generate the default stylesheet for CodeRay
  #
  # returns the default CodeRay stylesheet as a String
  def self.default_coderay_stylesheet
    Helpers.require_library 'coderay'
    ::CodeRay::Encoders[:html]::CSS.new(:default).stylesheet
  end

  def template
    @template ||= @eruby.new <<-EOS
<%#encoding:UTF-8%><!DOCTYPE html>
<html lang="<%= attr :lang, 'en' %>">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=<%= attr :encoding %>">
    <meta name="generator" content="Asciidoctor <%= attr 'asciidoctor-version' %>">
    <% if attr? :description %><meta name="description" content="<%= attr :description %>"><% end %>
    <% if attr? :keywords %><meta name="keywords" content="<%= attr :keywords %>"><% end %>
    <title><%= doctitle %></title>
    <% if attr? :toc %>
    <style>
#toc > ol { padding-left: 0; }
#toc ol { list-style-type: none; }
    </style>
    <% end %>
    <% unless attr(:stylesheet, '').empty? %>
    <link rel="stylesheet" href="<%= (attr? :stylesdir) ? File.join((attr :stylesdir), (attr :stylesheet)) : (attr :stylesheet) %>">
    <% end %>
    <%
    case attr 'source-highlighter' %><%
    when 'coderay' %>
    <style>
pre.highlight { border: none; background-color: #F8F8F8; }
pre.highlight code, pre.highlight pre { color: #333; }
pre.highlight span.line-numbers { display: inline-block; margin-right: 4px; padding: 1px 4px; }
pre.highlight .line-numbers { background-color: #D5F6F6; color: gray; }
pre.highlight .line-numbers pre { color: gray; }
<% if (attr 'coderay-css', 'class') == 'class' %><%= template.class.default_coderay_stylesheet %><% end %>
    </style><%
    when 'highlightjs' %>
    <link rel="stylesheet" href="<%= (attr :highlightjsdir, 'http://cdnjs.cloudflare.com/ajax/libs/highlight.js/7.3') %>/styles/<%= (attr 'highlightjs-theme', 'default') %>.min.css">
    <style>
pre code { background-color: #F8F8F8; padding: 0; }
    </style>
    <script src="<%= (attr :highlightjsdir, 'http://cdnjs.cloudflare.com/ajax/libs/highlight.js/7.3') %>/highlight.min.js"></script>
    <script>hljs.initHighlightingOnLoad()</script>
    <% end %>
  </head>
  <body#{id} class="<%= doctype %>"<% if attr? 'max-width' %> style="max-width: <%= attr 'max-width' %>;"<% end %>>
    <% unless noheader %>
    <div id="header">
      <% if has_header? %>
      <% unless notitle %>
      <h1><%= @header.title %></h1>
      <% end %>
      <% if attr? :author %><span id="author"><%= attr :author %></span><br><% end %>
      <% if attr? :email %><span id="email" class="monospaced">&lt;<%= attr :email %>&gt;</span><br><% end %>
      <% if attr? :revnumber %><span id="revnumber">version <%= attr :revnumber %><%= attr?(:revdate) ? ',' : '' %></span><% end %>
      <% if attr? :revdate %><span id="revdate"><%= attr :revdate %></span><% end %>
      <% if attr? :revremark %><br><span id="revremark"><%= attr :revremark %></span><% end %>
      <% end %>
      <% if attr? :toc %>
      <div id="toc">
        <div id="toctitle"><%= attr 'toc-title' %></div>
<%= template.class.outline(self, (attr :toclevels, 2).to_i) %>
      </div>
      <% end %>
    </div>
    <% end %>
    <div id="content">
<%= content %>
    </div>
    <% if footnotes? %>
    <div id="footnotes">
      <hr>
      <% footnotes.each do |fn| %>
      <div class="footnote" id="_footnote_<%= fn.index %>">
        <a href="#_footnoteref_<%= fn.index %>"><%= fn.index %></a>. <%= fn.text %>
      </div>
      <% end %>
    </div>
    <% end %>
    <div id="footer">
      <div id="footer-text">
        <% if attr? :revnumber %>Version <%= attr :revnumber %><br><% end %>
        Last updated <%= attr :docdatetime %>
      </div>
    </div>
  </body>
</html>
    EOS
  end
end

class EmbeddedTemplate < BaseTemplate
  def template
    @template ||= @eruby.new <<-EOS
<%#encoding:UTF-8%><% unless notitle || !has_header? %><h1#{id}><%= header.title %></h1>
<% end %><%= content %>
    EOS
  end
end

class BlockPreambleTemplate < BaseTemplate
  def template
    @template ||= @eruby.new <<-EOS
<%#encoding:UTF-8%><div id="preamble">
  <div class="sectionbody">
<%= content %>
  </div>
</div>
    EOS
  end
end

class SectionTemplate < BaseTemplate
  def template
    @template ||= @eruby.new <<-EOS
<%#encoding:UTF-8%><%
if @level == 0 %>
<h1#{id}><%= title %></h1>
<%= content %>
<% else %>
<div class="sect<%= @level %>#{role_class}">
  <h<%= @level + 1 %>#{id}><% if !@special && (attr? :numbered) && @level < 4 %><%= sectnum %> <% end %><%= attr :caption %><%= title %></h<%= @level + 1 %>>
  <% if @level == 1 %>
  <div class="sectionbody">
<%= content %>
  </div>
  <% else %>
<%= content %>
  <% end %>
</div>
<% end %>
    EOS
  end
end

class BlockFloatingTitleTemplate < BaseTemplate
  def template
    @template ||= @eruby.new <<-EOS
<%#encoding:UTF-8%><h<%= @level + 1 %>#{id} class="#{style_class false}#{role_class}"><%= title %></h<%= @level + 1 %>>
    EOS<commit_msg>pull section rendering into method for speed boost<commit_after>module Asciidoctor
class BaseTemplate

  # create template matter to insert a style class from the role attribute if specified
  def role_class
    attrvalue(:role)
  end

  # create template matter to insert a style class from the style attribute if specified
  def style_class(sibling = true)
    attrvalue(:style, sibling)
  end

  def title_div(opts = {})
    %(<% if title? %><div class="title">#{opts.has_key?(:caption) ? '<%= @caption %>' : ''}<%= title %></div><% end %>)
  end
end

module HTML5
class DocumentTemplate < BaseTemplate
  def self.outline(node, to_depth = 2)
    toc_level = nil
    sections = node.sections
    unless sections.empty?
      toc_level, indent = ''
      nested = true
      unless node.is_a?(Document)
        if node.document.doctype == 'book'
          indent = '    ' * node.level unless node.level == 0
          nested = node.level > 0
        else
          indent = '    ' * node.level
        end
      end
      toc_level << "#{indent}<ol>\n" if nested
      sections.each do |section|
        toc_level << "#{indent}  <li><a href=\"##{section.id}\">#{!section.special && section.level > 0 ? "#{section.sectnum} " : ''}#{section.attr('caption')}#{section.title}</a></li>\n"
        if section.level < to_depth && (child_toc_level = outline(section, to_depth))
          if section.document.doctype != 'book' || section.level > 0
            toc_level << "#{indent}  <li>\n#{child_toc_level}\n#{indent}  </li>\n"
          else
            toc_level << "#{indent}#{child_toc_level}\n"
          end
        end
      end
      toc_level << "#{indent}</ol>" if nested
    end
    toc_level
  end

  # Internal: Generate the default stylesheet for CodeRay
  #
  # returns the default CodeRay stylesheet as a String
  def self.default_coderay_stylesheet
    Helpers.require_library 'coderay'
    ::CodeRay::Encoders[:html]::CSS.new(:default).stylesheet
  end

  def template
    @template ||= @eruby.new <<-EOS
<%#encoding:UTF-8%><!DOCTYPE html>
<html lang="<%= attr :lang, 'en' %>">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=<%= attr :encoding %>">
    <meta name="generator" content="Asciidoctor <%= attr 'asciidoctor-version' %>">
    <% if attr? :description %><meta name="description" content="<%= attr :description %>"><% end %>
    <% if attr? :keywords %><meta name="keywords" content="<%= attr :keywords %>"><% end %>
    <title><%= doctitle %></title>
    <% if attr? :toc %>
    <style>
#toc > ol { padding-left: 0; }
#toc ol { list-style-type: none; }
    </style>
    <% end %>
    <% unless attr(:stylesheet, '').empty? %>
    <link rel="stylesheet" href="<%= (attr? :stylesdir) ? File.join((attr :stylesdir), (attr :stylesheet)) : (attr :stylesheet) %>">
    <% end %>
    <%
    case attr 'source-highlighter' %><%
    when 'coderay' %>
    <style>
pre.highlight { border: none; background-color: #F8F8F8; }
pre.highlight code, pre.highlight pre { color: #333; }
pre.highlight span.line-numbers { display: inline-block; margin-right: 4px; padding: 1px 4px; }
pre.highlight .line-numbers { background-color: #D5F6F6; color: gray; }
pre.highlight .line-numbers pre { color: gray; }
<% if (attr 'coderay-css', 'class') == 'class' %><%= template.class.default_coderay_stylesheet %><% end %>
    </style><%
    when 'highlightjs' %>
    <link rel="stylesheet" href="<%= (attr :highlightjsdir, 'http://cdnjs.cloudflare.com/ajax/libs/highlight.js/7.3') %>/styles/<%= (attr 'highlightjs-theme', 'default') %>.min.css">
    <style>
pre code { background-color: #F8F8F8; padding: 0; }
    </style>
    <script src="<%= (attr :highlightjsdir, 'http://cdnjs.cloudflare.com/ajax/libs/highlight.js/7.3') %>/highlight.min.js"></script>
    <script>hljs.initHighlightingOnLoad()</script>
    <% end %>
  </head>
  <body#{id} class="<%= doctype %>"<% if attr? 'max-width' %> style="max-width: <%= attr 'max-width' %>;"<% end %>>
    <% unless noheader %>
    <div id="header">
      <% if has_header? %>
      <% unless notitle %>
      <h1><%= @header.title %></h1>
      <% end %>
      <% if attr? :author %><span id="author"><%= attr :author %></span><br><% end %>
      <% if attr? :email %><span id="email" class="monospaced">&lt;<%= attr :email %>&gt;</span><br><% end %>
      <% if attr? :revnumber %><span id="revnumber">version <%= attr :revnumber %><%= attr?(:revdate) ? ',' : '' %></span><% end %>
      <% if attr? :revdate %><span id="revdate"><%= attr :revdate %></span><% end %>
      <% if attr? :revremark %><br><span id="revremark"><%= attr :revremark %></span><% end %>
      <% end %>
      <% if attr? :toc %>
      <div id="toc">
        <div id="toctitle"><%= attr 'toc-title' %></div>
<%= template.class.outline(self, (attr :toclevels, 2).to_i) %>
      </div>
      <% end %>
    </div>
    <% end %>
    <div id="content">
<%= content %>
    </div>
    <% if footnotes? %>
    <div id="footnotes">
      <hr>
      <% footnotes.each do |fn| %>
      <div class="footnote" id="_footnote_<%= fn.index %>">
        <a href="#_footnoteref_<%= fn.index %>"><%= fn.index %></a>. <%= fn.text %>
      </div>
      <% end %>
    </div>
    <% end %>
    <div id="footer">
      <div id="footer-text">
        <% if attr? :revnumber %>Version <%= attr :revnumber %><br><% end %>
        Last updated <%= attr :docdatetime %>
      </div>
    </div>
  </body>
</html>
    EOS
  end
end

class EmbeddedTemplate < BaseTemplate
  def template
    @template ||= @eruby.new <<-EOS
<%#encoding:UTF-8%><% unless notitle || !has_header? %><h1#{id}><%= header.title %></h1>
<% end %><%= content %>
    EOS
  end
end

class BlockPreambleTemplate < BaseTemplate
  def template
    @template ||= @eruby.new <<-EOS
<%#encoding:UTF-8%><div id="preamble">
  <div class="sectionbody">
<%= content %>
  </div>
</div>
    EOS
  end
end

class SectionTemplate < BaseTemplate
  def section(sec)
    slevel = sec.level
    htag = "h#{slevel + 1}"
    id = sec.id && " id=\"#{sec.id}\""
    if slevel == 0
      %(<h1#{id}>#{sec.title}</h1>
#{sec.content})
    else
      role = sec.attr?('role') ? " #{sec.attr('role')}" : nil
      if !sec.special && (sec.attr? 'numbered') && slevel < 4
        sectnum = "#{sec.sectnum} "
      else
        sectnum = nil
      end

      if slevel == 1
        content = %(  <div class="sectionbody">
#{sec.content}
  </div>)
      else
        content = sec.content
      end
      %(<div class="sect#{slevel}#{role}">
  <#{htag}#{id}>#{sectnum}#{sec.attr 'caption'}#{sec.title}</#{htag}>
#{content}
</div>)
    end
  end

  def template
    # hot piece of code, optimized for speed
    @template ||= @eruby.new <<-EOS
<%#encoding:UTF-8%><%= template.section(self) %>
    EOS
  end
end

class BlockFloatingTitleTemplate < BaseTemplate
  def template
    @template ||= @eruby.new <<-EOS
<%#encoding:UTF-8%><h<%= @level + 1 %>#{id} class="#{style_class false}#{role_class}"><%= title %></h<%= @level + 1 %>>
    EOS
{'fraction_non_alphanumeric': 0.20621364941470957, 'fraction_numerical': 0.006699550909224766, 'mean_word_length': 3.130130738826391, 'pattern_counts': {'":': 0, '<': 420, '<?xml version=': 0, '>': 374, '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': '3083195', 'n_tokens_mistral': 5060, 'n_tokens_neox': 4659, 'n_words': 1128}