Sha256: 85e07c7134c1e1732c930ed2e37915e9c7692f077a93948f0b58d619d3129c67

Contents?: true

Size: 1.87 KB

Versions: 73

Compression:

Stored size: 1.87 KB

Contents

require 'sinatra/base'
require 'sinatra/respond_to'
require 'sinatra/async'

class Softcover::App < Sinatra::Base
  register Sinatra::Async

  set :public_folder, 'html'

  configure do
    mime_type :map, 'application/javascript'
  end

  before do
    origin = Dir.glob('chapters/*.md').empty? ? :polytex : :markdown
    @manifest = Softcover::BookManifest.new(origin: origin)
  end

  get '/' do
    redirect @manifest.first_chapter.slug
  end

  get '/main.js' do
    require 'coffee_script'
    @mathjax_src    = Softcover::Mathjax::AMS_HTML
    @mathjax_config = Softcover::Utils.article? ?
                      Softcover::Mathjax.escaped_config(chapter_number: false) :
                      Softcover::Mathjax.escaped_config
    coffee erb :'main.js'
  end

  get '/assets/:path' do
    extension = params[:path].split('.').last
    content_type extension
    File.read(File.join(File.dirname(__FILE__),'assets', params[:path]))
  end

  # Returns the image filename for the local document.
  def image_filename(path, extension)
    "html/images/#{path}.#{extension}"
  end

  get '/:chapter_slug.?:format?' do
    get_chapter
    doc = Nokogiri::HTML.fragment(File.read(@chapter.fragment_path))
    doc.css('a.hyperref').each do |node|
      node['href'] = node['href'].gsub(/_fragment\.html/, '')
    end
    @html = doc.to_xhtml

    if params[:format] == 'js'
      content_type :html
      @html
    else
      @title = @chapter.menu_heading
      @local_server = true
      erb :'book.html'
    end
  end

  aget '/:chapter_slug/wait' do
    require 'json'
    Signal.trap("SIGINT") { exit 0 }
    Signal.trap("HUP") do
      Thread.new { body({ time: Time.now }.to_json) }
    end
  end

  not_found do
    '404'
  end

  def get_chapter
    if params[:chapter_slug]
      @chapter = @manifest.find_chapter_by_slug(params[:chapter_slug])
      raise Sinatra::NotFound unless @chapter
    end
  end
end

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
softcover-1.10.5 lib/softcover/server/app.rb
softcover-1.10.4 lib/softcover/server/app.rb
softcover-1.10.3 lib/softcover/server/app.rb
softcover-1.10.2 lib/softcover/server/app.rb
softcover-1.10.1 lib/softcover/server/app.rb
softcover-1.10.0 lib/softcover/server/app.rb
softcover-1.9.1 lib/softcover/server/app.rb
softcover-1.9.0 lib/softcover/server/app.rb
softcover-1.8.3 lib/softcover/server/app.rb
softcover-1.8.2 lib/softcover/server/app.rb
softcover-1.8.1 lib/softcover/server/app.rb
softcover-1.8.0 lib/softcover/server/app.rb
softcover-1.7.5 lib/softcover/server/app.rb
softcover-1.7.4 lib/softcover/server/app.rb
softcover-1.7.3 lib/softcover/server/app.rb
softcover-1.7.2 lib/softcover/server/app.rb
softcover-1.7.1 lib/softcover/server/app.rb
softcover-1.7.0 lib/softcover/server/app.rb
softcover-1.6.5 lib/softcover/server/app.rb
softcover-1.6.4 lib/softcover/server/app.rb