trmnl_preview 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +42 -0
  3. data/README.md +55 -21
  4. data/bin/trmnlp +14 -0
  5. data/lib/trmnlp/api_client.rb +62 -0
  6. data/lib/trmnlp/app.rb +98 -0
  7. data/lib/trmnlp/cli.rb +51 -0
  8. data/lib/trmnlp/commands/base.rb +23 -0
  9. data/lib/trmnlp/commands/build.rb +22 -0
  10. data/lib/trmnlp/commands/login.rb +25 -0
  11. data/lib/trmnlp/commands/pull.rb +45 -0
  12. data/lib/trmnlp/commands/push.rb +43 -0
  13. data/lib/trmnlp/commands/serve.rb +25 -0
  14. data/lib/trmnlp/commands.rb +1 -0
  15. data/lib/trmnlp/config/app.rb +39 -0
  16. data/lib/trmnlp/config/plugin.rb +74 -0
  17. data/lib/trmnlp/config/project.rb +47 -0
  18. data/lib/trmnlp/config.rb +15 -0
  19. data/lib/trmnlp/context.rb +211 -0
  20. data/lib/trmnlp/custom_filters.rb +14 -0
  21. data/lib/trmnlp/paths.rb +50 -0
  22. data/lib/trmnlp/screen_generator.rb +137 -0
  23. data/lib/trmnlp/version.rb +5 -0
  24. data/lib/trmnlp.rb +13 -0
  25. data/trmnl_preview.gemspec +32 -14
  26. data/web/public/index.css +98 -0
  27. data/web/public/index.js +75 -0
  28. data/web/views/index.erb +15 -80
  29. data/web/views/{render_view.erb → render_html.erb} +1 -6
  30. metadata +148 -26
  31. data/.ruby-version +0 -1
  32. data/config.example.toml +0 -14
  33. data/docs/preview.png +0 -0
  34. data/exe/trmnlp +0 -12
  35. data/lib/trmnl_preview/app.rb +0 -78
  36. data/lib/trmnl_preview/cmd/build.rb +0 -25
  37. data/lib/trmnl_preview/cmd/serve.rb +0 -31
  38. data/lib/trmnl_preview/cmd/usage.rb +0 -10
  39. data/lib/trmnl_preview/context.rb +0 -129
  40. data/lib/trmnl_preview/liquid_filters.rb +0 -8
  41. data/lib/trmnl_preview/version.rb +0 -5
  42. data/lib/trmnl_preview.rb +0 -10
  43. data/web/public/live-render.js +0 -23
@@ -1,31 +0,0 @@
1
- require 'optionparser'
2
-
3
- options = {
4
- bind: '127.0.0.1',
5
- port: 4567
6
- }
7
-
8
- # Parse options BEFORE requiring the Sinatra app, since it has its own option-parsing behavior.
9
- # This will remove the option items from ARGV, which is what we want.
10
- OptionParser.new do |opts|
11
- opts.banner = "Usage: trmnlp serve [directory] [options]"
12
-
13
- opts.on("-b", "--bind [HOST]", "Bind to host address (default: 127.0.0.1)") do |host|
14
- options[:bind] = host
15
- end
16
-
17
- opts.on("-p", "--port [PORT]", "Use port (default: 4567)") do |port|
18
- options[:port] = port
19
- end
20
- end.parse!
21
-
22
- # Must come AFTER parsing options
23
- require_relative '../app'
24
-
25
- # Now we can configure things
26
- TRMNLPreview::App.set(:user_dir, ARGV[1] || Dir.pwd)
27
- TRMNLPreview::App.set(:bind, options[:bind])
28
- TRMNLPreview::App.set(:port, options[:port])
29
-
30
- # Finally, start the app!
31
- TRMNLPreview::App.run!
@@ -1,10 +0,0 @@
1
- puts <<-USAGE
2
- Usage:
3
-
4
- trmnlp [command] [options]
5
-
6
- Commands (-h for command-specific help):
7
-
8
- serve Start the TRMNL Preview server
9
- build Generate static HTML files
10
- USAGE
@@ -1,129 +0,0 @@
1
- require 'erb'
2
- require 'fileutils'
3
- require 'filewatcher'
4
- require 'json'
5
- require 'liquid'
6
- require 'open-uri'
7
- require 'toml-rb'
8
-
9
- require_relative 'liquid_filters'
10
-
11
- module TRMNLPreview
12
- class Context
13
- attr_reader :strategy, :temp_dir, :live_render
14
-
15
- def initialize(root)
16
- config_path = File.join(root, 'config.toml')
17
- @user_views_dir = File.join(root, 'views')
18
- @temp_dir = File.join(root, 'tmp')
19
- @data_json_path = File.join(@temp_dir, 'data.json')
20
-
21
- @liquid_environment = Liquid::Environment.build do |env|
22
- env.register_filter(LiquidFilters)
23
- end
24
-
25
- unless File.exist?(config_path)
26
- raise "No config.toml found in #{root}"
27
- end
28
-
29
- unless Dir.exist?(@user_views_dir)
30
- raise "No views found at #{@user_views_dir}"
31
- end
32
-
33
- config = TomlRB.load_file(config_path)
34
- @strategy = config['strategy']
35
- @url = config['url']
36
- @polling_headers = config['polling_headers'] || {}
37
- @live_render = config['live_render'] != false
38
-
39
- unless ['polling', 'webhook'].include?(@strategy)
40
- raise "Invalid strategy: #{strategy} (must be 'polling' or 'webhook')"
41
- end
42
-
43
- FileUtils.mkdir_p(@temp_dir)
44
-
45
- start_filewatcher_thread if @live_render
46
- end
47
-
48
- def start_filewatcher_thread
49
- Thread.new do
50
- loop do
51
- begin
52
- Filewatcher.new(@user_views_dir).watch do |changes|
53
- views = changes.map { |path, _change| File.basename(path, '.liquid') }
54
- views.each do |view|
55
- @view_change_callback.call(view) if @view_change_callback
56
- end
57
- end
58
- rescue => e
59
- puts "Error during live render: #{e}"
60
- end
61
- end
62
- end
63
- end
64
-
65
- def on_view_change(&block)
66
- @view_change_callback = block
67
- end
68
-
69
- def user_data
70
- data = JSON.parse(File.read(@data_json_path))
71
- data = { data: data } if data.is_a?(Array) # per TRMNL docs, bare array is wrapped in 'data' key
72
- data
73
- end
74
-
75
- def poll_data
76
- if @url.nil?
77
- raise "URL is required for polling strategy"
78
- end
79
-
80
- print "Fetching #{@url}... "
81
-
82
- if @url.match?(/^https?:\/\//)
83
- payload = URI.open(@url, @polling_headers).read
84
- else
85
- payload = File.read(@url)
86
- end
87
-
88
- File.write(@data_json_path, payload)
89
- puts "got #{payload.size} bytes"
90
-
91
- user_data
92
- end
93
-
94
- def set_data(payload)
95
- File.write(@data_json_path, payload)
96
- end
97
-
98
- def view_path(view)
99
- File.join(@user_views_dir, "#{view}.liquid")
100
- end
101
-
102
- def render_template(view)
103
- path = view_path(view)
104
- unless File.exist?(path)
105
- return "Missing plugin template: views/#{view}.liquid"
106
- end
107
-
108
- user_template = Liquid::Template.parse(File.read(path), environment: @liquid_environment)
109
- user_template.render(user_data)
110
- rescue StandardError => e
111
- e.message
112
- end
113
-
114
- def render_full_page(view)
115
- page_erb_template = File.read(File.join(__dir__, '..', '..', 'web', 'views', 'render_view.erb'))
116
-
117
- ERB.new(page_erb_template).result(ERBBinding.new(view).get_binding do
118
- render_template(view)
119
- end)
120
- end
121
-
122
- private
123
-
124
- class ERBBinding
125
- def initialize(view) = @view = view
126
- def get_binding = binding
127
- end
128
- end
129
- end
@@ -1,8 +0,0 @@
1
- module TRMNLPreview
2
- module LiquidFilters
3
- def number_with_delimiter(number)
4
- # TODO: Replace with ActiveSupport's number_with_delimiter
5
- number.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
6
- end
7
- end
8
- end
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TRMNLPreview
4
- VERSION = "0.3.1"
5
- end
data/lib/trmnl_preview.rb DELETED
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TRMNLPreview; end
4
-
5
- require_relative "trmnl_preview/context"
6
- require_relative "trmnl_preview/version"
7
-
8
- module TRMNLPreview
9
- VIEWS = %w{full half_horizontal half_vertical quadrant}
10
- end
@@ -1,23 +0,0 @@
1
- live_render = {};
2
-
3
- live_render.connect = function () {
4
- const view = document.querySelector("meta[name='trmnl-view']").content;
5
- const ws = new WebSocket("/live_render/" + view);
6
-
7
- ws.onopen = function () {
8
- console.log("Connected to live push server");
9
- };
10
-
11
- ws.onmessage = function (msg) {
12
- document.querySelector(".view").innerHTML = msg.data;
13
- };
14
-
15
- ws.onclose = function () {
16
- console.log("Reconnecting to live push server...");
17
- setTimeout(live_render.connect, 1000);
18
- };
19
- };
20
-
21
- document.addEventListener("DOMContentLoaded", function () {
22
- live_render.connect();
23
- });