Class: PandocAbnt::FiguraFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/pandoc_abnt/figura_filter.rb

Overview

Essa classe é responsável por

See Also:

  • Trabalho#configuracao

Instance Method Summary collapse

Instance Method Details

#convert_to_latex(node) ⇒ Object



39
40
# File 'lib/pandoc_abnt/figura_filter.rb', line 39

def convert_to_latex(node)
end

#filtra_json(pandoc_json_tree) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pandoc_abnt/figura_filter.rb', line 42

def filtra_json(pandoc_json_tree)
  # Exemplo de código:
  # [{"unMeta":{}},[{"t":"Para","c":[{"t":"Image","c":[["id",[],[["width","30%"]]],[{"t":"Str","c":"Título"}],["imagem.png","fig:"]]}]},{"t":"Para","c":[{"t":"Str","c":"Fonte:"},{"t":"Space","c":[]},{"t":"Str","c":"Autor."}]}]]
  tree = JSON.parse(pandoc_json_tree)
  meta = tree[0]
  
  filtrados = []
  anterior = nil
  tree[1].each do |node|
    
    if (fonte?(node) and imagem?(anterior)) then
      imagem_latex = convert_to_latex(anterior)
      fonte_latex = convert_to_latex(node)
      texcode = <<TEX
\\begin{figure}[htbp]
\\caption{Legenda da figura}\\label{id}
\\begin{center}
\\includegraphics[width=0.30000\\textwidth]{imagem.png}
\\end{center}
\\legend{Fonte: Autor}
\\end{figure}
TEX
      raw_tex = {"t"=>"RawBlock","c"=>["latex",texcode]}
      
      filtrados.pop # remote o anterior
      filtrados << raw_tex
    else
      filtrados << node
    end
    
    anterior = node
  end
   
  JSON.generate([meta,filtrados])
  
  
#      result = <<-LATEX [{"unMeta":{}},[{"t":"RawBlock","c":["latex","\\begin{figure}[htbp]\n\\caption{Legenda da figura}\\label{id}\n\\begin{center}\n\\includegraphics[width=0.30000\\textwidth]{imagem.png}\n\\end{center}\n\\legend{Fonte: Autor.}\n\\end{figure}"]}]]

end

#fonte?(node) ⇒ Boolean

Verifica se node é um parágrafo que inicia com “Fonte:”

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/pandoc_abnt/figura_filter.rb', line 27

def fonte?(node)
# {"t":"Para","c":[{"t":"Str","c":"Fonte:"},{"t":"Space","c":[]},{"t":"Str","c":"Autor."}]}
  node["t"] == "Para" and node["c"][0]["c"] == "Fonte:"
end

#imagem?(node) ⇒ Boolean

Verifica se node contém apenas uma imagem

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/pandoc_abnt/figura_filter.rb', line 33

def imagem?(node)
# {"t":"Para","c":[{"t":"Image","c":[["id",[],[["width","30%"]]],[{"t":"Str","c":"Título"}],["imagem.png","fig:"]]}]}

  node["t"] == "Para" and node["c"][0]["t"] == "Image"
end

#reformata_figura_latex(latex_code, fonte) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pandoc_abnt/figura_filter.rb', line 9

def reformata_figura_latex(latex_code, fonte)

image_regex = /\\includegraphics.*/
caption_regex = /\\caption.*/


figura_abntex = <<LATEX
\\begin{figure}[htbp]
#{latex_code.match caption_regex}
\\begin{center}
#{latex_code.match image_regex}
\\end{center}
\\legend{#{fonte}}
\\end{figure}
LATEX
end