批量将HTML转换为标记
我在一个文件夹中有很多html文件。我需要把这些转换成降价,我在那里找到了一些宝石,它们一个接一个地表现得很好。 我的问题是。。。 如何循环遍历文件夹中的每个文件,并在单独的文件夹中运行命令将这些文件转换为md 更新批量将HTML转换为标记,html,ruby,gem,markdown,Html,Ruby,Gem,Markdown,我在一个文件夹中有很多html文件。我需要把这些转换成降价,我在那里找到了一些宝石,它们一个接一个地表现得很好。 我的问题是。。。 如何循环遍历文件夹中的每个文件,并在单独的文件夹中运行命令将这些文件转换为md 更新 #!/usr/bin/ruby root = 'C:/Doc' inDir = File.join(root, '/input') outDir = File.join(root, '/output') extension = nil fileName = nil Dir.
#!/usr/bin/ruby
root = 'C:/Doc'
inDir = File.join(root, '/input')
outDir = File.join(root, '/output')
extension = nil
fileName = nil
Dir.foreach(inDir) do |file|
# Dir.foreach will always show current and parent directories
if file == '.' or item == '..' then
next
end
# makes sure the current iteration is not a sub directory
if not File.directory?(file) then
extension = File.extname(file)
fileName = File.basename(file, extension)
end
# strips off the last string if it contains a period
if fileName[fileName.length - 1] == "." then
fileName = fileName[0..-1]
end
# this is where I got stuck
reverse_markdown File.join(inDir, fileName, '.html') > File.join(outDir, fileName, '.md')
Dir.glob(directory){| f |…}
将循环遍历目录中的所有文件。例如,使用红毯库,您可以执行以下操作:
require 'redcarpet'
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true)
Dir.glob('*.md') do |in_filename|
out_filename = File.join(File.dirname(in_filename), "#{File.basename(in_filename,'.*')}.html")
File.open(in_filename, 'r') do |in_file|
File.open(out_filename, 'w') do |out_file|
out_file.write markdown.render(in_file.read)
end
end
end
这是一个关于迭代和循环的问题。你看过Ruby内置的类
Dir
和File
能为你做些什么吗?最好的答案将考虑您的文件的结构,以及您的gems中可能存在的任何限制(请说出它们的名称)。对你的问题进行第一次尝试,我们就可以开始了!