Skip to content

Instantly share code, notes, and snippets.

@jamtur01
Created March 1, 2014 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamtur01/9286822 to your computer and use it in GitHub Desktop.
Save jamtur01/9286822 to your computer and use it in GitHub Desktop.
require 'yaml'
require 'pp'
def run(command)
puts " + #{command}" if ENV['verbose']
if ENV['verbose']
puts " - #{system command}"
else
system command
end
end
Dir.glob("**/*.md~") do |f|
File.delete(f)
end
Dir.glob("**/*.md") do |f|
File.delete(f)
end
Dir.glob("**/*.rst") do |file|
name = file.chomp(File.extname(file) )
h = IO.readlines(file)[0..2]
header = h.join("")
start = "---\n"
ends = "---\n"
if header =~ /\:(\w+)\:\s(.*)/m
header.gsub!(/\:(\w+)\:/, '\1:')
output = <<-END
---
layout: default
#{header.chomp}
---
END
#content = File.read(file)
#content.gsub!(/^\.\.\s\_(\w+)\:$/, '<a name="\1"></a>')
#File.open(file, "w") do |o|
# o.write(content)
#end
run "pandoc -f rst -t html -o #{name}.html #{file}"
run "pandoc -f html -t markdown -o #{name}.md #{name}.html"
tmp = "#{name}.md" + "~"
File.open(tmp, "w") do |out|
out << output
File.open("#{name}.md").each_line do |line|
next if line =~ /^(title|description|keywords)$/
next if line =~ /^\:\s\s\s(.*)$/
out.puts line
end
end
File.delete("#{name}.md")
File.rename(tmp,"#{name}.md")
#File.delete("#{name}.html")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment