module Dyndoc::Converter
Constants
- SOFTWARE
Public Class Methods
asciidoctor(code)
click to toggle source
# File lib/dyndoc-converter.rb, line 136 def Converter.asciidoctor(code) require 'asciidoctor' Asciidoctor.convert(code,:attributes => {"icons" => "font"}) end
convert(input,format,outputFormat,to_protect=nil)
click to toggle source
# File lib/dyndoc-converter.rb, line 181 def Converter.convert(input,format,outputFormat,to_protect=nil) ## format=format.to_s unless format.is_a? String ## Dyndoc.warn "convert input",[input,format] outputFormat=outputFormat.to_s unless outputFormat.is_a? String res="" input.split("__PROTECTED__FORMAT__").each_with_index do |code,i| ##Dyndoc.warn "code",[i,code,format,outputFormat] if i%2==0 res << case format+outputFormat when "adoc>html" Dyndoc::Converter.tag!(:adoc) Dyndoc::Converter.asciidoctor(code) when "md>html" ## DEBUG MODE ##tmp=Dyndoc::Converter.markdown(code) ##Dyndoc.warn :res,[code,tmp] ##tmp ## Normal Mode Dyndoc::Converter.tag!(:md) Dyndoc::Converter.markdown(code) when "md>tex" #puts "latex documentclass";p Dyndoc::Utils.dyndoc_globvar("_DOCUMENTCLASS_") if Dyndoc::Utils.dyndoc_globvar("_DOCUMENTCLASS_")=="beamer" Dyndoc::Converter.pandoc(code,"-t beamer") else Dyndoc::Converter.pandoc(code,"-t latex") end when "md>odt" ##PandocRuby.new(code, :from => :markdown, :to => :opendocument).convert Dyndoc::Converter.pandoc(code,"-t opendocument") when "txtl>html" # (rc=RedCloth.new(code)) # rc.hard_breaks=false # rc.to_html Dyndoc::Converter.pandoc(code,"-f textile -t html") when "txtl>tex" # RedCloth.new(code).to_latex Dyndoc::Converter.pandoc(code,"-f textile -t latex") when "ttm>html" Dyndoc::Converter.tag!(:ttm) Dyndoc::Converter.ttm(code,"-e2 -r -y1 -L").gsub(/<mtable[^>]*>/,"<mtable>").gsub("\\ngtr","<mtext>≯</mtext>").gsub("\\nless","<mtext>≮</mtext>").gsub("è","<mtext>è</mtext>") when "tex>odt" puts "tex => odt" tmp="<text:p><draw:frame draw:name=\""+`uuidgen`.strip+"\" draw:style-name=\"mml-inline\" text:anchor-type=\"as-char\" draw:z-index=\"0\" ><draw:object>"+Dyndoc::Converter.pandoc(code,"--mathml -f latex -t html").gsub(/<\/?p>/,"").gsub(/<(\/?)([^\<]*)>/) {|e| "<"+($1 ? $1 : "")+"math:"+$2+">"}+"</draw:object></draw:frame></text:p>" ##p tmp tmp when "tex>html" ##PandocRuby.new(code, :from => :markdown, :to => :html).convert Dyndoc::Converter.pandoc(code,"--mathjax -f latex -t html") when "ttm>tex", "html>html",'tex>tex' code else ## the rest returns nothing! Dyndoc.warn "Warning: unknown conversion!" "" end else res << code end #Dyndoc.warn "res",res end return (to_protect ? "__PROTECTED__FORMAT__"+res+"__PROTECTED__FORMAT__": res) end
markdown(code)
click to toggle source
# File lib/dyndoc-converter.rb, line 165 def Converter.markdown(code) if Dyndoc::Converter.markdown_engine == :pandoc if Dyndoc::Converter.pandoc_available? return Dyndoc::Converter.pandoc(code) else Converter.markdown_engine=:redcarpet end end if Dyndoc::Converter.markdown_engine == :redcarpet Converter.redcarpet(code) else ## default output if no available engine found "" end end
markdown_engine()
click to toggle source
# File lib/dyndoc-converter.rb, line 161 def Converter.markdown_engine (@@markdown_engine ||= :pandoc) end
markdown_engine=(engine)
click to toggle source
# File lib/dyndoc-converter.rb, line 156 def Converter.markdown_engine=(engine) @@markdown_engine=engine unless Dyndoc::Converter.markdown_engine == engine end
mathlink(input)
click to toggle source
# File lib/dyndoc-converter.rb, line 41 def Converter.mathlink(input) unless SOFTWARE[:mathlink] cmd=`type "math"` unless cmd.empty? require 'mathematica' SOFTWARE[:mathlink]=Mathematica::Mathematica.new.start #cmd.strip.split(" ")[2] unless cmd.empty? end end SOFTWARE[:mathlink] ? SOFTWARE[:mathlink].eval_foreground(input) : "" end
pandoc(input,opt='')
click to toggle source
# File lib/dyndoc-converter.rb, line 85 def Converter.pandoc(input,opt='') output = '' if Converter.pandoc_available? ##DEBUG: p [:pandoc_soft, SOFTWARE[:pandoc]+" #{opt}"] if input ##DEBUG: p [:pandoc_iput,input] ##DEBUG: p [:pandoc_options, opt] Open3::popen3(SOFTWARE[:pandoc]+" #{opt}") do |stdin, stdout, stderr| stdin.puts input stdin.close output = stdout.read.strip end ##DEBUG: p [:pandoc_output,output] output else ##DEBUG: p SOFTWARE[:pandoc]+" #{opt}" system(SOFTWARE[:pandoc]+" #{opt}") end else if $dyn_logger $dyn_logger.write("ERROR pandoc: software not installed!\n") else Dyndoc.warn "ERROR pandoc: software not installed!\n" end "" end end
pandoc_available?()
click to toggle source
# File lib/dyndoc-converter.rb, line 71 def Converter.pandoc_available? unless SOFTWARE[:pandoc] if File.exist? File.join(ENV["HOME"],".cabal","bin","pandoc") SOFTWARE[:pandoc]=File.join(ENV["HOME"],".cabal","bin","pandoc") else cmd = Dyndoc.which_path("pandoc") SOFTWARE[:pandoc]=cmd unless cmd.empty? #cmd=`type "pandoc"` #SOFTWARE[:pandoc]=cmd.strip.split(" ")[2] unless cmd.empty? end end SOFTWARE[:pandoc] end
pdflatex(input,opt='')
click to toggle source
# File lib/dyndoc-converter.rb, line 52 def Converter.pdflatex(input,opt='') output = '' unless SOFTWARE[:pdflatex] cmd=`type "pdflatex"` SOFTWARE[:pdflatex]=cmd.strip.split(" ")[2] unless cmd.empty? end if SOFTWARE[:pdflatex] Open3.popen3("#{SOFTWARE[:pdflatex]} #{opt}") {|stdin,stdout,stderr| stdin.print input stdin.close output=stdout.read } return nil else $dyn_logger.write("ERROR pdflatex: software not installed!\n") return nil end end
redcarpet(code)
click to toggle source
# File lib/dyndoc-converter.rb, line 151 def Converter.redcarpet(code) Dyndoc::Converter.redcarpet_engine ? @@markdown.render(code) : "" end
redcarpet_engine()
click to toggle source
# File lib/dyndoc-converter.rb, line 143 def Converter.redcarpet_engine unless @@markdown require 'redcarpet' @@markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML) end @@markdown end
tag!(tag)
click to toggle source
# File lib/dyndoc-converter.rb, line 37 def Converter.tag!(tag) @@tags=(@@tags+[tag]).uniq end
ttm(input,opt='-e2')
click to toggle source
ttm converter
# File lib/dyndoc-converter.rb, line 114 def Converter.ttm(input,opt='-e2') #puts "ttm:begin" output=nil unless SOFTWARE[:ttm] cmd=`type "ttm"` SOFTWARE[:ttm]=cmd.strip.split(" ")[2] unless cmd.empty? end if SOFTWARE[:ttm] Open3.popen3("#{SOFTWARE[:ttm]} #{opt}") {|stdin,stdout,stderr| stdin.print input stdin.close output=stdout.read #puts "ttm:wait" } #puts "ttm:end" output.gsub("__VERBATIM__","verbatim").sub(/\A\n*/,"") #the last is because ttm adds 6 \n for nothing! else $dyn_logger.write("ERROR ttm: software not installed!\n") "" end end