octodown 0.0.1alpha1

Sign up to get free protection for your applications and to get access to all the features.
data/bin/octodown ADDED
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'octodown'
4
+ require 'optparse'
5
+ require 'pathname'
6
+ require 'securerandom'
7
+
8
+ include Octodown
9
+
10
+ options = {}
11
+
12
+ OptionParser.new do |opts|
13
+ opts.banner = "Usage: octodown [options]"
14
+
15
+ opts.on("-s", "--style [STYLE]", [:github, :atom], "Choose style (atom, github)") do |s|
16
+ options[:style] = s
17
+ end
18
+
19
+ opts.on_tail("-h", "--help", "Show this message") do
20
+ puts opts
21
+ exit
22
+ end
23
+ end.parse!
24
+
25
+ def create_html_from_md(filename, template)
26
+ unstyled_html = Renderer::GithubMarkdown.new(filename).to_html
27
+ Renderer::HTML.new(unstyled_html, template).render
28
+ end
29
+
30
+
31
+ def open_browser_cmd
32
+ if RUBY_PLATFORM.include? 'darwin'
33
+ return '/usr/bin/open'
34
+ elsif RUBY_PLATFORM.include? 'linux'
35
+ candidates = %w(/usr/bin/xdg-open /usr/bin/x-www-browser)
36
+
37
+ candidates.each do |cmd|
38
+ return cmd if File.exists? cmd
39
+ end
40
+ else
41
+ return 'start'
42
+ end
43
+
44
+ false
45
+ end
46
+
47
+ def open_in_browser(html)
48
+ tmp = Pathname.new "/tmp/octodown_#{SecureRandom.hex}.html"
49
+ file = File.open(tmp, 'w') { |file| file.write html }
50
+
51
+ if cmd = open_browser_cmd
52
+ Process.spawn(cmd, tmp.to_s, [:out, :err] => File.open('/dev/null').fileno)
53
+ else
54
+ raise RuntimeError, 'No compatible shell command to open file in browser'
55
+ end
56
+ end
57
+
58
+ html = create_html_from_md ARGF.read, options[:style] || 'github'
59
+ open_in_browser html
@@ -0,0 +1,36 @@
1
+ require 'github/markup'
2
+ require 'pygments'
3
+ require 'html/pipeline'
4
+
5
+ module Octodown
6
+ module Renderer
7
+ class GithubMarkdown
8
+ attr_reader :content
9
+
10
+ def initialize(content)
11
+ @content = content
12
+ end
13
+
14
+ def to_html
15
+ pipeline.call(content)[:output].to_s
16
+ end
17
+
18
+ private
19
+
20
+ def context
21
+ { :asset_root => 'https://assets-cdn.github.com/images/icons/'}
22
+ end
23
+
24
+ def pipeline
25
+ ::HTML::Pipeline.new [
26
+ ::HTML::Pipeline::MarkdownFilter,
27
+ ::HTML::Pipeline::SanitizationFilter,
28
+ ::HTML::Pipeline::ImageMaxWidthFilter,
29
+ ::HTML::Pipeline::MentionFilter,
30
+ ::HTML::Pipeline::EmojiFilter,
31
+ ::HTML::Pipeline::SyntaxHighlightFilter
32
+ ], context.merge(:gfm => true)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,41 @@
1
+ require 'erb'
2
+
3
+ module Octodown
4
+ module Renderer
5
+ class HTML
6
+ attr_reader :rendered_markdown, :template
7
+
8
+ def initialize(rendered_markdown, template)
9
+ @rendered_markdown = rendered_markdown
10
+ @template = template
11
+ end
12
+
13
+ def render
14
+ template_text = File.read template_filepath
15
+ erb_template = ERB.new template_text
16
+ erb_template.result binding
17
+ end
18
+
19
+ def title
20
+ 'Octodown Preview'
21
+ end
22
+
23
+ def stylesheet
24
+ stylesheet_file = File.join Octodown.root, 'assets',
25
+ "#{template}.css"
26
+ File.read stylesheet_file
27
+ end
28
+
29
+ private
30
+
31
+ def parent_dir
32
+ current_file = File.dirname __FILE__
33
+ File.expand_path '..', current_file
34
+ end
35
+
36
+ def template_filepath
37
+ File.join parent_dir, 'template', 'octodown.html.erb'
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE html>
2
+
3
+ <html>
4
+ <head>
5
+ <title><%= title %></title>
6
+
7
+ <style>
8
+ <%= stylesheet %>
9
+ </style>
10
+ </head>
11
+
12
+ <body>
13
+ <article class="markdown-body">
14
+ <%= rendered_markdown %>
15
+ </article>
16
+ </body>
17
+ </html>
@@ -0,0 +1,3 @@
1
+ module Octodown
2
+ VERSION = '0.0.1alpha1'
3
+ end
data/lib/octodown.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'octodown/version'
2
+ require 'octodown/renderer/github_markdown'
3
+ require 'octodown/renderer/html'
4
+
5
+ module Octodown
6
+ def self.root
7
+ spec = Gem::Specification.find_by_name 'octodown'
8
+ spec.gem_dir
9
+ end
10
+ end
data/octodown.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'octodown/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'octodown'
8
+ spec.version = Octodown::VERSION
9
+ spec.authors = ['Ian Ker-Seymer']
10
+ spec.email = ['i.kerseymer@gmail.com']
11
+ spec.summary = %q{Simple and precise markdown previewing from your terminal.}
12
+ spec.homepage = 'https://github.com/ianks/octodown'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_dependency 'github-markup'
21
+ spec.add_dependency 'github-linguist'
22
+ spec.add_dependency 'html-pipeline'
23
+ spec.add_dependency 'sanitize'
24
+ spec.add_dependency 'github-markdown'
25
+ spec.add_dependency 'gemoji'
26
+ spec.add_dependency 'pygments.rb'
27
+
28
+ spec.add_development_dependency 'rspec'
29
+ spec.add_development_dependency 'bundler', '~> 1.7'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
31
+ end
@@ -0,0 +1,9 @@
1
+ # Hello world!
2
+
3
+ You are now reading markdown. How lucky you are!
4
+
5
+ ```ruby
6
+ def some_code()
7
+ puts 'Yeah, this is some code.'
8
+ end
9
+ ```
@@ -0,0 +1,15 @@
1
+ require 'tempfile'
2
+
3
+ describe Octodown::Renderer::GithubMarkdown do
4
+ let(:dummy_path) { File.join(File.dirname(__FILE__), 'dummy', 'test.md') }
5
+ let(:html) { Octodown::Renderer::GithubMarkdown.new(File.read(dummy_path)).to_html }
6
+
7
+ it 'create HTML from markdown file' do
8
+ expect(html).to include '<h1>Hello world!</h1>'
9
+ expect(html).to include '<p>You are now reading markdown. How lucky you are!</p>'
10
+ end
11
+
12
+ it 'highlights the code' do
13
+ expect(html).to include 'highlight-ruby'
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ require 'octodown'
2
+
3
+ RSpec.configure do |config|
4
+ config.expect_with :rspec do |expectations|
5
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
6
+ end
7
+
8
+ config.default_formatter = 'doc' if config.files_to_run.one?
9
+ config.order = :random
10
+
11
+ Kernel.srand config.seed
12
+ end
@@ -0,0 +1,24 @@
1
+ require 'tempfile'
2
+
3
+ describe Octodown::Renderer::HTML do
4
+ let(:dummy_path) { File.join(File.dirname(__FILE__), 'dummy', 'test.md') }
5
+ let(:html) { Octodown::Renderer::GithubMarkdown.new(File.read(dummy_path)).to_html }
6
+ subject { Octodown::Renderer::HTML.new(html, 'github').render }
7
+
8
+ before { allow(Octodown).to receive(:root) { '.' } }
9
+
10
+ it 'includes HTML from markdown rendering phase' do
11
+ expect(subject).to include '<h1>Hello world!</h1>'
12
+ expect(subject).to include '<p>You are now reading markdown. How lucky you are!</p>'
13
+ expect(subject).to include 'highlight-ruby'
14
+ end
15
+
16
+ it 'sets the title' do
17
+ expect(subject).to include '<title>Octodown Preview</title>'
18
+ end
19
+
20
+ it 'injects Github CSS' do
21
+ css = File.read(File.join(Octodown.root, 'assets', 'github.css'))
22
+ expect(subject).to include css
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,231 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: octodown
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1alpha1
5
+ prerelease: 5
6
+ platform: ruby
7
+ authors:
8
+ - Ian Ker-Seymer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-12-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: github-markup
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: github-linguist
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: html-pipeline
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
+ - !ruby/object:Gem::Dependency
63
+ name: sanitize
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: github-markdown
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: gemoji
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: pygments.rb
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rspec
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: bundler
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ~>
148
+ - !ruby/object:Gem::Version
149
+ version: '1.7'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ~>
156
+ - !ruby/object:Gem::Version
157
+ version: '1.7'
158
+ - !ruby/object:Gem::Dependency
159
+ name: rake
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ~>
164
+ - !ruby/object:Gem::Version
165
+ version: '10.0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ~>
172
+ - !ruby/object:Gem::Version
173
+ version: '10.0'
174
+ description:
175
+ email:
176
+ - i.kerseymer@gmail.com
177
+ executables:
178
+ - octodown
179
+ extensions: []
180
+ extra_rdoc_files: []
181
+ files:
182
+ - .gitignore
183
+ - .rspec
184
+ - .travis.yml
185
+ - Gemfile
186
+ - LICENSE.txt
187
+ - README.md
188
+ - Rakefile
189
+ - assets/atom.css
190
+ - assets/github.css
191
+ - bin/octodown
192
+ - lib/octodown.rb
193
+ - lib/octodown/renderer/github_markdown.rb
194
+ - lib/octodown/renderer/html.rb
195
+ - lib/octodown/template/octodown.html.erb
196
+ - lib/octodown/version.rb
197
+ - octodown.gemspec
198
+ - spec/dummy/test.md
199
+ - spec/markdown_generation_spec.rb
200
+ - spec/spec_helper.rb
201
+ - spec/template_rendering_spec.rb
202
+ homepage: https://github.com/ianks/octodown
203
+ licenses:
204
+ - MIT
205
+ post_install_message:
206
+ rdoc_options: []
207
+ require_paths:
208
+ - lib
209
+ required_ruby_version: !ruby/object:Gem::Requirement
210
+ none: false
211
+ requirements:
212
+ - - ! '>='
213
+ - !ruby/object:Gem::Version
214
+ version: '0'
215
+ required_rubygems_version: !ruby/object:Gem::Requirement
216
+ none: false
217
+ requirements:
218
+ - - ! '>'
219
+ - !ruby/object:Gem::Version
220
+ version: 1.3.1
221
+ requirements: []
222
+ rubyforge_project:
223
+ rubygems_version: 1.8.23.2
224
+ signing_key:
225
+ specification_version: 3
226
+ summary: Simple and precise markdown previewing from your terminal.
227
+ test_files:
228
+ - spec/dummy/test.md
229
+ - spec/markdown_generation_spec.rb
230
+ - spec/spec_helper.rb
231
+ - spec/template_rendering_spec.rb