Class: Slideshow::Build

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/slideshow/commands/build.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Build

Returns a new instance of Build.



11
12
13
14
15
16
17
# File 'lib/slideshow/commands/build.rb', line 11

def initialize( config )
  @config  = config
  @headers = Headers.new( config )    

  ## todo: check if we need to use expand_path - Dir.pwd always absolute (check ~/user etc.)
  @usrdir = File.expand_path( Dir.pwd )  # save original (current) working directory 
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



20
21
22
# File 'lib/slideshow/commands/build.rb', line 20

def config
  @config
end

#headersObject (readonly)

Returns the value of attribute headers.



20
21
22
# File 'lib/slideshow/commands/build.rb', line 20

def headers
  @headers
end

#usrdirObject (readonly)

original working dir (user called slideshow from)



19
20
21
# File 'lib/slideshow/commands/build.rb', line 19

def usrdir
  @usrdir
end

Instance Method Details

#create_deck_from_string(buf) ⇒ Object



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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/slideshow/commands/build.rb', line 208

def create_deck_from_string( buf )
   
   ##
   ## todo/fix: cleanup how to handle files/folders
   ##   e.g. do we need outdir,srcdir,usrdir ??
   ##   does srcdir make any sense for "inline" buffer??
   ##   use srcdir==usrdir  - and what is usrdir default ?? etc.
   

   ##  use a tmp folder for output - why? why not?
   #    - no "real" output other than debug files
   
   # expand output path in current dir and make sure output path exists
   outdir = File.expand_path( config.output_path, usrdir )
   logger.debug "setting outdir to >#{outdir}<"
   
   FileUtils.makedirs( outdir ) unless File.directory? outdir


   ###  todo/fix:
   ##  reset headers too - why? why not?
    
   # shared variables for templates (binding)
   content_for = {}  # reset content_for hash
   # give helpers/plugins a session-like hash
   session     = {}  # reset session hash for plugins/helpers

   name = 'untitled'     ## default name (auto-detect from first file e.g. rest.txt => rest etc.)

   content = ''

   gen = Gen.new( @config,
                  @headers,
                  session,
                  content_for )

     ## use a "fake" filename for now  - why? why not?
     fn      = 'index.txt'

     dirname  = File.dirname( fn )
     basename = File.basename( fn, '.*' )
     extname  = File.extname( fn )
     logger.debug "dirname=#{dirname}, basename=#{basename}, extname=#{extname}"

     name = basename
     puts "Preparing slideshow '#{basename}'..."


     # change working dir to sourcefile dir
     # todo: add a -c option to commandline? to let you set cwd?

     srcdir = File.expand_path( dirname, usrdir )
     logger.debug "setting srcdir to >#{srcdir}<"
   
     logger.debug "changing cwd to src - new >#{srcdir}<, old >#{Dir.pwd}<"
     Dir.chdir srcdir


     ####################
     ## todo/fix: move ctx to Gen.initialize - why? why not?
     #    move outdir, usrdir, name to Gen.initialize ??
     #    add basename, dirname ?
     gen_ctx = {
       name:    name,
       srcdir:  srcdir,
       outdir:  outdir,
       usrdir:  usrdir,
     }

     content = gen.render( buf, gen_ctx )

     logger.debug "restoring cwd to usr - new >#{usrdir}<, old >#{Dir.pwd}<"
     Dir.chdir( usrdir )

     # post-processing (all-in-one HTML with directive as HTML comments)
     deck = Deck.new( content, header_level: config.header_level,
                               use_slide:    config.slide? )

     deck
end

#create_slideshow(args) ⇒ Object



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
144
145
146
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
# File 'lib/slideshow/commands/build.rb', line 72

def create_slideshow( args )

    ## first check if manifest exists / available / valid
    manifestsrc = ManifestFinder.new( config ).find_manifestsrc( config.manifest )


    # expand output path in current dir and make sure output path exists
    outdir = File.expand_path( config.output_path, usrdir )
    logger.debug "setting outdir to >#{outdir}<"
    
    FileUtils.makedirs( outdir ) unless File.directory? outdir


    if args.is_a? String
      args = [args]    ## for now for testing always assume array
    end      
    
    buffers = process_files( args )

    puts "buffers:"
    pp buffers


    ###  todo/fix:
    ##  reset headers too - why? why not?
     
    # shared variables for templates (binding)
    content_for = {}  # reset content_for hash
    # give helpers/plugins a session-like hash
    session     = {}  # reset session hash for plugins/helpers

    name = 'untitled'     ## default name (auto-detect from first file e.g. rest.txt => rest etc.)

    content = ''

    ## check for css and js buffers
    ##    todo/fix: check if content_for key is a symbol or just a string !!!!!!
    if buffers[:css]
      ## concat files (separate with newlines)
      content_for[:css] = buffers[:css][:contents].join( "\n\n" )
    end

    if buffers[:js]
      ## concat files (separate with newlines)
      content_for[:js] = buffers[:js][:contents].join( "\n\n" )
    end


    gen = Gen.new( @config,
                   @headers,
                   session,
                   content_for )

    chunk_size = buffers[:text] ? buffers[:text][:contents].size : 0
    
    (0...chunk_size).each do |i|
      
      chunk   = buffers[:text][:contents][i]
      fn      = buffers[:text][:files][i]

      dirname  = File.dirname( fn )
      basename = File.basename( fn, '.*' )
      extname  = File.extname( fn )
      logger.debug "dirname=#{dirname}, basename=#{basename}, extname=#{extname}"

      ## for now use first text file for (auto-)caluclation name and source folder
      if i==0
        name = basename
        puts "Preparing slideshow '#{basename}'..."
      end

      puts "  [#{i+1}/#{chunk_size}] Generating '#{basename}' (#{dirname})..."


      # change working dir to sourcefile dir
      # todo: add a -c option to commandline? to let you set cwd?

      srcdir = File.expand_path( dirname, usrdir )
      logger.debug "setting srcdir to >#{srcdir}<"
    
      logger.debug "changing cwd to src - new >#{srcdir}<, old >#{Dir.pwd}<"
      Dir.chdir srcdir


      ####################
      ## todo/fix: move ctx to Gen.initialize - why? why not?
      #    move outdir, usrdir, name to Gen.initialize ??
      #    add basename, dirname ?
      gen_ctx = {
        name:    name,
        srcdir:  srcdir,
        outdir:  outdir,
        usrdir:  usrdir,
      }

      chunk = gen.render( chunk, gen_ctx )

      if i==0   ## first chunk
        content << chunk
      else      ## follow-up chunk (start off with two newlines)
        content << "\n\n"
        content << chunk
      end
    end   # each buffer.text.contents


    logger.debug "restoring cwd to usr - new >#{usrdir}<, old >#{Dir.pwd}<"
    Dir.chdir( usrdir )


    # post-processing (all-in-one HTML with directive as HTML comments)
    deck = Deck.new( content, header_level: config.header_level,
                              use_slide:    config.slide? )


  ## note: merge for now requires resetting to
  ##         original working dir (user called slideshow from)
  merge = Merge.new( config )
    
  merge_ctx = {
    manifestsrc: manifestsrc,
    outdir:      outdir,
    name:        name,
  }
  
  merge.merge( deck,
               merge_ctx,
               headers,
               content_for )


  puts 'Done.'
end

#process_files(args) ⇒ Object



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
# File 'lib/slideshow/commands/build.rb', line 23

def process_files( args )

   ###
   #  returns a hash of merged buffers e.g.
   #  { text: '...',
   #     js:   '...',
   #     css:  '...',
   #  }
   #  
  
  buffers = {}   
  
  args.each do |fn|

    dirname  = File.dirname( fn )
    basename = File.basename( fn, '.*' )
    extname  = File.extname( fn )    ## note: returns ext with dot (e.g. .css or .js etc.)
    
    logger.debug "dirname=#{dirname}, basename=#{basename}, extname=#{extname}"

    content = File.read_utf8( fn )

    if extname.downcase == '.css'
      key = :css    # buffer key
    elsif extname.downcase == '.js'
      key = :js
    else  ## assume main text/content
      ##
      ##  todo/check:  process text files with gen(erator) one-by-one later? why? why not?
      #     for now process as one block all together (use sourcedir of first text file)
      key = :text
    end
    
    if buffers[ key ].nil?   ## first entry
      h = { contents: [content],
            files:    [fn],
          }
      buffers[ key ] = h
    else                    ## second, third, etc. entry
      h = buffers[ key ]
      h[:contents] << content
      h[:files]    << fn
    end
  end

  buffers
end