Class: PandocAbnt::FiguraFilter
- Inherits:
-
Object
- Object
- PandocAbnt::FiguraFilter
- Defined in:
- lib/pandoc_abnt/figura_filter.rb
Overview
Essa classe é responsável por
Instance Method Summary collapse
-
#convert_to_latex(node) ⇒ Object
Converte node para latex.
- #filtra_json(pandoc_json_tree) ⇒ Object
-
#fonte?(node) ⇒ Boolean
Verifica se node é um parágrafo que inicia com “Fonte:”.
-
#imagem?(node) ⇒ Boolean
Verifica se node contém apenas uma imagem.
- #reformata_figura_latex(latex_code, fonte) ⇒ Object
- #reformata_tabela_latex(latex_code, fonte) ⇒ Object
-
#tabela?(node) ⇒ Boolean
Verifica se node é uma tabela.
Instance Method Details
#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
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/pandoc_abnt/figura_filter.rb', line 67 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) = 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 imagem_latex = convert_to_latex({"blocks"=>[anterior], "pandoc-api-version" => api, "meta" => }) fonte_latex = convert_to_latex({"blocks"=>[node], "pandoc-api-version" => api, "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" => }) fonte_latex = convert_to_latex({"blocks"=>[node], "pandoc-api-version" => api, "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" => }) # 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:”
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
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 = "\\\\begin{figure}[htbp]\n\#{latex_code.match caption_regex}\n\\\\begin{center}\n\#{latex_code.match image_regex}\n\\\\end{center}\n\\\\legend{\#{fonte.strip}}\n\\\\end{figure}\n" 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 = "\#{inicio}\\\\caption*{\#{fonte.strip}}\n\\\\end{longtable}\n" end |
#tabela?(node) ⇒ Boolean
Verifica se node é uma tabela
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 |