<commit_before>module Docxtor2
  class ContentBuilder
    include BlockEvaluator

    class << self
      def build(&block)
        instance = new(block)
        instance.build
      end
    end

    @@map = {
      :table_of_contents  => Package::Document::TableOfContents,
      :p                  => Package::Document::Paragraph,
      :h                  => Package::Document::Heading
    }

    def initialize(block)
      @elements = []
      evaluate &block
    end

    def build
      result = ''
      xml = Builder::XmlMarkup.new(:target => result)
      @elements.each do |el|
        el.render(xml)
      end
      result
    end

    def method_missing(name, *args, &block)
      klass = @@map[name]
      super if klass.nil?

      cache_method_call(name, klass)
      self << self.send(name, *args, &block)
    end

    def respond_to_missing?(name, include_private = false)
      @@map.key?(name) || super
    end

    def <<(el)
      @elements << el
      el
    end
    
    private

    def cache_method_call(name, klass)
      self.class.define_method(name) do |*args, &block| 
        klass.new(*args, &block)
      end
    end
  end
end<commit_msg>ContentBuilder bug fix<commit_after>module Docxtor2
  class ContentBuilder
    include BlockEvaluator

    class << self
      def build(&block)
        instance = new(block)
        instance.build
      end
    end

    { :table_of_contents  => Package::Document::TableOfContents,
      :p                  => Package::Document::Paragraph,
      :h                  => Package::Document::Heading,
      :table              => NoMethodError.new("Element is not supported yet")
    }.each do |name, klass|
      define_method(name) do |*args, &block| 
        self << klass.new(*args, &block)
      end
    end

    def initialize(block)
      @elements = []
      evaluate &block
    end

    def build
      result = ''
      xml = Builder::XmlMarkup.new(:target => result)
      @elements.each { |el| el.render(xml) }
      result
    end

    def <<(el)
      @elements << el
      el
    end
  
  end
end
{'fraction_non_alphanumeric': 0.11004324843825083, 'fraction_numerical': 0.0009610764055742432, 'mean_word_length': 1.9323943661971832, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '4715792', 'n_tokens_mistral': 641, 'n_tokens_neox': 625, 'n_words': 150}