class Lurker::Cli

A Thor definition for an lurker to HTML conversion operation

Attributes

content[RW]

Public Class Methods

assets_root() click to toggle source
# File lib/lurker/cli.rb, line 24
def self.assets_root
  options[:assets].present? ? Pathname.new(options[:assets]).expand_path : Lurker::BUNDLED_ASSETS_PATH
end
templates_root() click to toggle source
# File lib/lurker/cli.rb, line 20
def self.templates_root
  Lurker::BUNDLED_TEMPLATES_PATH
end

Public Instance Methods

cleanup_assets!() click to toggle source
# File lib/lurker/cli.rb, line 122
def cleanup_assets!
  actual = assets.values
  Dir.glob('*.{js,css,gz}').each do |fname|
    remove_file fname unless actual.include? fname.sub(/\.gz/, '')
  end
end
convert(lurker_path=Lurker::DEFAULT_SERVICE_PATH) click to toggle source
# File lib/lurker/cli.rb, line 55
def convert(lurker_path=Lurker::DEFAULT_SERVICE_PATH)
  say_status nil, "Converting lurker to #{options[:format]}"

  setup_schema_root! lurker_path
  require "#{Dir.pwd}/config/environment" if options[:rails]

  # for backward compatibility
  if options[:content]
    Lurker.service.documentation = open(File.expand_path(options[:content])).read
  end

  setup_rendering_engine!

  inside(output_path) do
    say_status :inside, output_path
    prepare_assets!
    if options[:format] == 'pdf'
      convert_to_pdf
    else
      convert_to_html
    end
    cleanup_assets!
  end
end
convert_to_html() click to toggle source
# File lib/lurker/cli.rb, line 91
def convert_to_html
  create_file 'index.html', service_presenter.to_html, force: true

  service_presenter.endpoints.each do |endpoint_prefix_group|
    endpoint_prefix_group.each do |endpoint_presenter|
      create_file(endpoint_presenter.relative_path, endpoint_presenter.to_html, force: true)
    end
  end
end
convert_to_pdf() click to toggle source
# File lib/lurker/cli.rb, line 81
def convert_to_pdf
  Lurker.safe_require('pdfkit')
  print_html = service_presenter.to_print
  create_file "#{service_presenter.url_name}_print.html", print_html, force: true

  kit = PDFKit.new(print_html)
  kit.stylesheets << assets['application.css']
  create_file "#{service_presenter.url_name}.pdf", kit.to_pdf, force: true
end
digest_asset!(name) click to toggle source
# File lib/lurker/cli.rb, line 129
def digest_asset!(name)
  if (asset_path = assets_root + name).exist?
    digest_path = asset_digest_path(asset_path).basename
    assets[asset_path.basename.to_s] = digest_path.to_s
    copy_file asset_path, digest_path, skip: true
    spawn "cat #{digest_path} | gzip -9 > #{digest_path}.gz"
  end
end
filtering_block() click to toggle source
# File lib/lurker/cli.rb, line 142
def filtering_block
  if options['select'].present?
    select = /#{options['select']}/
  end

  if options['exclude'].present?
    exclude = /#{options['exclude']}/
  end

  ->(endpoint_presenter) {
    prefix = endpoint_presenter.endpoint.schema['prefix']
    if prefix.present?
      return if select.present? && !prefix.match(select)
      return if exclude.present? && prefix.match(exclude)
    end
    endpoint_presenter
  }
end
html_options() click to toggle source
# File lib/lurker/cli.rb, line 161
def html_options
  @html_options ||= {
      static_html: true,
      url_base_path: url_base_path.prepend('/'),
      assets_directory: assets_root,
      assets: assets,
      html_directory: output_path,
      footer: footer,
      lurker: gem_info
  }
end
init_docs(lurker_path=Lurker::DEFAULT_SERVICE_PATH) click to toggle source
# File lib/lurker/cli.rb, line 32
def init_docs(lurker_path=Lurker::DEFAULT_SERVICE_PATH)
  say_status nil, 'Creating documentation stubs'

  setup_schema_root! lurker_path

  schemas = Lurker.service.endpoints.map(&:schema) << Lurker.service.schema
  schemas.each do |schema|
    rel_path = Pathname.new(schema.documentation_uri).relative_path_from(Pathname.getwd)
    template 'documentation.md.tt', schema.documentation_uri, skip: true, path: rel_path
  end
end
prepare_assets!() click to toggle source
# File lib/lurker/cli.rb, line 116
def prepare_assets!
  directory assets_root, '.', exclude_pattern: /application\.(js|css)$/
  digest_asset!('application.js')
  digest_asset!('application.css')
end
service_presenter() click to toggle source
# File lib/lurker/cli.rb, line 138
def service_presenter
  @service_presenter ||= Lurker::ServicePresenter.new(Lurker.service, html_options, &filtering_block)
end
setup_rendering_engine!() click to toggle source
# File lib/lurker/cli.rb, line 107
def setup_rendering_engine!
  I18n.config.enforce_available_locales = true
  Lurker::RenderingController.prepend_view_path Lurker::Cli.templates_root
  if options[:templates].present?
    Lurker::RenderingController.prepend_view_path Pathname.new(options[:templates]).expand_path
  end
  Lurker::RenderingController.config.assets_dir = assets_root
end
setup_schema_root!(path) click to toggle source
# File lib/lurker/cli.rb, line 101
def setup_schema_root!(path)
  Lurker.service_path = File.expand_path(path)
  raise Lurker::NotFound.new(Lurker.service_path) unless Lurker.valid_service_path?
  say_status :using, path
end

Private Instance Methods

asset_digest_path(path) click to toggle source
# File lib/lurker/cli.rb, line 207
def asset_digest_path(path)
  path = Pathname.new(path) unless path.is_a? Pathname
  asset_logical_path(path).sub(/\.(\w+)$/) { |ext| "-#{hexdigest(path)}#{ext}" }
end
asset_logical_path(path) click to toggle source
# File lib/lurker/cli.rb, line 202
def asset_logical_path(path)
  path = Pathname.new(path) unless path.is_a? Pathname
  path.sub %r{-[0-9a-f]{40}\.}, '.'
end
assets() click to toggle source
# File lib/lurker/cli.rb, line 194
def assets
  @assets ||= {}
end
assets_root() click to toggle source
# File lib/lurker/cli.rb, line 198
def assets_root
  Lurker::Cli.assets_root
end
gem_info() click to toggle source
# File lib/lurker/cli.rb, line 226
def gem_info
  spec = if Bundler.respond_to? :locked_gems
           Bundler.locked_gems.specs.select { |s| s.name == 'lurker' }.first # 1.6
         else
           Bundler.definition.sources.detect { |s| s.specs.map(&:name).include?('lurker') } # 1.3
         end

  if spec.source.respond_to? :revision, true # bundler 1.3 private
    "#{spec.name} (#{spec.source.send(:revision)})"
  else
    spec.to_s
  end
rescue => e
  puts e
  'lurker (unknown)'
end
hexdigest(path) click to toggle source
# File lib/lurker/cli.rb, line 212
def hexdigest(path)
  Digest::SHA1.hexdigest(open(path).read)
end
output_path() click to toggle source
# File lib/lurker/cli.rb, line 176
def output_path
  "#{output_prefix}/#{url_base_path}"
end
output_prefix() click to toggle source
# File lib/lurker/cli.rb, line 180
def output_prefix
  if explicit = options[:output]
    explicit.sub(/\/?#{url_base_path}\/?$/, '')
  elsif File.exists? 'public'
    'public'
  else
    raise 'Please, run it from `Rails.root` or pass `-o` option'
  end
end
path_extnames(path) click to toggle source
# File lib/lurker/cli.rb, line 216
def path_extnames(path)
  File.basename(path).scan(/\.[^.]+/)
end
url_base_path() click to toggle source
# File lib/lurker/cli.rb, line 190
def url_base_path
  options[:url_base_path].presence.try(:strip).try(:sub, /^\/+/, '') || Lurker::DEFAULT_URL_BASE
end