<commit_before># encoding: utf-8

require 'yaml'

module Marsdawn
  class Command
    attr_accessor :editor, :auto_editor

    class ParamError < Exception; end
    class RuntimeError < Exception; end

    def self.exec argv
      raise ParamError.new("No command is specified.") if argv.empty?
      cmd = new
      command = argv.shift.to_sym
      raise ParamError.new("Unknown command '#{command}'.") unless cmd.respond_to?(command)
      value = (argv.size > 0 ? argv.shift : nil)
      opts = parse_options(argv)
      cmd.__send__ command, value, opts
    end

    def initialize
      @editor = 'vim'
      @auto_editor = true
      read_dot_marsdawn
    end

    def create title, opts={}
      key = (opts.key?(:file) ? opts[:file] : file_namize(title))
      path = File.expand_path(key)
      raise "The directory '#{doc_name}' already exists." if File.exists?(path)
      Dir.mkdir path
      data = {key: key, title: title, lang: 'en', version: '0.0.1'}
      dot_file = File.join(path, '.marsdawn.yml')
      index_file = File.join(path, '.index.md')
      File.write dot_file, YAML.dump(data)
      create_page index_file, title, 'title' => title
      edit_cmd dot_file, index_file if @auto_editor || opts[:edit]
    end

    def dir title, opts={}
      dir = (opts.key?(:file) ? opts[:file] : file_namize(title))
      dir = add_num(dir, opts)
      path = File.expand_path(dir)
      Dir.mkdir path
      index_file = File.join(path, '.index.md')
      create_page index_file, title, 'title' => title
      edit_cmd index_file if @auto_editor || opts[:edit]
    end

    def page title, opts={}
      file = (opts.key?(:file) ? "#{opts[:file]}.md" : file_namize(title, '.md'))
      file = File.expand_path(add_num(file, opts))
      create_page file, title, 'title' => title
      edit_cmd file if @auto_editor || opts[:edit]
    end

    def renum step, opts={}
      step = (step.nil? ? 10 : step.to_i)
      num = 0
      list = Dir.glob('*').each_with_object({}) do |item, ret|
        num += step
        ret[item] = add_num(item, num: num, step: step)
      end
      list.each do |src, dest|
        FileUtils.mv src, dest unless src == dest
      end
      'ls -1'
    end

    def debug type, opts={} 
      case type
      when 'options'
        opts
      end
    end

    private
    def self.parse_options argv
      opts = {}
      while argv.size > 0 do
        key = opt_key(argv.shift)
        val = true
        val = argv.shift if argv.size > 0 && !argv.first.start_with?('-')
        opts[key] = val
      end
      opts
    end

    def self.opt_key switch
      opt_keys = {<commit_msg>still investigating...<commit_after># encoding: utf-8

require 'yaml'

module Marsdawn
  class Command
    attr_accessor :editor, :auto_editor

    class ParamError < Exception; end
    class RuntimeError < Exception; end

    def self.exec argv
      raise ParamError.new("No command is specified.") if argv.empty?
      cmd = new
      command = argv.shift.to_sym
      raise ParamError.new("Unknown command '#{command}'.") unless cmd.respond_to?(command)
      value = (argv.size > 0 ? argv.shift : nil)
      opts = parse_options(argv)
      cmd.__send__ command, value, opts
    end

    def initialize
      @editor = 'vim'
      @auto_editor = true
      read_dot_marsdawn
    end

    def create title, opts={}
      key = (opts.key?(:file) ? opts[:file] : file_namize(title))
      path = File.expand_path(key)
      raise "The directory '#{doc_name}' already exists." if File.exists?(path)
      Dir.mkdir path
      data = {key: key, title: title, lang: 'en', version: '0.0.1'}
      dot_file = File.join(path, '.marsdawn.yml')
      index_file = File.join(path, '.index.md')
      File.write dot_file, YAML.dump(data)
      create_page index_file, title, 'title' => title
      edit_cmd dot_file, index_file if @auto_editor || opts[:edit]
    end

    def dir title, opts={}
      dir = (opts.key?(:file) ? opts[:file] : file_namize(title))
      dir = add_num(dir, opts)
      path = File.expand_path(dir)
      Dir.mkdir path
      index_file = File.join(path, '.index.md')
      create_page index_file, title, 'title' => title
      edit_cmd index_file if @auto_editor || opts[:edit]
    end

    def page title, opts={}
      file = (opts.key?(:file) ? "#{opts[:file]}.md" : file_namize(title, '.md'))
      file = File.expand_path(add_num(file, opts))
      create_page file, title, 'title' => title
      edit_cmd file if @auto_editor || opts[:edit]
    end

    def renum step, opts={}
      step = (step.nil? ? 10 : step.to_i)
      num = 0
      list = Dir.glob('*').each_with_object({}) do |item, ret|
        num += step
        ret[item] = add_num(item, num: num, step: step)
      end
      list.each do |src, dest|
        #FileUtils.mv src, dest unless src == dest
        File.rename src, dest unless src == dest
      end
      'ls -1'
    end

    def debug type, opts={} 
      case type
      when 'options'
        opts
      end
    end

    private
    def self.parse_options argv
      opts = {}
      while argv.size > 0 do
        key = opt_key(argv.shift)
        val = true
        val = argv.shift if argv.size > 0 && !argv.first.start_with?('-')
        opts[key] = val
      end
      opts
    end

    def self.opt_key switch
      opt_keys = {
{'fraction_non_alphanumeric': 0.12789166823396653, 'fraction_numerical': 0.004137671619334211, 'mean_word_length': 2.3467589679043424, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '7425351', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1742, 'n_words': 485}