Class: Slideshow::Gen

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging, BackgroundHelper, DebugFilter, DirectiveHelper, HeadersFilter, ManifestHelper, MarkdownEngines, MarkdownHelper, MediawikiEngines, RestEngines, SlideFilter, SourceHelper, StepHelper, Syntax::CodeRayHelper, Syntax::ShHelper, Syntax::UvHelper, TextFilter, TextileEngines
Defined in:
lib/slideshow/markup/rest.rb,
lib/slideshow/markup/textile.rb,
lib/slideshow/markup/markdown.rb,
lib/slideshow/cli/commands/gen.rb,
lib/slideshow/markup/mediawiki.rb,
lib/slideshow/filters/text_filter.rb,
lib/slideshow/helpers/step_helper.rb,
lib/slideshow/filters/debug_filter.rb,
lib/slideshow/filters/slide_filter.rb,
lib/slideshow/helpers/source_helper.rb,
lib/slideshow/filters/headers_filter.rb,
lib/slideshow/helpers/markdown_helper.rb,
lib/slideshow/helpers/directive_helper.rb,
lib/slideshow/helpers/syntax/sh_helper.rb,
lib/slideshow/helpers/syntax/uv_helper.rb,
lib/slideshow/helpers/background_helper.rb,
lib/slideshow/helpers/syntax/coderay_helper.rb

Overview

module Slideshow

Constant Summary

Constants included from Syntax::CodeRayHelper

Syntax::CodeRayHelper::CR_LANG, Syntax::CodeRayHelper::CR_LINE_NUMBERS

Constants included from Syntax::UvHelper

Syntax::UvHelper::UV_LANG, Syntax::UvHelper::UV_LINE_NUMBERS, Syntax::UvHelper::UV_THEME

Constants included from Syntax::ShHelper

Syntax::ShHelper::SH_LANG, Syntax::ShHelper::SH_LINE_NUMBERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Syntax::CodeRayHelper

#coderay, #coderay_worker

Methods included from BackgroundHelper

#background, #gradient, #gradient_from_headers

Methods included from Syntax::UvHelper

#uv, #uv_worker

Methods included from Syntax::ShHelper

#sh_worker, #sv

Methods included from DirectiveHelper

#css, #slide, #style

Methods included from MarkdownHelper

#clear, #s9_class

Methods included from HeadersFilter

#leading_headers

Methods included from SourceHelper

#source

Methods included from SlideFilter

#add_slide_directive_before_div_h1, #add_slide_directive_before_h1, #add_slide_directive_before_h2, #takahashi_slide_breaks

Methods included from DebugFilter

#dump_content_to_file_debug_html, #dump_content_to_file_debug_text, #dump_content_to_file_debug_text_erb

Methods included from StepHelper

#step

Methods included from TextFilter

#comments_percent_style, #directives_bang_style_to_percent_style, #directives_percent_style, #erb_rename_helper_hack, #old_comments_percent_style

Methods included from MediawikiEngines

#mediawiki_to_html, #setup_mediawiki_engine

Methods included from ManifestHelper

#installed_plugin_manifest_patterns, #installed_plugin_manifests, #installed_quick_manifest_patterns, #installed_quick_manifests, #installed_template_manifest_patterns, #installed_template_manifests

Methods included from MarkdownEngines

#markdown_to_html

Methods included from TextileEngines

#redcloth_java_fix_escape_nonascii, #redcloth_java_fix_escape_nonascii_exclude_pre, #setup_textile_engine, #textile_to_html

Methods included from RestEngines

#rest_to_html

Constructor Details

#initialize(opts, config, headers) ⇒ Gen

fix: remove opts, use config (wrapped!!)



16
17
18
19
20
21
22
23
# File 'lib/slideshow/cli/commands/gen.rb', line 16

def initialize( opts, config, headers )
  @opts    = opts
  @config  = config
  @headers = headers
  
  ## 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.



29
30
31
# File 'lib/slideshow/cli/commands/gen.rb', line 29

def config
  @config
end

#headersObject (readonly)

Returns the value of attribute headers.



29
30
31
# File 'lib/slideshow/cli/commands/gen.rb', line 29

def headers
  @headers
end

#markup_typeObject (readonly)

:textile, :markdown, :mediawiki, :rest



32
33
34
# File 'lib/slideshow/cli/commands/gen.rb', line 32

def markup_type
  @markup_type
end

#optsObject (readonly)

Returns the value of attribute opts.



29
30
31
# File 'lib/slideshow/cli/commands/gen.rb', line 29

def opts
  @opts
end

#outdirObject (readonly)

NB: “initalized” in create_slideshow



26
27
28
# File 'lib/slideshow/cli/commands/gen.rb', line 26

def outdir
  @outdir
end

#pakdirObject (readonly)

NB: “initalized” in create_slideshow



26
27
28
# File 'lib/slideshow/cli/commands/gen.rb', line 26

def pakdir
  @pakdir
end

#sessionObject (readonly)

give helpers/plugins a session-like hash



30
31
32
# File 'lib/slideshow/cli/commands/gen.rb', line 30

def session
  @session
end

#srcdirObject (readonly)

NB: “initalized” in create_slideshow



26
27
28
# File 'lib/slideshow/cli/commands/gen.rb', line 26

def srcdir
  @srcdir
end

#usrdirObject (readonly)

original working dir (user called slideshow from)



25
26
27
# File 'lib/slideshow/cli/commands/gen.rb', line 25

def usrdir
  @usrdir
end

Instance Method Details

#create_slideshow(fn) ⇒ Object



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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/slideshow/cli/commands/gen.rb', line 184

def create_slideshow( fn )

    manifest_path_or_name = opts.manifest
    
    # add .txt file extension if missing (for convenience)
    if manifest_path_or_name.downcase.ends_with?( '.txt' ) == false
      manifest_path_or_name << '.txt'
    end
  
    logger.debug "manifest=#{manifest_path_or_name}"
    
    # check if file exists (if yes use custom template package!) - allows you to override builtin package with same name 
    if File.exists?( manifest_path_or_name )
      manifestsrc = manifest_path_or_name
    else
      # check for builtin manifests
      manifests = installed_template_manifests
      matches = manifests.select { |m| m[0] == manifest_path_or_name } 

      if matches.empty?
        puts "*** error: unknown template manifest '#{manifest_path_or_name}'"
        # todo: list installed manifests
        exit 2
      end
        
      manifestsrc = matches[0][1]
    end

    ### todo: use File.expand_path( xx, relative_to ) always with second arg
    ##   do NOT default to cwd (because cwd will change!)
    
    # Reference src with absolute path, because this can be used with different pwd
    manifestsrc = File.expand_path( manifestsrc, usrdir )

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

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

    # 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}<"

    unless usrdir == srcdir
      logger.debug "changing cwd to src - new >#{srcdir}<, old >#{Dir.pwd}<"
      Dir.chdir srcdir
    end

    puts "Preparing slideshow '#{basename}'..."
   
  if config.known_textile_extnames.include?( extname )
    @markup_type = :textile
  elsif config.known_rest_extnames.include?( extname )
    @markup_type = :rest
  elsif config.known_mediawiki_extnames.include?( extname )
    @markup_type = :mediawiki
  else  # default/fallback use markdown
    @markup_type = :markdown
  end
  
  # shared variables for templates (binding)
  @content_for = {}  # reset content_for hash

  @name        = basename
  @extname     = extname

  @session     = {}  # reset session hash for plugins/helpers

  inname  =  "#{basename}#{extname}"

  logger.debug "inname=#{inname}"
    
  content = File.read( inname )

  # run text filters
  
  config.text_filters.each do |filter|
    mn = filter.tr( '-', '_' ).to_sym  # construct method name (mn)
    content = send( mn, content )   # call filter e.g.  include_helper_hack( content )  
  end


  if config.takahashi?
    content = takahashi_slide_breaks( content )
  end


  # convert light-weight markup to hypertext

  content = text_to_html( content )

  # post-processing

  # make content2 and slide2 available to erb template
    # -- todo: cleanup variable names and use attr_readers for content and slide
  
  if @markup_type == :markdown && config.markdown_post_processing?( Markdown.lib ) == false
    puts "  Skipping post-processing (passing content through as is)..."
    @content = content  # content all-in-one - make it available in erb templates
  else
    # sets @content (all-in-one string) and @slides (structured content; split into slides)
    post_processing_slides( content )
  end


  #### pak merge
  #  nb: change cwd to template pak root

  @pakdir = File.dirname( manifestsrc )  # template pak root - make availabe too in erb via binding
  logger.debug " setting pakdir to >#{pakdir}<"

  #  todo/fix: change current work dir (cwd) in pakman gem itself
  #   for now lets do it here

  logger.debug "changing cwd to pak - new >#{pakdir}<, old >#{Dir.pwd}<"
  Dir.chdir( pakdir )


  pakpath     = outdir

  logger.debug( "manifestsrc >#{manifestsrc}<, pakpath >#{pakpath}<" )

  Pakman::Templater.new( logger ).merge_pak( manifestsrc, pakpath, binding, basename )

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

  ## pop/restore org (original) working folder/dir
  unless usrdir == srcdir
    logger.debug "restoring cwd to usr - new >#{usrdir}<, old >#{Dir.pwd}<"
    Dir.chdir( usrdir )
  end

  puts "Done."
end

#guard_block(text) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/slideshow/cli/commands/gen.rb', line 61

def guard_block( text )
  if markup_type == :textile
    # saveguard with notextile wrapper etc./no further processing needed
    # note: add leading newlines to avoid block-runons
    "\n\n<notextile>\n#{text}\n</notextile>\n"
  elsif markup_type == :markdown
    # wrap in newlines to avoid runons
    "\n\n#{text}\n\n"
  elsif markup_type == :mediawiki
    "\n\n<nowiki>\n#{text}\n</nowiki>\n"
  else
    text
  end
end

#guard_inline(text) ⇒ Object



76
77
78
# File 'lib/slideshow/cli/commands/gen.rb', line 76

def guard_inline( text )
  wrap_markup( text )
end

#guard_text(text) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/slideshow/cli/commands/gen.rb', line 49

def guard_text( text )
  # todo/fix 2: note for Textile we need to differentiate between blocks and inline
  #   thus, to avoid runs - use guard_block (add a leading newline to avoid getting include in block that goes before)
  
  # todo/fix: remove wrap_markup; replace w/ guard_text
  #   why: text might be css, js, not just html
  
  ## todo: add print depreciation warning
  
  wrap_markup( text )
end

#post_processing_slides(content) ⇒ Object

move into a filter??



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
# File 'lib/slideshow/cli/commands/gen.rb', line 92

def post_processing_slides( content )
  
  # 1) add slide breaks

  if config.slide?  # only allow !SLIDE directives fo slide breaks?
     # do nothing (no extra automagic slide breaks wanted)
  else  
    if (@markup_type == :markdown && Markdown.lib == 'pandoc-ruby') || @markup_type == :rest
      content = add_slide_directive_before_div_h1( content )
    else
      if config.header_level == 2
        content = add_slide_directive_before_h2( content )
      else # assume level 1
        content = add_slide_directive_before_h1( content )
      end
    end
  end


  dump_content_to_file_debug_html( content )

  # 2) use generic slide break processing instruction to
  #   split content into slides

  slide_counter = 0

  slides       = []
  slide_buf = ""
   
  content.each_line do |line|
     if line.include?( '<!-- _S9SLIDE_' )
        if slide_counter > 0   # found start of new slide (and, thus, end of last slide)
          slides   << slide_buf  # add slide to slide stack
          slide_buf = ""         # reset slide source buffer
        else  # slide_counter == 0
          # check for first slide with missing leading SLIDE directive (possible/allowed in takahashi, for example)
          ##  remove html comments and whitspaces (still any content?)
          ### more than just whitespace? assume its  a slide
          if slide_buf.gsub(/<!--.*?-->/m, '').gsub( /[\n\r\t ]/, '').length > 0
            logger.debug "add slide with missing leading slide directive >#{slide_buf}< with slide_counter == 0"
            slides    << slide_buf
            slide_buf = ""
          else
            logger.debug "skipping slide_buf >#{slide_buf}< with slide_counter == 0"
          end
        end
        slide_counter += 1
     end
     slide_buf  << line
  end

  if slide_counter > 0
    slides   << slide_buf     # add slide to slide stack
    slide_buf = ""            # reset slide source buffer 
  end


  slides2 = []
  slides.each do |source|
    slides2 << Slide.new( source, config )
  end


  puts "#{slides2.size} slides found:"
  
  slides2.each_with_index do |slide,i|
    print "  [#{i+1}] "
    if slide.header.present?
      print slide.header
    else
      # remove html comments
      print "-- no header -- | #{slide.content.gsub(/<!--.*?-->/m, '').gsub(/\n/,'$')[0..40]}"
    end
    puts
  end
 
 
  # make content2 and slide2 available to erb template
  # -- todo: cleanup variable names and use attr_readers for content and slide

  ### fix: use class SlideDeck or Deck?? for slides array?
  
  content2 = ""
  slides2.each do |slide|
    content2 << slide.to_classic_html
  end

  @content  = content2
  @slides   = slides2     # strutured content
end

#text_to_html(content) ⇒ Object

uses configured markup processor (textile,markdown,rest,mediawiki) to generate html



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/slideshow/cli/commands/gen.rb', line 35

def text_to_html( content )
  content = case @markup_type
    when :markdown
      markdown_to_html( content )
    when :textile
      textile_to_html( content )
    when :mediawiki
      mediawiki_to_html( content )
    when :rest
      rest_to_html( content )
  end
  content
end

#wrap_markup(text) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/slideshow/cli/commands/gen.rb', line 81

def wrap_markup( text )
  if markup_type == :textile
    # saveguard with notextile wrapper etc./no further processing needed
    "<notextile>\n#{text}\n</notextile>"
  else
    text
  end
end