ruby - batch convert HTML to Markdown -
i have whole lot of html files live in 1 folder. need convert these markdown found couple gems out there great 1 one. question is... how can loop though each file in folder , run command convert these md on separate folder.
update
#!/usr/bin/ruby root = 'c:/doc' indir = file.join(root, '/input') outdir = file.join(root, '/output') extension = nil filename = nil dir.foreach(indir) |file| # dir.foreach show current , parent directories if file == '.' or item == '..' next end # makes sure current iteration not sub directory if not file.directory?(file) extension = file.extname(file) filename = file.basename(file, extension) end # strips off last string if contains period if filename[filename.length - 1] == "." filename = filename[0..-1] end # got stuck reverse_markdown file.join(indir, filename, '.html') > file.join(outdir, filename, '.md')
dir.glob(directory) {|f| ... }
loop through files inside directory. example using redcarpet library this:
require 'redcarpet' markdown = redcarpet::markdown.new(redcarpet::render::html, :autolink => true) dir.glob('*.md') |in_filename| out_filename = file.join(file.dirname(in_filename), "#{file.basename(in_filename,'.*')}.html") file.open(in_filename, 'r') |in_file| file.open(out_filename, 'w') |out_file| out_file.write markdown.render(in_file.read) end end end
Comments
Post a Comment