Class: Vimdeck::Slideshow

Inherits:
Object
  • Object
show all
Defined in:
lib/vimdeck.rb

Class Method Summary collapse

Class Method Details

.generate(filename, options) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/vimdeck.rb', line 147

def self.generate(filename, options)
  @options = options
  extension = options[:no_filetype] ? ".txt" : ".md"
  if options[:dos_newlines]
    $nl = "\r\n"
  end
  slides = File.read(filename)

  renderer = Redcarpet::Markdown.new(Vimdeck::Render, :fenced_code_blocks => true)
  Dir.mkdir("presentation") unless File.exists?("presentation")
  @buffers = []

  # Slide separator is 3 newlines
  slides = slides.split(/(?:\r\n?|\n)(?:\r\n?|\n)(?:\r\n?|\n)/)
  i = 0
  slides.each do |slide|
    # Pad file names with zeros. e.g. slide001.md, slide023.md, etc.
    slide_num = "%03d" % (i+1)
    slide = renderer.render(slide)

    regex = /\<\@\~(.*?)\~\@\>/m
    match = slide.match(regex)
    while match && match[1] && match.post_match do
      list = match[1].split(/\r\n?|\n/)
      j = 0
      list = list.map do |li|
        j += 1
        "#{j}. #{li}"
      end
      slide.sub!(regex, list.join($nl))
      match = match.post_match.match(regex)
    end

    regex = /\<\!\~(.*?)\~\!\>/m
    match = slide.match(regex)
    while match && match[1] && match.post_match do
      list = match[1].split(/\r\n?|\n/)
      list = list.map do |li|
        "\u2022 #{li}"
      end
      slide.sub!(regex, list.join($nl))
      match = match.post_match.match(regex)
    end

    # buffer gets stashed into @buffers array for script template
    # needs to track things like the buffer number, code highlighting
    # and focus/unfocus stuff
    buffer = {:num => i + 1}
    code_height = 0
    code = nil
    code = slide.match( /```([^\r\n]*)(\r\n?|\n).*(\r\n?|\n)```/m )
    if code
      buffer[:code] = []
      code_hash = { :language => code[1] }
      code_height = code[0].split(/\r\n?|\n/).length - 2
      code = code[0].gsub( /```[^\r\n]*(\r\n?|\n)/, '' ).gsub( /(\r\n?|\n)```/, '' )
      slide = slide.gsub( /```[^\r\n]*(\r\n?|\n)/, '' ).gsub( /(\r\n?|\n)```/, '' )

      if code_height > 0
        start = slide.index(code)
        start = slide[0..start].split(/\r\n?|\n/).length
        code_hash[:end] = code_height + start - 1
        code_hash[:start] = start
      end
      buffer[:code] << code_hash
    end

    # Prepending each line with slide_padding
    # Removing trailing spaces
    # Add newlines at end of the file to hide the slide identifier
    slide = slide_padding + slide.gsub( /\r\n?|\n/, "#{$nl}#{slide_padding}" ).gsub( / *$/, "" ) + ($nl * 80) + "slide #{slide_num}"

    # Buffers comments refers to items that need to be less focused/"unhighlighted"
    # We add a regex to the vimscript for each slide with "comments"
    # We use the hidden slide identifier to differentiate between slides
    regex = /\{\~(.*?)\~\}/m
    match = slide.match(regex)
    buffer[:comments] = []
    while match && match[1] && match.post_match do
      slide.sub!(regex, match[1])
      pattern = match[1] + "||(||_.*slide #{slide_num}||)||@="
      buffer[:comments] << pattern.gsub(/\r\n?|\n/, "||n").gsub(/\[/, "||[").gsub(/\]/, "||]").gsub(/\|/, "\\").gsub(/\"/, "\\\"")
      match = match.post_match.match(regex)
    end

    File.open("presentation/slide#{slide_num}#{extension}", "w") do |file|
      file.write("#{slide}#{$nl}")
    end

    @buffers << buffer
    i += 1
  end

  File.open("presentation/script.vim", "w") do |file|
    file.write script_template
  end
end

.openObject



245
246
247
248
249
# File 'lib/vimdeck.rb', line 245

def self.open
  extension = @options[:no_filetype] ? ".txt" : ".md"
  editor = options[:editor] || "vim"
  exec "#{editor} presentation/*#{extension} -S presentation/script.vim"
end

.optionsObject



134
135
136
# File 'lib/vimdeck.rb', line 134

def self.options
  @options
end

.script_templateObject



142
143
144
145
# File 'lib/vimdeck.rb', line 142

def self.script_template
  template = ERB.new(File.read(File.dirname(__FILE__) + "/templates/script.vim.erb"), nil, '-')
  template.result(binding)
end

.slide_paddingObject



138
139
140
# File 'lib/vimdeck.rb', line 138

def self.slide_padding
  @options[:no_indent] ? "" : "        "
end

.start(filename, options) ⇒ Object



251
252
253
254
# File 'lib/vimdeck.rb', line 251

def self.start(filename, options)
  generate(filename, options)
  open
end