Class: Vimdeck::Slideshow

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

Class Method Summary collapse

Class Method Details

.generate(filename) ⇒ Object



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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/vimdeck.rb', line 92

def self.generate(filename)
  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("\n\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)

    # 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( /```([^\n]*)\n.*\n```/m )
    if code
      buffer[:code] = { :language => code[1] }
      code_height = code[0].split("\n").length - 2
      code = code[0].gsub( /```[^\n]*\n/, '' ).gsub( /\n```/, '' )
      slide = slide.gsub( /```[^\n]*\n/, '' ).gsub( /\n```/, '' )

      if code_height > 0
        start = slide.index(code)
        start = slide[0..start].split("\n").length
        buffer[:code][:end] = code_height + start - 1
        buffer[:code][:start] = start
      end
    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( /\n/, "\n#{slide_padding}" ).gsub( / *$/, "" ) + ("\n" * 80) + "slide #{i+1}"

    # 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 #{i+1}||)||@="
      buffer[:comments] << pattern.gsub(/\n/, "||n").gsub(/\[/, "||[").gsub(/\]/, "||]").gsub(/\|/, "\\").gsub(/\"/, "\\\"")
      match = match.post_match.match(regex)
    end

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

    @buffers << buffer
    i += 1
  end

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

.openObject



159
160
161
# File 'lib/vimdeck.rb', line 159

def self.open
  exec 'vim presentation/*.md -S presentation/script.vim'
end

.script_templateObject



87
88
89
90
# File 'lib/vimdeck.rb', line 87

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

.slide_paddingObject



83
84
85
# File 'lib/vimdeck.rb', line 83

def self.slide_padding
  "        "
end

.start(filename) ⇒ Object



163
164
165
166
# File 'lib/vimdeck.rb', line 163

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