Class: Markdown::Gen

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGen

Returns a new instance of Gen.



22
23
24
25
26
# File 'lib/markdown/gen.rb', line 22

def initialize
  @logger       = Logger.new(STDOUT)
  @logger.level = Logger::INFO
  @opts         = Opts.new
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



19
20
21
# File 'lib/markdown/gen.rb', line 19

def logger
  @logger
end

#optsObject (readonly)

Returns the value of attribute opts.



20
21
22
# File 'lib/markdown/gen.rb', line 20

def opts
  @opts
end

Instance Method Details

#create_doc(fn) ⇒ Object



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
85
86
87
88
89
# File 'lib/markdown/gen.rb', line 28

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 )
  content = Markdown.new( content ).to_html  # convert light-weight markup to hypertext


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

#find_file_with_markdown_extension(fn) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/markdown/gen.rb', line 103

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

  Markdown.extnames.each do |e|
    newname = File.join( dirname, "#{basename}#{e}" ) 
    logger.debug "File.exists? #{newname}"
    return newname if File.exists?( newname )
  end  # each extension (e)
  
  nil   # not found; return nil
end

#find_files(file_or_dir_or_pattern) ⇒ Object



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

def find_files( file_or_dir_or_pattern )

  filtered_files = []
  
  # assume pattern if includes * or ? or {} or []
  if file_or_dir_or_pattern =~ /[*?{}\[\]]/
    puts "searching glob pattern '#{file_or_dir_or_pattern}'..."
    Dir.glob( file_or_dir_or_pattern ).each do |file|
      if File.directory?( file )  # skip (sub)directories
        puts "  skipping folder '#{file}'..."
        next
      else
        if has_markdown_extension?( file )
          logger.debug "  adding file '#{file}'..."
          filtered_files << file
        else
          puts "  skipping file   '#{file}'..."
        end
      end
    end
  elsif File.directory?(file_or_dir_or_pattern)
    puts "searching folder '#{file_or_dir_or_pattern}'..."
    Dir.entries( file_or_dir_or_pattern ).each do |entry|
      next if entry == '.' || entry == '..' # silently skip current and up dirs
      
      if file_or_dir_or_pattern == '.'
        file = entry
      else  # add dir (if not working dir)
        file = File.join( file_or_dir_or_pattern, entry )  
      end
       
      if File.directory?( file )  # skip (sub)directories
        puts "  skipping folder '#{file}'..."
        next
      else
        if has_markdown_extension?( file )
          logger.debug "  adding file '#{file}'..."
          filtered_files << file
        else
          puts "  skipping file   '#{file}'..."
        end
      end
    end
  else  # assume it's a single file (check for missing extension)
    if File.exists?( file_or_dir_or_pattern )
      file = file_or_dir_or_pattern
      if has_markdown_extension?( file )
        logger.debug "  adding file '#{file}'..."
        filtered_files << file
      else
        puts "  skipping file   '#{file}'..."
      end
    else  # check for existing file w/ missing extension
      file = find_file_with_markdown_extension( file_or_dir_or_pattern )
      if file.nil?
        puts "  skipping missing file '#{file_or_dir_or_pattern}{#{Markdown.extnames.join(',')}}'..."
      else
        logger.debug "  adding file '#{file}'..."
        filtered_files << file
      end
    end
  end

  filtered_files
end

#has_markdown_extension?(fn) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
101
# File 'lib/markdown/gen.rb', line 92

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

  return false if extname.empty?   # no extension
  
  Markdown.extnames.include?( extname.downcase )
end

#run(args) ⇒ Object

find_files



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

def run( args )
  opt=OptionParser.new do |cmd|

    cmd.banner = "Usage: markdown [options] files_or_dirs"

    cmd.on( '-o', '--output PATH', 'Output Path' ) { |path| opts.output_path = path }

    # todo: find different letter for debug trace switch (use v for version?)
    cmd.on( "-v", "--verbose", "Show debug trace" )  do
       logger.datetime_format = "%H:%H:%S"
       logger.level = Logger::DEBUG
    end
 
 
    usage =<<EOS

markdown - Lets you convert plain text documents (#{Markdown.extnames.join(', ')}) to hypertext (.html) with your Markdown engine of choice (#{Markdown.lib}).

#{cmd.help}

Examples:
  markdown                   # Process all documents in working folder (that is, .)
  markdown quickref          # Process document or folder using Markdown
  markdown quickref.text     # Process document using Markdown
  markdown -o site quickref  # Output documents to site folder

Further information:
  http://geraldb.github.com/markdown
  
EOS

    cmd.on_tail( "-h", "--help", "Show this message" ) do
       puts usage
       exit
    end
  end

  opt.parse!( args )
  
  puts Markdown.banner
  
  # force loading of config
  Markdown.lib
  
  logger.debug "args.length: #{args.length}"
  logger.debug "args: >#{args.join(',')}<"
  
  # if no file args given; default to working folder (that is, .)
  args = ['.'] if args.length == 0

  args.each do |arg|
    files = find_files( arg )
    files.each do |file|
      create_doc( file )
    end
  end
  
  puts "Done."
  
end