apphtml_layer 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b3aced5a8e6c3bf21bb153679c3714739adb87bc8361868c9cd0fc4796f6912c
4
+ data.tar.gz: ed25dc425646840ee82fdf23236d0665b72922465e07c2a1c18ab89010970311
5
+ SHA512:
6
+ metadata.gz: d2499af62de9fa482220ec1b0caa265b8e13158c982192ef9b0e30fc90763a66dedfaae1b0863d1d6c37396b48a18d757e41159c91392670472c82bc5b8cde2e
7
+ data.tar.gz: c36cda8d3b70376348cecf7861bd0a343be1e9ad80122bcaa7472053517d3e72c610e2adc3beb45c5b83ab0e0c5eeeff6c634ae126b03275cc75d52e1f9f6d33
Binary file
Binary file
@@ -0,0 +1,159 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # file: apphtml_layer.rb
4
+
5
+ require 'uri'
6
+ require 'json'
7
+ require 'c32'
8
+ require 'kramdown'
9
+
10
+ class AppHtmlLayer
11
+ using ColouredText
12
+
13
+ def initialize(app, filepath: '.', headings: false, debug: false)
14
+ @app, @filepath, @headings, @debug = app, filepath, headings, debug
15
+ end
16
+
17
+ def lookup(s)
18
+
19
+ if s == '/' then
20
+
21
+ fp = File.join(@filepath, 'index.html')
22
+ return (File.exists?(fp) ? File.read(fp) : default_index() )
23
+
24
+ end
25
+
26
+ return [s.to_s, 'text/plain'] if s =~ /^\/\w+\.\w+/
27
+
28
+ uri = URI(s)
29
+
30
+ a = uri.path.split('/')
31
+ a.shift
32
+ name, *args = a
33
+
34
+ if uri.query then
35
+
36
+ h = URI.decode_www_form(uri.query).inject({}) do |r,x|
37
+ r.merge!(x[0].to_sym => x[1])
38
+ end
39
+
40
+ puts ('h: ' + h.inspect).debug if @debug
41
+ args = h[:arg] ? [h[:arg]] : h
42
+
43
+ end
44
+
45
+ if @app.respond_to? name.to_sym then
46
+
47
+ begin
48
+
49
+ method = @app.method(name.to_sym)
50
+
51
+ if method.arity > 0 and args.length <= 0 then
52
+
53
+ r = "
54
+ <form action='#{name}'>
55
+ <input type='text' name='arg'/>
56
+ <input type='submit'/>
57
+ </form>"
58
+ else
59
+
60
+ puts ('args: ' + args.inspect).debug if @debug
61
+ r = args.is_a?(Array) ? method.call(*args) : method.call(args)
62
+
63
+ end
64
+
65
+ fp = File.join(@filepath, File.basename(name) + '.html')
66
+
67
+ content = if File.exists?(fp) then
68
+ render_html(fp, r)
69
+ else
70
+
71
+ if @headings then
72
+ markdown = "
73
+ # #{name.capitalize}
74
+
75
+ <div>
76
+ #{r}
77
+ </div>
78
+ "
79
+
80
+ Kramdown::Document.new(markdown).to_html
81
+ else
82
+
83
+ r
84
+ end
85
+
86
+
87
+ end
88
+
89
+ rescue
90
+ content = ($!).inspect
91
+ end
92
+
93
+ puts ('content: ' + content.inspect).debug if @debug
94
+
95
+ case content.class.to_s
96
+ when "String"
97
+ media = content.lstrip[0] == '<' ? 'html' : 'plain'
98
+ [content, 'text/' + media]
99
+ when "Hash"
100
+ [content.to_json,'application/json']
101
+ else
102
+ [content.to_s, 'text/plain']
103
+ end
104
+ end
105
+
106
+ end
107
+
108
+ def default_index()
109
+
110
+ a = (@app.public_methods - Object.public_methods).sort
111
+ s = a.map {|x| "* [%s](%s)" % [x,x]}.join("\n")
112
+
113
+ markdown = "
114
+ <html>
115
+ <head>
116
+ <title>#{@app.class.to_s}</title>
117
+ <style>
118
+ h1 {color: green}
119
+ h2 {color: orange}
120
+ div {height: 60%; overflow-y: auto; width: 200px; float: left}
121
+ </style>
122
+ </head>
123
+ <body markdown='1'>
124
+
125
+ # #{@app.class.to_s} Index
126
+
127
+ ## Public Methods
128
+
129
+ <div markdown='1'>
130
+ #{s}
131
+ </div>
132
+ <iframe name='i1'></iframe>
133
+ <div style='clear: both' />
134
+ <hr/>
135
+ </body>
136
+ </html>
137
+ "
138
+ #markdown = s
139
+ doc = Rexle.new(Kramdown::Document.new(markdown).to_html)
140
+
141
+ doc.root.xpath('body/div/ul/li/a') do |link|
142
+ link.attributes[:target] = 'i1'
143
+ end
144
+
145
+ [doc.xml(pretty: true), 'text/html']
146
+ end
147
+
148
+ def render_html(fp, s)
149
+
150
+ doc = Rexle.new(File.read fp)
151
+ e = doc.root.element('//[@class="output"]')
152
+ e.text = s
153
+
154
+ doc.xml pretty: true
155
+
156
+ end
157
+
158
+ end
159
+
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apphtml_layer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Robertson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIEXjCCAsagAwIBAgIBATANBgkqhkiG9w0BAQsFADAsMSowKAYDVQQDDCFnZW1t
14
+ YXN0ZXIvREM9amFtZXNyb2JlcnRzb24vREM9ZXUwHhcNMjAxMDI2MTkzMzQyWhcN
15
+ MjExMDI2MTkzMzQyWjAsMSowKAYDVQQDDCFnZW1tYXN0ZXIvREM9amFtZXNyb2Jl
16
+ cnRzb24vREM9ZXUwggGiMA0GCSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQDNLDBO
17
+ /4LaMbqL2Dl6bPhriFIzKgMzbRDRV2AkGJbrvrsBu2W8iN23cJ1xevSLUqt5Br1L
18
+ GtSgzcBxY0z4q4yCTAL/Epb7s9JswGOWCXEJlf4yjnmpTrUH/98doGlEMFQ9rfwv
19
+ UjGQnvvKu0FImJy8igF0fGpraiuzJ2Ch9Ew/jTqUU+Tpk4eatd0ZeP7X6m8JB0O9
20
+ 2+e5PPV1iBMt+Y3ujTERzWLV8WZ1+ntLNhzJqyzd0kacm+hS3a7pIN4cw38sV8y8
21
+ pj4/MRfLeFVxA1OXZZgM1wwVK5ayLNCN4iXcqqjE8anCqWtxqpJJLYGWw21Ulg1g
22
+ MuxJfVV3m3Ms2qIkGZtwlMnj4/xyyI/JZmW09Y65H7cJ+9diYZbeQIDaNjtxucE2
23
+ +zhDekN2hI2MSVR8ZwIDkGWbnf5Rabq3LDkfU1fRuCwnW4bPmTE+lYWG9is1r3uz
24
+ 4OxGtYPiBNaAjfUyxscULIl80XIOdIpImSwTUpgkivIFxBK9Vu1zTv/Uu9MCAwEA
25
+ AaOBijCBhzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQUYfl/aY32
26
+ h9xIl+vnkNdsxOK8vrEwJgYDVR0RBB8wHYEbZ2VtbWFzdGVyQGphbWVzcm9iZXJ0
27
+ c29uLmV1MCYGA1UdEgQfMB2BG2dlbW1hc3RlckBqYW1lc3JvYmVydHNvbi5ldTAN
28
+ BgkqhkiG9w0BAQsFAAOCAYEArHWWZxk8tLHb+remCOXnFU4l06UK6Bt/oyjYGwSA
29
+ SMk0+dcqEjCfeCLJKf5eBKEoxQYijcg+O9kcciSpWAJ7M7NRLkbDFJk3tSBgXKuG
30
+ fz9IFST7lnPmrSODmQw/artz1OLlQOUxRAHtaxVQrae0hY0vXhQaeXgQizlbHSYM
31
+ c3O08E7GofHxkuwwQWlBY6zXfqZzhpvzvAWIm5avzL4h78ZzAMEURi9qD+SDzDRN
32
+ /EYAideeDImy5TePWbsrPxDaKJpE6WLeqrEmQavDifYs54MUbYajKf6qE9ildd/k
33
+ rfTeb4GdZg9vHephIKgXSeXXKBfm6TUy2AqJ1pPBPWN/qh3AFkASMY1g2JHRJwwU
34
+ ShGWnySTEzCZWvxvyMfBhVRQjK1HTwxseuxn58zg37xEe4hellrze7ABAV+Vimcm
35
+ yoJvKaZL2qWoAZHs/MGOlsTMVHkz5YV3AaLaViO2SE/hVvFfFHJDX224xRsUHIHx
36
+ za7UEBvPemTf/f9WiN5bG3dD
37
+ -----END CERTIFICATE-----
38
+ date: 2020-10-26 00:00:00.000000000 Z
39
+ dependencies:
40
+ - !ruby/object:Gem::Dependency
41
+ name: c32
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 0.3.0
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0.3'
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 0.3.0
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '0.3'
60
+ - !ruby/object:Gem::Dependency
61
+ name: kramdown
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 2.3.0
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '2.3'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 2.3.0
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '2.3'
80
+ description:
81
+ email: james@jamesrobertson.eu
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - lib/apphtml_layer.rb
87
+ homepage: https://github.com/jrobertson/apphtml_layer
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.7.10
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Add a basic HTML wrapper to your Ruby object. Suitable for use with a web
111
+ server.
112
+ test_files: []
Binary file