Class: Markdown::Gen

Inherits:
Object
  • Object
show all
Includes:
TextUtils::Filter
Defined in:
lib/markdown/cli/gen.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, opts) ⇒ Gen

Returns a new instance of Gen.



10
11
12
13
# File 'lib/markdown/cli/gen.rb', line 10

def initialize( logger, opts )
  @logger = logger
  @opts   = opts
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/markdown/cli/gen.rb', line 7

def logger
  @logger
end

#optsObject (readonly)

Returns the value of attribute opts.



8
9
10
# File 'lib/markdown/cli/gen.rb', line 8

def opts
  @opts
end

Instance Method Details

#create_doc(fn) ⇒ Object



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
# File 'lib/markdown/cli/gen.rb', line 15

def create_doc( fn )
  dirname  = File.dirname(  fn )
  basename = File.basename( fn, '.*' )
  extname  = File.extname(  fn )

  logger.debug "dirname=#{dirname}, basename=#{basename}, extname=#{extname}"

  if opts.output_path == '.'
    # expand output path in current dir
    outpath = File.expand_path( dirname ) 
  else
    # expand output path in user supplied dir and make sure output path exists
    outpath = File.expand_path( opts.output_path ) 
    FileUtils.makedirs( outpath ) unless File.directory? outpath 
  end
  logger.debug "outpath=#{outpath}"
  
  # todo: add a -c option to commandline? to let you set cwd?


  # change working dir to sourcefile dir (that is, dirname); push working folder/dir
  newcwd  = File.expand_path( dirname )
  oldcwd  = File.expand_path( Dir.pwd )

  unless newcwd == oldcwd
    logger.debug "oldcwd=>#{oldcwd}<, newcwd=>#{newcwd}<"
    Dir.chdir( newcwd )
  end  

  inname  = "#{basename}#{extname}"
  outname = "#{basename}.html"
  
  logger.debug "inname=#{inname}, outname=#{outname}"
  
  puts "*** #{inname} (#{dirname}) => #{outname} (#{(opts.output_path == '.') ? dirname : opts.output_path})..."
  
  content = File.read( inname )
  
  # step 1) run (optional) preprocessing text filters
  Markdown.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

  # step 2) convert light-weight markup to hypertext
  content = Markdown.new( content ).to_html


## todo: add Markdown.lib_options inspect/dump to banner
        
  banner =<<EOS
<!-- ======================================================================
  generated by #{Markdown.banner}
            on #{Time.now} with Markdown engine '#{Markdown.lib}'
 ====================================================================== -->
EOS
  
  out = File.new( File.join( outpath, outname ), "w+" )
####      out << banner
  out << content
  out.flush
  out.close

  ## pop/restore working folder/dir
  unless newcwd == oldcwd
    logger.debug "oldcwd=>#{oldcwd}<, newcwd=>#{newcwd}<"
    Dir.chdir( oldcwd )
  end  
        
end