summaryrefslogtreecommitdiff
path: root/web/fmt-as-table
blob: 186a5a9cc351627c5b4f18be12f72c87a210cc22 (plain) (blame)
1
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env ruby
# fmt-as-table ORIGIN HEADER OPTION
# ORIGIN: the file to format as a table. Each row must be newline-delimited, 
#         and each column tab-delimited. Anything following a # is treated as 
#         a comment and ignored.
# HEADER: the headers. This must be passed as a single string with each column
#         tab-delimited.
# OPTION: a string specifying output format options, comma-delimited. Currently
# supported are: 
#       collapsible(summary)  Create a <details> tag surrounding it, to make
#                             the table collapsible. `summary` must be a string
#                             which contains the text to place in the <summary>
#                             tag of the <details>.


if ARGV[0] == nil or ARGV[1] == nil then 
  puts "No argument passed! Exitting early.\n"
  exit
end

## Handle options
is_collapsible = false
collapsible_summary = ""
allowwrap = false



option=""
if ARGV[2] != nil then 
  option=ARGV[2]
end

options=option.split ","
options.each do |opt| 
  if opt[0..10] == "collapsible" then 
    is_collapsible = true
    collapsible_summary = opt.split("(")[1].split(")")[0]
  elsif opt == "allowwrap" then
    allowwrap = true
  end
end

table=""

if is_collapsible then
  table << "<details>\n"
  table << "<summary>" << collapsible_summary << "</summary>\n"
end

# Sort out the header.
if allowwrap then
  table << "<table class=\"allowwrap\">\n"
else
  table << "<table>"
end

table << "\t<tr class=\"header\">\n"
ARGV[1].split("\t").each do |entry|
  table << ("\t\t<th>" + entry + "</th>\n")
end
table << "\t</tr>\n\n"

# Now sort out the actual file.

File.foreach(ARGV[0]).with_index do |line, line_num| 
  next if line[0] == '#'
  next if line == "\n" or line == "\t" or line == ""
  table << "\t<tr>\n"
  line.split("\t").each do |entry|
    table << ("\t\t<td>" + entry.delete("\n") + "</td>\n")
  end
  table << "\t</tr>\n"
end

table << "</table>\n"

if is_collapsible then 
  table << "</details>"
end

# Echo the file. 
puts table