<commit_before>require 'fileutils'

module YARD
  module Serializers
    class FileSystemSerializer < Base
      attr_reader :basepath, :extension
      
      def initialize(opts = {})
        super
        @basepath = (options[:basepath] || 'doc').to_s
        @extension = (options[:extension] || 'html').to_s
      end
      
      def serialize(object, data)
        path = serialized_path(object)
        FileUtils.mkdir_p File.join(*path.split('/')[0..-2])
        YARD.logger.debug "Serializing to #{path}"
        File.open(path, "w") {|f| f.write data }
      end
      
      def serialized_path(object)
        fspath = [object.name.to_s + ".#{extension}"]
        if object.namespace && object.namespace.path != ""
          fspath.unshift *object.namespace.path.split(CodeObjects::NSEP)
        end
        
        # Don't change the filenames, it just makes it more complicated
        # to figure out the original name.
        #fspath.map! do |p| 
        #  p.gsub(/([a-z])([A-Z])/, '\1_\2').downcase 
        #end
        
        File.join(basepath, *fspath)
      end
    end
  end
end<commit_msg>Refactor FS serializer code<commit_after>require 'fileutils'

module YARD
  module Serializers
    class FileSystemSerializer < Base
      attr_reader :basepath, :extension
      
      def initialize(opts = {})
        super
        @basepath = (options[:basepath] || 'doc').to_s
        @extension = (options[:extension] || 'html').to_s
      end
      
      def serialize(object, data)
        path = File.join(basepath, *serialized_path(object))
        FileUtils.mkdir_p File.dirname(path)
        YARD.logger.debug "Serializing to #{path}"
        File.open(path, "w") {|f| f.write data }
      end
      
      def serialized_path(object)
        fspath = [object.name.to_s + ".#{extension}"]
        if object.namespace && object.namespace.path != ""
          fspath.unshift *object.namespace.path.split(CodeObjects::NSEP)
        end
        
        # Don't change the filenames, it just makes it more complicated
        # to figure out the original name.
        #fspath.map! do |p| 
        #  p.gsub(/([a-z])([A-Z])/, '\1_\2').downcase 
        #end
        
        File.join(fspath)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.1277258566978193, 'fraction_numerical': 0.0026702269692923898, 'mean_word_length': 2.0964187327823693, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5003616', 'n_tokens_mistral': 709, 'n_tokens_neox': 709, 'n_words': 158}