Class: RteHtmlWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/udise_school_report_reader/rte_html_writer.rb

Class Method Summary collapse

Class Method Details

.generate_html(data, html_path) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/udise_school_report_reader/rte_html_writer.rb', line 2

def self.generate_html(data, html_path)
  return unless data

  grade_rows = data[:grade_rows]
  bg_pairs = data[:bg_pairs]

  categories = [
    ['EWS', data[:ews_numbers] || {}],
  ]

  # Generate table rows for all categories
  table_rows = categories.map do |category, numbers|
    cells = bg_pairs.map do |x_mid, _|
      nums = numbers[x_mid.to_s] || numbers[x_mid] || []
      b_num = nums&.first
      g_num = nums&.last
      "<td>#{b_num ? b_num['text'] : ''}</td><td>#{g_num ? g_num['text'] : ''}</td>"
    end.join

    "    <tr>\n" \
    "      <td class=\"category\">#{category}</td>\n" \
    "      #{cells}\n" \
    "    </tr>"
  end.join("\n")

  # Generate grade headers
  grade_headers = grade_rows.map { |row| "<th colspan='2'>#{row['text']}</th>" }.join
  bg_headers = grade_rows.map { |_| "<td>B</td><td>G</td>" }.join

  html_content = "    <!DOCTYPE html>\n    <html>\n    <head>\n      <title>Enrollment Table</title>\n      <style>\n        table { border-collapse: collapse; margin-top: 20px; width: 100%; }\n        th, td { border: 1px solid black; padding: 8px; text-align: center; }\n        .header { font-weight: bold; background-color: #f0f0f0; }\n        .grade { font-weight: bold; background-color: #e0e0e0; }\n        .bg-pair { background-color: #f8f8f8; }\n        .category { font-weight: bold; text-align: left; }\n      </style>\n    </head>\n    <body>\n      <h2>Enrolment (By Social Category)</h2>\n      <table>\n        <tr class=\"grade\">\n          <th rowspan=\"2\">Category</th>\n          \#{grade_headers}\n        </tr>\n        <tr class=\"bg-pair\">\n          \#{bg_headers}\n        </tr>\n    \#{table_rows}\n      </table>\n    </body>\n    </html>\n  HTML\n\n  File.write(html_path, html_content)\nend\n"