<commit_before>require 'active_support'
require 'active_support/core_ext/hash/indifferent_access'
require 'active_support/core_ext/string/conversions'
require 'active_support/core_ext/time/conversions'
require 'active_support/inflector'

require 'tumblargh/version'

module Tumblargh

  autoload :API,       'tumblargh/api'
  autoload :Node,      'tumblargh/node'
  autoload :Parser,    'tumblargh/parser'
  autoload :Renderer,  'tumblargh/renderer'
  autoload :Resource,  'tumblargh/resource'

  class << self

    attr_accessor :config

    def render_file(file, blog, options={})
      render(:file, file, blog)
    end

    def render_html(string, blog, options={})
      render(:html, string, blog, options)
    end

    private

    def render(setter, theme, blog, options)
      parser = Parser.new
      parser.send("#{setter}=", theme)

      blog = create_blog blog

      options = parser.options.merge(options)

      Renderer::Document.new(parser.tree, blog, options).render
    end

    def create_blog(blog)
      if blog.is_a? Resource::Blog
        blog
      elsif blog.is_a? Hash
        blog = blog["response"] if blog.key? "response"
        Resource::Blog.new("#{blog["blog"]["name"]}.tumblr.com", blog)
      elsif File.exists? blog
        json = ActiveSupport::JSON.decode(open(blog).read)
        json = json["response"] if json.key? "response"
        Resource::Blog.new("#{json["blog"]["name"]}.tumblr.com", json)
      else
        Resource::Blog.new(blog)
      end
    end

  end

end<commit_msg>DRY a bit of the blog factory stuff in top level render methods<commit_after>require 'active_support'
require 'active_support/core_ext/hash/indifferent_access'
require 'active_support/core_ext/string/conversions'
require 'active_support/core_ext/time/conversions'
require 'active_support/inflector'

require 'tumblargh/version'

module Tumblargh

  autoload :API,       'tumblargh/api'
  autoload :Node,      'tumblargh/node'
  autoload :Parser,    'tumblargh/parser'
  autoload :Renderer,  'tumblargh/renderer'
  autoload :Resource,  'tumblargh/resource'

  class << self

    attr_accessor :config

    def render_file(file, blog, options={})
      render(:file, file, blog)
    end

    def render_html(string, blog, options={})
      render(:html, string, blog, options)
    end

    private

    def render(setter, theme, blog, options)
      parser = Parser.new
      parser.send("#{setter}=", theme)

      blog = create_blog blog

      options = parser.options.merge(options)

      Renderer::Document.new(parser.tree, blog, options).render
    end

    def create_blog(blog)
      if blog.is_a? Resource::Blog
        blog
      elsif blog.is_a? Hash
        create_blog_from_hash blog
      elsif File.exists? blog
        json = ActiveSupport::JSON.decode(open(blog).read)
        create_blog_from_hash json
      else
        Resource::Blog.new(blog)
      end
    end

    def create_blog_from_hash(hash)
      hash = hash["response"] if hash.key? "response"
      Resource::Blog.new("#{hash["blog"]["name"]}.tumblr.com", hash)
    end

  end

end
{'fraction_non_alphanumeric': 0.1172279792746114, 'fraction_numerical': 0.0, 'mean_word_length': 3.225718194254446, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5064034', 'n_tokens_mistral': 1041, 'n_tokens_neox': 998, 'n_words': 217}