-
Notifications
You must be signed in to change notification settings - Fork 32
/
hammer.rb
143 lines (122 loc) · 3.38 KB
/
hammer.rb
1
2
3
4
5
6
7
8
9
10
11
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
module Hammer
class << self
#
# @return [Array<String>]
# List of dependencies required for Hammer to boot.
#
def dependencies
@dependencies ||=
[
"rubygems",
"hammer/env" ,
"vim/improvedbuffer",
"erb",
"shellwords",
"github/markup",
"tilt"
]
end
#
# @return [Boolean]
# Could hammer load all its dependencies?
#
def dependencies_met?
dependencies == met_dependencies
end
#
# @return [Array<String>]
# A list of dependencies hammer could load successfully.
#
def met_dependencies
@met_depedencies ||= []
end
#
# Loads all dependencies from {dependencies}.
#
# @return [void]
#
def load_dependencies!
dependencies.each do |dependency|
begin
require(dependency)
rescue LoadError
else
met_dependencies << dependency
end
end
end
#
# Loads all renderers present in {Hammer::ENV.renderers_path}.
#
# @return [void]
#
def load_renderers!
return nil unless dependencies_met?
glob = File.join(Hammer::ENV.renderers_path, "*.rb")
Dir[glob].each do |renderer|
to_ruby = File.read(renderer)
GitHub::Markup.instance_eval(to_ruby)
end
end
#
# @return [Array<String>]
# A list of dependencies hammer cannot load.
#
def missing_dependencies
dependencies - met_dependencies
end
#
# @param [String] path
# The path to a file to open with {Hammer::ENV.browser}
#
# @return [void]
#
def open_browser path
browser_path = Shellwords.escape(Hammer::ENV.browser)
browser_args = Hammer::ENV.browser_args
file_path = Shellwords.escape(path)
Vim.command "silent ! #{browser_path} #{browser_args} #{file_path}"
Vim.command "redraw!"
end
#
# Converts the contents of the buffer to HTML, and displays them in a
# browser.
#
# @param [Vim::ImprovedBuffer]
# A {Vim::ImprovedBuffer} object.
#
# @return [void]
#
def render! buffer
unless dependencies_met?
msg = "Hammer is missing dependencies: #{missing_dependencies.join(', ')}"
Vim.message(msg)
return nil
end
has_tohtml = if Vim.evaluate('!&cp && exists(":TOhtml") && has("user_commands")') == 1 then true else nil end
cleanup = nil
unless GitHub::Markup.can_render?(buffer.basename) || has_tohtml
msg = "Cannot render '#{buffer.extname}' files. Missing renderer?"
Vim.message(msg)
return nil
end
path = File.join Hammer::ENV.directory, "#{buffer.basename}.html"
File.open path, 'w' do |f|
tilt = Tilt.new(Hammer::ENV.template)
output = tilt.render do
if ! GitHub::Markup.can_render?(buffer.basename) && has_tohtml
Vim.command 'TOhtml'
tohtml_buffer = Vim::Buffer.current.extend Vim::ImprovedBuffer
cleanup = if tohtml_buffer != buffer then true else nil end
GitHub::Markup.render(tohtml_buffer.basename, tohtml_buffer[1..-1])
else
GitHub::Markup.render(buffer.basename, buffer[1..-1])
end
end
f.write(output)
Vim.command 'bdelete!' if cleanup
end
Hammer.open_browser path
end
end
end