Class: GithubMarkdownServer::Server
- Inherits:
-
Object
- Object
- GithubMarkdownServer::Server
- Defined in:
- lib/github-markdown-server/server.rb
Overview
Serves files and converts markdown to github like html (with live.js as well).
Instance Method Summary collapse
- #directory_listing(dir, root, bonus) ⇒ Object
- #emptystyles ⇒ Object
-
#initialize(options = {}) ⇒ Server
constructor
A new instance of Server.
- #md2html(file) ⇒ Object
- #mime_type(ext) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Server
Returns a new instance of Server.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/github-markdown-server/server.rb', line 12 def initialize( = {}) port = [:server_port] || 8000 @directory = File.([:directory]) @file_name = File.([:file_name]) url = "http://localhost:#{port}/" if !File.directory?(@directory) @directory = File.dirname(@file_name) end url = url + @file_name[@directory.length + 1..-1] if @file_name != @directory = { :Port => port, :DocumentRoot => @directory, Logger: WEBrick::Log.new("/dev/null"), AccessLog: [], } if [:browser] [:StartCallback] = Proc.new { system('open', url) } end server = WEBrick::HTTPServer.new() trap 'INT' do server.shutdown end server.mount_proc '/' do |req, res| if req.path == '/favicon.ico' source_file = Resources.(File.join('image','favicon.ico')) else source_file = File.join(@directory, req.path) end case req.request_method when 'HEAD' res['last-modified'] = File.mtime(source_file).to_s else livejs = "<script>\n#{IO.read(Resources.(File.join('js','live.js')))}\n</script>" if source_file.end_with? '.md' res.body = md2html(source_file) res.body += livejs elsif File.directory?(source_file) directory = source_file for index in ['index.md', 'index.html', 'index.htm'] source_file = File.join(directory, index) if File.exists?(source_file) res.body = IO.read(source_file) res.body += livejs break end end if !File.exists?(source_file) bonus = nil readme = File.join(directory, '/README.md') if File.exists?(readme) bonus = md2html(readme) else bonus = emptystyles end res.body = directory_listing(directory, req.path == '/', bonus) end else res.body = IO.read(source_file) end end res['content-type'] = mime_type(File.extname(source_file)) end puts "Starting server #{url}" server.start end |
Instance Method Details
#directory_listing(dir, root, bonus) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/github-markdown-server/server.rb', line 97 def directory_listing(dir, root, bonus) body = '<ul>' body += '<li><a href="../">..</a>' unless root dirs = Pathname.glob(File.join(dir, '/*/')).map do |x| d = x.basename.to_s "<li><a href=\"#{d}/\">#{d}</a></li>" end mds = Pathname.glob(File.join(dir, '*.md')).map do |x| md = x.basename.to_s "<li><a href=\"#{md}\">#{md}</a></li>" end body += dirs.join('') unless dirs.empty? body += mds.join('') unless mds.empty? body += '</ul>' if bonus sep = '<div class="readme-content">' body = bonus.sub!(sep, (body + sep)) end body end |
#emptystyles ⇒ Object
91 92 93 94 95 |
# File 'lib/github-markdown-server/server.rb', line 91 def emptystyles file = Tempfile.new('') out = Tempfile.new('') GithubMarkdownPreview::HtmlPreview.new(file, {:preview_file => out.path}).wrap_preview('') end |
#md2html(file) ⇒ Object
85 86 87 88 89 |
# File 'lib/github-markdown-server/server.rb', line 85 def md2html(file) out = Tempfile.new(File.basename(file)) GithubMarkdownPreview::HtmlPreview.new(file, {:preview_file => out.path}) IO.read(out.path) end |
#mime_type(ext) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/github-markdown-server/server.rb', line 120 def mime_type(ext) ext = ext.downcase.sub(/^\./, '') case ext when 'gif', 'jpg', 'png' "image/#{ext}" when 'md', 'html', 'htm' 'text/html' when 'js', 'css' "text/#{ext}" when 'ico' 'image/vnd.microsoft.icon' else 'application/octet-stream' end end |