Module: Dyndoc::Converter

Defined in:
lib/dyndoc-converter.rb

Constant Summary collapse

SOFTWARE =
{}

Class Method Summary collapse

Class Method Details

.asciidoctor(code) ⇒ Object



122
123
124
125
# File 'lib/dyndoc-converter.rb', line 122

def Converter.asciidoctor(code)
  require 'asciidoctor'
  Asciidoctor.convert(code,:attributes => {"icons" => "font"})
end

.convert(input, format, outputFormat, to_protect = nil) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/dyndoc-converter.rb', line 165

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.asciidoctor(code)
      when "md>html"
        ##PandocRuby.new(code, :from => :markdown, :to => :html).convert
        tmp=Dyndoc::Converter.markdown(code)
        Dyndoc.warn :res,[code,tmp]
        tmp
      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.ttm(code,"-e2 -r -y1 -L").gsub(/<mtable[^>]*>/,"<mtable>").gsub("\\ngtr","<mtext>&ngtr;</mtext>").gsub("\\nless","<mtext>&nless;</mtext>").gsub("&#232;","<mtext>&egrave;</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) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/dyndoc-converter.rb', line 149

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_engineObject



145
146
147
# File 'lib/dyndoc-converter.rb', line 145

def Converter.markdown_engine
  (@@markdown_engine ||= :pandoc)
end

.markdown_engine=(engine) ⇒ Object



140
141
142
143
# File 'lib/dyndoc-converter.rb', line 140

def Converter.markdown_engine=(engine)
   @@markdown_engine=engine unless Dyndoc::Converter.markdown_engine == engine

end


27
28
29
30
31
32
33
34
35
36
# File 'lib/dyndoc-converter.rb', line 27

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 = '') ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dyndoc-converter.rb', line 71

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?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dyndoc-converter.rb', line 57

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 = '') ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dyndoc-converter.rb', line 38

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) ⇒ Object



135
136
137
# File 'lib/dyndoc-converter.rb', line 135

def Converter.redcarpet(code)
    Dyndoc::Converter.redcarpet_engine ? @@markdown.render(code) : ""
end

.redcarpet_engineObject



127
128
129
130
131
132
133
# File 'lib/dyndoc-converter.rb', line 127

def Converter.redcarpet_engine
  unless @@markdown
    require 'redcarpet'
    @@markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
  end
  @@markdown
end

.ttm(input, opt = '-e2') ⇒ Object

ttm converter



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/dyndoc-converter.rb', line 100

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