Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jimeh committed May 3, 2012
0 parents commit 9f32df1
Show file tree
Hide file tree
Showing 16 changed files with 1,090 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in manservant.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2012 Jim Myhrberg

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
@@ -0,0 +1,29 @@
# Manservant

TODO: Write a gem description

## Installation

Add this line to your application's Gemfile:

gem 'manservant'

And then execute:

$ bundle

Or install it yourself as:

$ gem install manservant

## Usage

TODO: Write usage instructions here

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
2 changes: 2 additions & 0 deletions Rakefile
@@ -0,0 +1,2 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
4 changes: 4 additions & 0 deletions config.ru
@@ -0,0 +1,4 @@
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)

require 'manservant'
run Manservant::Server
6 changes: 6 additions & 0 deletions lib/manservant.rb
@@ -0,0 +1,6 @@
require 'manservant/version'
require 'manservant/server'

module Manservant

end
73 changes: 73 additions & 0 deletions lib/manservant/man_page.rb
@@ -0,0 +1,73 @@
module Manservant
class ManPage

class NotFound < StandardError; end

attr_accessor :name
attr_accessor :section
attr_accessor :options

def initialize(name, section = nil, options = {})
@name = name
@section = section
@man2html_path = options[:man2html_path] || defaults[:man2html_path]
@man2html_args = defaults[:man2html_args].merge(options[:man2html_args])
end

def sections
@sections ||= {}
end

def to_text
return @text if @text
text = `#{man_cmd}`
raise NotFound, "No manual entry for #{name}" if $?.to_i != 0
@text = text.strip
end

def to_html
return @html if @html
to_text # raise exception is man page doesn't exist
@html = post_process_html `#{man_cmd} | #{man2html_cmd}`.strip
end

private

def post_process_html(html)
html.gsub!(/^<PRE>\s*<\!\-\-.+\-\->\s*/, "<PRE>\n")
html.gsub!(/<\/PRE>\s*<H2>\s*(.+?)\s*<\/H2>\s*<PRE>/) do |match|
title = $1
id = title.gsub(/[^a-z0-9\-_\.\s]/i, '').
gsub(/[^a-z0-9]/i, '-').downcase
sections[id] = title
"<H2 id=\"#{id}\">#{title}</H2>"
end
html
end

def man_cmd
section_arg = "-S \"#{section}\"" if section
"man -P cat #{name} #{section_arg}"
end

def man2html_cmd
args = @man2html_args.inject([]) do |result, (opt, val)|
result << "-#{opt}"
result << val.to_s if val && val != true
result
end
"#{@man2html_path} #{args.join(' ')}"
end

def defaults
{
:man2html_path => `command -v man2html`,
:man2html_args => {
:bare => true,
:topm => 0
}
}
end

end # ManPage
end # Manservant
53 changes: 53 additions & 0 deletions lib/manservant/server.rb
@@ -0,0 +1,53 @@
require 'sinatra'

require 'manservant/man_page'

module Manservant
class Server < Sinatra::Base

set :public_folder, File.expand_path('../server/public', __FILE__)
set :views, File.expand_path('../server/views', __FILE__)

helpers do
def parse_page_name(dirty_name)
name = sanitize_name(dirty_name)
redirect to("/#{name}") if name != dirty_name
if name =~ /^(.+)\.(\d+?)$/
[$1, $2]
else
[name, nil]
end
end

def sanitize_name(name)
name.downcase.gsub(/[^a-z0-9\-_\.]/i, '')
end

def find_page(name, section = nil)
ManPage.new(name, section,
:man2html_path => man2html_path,
:man2html_args => { :cgiurl => '\'/${title}.${section}\'' })
end

def man2html_path
File.expand_path('../../../libexec/man2html', __FILE__)
end
end # helpers

get '/' do
redirect to('/man')
end

get '/:page' do
name, section = parse_page_name(params[:page])
@page = find_page(name, section)
begin
@page.to_html # render and populate sections
rescue Manservant::ManPage::NotFound => e
raise Sinatra::NotFound
end
erb :page, :layout => :layout
end

end
end

0 comments on commit 9f32df1

Please sign in to comment.