#!/usr/bin/ruby -w # Copyright (c) 2014, Guido de Melo # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'fileutils' require 'erb' require 'rubygems' require 'rdiscount' require 'kramdown' require 'slim' require 'yaml' require 'pp' if ARGV.length != 2 puts "please specify src dest\n" exit 1 end dest = ARGV.pop src = ARGV.pop if src[-1] != "/" then src = src + "/" end # erzeugt aus einem Markdown-Dokument ein HTML-Dokument und nutzt dazu ein # Slim-Template def md2html(file, dest, template, title) # src/path/file.md kommt rein # dest/path/file.html soll raus path, filename = File.split(file) # path in Array und erstes Element (src) wegwerfen destdir = File.join(dest, path.split('/')[1..-1]) # von file Anhang weg und html hin destfile = File.join(destdir, (filename[0..-4]+".html")) # nur Verzeichnisse anlegen, wenn noch keine da (sonst Fehler) Dir.mkdir(destdir) unless File.exist?(destdir) # und jetzt das Dokument rendern htmlfile = File.new(destfile, 'w') htmlfile.puts template.render(title) htmlfile.close end # main # HTML soll schön formatiert ausgegeben werden Slim::Engine.set_default_options :pretty => true #template = Tilt.new(src + '/template.slim') # Alle templates vorkompilieren templates = {} Dir["templates/*slim"].each do |t| templatename = File.basename(t, '.slim') templatefile = Tilt.new(t) templates[templatename] = templatefile end # alte Verzeichnisse löschen FileUtils.rm_rf(dest) FileUtils.mkdir(dest) # hier nur die Markdown-Dateien. Kann noch ausgebaut werden. # Alle files sammeln und dann nach Typ kopieren oder Methode zum wandeln aufrufen Dir[src + '**/*'].each do |f| puts "processing " + f case File.extname(f) # nach Dateiendung entscheiden was passieren soll # Markdown wird geparst und je nach template umgewandelt when ".md" first3 = File.open(f) { |fd| fd.read(3) } if first3 == "---" # Seite mit Metainformation content = File.read(f) if content =~ /^(---.*?\n.*?)\n---.*?\n(.*)/m data = YAML::load($1) content = $2 # template für Layout bestimmen if data['layout'] != nil template = templates[ data['layout'] ] # Seiteninhalt mit RDiscount rendern data[:content] = RDiscount.new(content).to_html # Seite selbst rendern md2html(f, dest, template, data) end end end # alle anderen Dateitypen werden kopiert else if File.file?(f) # src/path/file.md kommt rein # dest/path/file.html soll raus path, filename = File.split(f) # path in Array und erstes Element (src) wegwerfen destdir = File.join(dest, path.split('/')[1..-1]) # von file Anhang weg und html hin destfile = File.join(destdir, filename) FileUtils.cp(f, destfile) # wenn es ein Verzeichnis ist, muss es angelegt werden elsif File.directory?(f) # im Pfad src durch html ersetzen path = f.split('/')[1..-1] path = File.join(dest, path) puts "creating " + path FileUtils.mkdir_p path end end end exit 0