ghpreview 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +5 -1
- data/lib/ghpreview.rb +2 -2
- data/lib/ghpreview/converter.rb +23 -0
- data/lib/ghpreview/previewer.rb +11 -73
- data/lib/ghpreview/template.erb +2 -0
- data/lib/ghpreview/version.rb +1 -1
- data/lib/ghpreview/viewer.rb +24 -0
- data/lib/ghpreview/watcher.rb +16 -0
- data/lib/ghpreview/wrapper.rb +40 -0
- data/spec/converter_spec.rb +15 -0
- data/spec/preview_readme +5 -0
- data/spec/watch_readme +5 -0
- data/spec/wrapper_spec.rb +20 -0
- metadata +15 -3
data/README.md
CHANGED
@@ -33,10 +33,14 @@ option:
|
|
33
33
|
$ ghpreview -w README.md
|
34
34
|
```
|
35
35
|
|
36
|
-
To use an alternate browser
|
36
|
+
To use an alternate browser, use the `-a` (or `--application`) option:
|
37
37
|
|
38
38
|
```bash
|
39
|
+
# On Mac:
|
39
40
|
$ ghpreview -a Safari.app README.md
|
41
|
+
|
42
|
+
#On GNU/Linux:
|
43
|
+
$ ghpreview -a konqueror README.md
|
40
44
|
```
|
41
45
|
|
42
46
|
## Why is this better than X?
|
data/lib/ghpreview.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require_relative 'ghpreview/version'
|
2
|
+
require_relative 'ghpreview/previewer'
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'html/pipeline'
|
2
|
+
|
3
|
+
module GHPreview
|
4
|
+
class Converter
|
5
|
+
def self.to_html(markdown)
|
6
|
+
context = {
|
7
|
+
asset_root: "http://assets.github.com/images/icons/",
|
8
|
+
gfm: false
|
9
|
+
}
|
10
|
+
|
11
|
+
pipeline = HTML::Pipeline.new([
|
12
|
+
HTML::Pipeline::MarkdownFilter,
|
13
|
+
HTML::Pipeline::SanitizationFilter,
|
14
|
+
HTML::Pipeline::ImageMaxWidthFilter,
|
15
|
+
HTML::Pipeline::HttpsFilter,
|
16
|
+
HTML::Pipeline::MentionFilter,
|
17
|
+
HTML::Pipeline::EmojiFilter,
|
18
|
+
HTML::Pipeline::SyntaxHighlightFilter
|
19
|
+
], context)
|
20
|
+
result = pipeline.call(markdown)[:output].to_s
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/ghpreview/previewer.rb
CHANGED
@@ -1,94 +1,32 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
require_relative 'converter'
|
2
|
+
require_relative 'wrapper'
|
3
|
+
require_relative 'viewer'
|
4
|
+
require_relative 'watcher'
|
5
5
|
|
6
6
|
module GHPreview
|
7
7
|
class Previewer
|
8
|
-
HTML_FILEPATH = '/tmp/ghpreview.html'
|
9
|
-
RAW_TEMPLATE_FILEPATH = "#{File.dirname(__FILE__)}/template.erb"
|
10
|
-
STYLED_TEMPLATE_FILEPATH = "/tmp/ghpreview-template.erb"
|
11
|
-
HOMEPAGE = 'https://github.com'
|
12
|
-
TEMPLATE_CACHE_DURATION = 60 * 60 * 24 * 7 # one week
|
13
8
|
|
14
9
|
def initialize(md_filepath, options = {})
|
15
|
-
|
16
|
-
@http = HTTPClient.new
|
17
|
-
generate_template_with_fingerprinted_stylesheet_links
|
10
|
+
Wrapper.generate_template_with_fingerprinted_stylesheet_links
|
18
11
|
|
12
|
+
@md_filepath = md_filepath
|
19
13
|
@application = options[:application]
|
14
|
+
|
20
15
|
options[:watch] ? listen : open
|
21
16
|
end
|
22
17
|
|
23
18
|
def listen
|
24
19
|
puts "Previewing #{@md_filepath}. CTRL-C to stop."
|
25
|
-
|
26
|
-
|
27
|
-
filename = File.basename(@md_filepath)
|
28
|
-
dirname = File.dirname(File.expand_path(@md_filepath))
|
29
|
-
|
30
|
-
Listen.to(dirname, filter: /#{filename}$/) do |modified|
|
20
|
+
Watcher.watch @md_filepath do
|
31
21
|
open
|
32
22
|
end
|
33
23
|
end
|
34
24
|
|
35
25
|
def open
|
36
|
-
html =
|
37
|
-
html =
|
38
|
-
|
39
|
-
|
40
|
-
if RUBY_PLATFORM =~ /linux/
|
41
|
-
command = 'xdg-open'
|
42
|
-
else
|
43
|
-
command = @application ? "open -a #{@application}" : 'open'
|
44
|
-
end
|
45
|
-
`#{command} #{HTML_FILEPATH}`
|
26
|
+
html = Converter.to_html(File.read(@md_filepath))
|
27
|
+
html = Wrapper.wrap_html(html)
|
28
|
+
Viewer.view_html html, @application
|
46
29
|
end
|
47
30
|
|
48
|
-
private
|
49
|
-
|
50
|
-
def markdown_to_html
|
51
|
-
markdown = File.read(@md_filepath)
|
52
|
-
|
53
|
-
context = {
|
54
|
-
asset_root: "http://assets.github.com/images/icons/",
|
55
|
-
gfm: false
|
56
|
-
}
|
57
|
-
|
58
|
-
pipeline = HTML::Pipeline.new([
|
59
|
-
HTML::Pipeline::MarkdownFilter,
|
60
|
-
HTML::Pipeline::SanitizationFilter,
|
61
|
-
HTML::Pipeline::ImageMaxWidthFilter,
|
62
|
-
HTML::Pipeline::HttpsFilter,
|
63
|
-
HTML::Pipeline::MentionFilter,
|
64
|
-
HTML::Pipeline::EmojiFilter,
|
65
|
-
HTML::Pipeline::SyntaxHighlightFilter
|
66
|
-
], context)
|
67
|
-
result = pipeline.call(markdown)[:output].to_s
|
68
|
-
end
|
69
|
-
|
70
|
-
def generate_template_with_fingerprinted_stylesheet_links
|
71
|
-
if stale_template?(STYLED_TEMPLATE_FILEPATH)
|
72
|
-
stylesheet_links = @http.get(HOMEPAGE).body.split("\n").select do |line|
|
73
|
-
line =~ /https:.*github.*\.css/
|
74
|
-
end.join
|
75
|
-
|
76
|
-
raw_template = File.read(RAW_TEMPLATE_FILEPATH)
|
77
|
-
styled_template = ERB.new(raw_template).result(binding)
|
78
|
-
File.open(STYLED_TEMPLATE_FILEPATH, 'w') do |f|
|
79
|
-
f.write(styled_template)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
def stale_template?(filepath)
|
85
|
-
return true unless File.exists?(filepath)
|
86
|
-
File.mtime(filepath) < (Time.now - TEMPLATE_CACHE_DURATION)
|
87
|
-
end
|
88
|
-
|
89
|
-
def wrap_content_with_full_document(content)
|
90
|
-
template = File.read(STYLED_TEMPLATE_FILEPATH)
|
91
|
-
ERB.new(template).result(binding)
|
92
|
-
end
|
93
31
|
end
|
94
32
|
end
|
data/lib/ghpreview/template.erb
CHANGED
data/lib/ghpreview/version.rb
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
module GHPreview
|
2
|
+
class Viewer
|
3
|
+
HTML_FILEPATH = '/tmp/ghpreview.html'
|
4
|
+
|
5
|
+
def self.view_html(html, application = nil)
|
6
|
+
File.open(HTML_FILEPATH, 'w') { |f| f << html }
|
7
|
+
|
8
|
+
if RUBY_PLATFORM =~ /linux/
|
9
|
+
command = if application
|
10
|
+
`#{application} #{HTML_FILEPATH} </dev/null &>/dev/null &`
|
11
|
+
else
|
12
|
+
`xdg-open #{HTML_FILEPATH}`
|
13
|
+
end
|
14
|
+
else
|
15
|
+
command = if application
|
16
|
+
"open -a #{application}"
|
17
|
+
else
|
18
|
+
'open'
|
19
|
+
end
|
20
|
+
`#{command} #{HTML_FILEPATH}`
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'listen'
|
2
|
+
|
3
|
+
module GHPreview
|
4
|
+
class Watcher
|
5
|
+
def self.watch(filepath)
|
6
|
+
yield
|
7
|
+
|
8
|
+
filename = File.basename(filepath)
|
9
|
+
dirname = File.dirname(File.expand_path(filepath))
|
10
|
+
|
11
|
+
Listen.to(dirname, filter: /#{filename}$/) do |modified|
|
12
|
+
yield
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'httpclient'
|
3
|
+
|
4
|
+
module GHPreview
|
5
|
+
class Wrapper
|
6
|
+
GITHUB_URL = 'https://github.com'
|
7
|
+
STYLED_TEMPLATE_FILEPATH = "/tmp/ghpreview-template.erb"
|
8
|
+
TEMPLATE_CACHE_DURATION = 60 * 60 * 24 * 7 # one week
|
9
|
+
RAW_TEMPLATE_FILEPATH = "#{File.dirname(__FILE__)}/template.erb"
|
10
|
+
|
11
|
+
def self.generate_template_with_fingerprinted_stylesheet_links
|
12
|
+
http = HTTPClient.new
|
13
|
+
|
14
|
+
if stale_template?(STYLED_TEMPLATE_FILEPATH)
|
15
|
+
stylesheet_links = http.get(GITHUB_URL).body.split("\n").select do |line|
|
16
|
+
line =~ /https:.*github.*\.css/
|
17
|
+
end.join
|
18
|
+
|
19
|
+
raw_template = File.read(RAW_TEMPLATE_FILEPATH)
|
20
|
+
styled_template = ERB.new(raw_template).result(binding)
|
21
|
+
File.open(STYLED_TEMPLATE_FILEPATH, 'w') do |f|
|
22
|
+
f.write(styled_template)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.wrap_html(content)
|
28
|
+
template = File.read(STYLED_TEMPLATE_FILEPATH)
|
29
|
+
ERB.new(template).result(binding)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def self.stale_template?(filepath)
|
35
|
+
return true unless File.exists?(filepath)
|
36
|
+
File.mtime(filepath) < (Time.now - TEMPLATE_CACHE_DURATION)
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require_relative '../lib/ghpreview/converter'
|
4
|
+
|
5
|
+
describe GHPreview::Converter do
|
6
|
+
let(:converter) { GHPreview::Converter }
|
7
|
+
|
8
|
+
it "converts markdown to HTML" do
|
9
|
+
expect(converter.to_html('# Foo')).to eql('<h1>Foo</h1>')
|
10
|
+
end
|
11
|
+
|
12
|
+
it "passes through non-ASCII characters" do
|
13
|
+
expect(converter.to_html('# Baña')).to eql('<h1>Baña</h1>')
|
14
|
+
end
|
15
|
+
end
|
data/spec/preview_readme
ADDED
data/spec/watch_readme
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require_relative '../lib/ghpreview/wrapper'
|
4
|
+
|
5
|
+
describe GHPreview::Wrapper do
|
6
|
+
let(:wrapper) { GHPreview::Wrapper }
|
7
|
+
|
8
|
+
it "wraps an HTML fragment in a full document" do
|
9
|
+
fragment = '<h1>foo</h1>'
|
10
|
+
expect(wrapper.wrap_html(fragment)).to include('<html>')
|
11
|
+
end
|
12
|
+
|
13
|
+
it "passes through non-ASCII chars" do
|
14
|
+
expect(wrapper.wrap_html('<h1>Baña</h1>')).to include('Baña')
|
15
|
+
end
|
16
|
+
|
17
|
+
it "specifies the charset" do
|
18
|
+
expect(wrapper.wrap_html('# foo')).to include("charset='utf-8'")
|
19
|
+
end
|
20
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ghpreview
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-06-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: listen
|
@@ -107,9 +107,17 @@ files:
|
|
107
107
|
- bin/ghpreview
|
108
108
|
- ghpreview.gemspec
|
109
109
|
- lib/ghpreview.rb
|
110
|
+
- lib/ghpreview/converter.rb
|
110
111
|
- lib/ghpreview/previewer.rb
|
111
112
|
- lib/ghpreview/template.erb
|
112
113
|
- lib/ghpreview/version.rb
|
114
|
+
- lib/ghpreview/viewer.rb
|
115
|
+
- lib/ghpreview/watcher.rb
|
116
|
+
- lib/ghpreview/wrapper.rb
|
117
|
+
- spec/converter_spec.rb
|
118
|
+
- spec/preview_readme
|
119
|
+
- spec/watch_readme
|
120
|
+
- spec/wrapper_spec.rb
|
113
121
|
homepage: http://github.com/newcontext/ghpreview
|
114
122
|
licenses: []
|
115
123
|
post_install_message:
|
@@ -134,5 +142,9 @@ rubygems_version: 1.8.23
|
|
134
142
|
signing_key:
|
135
143
|
specification_version: 3
|
136
144
|
summary: Command line utility for previewing Markdown files with Github styling
|
137
|
-
test_files:
|
145
|
+
test_files:
|
146
|
+
- spec/converter_spec.rb
|
147
|
+
- spec/preview_readme
|
148
|
+
- spec/watch_readme
|
149
|
+
- spec/wrapper_spec.rb
|
138
150
|
has_rdoc:
|