Browse Source

Implement the generator

master
Aaron Mueller 9 years ago
parent
commit
36e9f4162d
  1. 17
      Rakefile
  2. 36
      scripts/importer.rb

17
Rakefile

@ -6,6 +6,21 @@ end
desc "Generate HTML stuff from markdown"
task :generate do
require "kramdown"
puts Kramdown::Document.new("*test*").to_html # .to_latex
require "fileutils"
import_folder = "import_results"
target_folder = "public_html"
#Dir.mkdir(target_folder)
FileUtils.rm_rf(target_folder) if Dir.exists?(target_folder)
Dir.mkdir(target_folder)
FileUtils.cp_r(File.join(import_folder, "images"), target_folder)
Dir.glob("#{import_folder}/*.mkd").each do |file|
content = Kramdown::Document.new(File.new(file, "r").read).to_html
File.open(File.join(target_folder, File.basename(file)[0..-4] + "html"), "w") do |fh|
fh.write(content)
end
end
end

36
scripts/importer.rb

@ -1,24 +1,26 @@
#!/usr/bin/env ruby
require "fileutils"
basepath = $1 || File.expand_path(File.join(File.absolute_path(__FILE__), "../.."))
import_folder = File.join(basepath, "import_results")
# Clean up for a clean start
Dir.unlink(import_folder) if Dir.exists?(import_folder)
FileUtils.rm_rf(import_folder) if Dir.exists?(import_folder)
Dir.mkdir(import_folder)
Dir.mkdir(File.join(import_folder, "images"))
Dir.glob("#{basepath}/git_repo/**/*").each do |file|
relative_path = File.dirname(file[basepath.size+"git_repo".size+2..-1])
# Files in the _att folders need to be stripped down
if relative_path[-4..-1] == "_att"
#Dir.mkdir(File.join(import_folder, relative_path)) -p
end
# Determine the subfolder
f = file["#{basepath}/gitrepo?".size..-1].split("/")
subfolder = f.size > 1 ? f.first : "."
# Regular files are Raki style pages and need to convert to a markdown file
if File.file?(file) and File.extname(file).empty?
content = File.read(file).force_encoding("ISO-8859-1").encode("UTF-8", :replace => nil)
content = File.read(file)
# Headings
content.gsub!(/^!([^!].+)$/, "#\\1")
content.gsub!(/^!!([^!].+)$/, "##\\1")
@ -27,24 +29,30 @@ Dir.glob("#{basepath}/git_repo/**/*").each do |file|
content.gsub!(/^!!!!!([^!].+)$/, "#####\\1")
# Links
content.gsub!(/\[([^|\]]+)\]/, "[\\1][\\1]")
content.gsub!(/\[([^|]+)\|([^\]]+)\]/, "[\\2][\\1]")
content.gsub!(/\[([^|\]]+)\]/, "[\\1](\\1)")
content.gsub!(/\[([^|]+)\|([^\]]+)\]/, "[\\2](\\1)")
# Images
content.gsub!(/\\img .* ([^ \\]+) ?\\/, "![Embedded image][\\1]")
content.gsub!(/\\img .* ([^ \\]+) ?\\/, "![Embedded image](images/\\1)")
# Other Plugins
content.gsub!(/\\red (.+) ?\\/, "**\\1**")
content.gsub!(/\\youtube (.*) ?\\/, "[Youtube link][\\1]")
content.gsub!(/\\osm ([^ ]+) ([^ ]+) ?\\/, "[Open Street Map link][http://www.openstreetmap.org/?lat=\\1&lon=\\2&zoom=18&layers=M]")
content.gsub!(/\\youtube (.*) ?\\/, "[Youtube link](\\1)")
content.gsub!(/\\osm ([^ ]+) ([^ ]+) ?\\/, "[Open Street Map link](http://www.openstreetmap.org/?lat=\\1&lon=\\2&zoom=18&layers=M)")
# Delete the useless stuff
content.gsub!(/\\index.+\\/, "")
content.gsub!(/\\recentchanges.+\\/, "")
file = File.new(File.join("import_results", File.basename(file+".mkd")), "w")
file.write(content)
file.close
File.write
# files
elsif File.file?(file) and File.extname(file) =~ /(jpg|jpeg|png)/
new_file = File.join(import_folder, "images", File.basename(file))
p file if File.exists?(new_file)
FileUtils.cp(file, new_file)
end
end