Class: Bddgenx::PDFExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/bddgenx/pdf_exporter.rb

Class Method Summary collapse

Class Method Details

.camel_case(str) ⇒ Object

Converte string para camelCase, removendo caracteres especiais



29
30
31
32
33
34
35
# File 'lib/bddgenx/pdf_exporter.rb', line 29

def self.camel_case(str)
  # Remove tudo que não for letra ou número ou espaço

  str = str.gsub(/[^0-9A-Za-z ]/, '')
  parts = str.split(/ |_/)
  # Primeira palavra minúscula, demais capitalizadas

  ([parts.first&.downcase] + parts[1..].map(&:capitalize)).join
end

.exportar_arquivo(origem, destino) ⇒ Object



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
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
# File 'lib/bddgenx/pdf_exporter.rb', line 59

def self.exportar_arquivo(origem, destino)
  conteudo = File.read(origem, encoding: 'utf-8')

  Prawn::Document.generate(destino) do |pdf|
    fonte_existe = File.exist?("assets/fonts/DejaVuSansMono.ttf")
    font_dir = File.expand_path("assets/fonts", __dir__)

    if File.exist?(File.join(font_dir, "DejaVuSansMono.ttf"))
      pdf.font_families.update(
        "DejaVu" => {
          normal: File.join(font_dir, "DejaVuSansMono.ttf"),
          bold: File.join(font_dir, "DejaVuSansMono-Bold.ttf"),
          italic: File.join(font_dir, "DejaVuSansMono-Oblique.ttf"),
          bold_italic: File.join(font_dir, "DejaVuSansMono-BoldOblique.ttf")
        }
      )
      pdf.font "DejaVu"
    else
      puts "⚠️ Fonte não encontrada: #{font_dir}"
      pdf.font "Courier"
    end

    pdf.font_size 10
    pdf.text "📄 #{File.basename(origem)}", style: :bold, size: 14
    pdf.move_down 10

    conteudo.each_line do |linha|
      linha = fonte_existe ? linha.strip : sanitizar_utf8_para_ascii(linha.chomp)

      case linha
      when /^#/
        pdf.fill_color "888888"
        pdf.text linha, style: :italic, size: 8
        pdf.fill_color "000000"
      when /^Funcionalidade:|^Feature:/
        pdf.move_down 6
        pdf.text linha, style: :bold, size: 12
        pdf.move_down 4
      when /^Cenário:|^Scenario:|^Esquema do Cenário:|^Scenario Outline:/
        pdf.move_down 4
        pdf.text linha, style: :bold
      when /^@/
        pdf.text linha, style: :italic, color: "555555"
      when /^(Dado|Quando|Então|E|Mas|Given|When|Then|And|But)\b/
        pdf.indent(20) { pdf.text linha }
      when /^Exemplos:|^Examples:/
        pdf.move_down 4
        pdf.text linha, style: :bold
      when /^\|.*\|$/
        pdf.indent(20) { pdf.text linha }
      when /^\s*$/
        pdf.move_down 4
      else
        pdf.text linha
      end
    end

    pdf.move_down 20
    pdf.number_pages "Página <page> de <total>", align: :right, size: 8
  end
rescue => e
  puts "❌ Erro ao gerar PDF de #{origem}: #{e.message}"
end

.exportar_todos(only_new: false) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bddgenx/pdf_exporter.rb', line 6

def self.exportar_todos(only_new: false)
  FileUtils.mkdir_p('reports/pdf')
  generated = []
  skipped   = []

  Dir.glob('features/*.feature').each do |feature|
    nome = File.basename(feature, '.feature')
    destino = "reports/pdf/#{camel_case(nome)}.pdf"

    if File.exist?(destino)
      skipped << destino
      next
    end

    exportar_arquivo(feature, destino)
    generated << destino
  end

  return { generated: generated, skipped: skipped }
end

.sanitizar_utf8_para_ascii(texto) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bddgenx/pdf_exporter.rb', line 37

def self.sanitizar_utf8_para_ascii(texto)
  if texto.respond_to?(:unicode_normalize)
    # Decompõe em base + acentos, remove acentos, e garante ASCII

    texto
      .unicode_normalize(:nfkd)           # separa letra + marca

      .chars
      .reject { |c| c.match?(/\p{Mn}/) }  # descarta marcas de acento

      .join
      .encode('ASCII', undef: :replace, replace: '?')
  else
    # Fallback simples se por algum motivo unicode_normalize não existir

    texto
      .gsub(/[áàâãä]/i, 'a')
      .gsub(/[éèêë]/i, 'e')
      .gsub(/[íìîï]/i, 'i')
      .gsub(/[óòôõö]/i, 'o')
      .gsub(/[úùûü]/i, 'u')
      .gsub(/[ç]/i,  'c')
      .encode('ASCII', undef: :replace, replace: '?')
  end
end