ghpreview 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in github-preview.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Adam McCrea
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # ghpreview
2
+
3
+ Say you're writing a README for your project, or any other Markdown that will
4
+ eventually be displayed on Github. You want a way to preview this locally
5
+ before you push it.
6
+
7
+ `ghpreview` opens a preview of your Markdown file in a browser. It uses Github
8
+ styling and (optionally) automatically refreshes every time you save your
9
+ source.
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ $ gem install ghpreview
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ `ghpreview` is a command-line utility.
20
+
21
+ ```bash
22
+ $ ghpreview README.md
23
+ ```
24
+
25
+ This will open your default browser with a preview of README.md exactly as it
26
+ will appear on Github. For a live-updating preview, use the -w (watch) option:
27
+
28
+ ```bash
29
+ $ ghpreview -w README.md
30
+ ```
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/ghpreview ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'ghpreview'
5
+
6
+ options = {}
7
+
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Usage: ghpreview [options] FILE"
10
+ opts.separator ""
11
+
12
+ opts.on("-w", "--watch", "Watch for changes") do |w|
13
+ options[:watch] = w
14
+ end
15
+ end.parse!
16
+
17
+ GHPreview::Previewer.new(ARGV[0], options)
data/ghpreview.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ghpreview/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "ghpreview"
8
+ gem.version = GHPreview::VERSION
9
+ gem.authors = ["Adam McCrea"]
10
+ gem.email = ["adam@adamlogic.com"]
11
+ gem.description = %q{Command line utility for previewing Markdown files with Github styling}
12
+ gem.summary = gem.description
13
+ gem.homepage = "http://github.com/newcontext/ghpreview"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'listen'
21
+ gem.add_dependency 'rb-fsevent'
22
+ gem.add_dependency 'httpclient'
23
+ end
data/lib/ghpreview.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'ghpreview/version'
2
+ require 'ghpreview/previewer'
@@ -0,0 +1,69 @@
1
+ require 'erb'
2
+ require 'httpclient'
3
+ require 'listen'
4
+
5
+ module GHPreview
6
+ class Previewer
7
+ API_URI = 'https://api.github.com/markdown/raw'
8
+ HOMEPAGE = 'https://github.com'
9
+ HTML_FILEPATH = '/tmp/ghpreview.html'
10
+ RAW_TEMPLATE_FILEPATH = "#{File.dirname(__FILE__)}/template.erb"
11
+ STYLED_TEMPLATE_FILEPATH = "/tmp/ghpreview-template.erb"
12
+
13
+ def initialize(md_filepath, options = {})
14
+ @md_filepath = md_filepath
15
+ @md_filename = md_filepath.split('/').last
16
+ @md_filedir = md_filepath.split('/').unshift('.').uniq[0..-2].join('/')
17
+ generate_template_with_fingerprinted_stylesheet_links
18
+
19
+ options[:watch] ? listen : open
20
+ end
21
+
22
+ def listen
23
+ puts "Previewing #{@md_filepath}. CTRL-C to stop."
24
+ open
25
+ Listen.to(@md_filedir, filter: /#{@md_filename}$/) do |modified|
26
+ open
27
+ end
28
+ end
29
+
30
+ def open
31
+ html = markdown_to_html
32
+ html = wrap_content_with_full_document(html)
33
+ File.open(HTML_FILEPATH, 'w') { |f| f << html }
34
+ `open #{HTML_FILEPATH}`
35
+ end
36
+
37
+ private
38
+
39
+ def markdown_to_html
40
+ markdown = File.read(@md_filepath)
41
+ client = HTTPClient.new
42
+ message = client.post API_URI, body: markdown, header: {'Content-Type' => 'text/plain'}
43
+ message.body
44
+ end
45
+
46
+ def generate_template_with_fingerprinted_stylesheet_links
47
+ if stale_template?(STYLED_TEMPLATE_FILEPATH)
48
+ uri = URI.parse(HOMEPAGE)
49
+ stylesheet_links = uri.read.split("\n").select do |line|
50
+ line =~ /https:.*github.*\.css/
51
+ end.join
52
+
53
+ raw_template = File.read(RAW_TEMPLATE_FILEPATH)
54
+ styled_template = ERB.new(raw_template).result(binding)
55
+ File.write(STYLED_TEMPLATE_FILEPATH, styled_template)
56
+ end
57
+ end
58
+
59
+ def stale_template?(filepath)
60
+ return true unless File.exists?(filepath)
61
+ File.mtime(filepath) < (Time.now - 60 * 60 * 24)
62
+ end
63
+
64
+ def wrap_content_with_full_document(content)
65
+ template = File.read(STYLED_TEMPLATE_FILEPATH)
66
+ ERB.new(template).result(binding)
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,16 @@
1
+ <html>
2
+ <head>
3
+ <%= stylesheet_links %>
4
+ <style>
5
+ body { padding: 30px 0; }
6
+ #readme { width: 914px; margin: 0 auto; }
7
+ </style>
8
+ </head>
9
+ <body>
10
+ <div id="readme">
11
+ <article class="markdown-body">
12
+ <%%= content %>
13
+ </article>
14
+ </div>
15
+ </body>
16
+ </html>
@@ -0,0 +1,3 @@
1
+ module GHPreview
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ghpreview
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam McCrea
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: listen
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rb-fsevent
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: httpclient
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Command line utility for previewing Markdown files with Github styling
63
+ email:
64
+ - adam@adamlogic.com
65
+ executables:
66
+ - ghpreview
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - bin/ghpreview
76
+ - ghpreview.gemspec
77
+ - lib/ghpreview.rb
78
+ - lib/ghpreview/previewer.rb
79
+ - lib/ghpreview/template.erb
80
+ - lib/ghpreview/version.rb
81
+ homepage: http://github.com/newcontext/ghpreview
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.24
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Command line utility for previewing Markdown files with Github styling
105
+ test_files: []