<commit_before># encoding: utf-8

require 'stringio'

require 'sinatra/base'
require 'sinatra/namespace'
require 'sinatra-websocket'

require 'nokogiri'
require 'listen'

require 'active_support'

require_relative 'compiler'
require_relative 'vendor/hash_binding'
require_relative 'helper'

require 'bade'
require 'stylus'
require 'coffee-script'

require_relative 'third_party/bower'


module Epuber

  # API:
  # [LATER]   /file/<path-or-pattern> -- displays pretty file (image, text file) (for example: /file/text/s01.xhtml or /file/text/s01.bade)
  #
  class Server < Sinatra::Base
    require_relative 'server/handlers'

    class ShowExceptions < Sinatra::ShowExceptions
      def call(env)
        e = env['sinatra.error']

        if prefers_plain_text?(env)
          content_type = 'text/plain'
          body = [dump_exception(e)]
        else
          content_type = 'text/html'
          body = pretty(env, e)
        end

        unless body.is_a?(Array)
          body = [body]
        end

        [500, { 'Content-Type'   => content_type,
                'Content-Length' => Rack::Utils.bytesize(body.join).to_s },
         body]
      end
    end

    def self.instance_class_accessor(name)
      instance_name = "@#{name}"

      define_singleton_method(name) do
        instance_variable_get(instance_name)
      end
      define_singleton_method("#{name}=") do |new_value|
        instance_variable_set(instance_name, new_value)
      end

      define_method(name) do
        self.class.send(name)
      end
      define_method("#{name}=") do |new_value|
        self.class.send("#{name}=", new_value)
      end
    end

    # @return [Epuber::Book::Book]
    #
    instance_class_accessor :book

    # @return [Epuber::Book::Target]
    #
    instance_class_accessor :target

    # @return [FalseClass, TrueClass]
    #
    instance_class_accessor :verbose

    # @return [Epuber::Compiler::FileResolver]
    #
    instance_class_accessor :file_resolver

    # @return [Array<Epuber::Compiler::File>]
    #
    instance_class_accessor :spine

    # @return [Array<SinatraWebsocket::Connection>]
    #
    instance_class_accessor :sockets

    def self.sockets
      @sockets ||= []
    end

    # @return [Listener]
    #
    instance_class_accessor :listener

    # @return nil
    #
    def self.run!(book, target, verbose: false)
      self.book = book
      self.target = target
      self.verbose = verbose

      start_listening_if_needed

      old_stderr = $stderr
      $stderr = StringIO.new unless verbose

      super() do |server|
        $stderr = old_stderr
        puts "Started development server on #{server.host}:#{server.port}"

        yield URI("http://#{server.host}:#{server.port}") if block_given?
      end
    end

    def self.verbose=(verbose)
      @verbose = verbose
      @default_thin_logger ||= Thin::Logging.logger

      unless verbose
        Thin::Logging.logger = Logger.new(nil)
        Thin::Logging.logger.level = nil
      else
        Thin::Logging.logger = @default_thin_logger
      end

      set :logging, verbose
    end

    def initialize
      super
      _log :ui, 'Init compile'<commit_msg>[Server] fix crash when using non-verbose mode on Ruby 2.3<commit_after># encoding: utf-8

require 'stringio'

require 'sinatra/base'
require 'sinatra/namespace'
require 'sinatra-websocket'

require 'nokogiri'
require 'listen'

require 'active_support'

require_relative 'compiler'
require_relative 'vendor/hash_binding'
require_relative 'helper'

require 'bade'
require 'stylus'
require 'coffee-script'

require_relative 'third_party/bower'


module Epuber

  # API:
  # [LATER]   /file/<path-or-pattern> -- displays pretty file (image, text file) (for example: /file/text/s01.xhtml or /file/text/s01.bade)
  #
  class Server < Sinatra::Base
    require_relative 'server/handlers'

    class ShowExceptions < Sinatra::ShowExceptions
      def call(env)
        e = env['sinatra.error']

        if prefers_plain_text?(env)
          content_type = 'text/plain'
          body = [dump_exception(e)]
        else
          content_type = 'text/html'
          body = pretty(env, e)
        end

        unless body.is_a?(Array)
          body = [body]
        end

        [500, { 'Content-Type'   => content_type,
                'Content-Length' => Rack::Utils.bytesize(body.join).to_s },
         body]
      end
    end

    def self.instance_class_accessor(name)
      instance_name = "@#{name}"

      define_singleton_method(name) do
        instance_variable_get(instance_name)
      end
      define_singleton_method("#{name}=") do |new_value|
        instance_variable_set(instance_name, new_value)
      end

      define_method(name) do
        self.class.send(name)
      end
      define_method("#{name}=") do |new_value|
        self.class.send("#{name}=", new_value)
      end
    end

    # @return [Epuber::Book::Book]
    #
    instance_class_accessor :book

    # @return [Epuber::Book::Target]
    #
    instance_class_accessor :target

    # @return [FalseClass, TrueClass]
    #
    instance_class_accessor :verbose

    # @return [Epuber::Compiler::FileResolver]
    #
    instance_class_accessor :file_resolver

    # @return [Array<Epuber::Compiler::File>]
    #
    instance_class_accessor :spine

    # @return [Array<SinatraWebsocket::Connection>]
    #
    instance_class_accessor :sockets

    def self.sockets
      @sockets ||= []
    end

    # @return [Listener]
    #
    instance_class_accessor :listener

    # @return nil
    #
    def self.run!(book, target, verbose: false)
      self.book = book
      self.target = target
      self.verbose = verbose

      start_listening_if_needed

      old_stderr = $stderr
      $stderr = StringIO.new unless verbose

      super() do |server|
        $stderr = old_stderr
        puts "Started development server on #{server.host}:#{server.port}"

        yield URI("http://#{server.host}:#{server.port}") if block_given?
      end
    end

    def self.verbose=(verbose)
      @verbose = verbose
      @default_thin_logger ||= Thin::Logging.logger

      unless verbose
        Thin::Logging.logger = Logger.new(nil)
        Thin::Logging.logger.level = :fatal
      else
        Thin::Logging.logger = @default_thin_logger
      end

      set :logging, verbose
    end

    def initialize
      super
      _log :ui, 'Init compile'
{'fraction_non_alphanumeric': 0.11137218045112782, 'fraction_numerical': 0.002819548872180451, 'mean_word_length': 2.6361047835990887, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '617628', 'n_tokens_mistral': 2099, 'n_tokens_neox': 2015, 'n_words': 422}