Module: Dyndoc::Converter

Defined in:
lib/dyndoc-converter.rb

Constant Summary collapse

SOFTWARE =
{}

Class Method Summary collapse

Class Method Details

.asciidoctor(code) ⇒ Object



118
119
120
121
# File 'lib/dyndoc-converter.rb', line 118

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

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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/dyndoc-converter.rb', line 123

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
        Dyndoc::Converter.pandoc(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.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
    #puts "res";p res
  end
  return (to_protect ? "__PROTECTED__FORMAT__"+res+"__PROTECTED__FORMAT__": res)
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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dyndoc-converter.rb', line 57

def Converter.pandoc(input,opt='')
  output = ''
  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
  if SOFTWARE[:pandoc]
    ##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

.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

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

ttm converter



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dyndoc-converter.rb', line 96

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