class Plantuml < Nanoc::Filter
identifier :plantuml
type :binary
def run(content, params={})
system("java -Xmx128m -jar lib/plantuml.jar -pipe < #{filename} > #{output_filename}")
end
end
Anything I'm missing here ? Does someone have a better idea ?
(2) Create items programatically (untested):
preprocess do
@items.select{|item| item[:extension] == 'pu'}.each do |pu|
file = ... # similar to above + create a temp file myself
png = Nanoc::Item.new(file, {}, "/plantuml#{item.identifier}", { :mtime => item.mtime, :binary => true })
@items << png
end
end
route '*' do
if item[:extension] != 'pu'
# ...
else
# do nothing, don't output anywhere, might have to return nil here, idk.
end
end
class ERB_binary < Nanoc::Filter requires 'erb' identifier :erbbinary type :binary
def run(f, params={}) assigns.merge!(params[:locals] || {}) context = ::Nanoc::Context.new(assigns) proc = assigns[:content] ? -> { assigns[:content] } : nil assigns_binding = context.get_binding(&proc)
content = IO.read(f)
# Get result safe_level = params[:safe_level] trim_mode = params[:trim_mode] erb = ::ERB.new(content, safe_level, trim_mode) erb.filename = filename result = erb.result(assigns_binding)
File.open(output_filename, "w") {|o| o.write(result)} endend> Problem is... the input is text and the output is binary and Nanoc assumes
> that the output "type" is the same as the input "type".
Declare the filter as text-to-binary:
type :text => :binary
class Plantuml < Nanoc::Filter
identifier :plantuml type :text => :binaryAnd an even better version below. It works great, except that Nanoc doesn't recompile when the PlantUML source file is modified.
class Plantuml < Nanoc::Filter identifier :plantuml type :text => :binary requires 'open3'
def run(content, params={}) content = "@startuml\n" + content unless content =~ %r{^\s*@startuml\b}x content += "\n@enduml" unless content =~ %r{\b@enduml$}x o, e, s = Open3.capture3( 'java', '-Djava.awt.headless=true', '-Xmx128m', '-jar', 'lib/plantuml.jar', '-pipe', :stdin_data => content, :binmode => true ) unless s.success? fail "error running plantuml: #{e}\n#{o}" end File.open(output_filename, "w") do |out| out.write(o) end endendcompile '*' do unless item.binary? filter :erb case item[:extension] when 'markdown' filter :kramdown layout 'default' filter :relativize_paths, :type => :html when 'html' layout 'default' filter :relativize_paths, :type => :html when 'pu' filter :plantuml end endend
route '*' do if item.binary? item.identifier.chop + (item[:extension] ? '.' + item[:extension] : '') elsif item.identifier == '/' '/index.html' else case item[:extension] when 'markdown', 'html' item.identifier.chop + '.html' when 'pu' item.identifier.chop + '.png' else item.identifier.chop + '.' + item[:extension] end endend
layout '*', :erb