<commit_before>require 'rdoc/rdoc'
require 'tomparse'

require 'api_tomdoc/markdown'
require 'api_tomdoc/github'

class RDoc::Generator::ApiTomDoc
  RDoc::RDoc.add_generator self

  def self.setup_options options
    options.dry_run = true
    op = options.option_parser

    op.on('--filename FILENAME', String, 'The output filename') do |filename|
      options.filename = filename.gsub(/\s+/, '-')
      options.filename = "#{options.filename}.md" unless options.filename.end_with?('.md')
    end

    op.on('--header HEADER', String, 'The header filename') do |header|
      options.header = header
    end

    op.on('--footer FOOTER', String, 'The footer filename') do |footer|
      options.footer = footer
    end
  end

  def initialize(options)
    @options = options
    @content = ''
    @h = ApiTomdoc::Markdown
  end

  def generate(files)
    files.each do |file|
      file.classes_or_modules.uniq.each do |clazz|
        generate_class_doc(clazz)
      end
    end

    @content = "#{File.read(@options.header)}\n#{@content}" if @options.header
    @content << File.read(@options.footer) if @options.footer

    FileUtils.cd(Dir.pwd.end_with?('/doc')? '..' : Dir.pwd) do
      ApiTomdoc::Github.new.update_wiki(@options.filename || 'API.md', @content)
    end
  end

  private

  def generate_class_doc(clazz)
    generate_class_header(clazz)
    clazz.instance_method_list.each { |method| generate_method_doc(method) }
  end

  def generate_class_header(clazz)
    @content << @h.h1(clazz.name.gsub(/Controller/, ''))
    @tomdoc = TomParse.parse(comment(clazz).split('---').first)
    @content << @h.p(@tomdoc.description)

    arguments('Fields')
    examples
  end<commit_msg>Add header and footer generation under change dir command<commit_after>require 'rdoc/rdoc'
require 'tomparse'

require 'api_tomdoc/markdown'
require 'api_tomdoc/github'

class RDoc::Generator::ApiTomDoc
  RDoc::RDoc.add_generator self

  def self.setup_options options
    options.dry_run = true
    op = options.option_parser

    op.on('--filename FILENAME', String, 'The output filename') do |filename|
      options.filename = filename.gsub(/\s+/, '-')
      options.filename = "#{options.filename}.md" unless options.filename.end_with?('.md')
    end

    op.on('--header HEADER', String, 'The header filename') do |header|
      options.header = header
    end

    op.on('--footer FOOTER', String, 'The footer filename') do |footer|
      options.footer = footer
    end
  end

  def initialize(options)
    @options = options
    @content = ''
    @h = ApiTomdoc::Markdown
  end

  def generate(files)
    files.each do |file|
      file.classes_or_modules.uniq.each do |clazz|
        generate_class_doc(clazz)
      end
    end

    FileUtils.cd(Dir.pwd.end_with?('/doc')? '..' : Dir.pwd) do
      @content = "#{File.read(@options.header)}\n#{@content}" if @options.header
      @content << File.read(@options.footer) if @options.footer
      ApiTomdoc::Github.new.update_wiki(@options.filename || 'API.md', @content)
    end
  end

  private

  def generate_class_doc(clazz)
    generate_class_header(clazz)
    clazz.instance_method_list.each { |method| generate_method_doc(method) }
  end

  def generate_class_header(clazz)
    @content << @h.h1(clazz.name.gsub(/Controller/, ''))
    @tomdoc = TomParse.parse(comment(clazz).split('---').first)
    @content << @h.p(@tomdoc.description)

    arguments('Fields')
    examples
  end
{'fraction_non_alphanumeric': 0.14181923859343215, 'fraction_numerical': 0.0005812263876780006, 'mean_word_length': 3.424164524421594, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, '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': '950141', 'n_tokens_mistral': 1161, 'n_tokens_neox': 1128, 'n_words': 229}