.lib/ruby/data_embed.rb

Mon, 15 Jul 2024 13:22:56 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Mon, 15 Jul 2024 13:22:56 -0500
changeset 1176
1cb4636d6d36
parent 1003
0cf4b33dd117
permissions
-rw-r--r--

Fix for no-nerdfonts experience

1003
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
1 module DataEmbed
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
2 module_function
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
3
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
4 # Returns a hash of filename => content read from the DATA section of a given
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
5 # source, or the caller. Does not cache for you. If given a handle, it is
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
6 # read; a string, used as a file path to read; a module or class, its file is
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
7 # guessed using +const_source_location+.
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
8 #
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
9 # Note: This isn't careful with encoding. A battle-tested version of this
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
10 # that should be okay with encodings can be found in Sinatra.
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
11 #
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
12 # @example Read my own data section
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
13 # files = DataEmbed.data_section(__FILE__)
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
14 #
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
15 # @example A specific module
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
16 # files = DataEmbed.data_section(Some::Class)
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
17 #
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
18 # @example The DATA global (only opened for $0 unlike perl)
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
19 # files = DataEmbed.data_section(DATA)
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
20 #
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
21 # @param source [Module, String, #read]
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
22 # @return [{String => String}]
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
23 def data_section(source)
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
24 data =
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
25 case source
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
26 when Module
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
27 File.read(Object.const_source_location(source.name)[0])
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
28 when String
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
29 File.read(source)
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
30 else
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
31 source.read
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
32 end
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
33
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
34 data
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
35 .split(/^__END__$/, 2)
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
36 .last
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
37 .split(/^@@\s*(.+?)\s*\r?\n/m)
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
38 .tap { _1.shift if _1.length.odd? }
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
39 .each_slice(2)
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
40 .to_h
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
41 .transform_values { _1.gsub(/\n+\z/, "\n") }
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
42 end
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
43
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
44 # Process the given tmpl using ERB, with % trim enabled. Either a binding or
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
45 # a hash may be given. Only locals are available to the template if a hash
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
46 # is used.
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
47 #
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
48 # @param tmpl [String]
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
49 # @param vars [Binding, Hash]
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
50 # @return [String]
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
51 def process_template(tmpl, vars)
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
52 require "erb"
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
53
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
54 ERB
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
55 .new(tmpl, trim_mode: "%")
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
56 .public_send(vars.is_a?(Binding) ? :result : :result_with_hash, vars)
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
57 end
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
58
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
59 # Process templates in both keys and values of the given hash
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
60 #
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
61 # @param data [{String => String}]
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
62 # @param vars [Binding, Hash]
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
63 # @return [{String => String}]
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
64 def process_templates(data, vars)
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
65 data
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
66 .transform_keys { process_template(_1, vars) }
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
67 .transform_values! { process_template(_1, vars) }
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
68 end
0cf4b33dd117 add DataEmbed
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
69 end

mercurial