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

#atualiza_imagem_width(node) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pandoc_abnt/figura_filter.rb', line 67

def atualiza_imagem_width(node)
  # {"t"=>"Para", "c"=>[{"t"=>"Image", "c"=>[["id", [], [["largura", "30%"]]], [{"t"=>"Str", "c"=>"Título"}], ["imagem.png", "fig:"]]}]}
  # node["c"][0]["c"][2] << ["width","30%"]
  atributos = node["c"][0]["c"][0][2]
  atributos.each_with_index do |att, index|
    if att[0] == "largura" then
      atributos[index][0] = "width"
      break
    end
  end
  node
end

#convert_to_latex(node) ⇒ Object

Converte node para latex



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pandoc_abnt/figura_filter.rb', line 54

def convert_to_latex(node)
  latex_code = nil
  Open3.popen3("pandoc -f json -t latex --wrap=none") {|stdin, stdout, stderr, wait_thr|
    stdin.write(node.to_json)
    stdin.close  # stdin, stdout and stderr should be closed explicitly in this form.
    latex_code = stdout.read

    pid = wait_thr.pid # pid of the started process.
    exit_status = wait_thr.value # Process::Status object returned.
  }
  latex_code
end

#filtra_json(pandoc_json_tree) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/pandoc_abnt/figura_filter.rb', line 80

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["meta"]
  blocks = tree["blocks"]
  api = tree["pandoc-api-version"]
  
  filtrados = []
  anterior = nil
  
  if not blocks
    raise ArgumentError, "Problema no argumento passado: #{pandoc_json_tree}"
  end
  
  blocks.each do |node|
    
    if (fonte?(node) and imagem?(anterior)) then
      image_node = atualiza_imagem_width(anterior)
      imagem_latex = convert_to_latex({"blocks"=>[image_node], "pandoc-api-version" => api, "meta" => meta})
      fonte_latex = convert_to_latex({"blocks"=>[node], "pandoc-api-version" => api, "meta" => meta})
      texcode = reformata_figura_latex(imagem_latex, fonte_latex)
      raw_tex = {"t"=>"RawBlock","c"=>["latex",texcode]}
      
      filtrados.pop # remote o anterior
      filtrados << raw_tex
    elsif (fonte?(node) and tabela?(anterior)) then
      tabela_latex = convert_to_latex({"blocks"=>[anterior], "pandoc-api-version" => api, "meta" => meta})
      fonte_latex = convert_to_latex({"blocks"=>[node], "pandoc-api-version" => api, "meta" => meta})
      texcode = reformata_tabela_latex(tabela_latex, fonte_latex)
      raw_tex = {"t"=>"RawBlock","c"=>["latex",texcode.strip]}
      
      filtrados.pop # remote o anterior
      filtrados << raw_tex
    else
      filtrados << node
    end
    
    anterior = node
  end
   
  JSON.generate({"blocks"=>filtrados, "pandoc-api-version" => api, "meta" => meta})
  
  
#      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)


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

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)


40
41
42
43
44
# File 'lib/pandoc_abnt/figura_filter.rb', line 40

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



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

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.strip}}
\\end{figure}
LATEX
end

#reformata_tabela_latex(latex_code, fonte) ⇒ Object



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

def reformata_tabela_latex(latex_code, fonte)
  inicio = latex_code.lines[0..-2].join ""
  abntex_code = <<LATEX
#{inicio}\\caption*{#{fonte.strip}}
\\end{longtable}
LATEX
end

#tabela?(node) ⇒ Boolean

Verifica se node é uma tabela

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/pandoc_abnt/figura_filter.rb', line 47

def tabela?(node)
# {"t":"Table","c":[[{"t":"Str","c":"Demonstration"},{"t":"Space"},{"t":"Str","c":"of"},{"t":"Space"},{"t":"Str","c":"simple"},{"t":"Space"},{"t":"Str","c":"table"},{"t":"Space"},{"t":"Str","c":"syntax."},{"t":"Space"},{"t":"RawInline","c":["tex","\\label{mytable}"]}],[{"t":"AlignRight"},{"t":"AlignLeft"},{"t":"AlignCenter"},{"t":"AlignDefault"}],[0,0,0,0],[[{"t":"Plain","c":[{"t":"Str","c":"Right"}]}],[{"t":"Plain","c":[{"t":"Str","c":"Left"}]}],[{"t":"Plain","c":[{"t":"Str","c":"Center"}]}],[{"t":"Plain","c":[{"t":"Str","c":"Default"}]}]],[[[{"t":"Plain","c":[{"t":"Str","c":"12"}]}],[{"t":"Plain","c":[{"t":"Str","c":"12"}]}],[{"t":"Plain","c":[{"t":"Str","c":"12"}]}],[{"t":"Plain","c":[{"t":"Str","c":"12"}]}]],[[{"t":"Plain","c":[{"t":"Str","c":"123"}]}],[{"t":"Plain","c":[{"t":"Str","c":"123"}]}],[{"t":"Plain","c":[{"t":"Str","c":"123"}]}],[{"t":"Plain","c":[{"t":"Str","c":"123"}]}]],[[{"t":"Plain","c":[{"t":"Str","c":"1"}]}],[{"t":"Plain","c":[{"t":"Str","c":"1"}]}],[{"t":"Plain","c":[{"t":"Str","c":"1"}]}],[{"t":"Plain","c":[{"t":"Str","c":"1"}]}]]]]}
  node["t"] == "Table"
end