Class: AppHtmlLayer

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

Instance Method Summary collapse

Constructor Details

#initialize(app, filepath: '.', headings: false, debug: false) ⇒ AppHtmlLayer

Returns a new instance of AppHtmlLayer.



13
14
15
# File 'lib/apphtml_layer.rb', line 13

def initialize(app, filepath: '.', headings: false, debug: false)
  @app, @filepath, @headings, @debug = app, filepath, headings, debug
end

Instance Method Details

#default_indexObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/apphtml_layer.rb', line 108

def default_index()
  
  a = (@app.public_methods - Object.public_methods).sort
  s = a.map {|x| "* [%s](%s)" % [x,x]}.join("\n")

  markdown = "
<html>
<head>
<title>#{@app.class.to_s}</title>
<style>
h1 {color: green}
h2 {color: orange}
div {height: 60%; overflow-y: auto; width: 200px; float: left}
</style>
</head>
<body markdown='1'>

# #{@app.class.to_s} Index

## Public Methods

<div markdown='1'>
#{s}
</div>
<iframe name='i1'></iframe>
<div style='clear: both' />
<hr/>
</body>
</html>    
  "    
  #markdown = s
  doc = Rexle.new(Kramdown::Document.new(markdown).to_html)
  
  doc.root.xpath('body/div/ul/li/a') do |link|
    link.attributes[:target] = 'i1'
  end
  
  [doc.xml(pretty: true), 'text/html']    
end

#lookup(s) ⇒ Object



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/apphtml_layer.rb', line 17

def lookup(s)

  if s == '/' then
    
    fp = File.join(@filepath, 'index.html')      
    return (File.exists?(fp) ? File.read(fp) : default_index() )

  end
 
  return [s.to_s, 'text/plain'] if s =~ /^\/\w+\.\w+/

  uri = URI(s)

  a = uri.path.split('/')
  a.shift
  name, *args = a

  if uri.query then

    h = URI.decode_www_form(uri.query).inject({}) do |r,x|
      r.merge!(x[0].to_sym => x[1])
    end

    puts ('h: ' + h.inspect).debug if @debug
    args = h[:arg] ? [h[:arg]] : h

  end  

  if @app.respond_to? name.to_sym then

    begin
      
      method = @app.method(name.to_sym)
      
      if method.arity > 0 and  args.length <= 0 then
        
        r = "
        <form action='#{name}'>
          <input type='text' name='arg'/>
          <input type='submit'/>
        </form>"
      else
        
        puts ('args: ' + args.inspect).debug if @debug
        r = args.is_a?(Array) ? method.call(*args) :  method.call(args)
        
      end

      fp = File.join(@filepath, File.basename(name) + '.html')

      content = if File.exists?(fp) then        
        render_html(fp, r)
      else

        if @headings then
          markdown = "
# #{name.capitalize}          

<div>
#{r}
</div>
"

          Kramdown::Document.new(markdown).to_html
        else
          
          r
        end


      end

    rescue
      content = ($!).inspect
    end
    
    puts ('content: ' + content.inspect).debug if @debug

    case content.class.to_s
    when "String"
      media = content.lstrip[0] == '<' ? 'html' : 'plain'
      [content, 'text/' + media]
    when "Hash"
      [content.to_json,'application/json']
    else
      [content.to_s, 'text/plain']
    end
  end

end

#render_html(fp, s) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/apphtml_layer.rb', line 148

def render_html(fp, s)
  
  doc = Rexle.new(File.read fp)
  e = doc.root.element('//[@class="output"]')    
  e.text = s
  
  doc.xml pretty: true    
  
end