Class: Limarka::Conversor

Inherits:
Object
  • Object
show all
Defined in:
lib/limarka/conversor.rb

Constant Summary collapse

PRETEXTUAL =
"templates/pretextual.tex"
POSTEXTUAL =
"templates/postextual.tex"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trabalho, options) ⇒ Conversor

Returns a new instance of Conversor.



23
24
25
26
27
# File 'lib/limarka/conversor.rb', line 23

def initialize(trabalho, options)
  self.t = trabalho
  self.options = options
  self.usa_pdftotext = true
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



16
17
18
# File 'lib/limarka/conversor.rb', line 16

def options
  @options
end

#postextual_texObject

Returns the value of attribute postextual_tex.



18
19
20
# File 'lib/limarka/conversor.rb', line 18

def postextual_tex
  @postextual_tex
end

#pretextual_texObject

Returns the value of attribute pretextual_tex.



17
18
19
# File 'lib/limarka/conversor.rb', line 17

def pretextual_tex
  @pretextual_tex
end

#tObject

trabalho



15
16
17
# File 'lib/limarka/conversor.rb', line 15

def t
  @t
end

#texto_texObject

Returns the value of attribute texto_tex.



19
20
21
# File 'lib/limarka/conversor.rb', line 19

def texto_tex
  @texto_tex
end

#txtObject

Returns the value of attribute txt.



20
21
22
# File 'lib/limarka/conversor.rb', line 20

def txt
  @txt
end

#usa_pdftotextObject

Returns the value of attribute usa_pdftotext.



21
22
23
# File 'lib/limarka/conversor.rb', line 21

def usa_pdftotext
  @usa_pdftotext
end

Class Method Details

.tex_file(configuracao) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/limarka/conversor.rb', line 215

def self.tex_file(configuracao)
  if (configuracao['graduacao'] and configuracao['projeto']) then
    'xxx-Monografia-projeto.tex'
  elsif (configuracao['graduacao'] and not configuracao['projeto']) then
    'xxx-Monografia.tex'
  elsif (configuracao['especializacao'] and configuracao['projeto']) then
    'xxx-TFC-projeto.tex'
  elsif (configuracao['especializacao'] and not configuracao['projeto']) then
    'xxx-TFC.tex'
  elsif (configuracao['mestrado'] and configuracao['projeto']) then
    'xxx-Dissertacao-projeto.tex'
  elsif (configuracao['mestrado'] and not configuracao['projeto']) then
    'xxx-Dissertacao.tex'
  elsif (configuracao['doutorado'] and configuracao['projeto']) then
    'xxx-Tese-projeto.tex'
  elsif (configuracao['doutorado'] and not configuracao['projeto']) then
    'xxx-Tese.tex'
  else
    # valor padrão, caso não configurado.
    'xxx-Monografia-projeto.tex'
  end

end

Instance Method Details

#compilaObject

Compila tex_file no diretorio atual, retorna o conteudo somente texto do PDF



54
55
56
57
58
59
60
61
62
63
# File 'lib/limarka/conversor.rb', line 54

def compila
  Dir.chdir(options[:output_dir]) do
    basename = File.basename(texto_tex_file, '.tex')
    system "latexmk --quiet --xelatex -f #{basename}",  :out=>File::NULL, :err=>File::NULL
    if (usa_pdftotext) then
      system "pdftotext -enc UTF-8 #{basename}.pdf"
      File.open("#{basename}.txt", 'r') {|f| @txt = f.read}
    end
  end
end

#convertObject

Cria o arquivo



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/limarka/conversor.rb', line 31

def convert()
  FileUtils.mkdir_p(options[:output_dir])

  # A invocação de pandoc passando parâmetro como --before-body necessita
  # de ser realizado através de arquivos, portanto, serão criados arquivos
  # temporários para sua execução
  pretextual_tempfile = Tempfile.new('pretextual')
  postextual_tempfile = Tempfile.new('postextual')
  begin
    pretextual(pretextual_tempfile)
    postextual(postextual_tempfile)
    textual(pretextual_tempfile,postextual_tempfile)
    
    ensure
      pretextual_tempfile.close
      pretextual_tempfile.unlink
      postextual_tempfile.close
      postextual_tempfile.unlink
  end
end

#cria_xxx_referenciasObject

arquivo temporário de referencias



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/limarka/conversor.rb', line 125

def cria_xxx_referencias
  referencias_tempfile = Tempfile.new('referencias')
  File.open(referencias_tempfile, 'w') {|file| file.write(t.referencias)}
  b = BibTeX.open(referencias_tempfile.path)
  b.each do |entry|
    if entry.title.include?(':') then
      s = entry.title.split(':')
      entry['title'] = s[0].strip
      entry['subtitle'] = s[1].strip
    end
  end
  
  b.save_to referencias_bib_file
end

#hash_to_yaml(h) ⇒ Object



65
66
67
68
69
70
# File 'lib/limarka/conversor.rb', line 65

def hash_to_yaml(h)
  s = StringIO.new
  s << h.to_yaml
  s << "---\n\n"
  s.string
end

#pdf_fileObject



203
204
205
# File 'lib/limarka/conversor.rb', line 203

def pdf_file
  texto_tex_file.sub('.tex','.pdf')
end

#postextual(tempfile) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/limarka/conversor.rb', line 103

def postextual(tempfile)
  # Referências (obrigatório)
  # Glossário (opcional)
  # Apêndice (opcional)
  # Anexo (opcional)
  # Índice (opcional)

  s = StringIO.new

  s << secao_referencias
  s << secao_glossario
  s << secao_apendices
  s << secao_anexos
  s << secao_indice

  cria_xxx_referencias
  
  @postextual_tex = s.string
  File.open(tempfile, 'w') { |file| file.write(postextual_tex) }
end

#postextual_tex_fileObject



196
197
198
# File 'lib/limarka/conversor.rb', line 196

def postextual_tex_file
  "#{options[:output_dir]}/xxx-postextual.tex"
end

#pretextual(tempfile) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/limarka/conversor.rb', line 76

def pretextual(tempfile)
  s = StringIO.new
  necessita_de_arquivo_de_texto = ["errata"]
  ["folha_de_rosto", "errata", "folha_de_aprovacao", "dedicatoria", "agradecimentos", 
  "epigrafe", "resumo", "abstract", "lista_ilustracoes", "lista_tabelas", 
  "lista_siglas", "lista_simbolos", "sumario"].each_with_index do |secao,indice|
    template = "pretextual#{indice+1}-#{secao}"
    Open3.popen3("pandoc -f markdown --data-dir=#{options[:templates_dir]} --template=#{template} -t latex") {|stdin, stdout, stderr, wait_thr|
      stdin.write(hash_to_yaml(t.configuracao))
      stdin.write("\n")
      if t.errata? and necessita_de_arquivo_de_texto.include?(secao) then
        arquivo_de_entrada = "#{secao}.md"
        conteudo = File.read(arquivo_de_entrada)
        stdin.write(conteudo)
      end
      stdin.close
      s << stdout.read
      exit_status = wait_thr.value # Process::Status object returned.
      if(exit_status!=0) then puts ("Erro: " + stderr.read).red end
    }
  end
  @pretextual_tex = s.string
  File.open(tempfile, 'w') { |file| file.write(pretextual_tex) }
#      puts "#{PRETEXTUAL} criado".green
end

#pretextual_tex_fileObject



193
194
195
# File 'lib/limarka/conversor.rb', line 193

def pretextual_tex_file
  "#{options[:output_dir]}/xxx-pretextual.tex"
end

#referencias_bib_fileObject



207
208
209
# File 'lib/limarka/conversor.rb', line 207

def referencias_bib_file
  "#{options[:output_dir]}/xxx-referencias.bib"
end

#secao(template, condicao_para_conteudo, conteudo_externo) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/limarka/conversor.rb', line 140

def secao(template, condicao_para_conteudo, conteudo_externo)
  s = StringIO.new
  
  Open3.popen3("pandoc -f markdown --data-dir=#{options[:templates_dir]} --template=#{template} --chapter -t latex") {|stdin, stdout, stderr, wait_thr|
    stdin.write(hash_to_yaml(t.configuracao))
    stdin.write("\n")
    if (condicao_para_conteudo) then
      stdin.write(conteudo_externo)
      stdin.write("\n")
    end
    stdin.close
    s << stdout.read
    exit_status = wait_thr.value # Process::Status object returned.
    if(exit_status!=0) then puts ("Erro: " + stderr.read).red end
  }
  s.string
end

#secao_anexosObject



167
168
169
# File 'lib/limarka/conversor.rb', line 167

def secao_anexos
  secao("postextual4-anexos", t.anexos?, t.anexos)
end

#secao_apendicesObject



163
164
165
# File 'lib/limarka/conversor.rb', line 163

def secao_apendices
  secao("postextual3-apendices", t.apendices?, t.apendices)
end

#secao_glossarioObject



171
172
# File 'lib/limarka/conversor.rb', line 171

def secao_glossario
end

#secao_indiceObject



174
175
# File 'lib/limarka/conversor.rb', line 174

def secao_indice
end

#secao_referenciasObject



159
160
161
# File 'lib/limarka/conversor.rb', line 159

def secao_referencias
  secao("postextual1-referencias", false, t.referencias)
end

#texto_tex_fileObject



200
201
202
# File 'lib/limarka/conversor.rb', line 200

def texto_tex_file
  "#{options[:output_dir]}/#{Conversor.tex_file(t.configuracao)}"
end

#textual(pretextual_tempfile, postextual_tempfile) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/limarka/conversor.rb', line 177

def textual(pretextual_tempfile, postextual_tempfile)
  valida_yaml
  Open3.popen3("pandoc -f markdown+raw_tex -t latex -s --data-dir=#{options[:templates_dir]} --template=trabalho-academico --normalize --chapter --include-before-body=#{pretextual_tempfile.path}  --include-after-body=#{postextual_tempfile.path}") {|stdin, stdout, stderr, wait_thr|
    stdin.write(File.read(options[:templates_dir] + '/templates/configuracao-tecnica.yaml'))
    stdin.write("\n")
    stdin.write(hash_to_yaml(t.configuracao))
    stdin.write("\n")
    stdin.write(t.texto)
    stdin.close
    @texto_tex = stdout.read
    exit_status = wait_thr.value # Process::Status object returned.
    if(exit_status!=0) then puts ("Erro: " + stderr.read).red end
  }
  File.open(texto_tex_file, 'w')  { |f| f.write(@texto_tex)}
end

#valida_yamlObject



211
212
213
# File 'lib/limarka/conversor.rb', line 211

def valida_yaml
  # não faz nada por enquanto
end