This repository was archived by the owner on Aug 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 962
/
Copy pathapiexample.rb
91 lines (75 loc) · 2.44 KB
/
apiexample.rb
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
84
85
86
87
88
89
90
91
module Jekyll
class ApiExample < Liquid::Block
def initialize(tag_name, markup, tokens)
attributes = markup.split
@identifier = attributes[0]
@request_type = attributes[1]
@url = attributes[2]
@data = attributes[3]
if attributes[4]
@show_livedoc = attributes[4]
end
super
end
def render(context)
output = super
livedoc = ""
if @show_livedoc != "false"
livedoc = "{% livedoc #{@identifier} #{@request_type} #{@url} #{@data} %}"
end
output = <<HTML
#{livedoc}
<div class="api-example" id="apiexample-#{@identifier}">
{% xmljsontabs #{@identifier}%}
<div class="tab-content">
#{output}
</div>
</div>
HTML
return Liquid::Template.parse(output).render context
end
def render_all(list, context)
context['identifier'] = @identifier
context['request_type'] = @request_type
context['url'] = @url
context['data'] = @data
super
end
end
class ResponseBlock < Liquid::Block
def initialize(tag_name, markup, tokens)
parameters = markup.split
if parameters[0]
@format = parameters[0].downcase
else
@format = "json"
end
super
end
def render(context)
output = super
active = @format == "json" ? "active" : ""
#let's parse the vars before we generate the output to avoid complications
identifier = Liquid::Template.parse("{{ identifier }}").render context
request_type = Liquid::Template.parse("{{ request_type }}").render context
url = Liquid::Template.parse("{{ url }}").render context
data = Liquid::Template.parse("{{ data }}").render context
request_url = request_type.upcase != "GET" ? "#{url}.#{@format}" : "#{url}.#{@format}?#{data}"
requestdata_block = request_type.upcase != "GET" ? "{% requestdata data #{request_type} %}#{data}{% endrequestdata %}" : ""
output = <<HTML
<div class="tab-pane #{active}" id="#{identifier}-#{@format}">
<h3>Call</h3>
{% requestblock %}
{% requesturl #{request_type} %}#{request_url}{% endrequesturl %}
#{requestdata_block}
{% endrequestblock %}
<h3>Response</h3>
{% codeblock lang:#{@format} %}#{output.strip}{% endcodeblock %}
</div>
HTML
return Liquid::Template.parse(output).render context
end
end
end
Liquid::Template.register_tag('response', Jekyll::ResponseBlock)
Liquid::Template.register_tag('apiexample', Jekyll::ApiExample)