nexmo_markdown_renderer 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/locales/en.yml +2 -2
- data/lib/nexmo_markdown_renderer/filters/block_escape_filter.rb +5 -5
- data/lib/nexmo_markdown_renderer/filters/code_filter.rb +1 -1
- data/lib/nexmo_markdown_renderer/filters/code_snippet_filter.rb +37 -37
- data/lib/nexmo_markdown_renderer/filters/markdown_filter.rb +15 -1
- data/lib/nexmo_markdown_renderer/services/code_snippet_renderer/java.rb +10 -10
- data/lib/nexmo_markdown_renderer/views/code_snippets/_code_only.html.erb +1 -1
- data/lib/nexmo_markdown_renderer/views/code_snippets/_configure_client.html.erb +1 -1
- data/lib/nexmo_markdown_renderer/views/code_snippets/_dependencies.html.erb +1 -1
- data/lib/nexmo_markdown_renderer/views/code_snippets/_write_code.html.erb +1 -1
- data/lib/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d556fff7ca8b92dfaa71da286fd16d2e54d4ce8d13b2d11433d55bf4f049d69
|
4
|
+
data.tar.gz: 3d38cdbd687132a0e4a014893ada68e292c723d1698a97aa9761ac1275374c24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d748be0b975cdc9cc403719e06b1bb9ba0ac2532823bd48c770c1234c85fc240c3060b5ab9367c09698db6f759c973a178362a9b4d05e98876b0c30a06f4516
|
7
|
+
data.tar.gz: ed02f86a2d2c91f66e6c8419998831ca2ae9119f5ec50a48a7e3dd9b294487dc50771863b76d3d63a0ea85f4680b995c7ada2fce663be9d96a18666b871f0447
|
data/config/locales/en.yml
CHANGED
@@ -152,7 +152,7 @@ en:
|
|
152
152
|
run_command: >-
|
153
153
|
## Run your code
|
154
154
|
Save this file to your machine and run it:
|
155
|
-
<pre class=
|
155
|
+
<pre class="Vlt-prism--dark command-line language-bash Vlt-prism--copy-disabled" data-prompt="$"><code>%{command}</code></pre>
|
156
156
|
|
157
157
|
curl:
|
158
158
|
only_permitted_dependency: The only permitted curl dependency is `jwt`
|
@@ -173,4 +173,4 @@ en:
|
|
173
173
|
|
174
174
|
Run the following `gradle` command to execute your application, replacing `%{chomped_package}` with the package containing `%{file}`:
|
175
175
|
|
176
|
-
<pre class="
|
176
|
+
<pre class="language-shell Vlt-prism--dark command-line Vlt-prism--copy-disabled" data-prompt="$"><code>gradle run -Pmain=%{main}</code></pre>
|
@@ -7,15 +7,15 @@ module Nexmo
|
|
7
7
|
lexer = Rouge::Lexer.find('text')
|
8
8
|
formatter = Rouge::Formatters::HTML.new
|
9
9
|
highlighted_source = formatter.format(lexer.lex($1))
|
10
|
-
|
10
|
+
|
11
11
|
output = <<~HEREDOC
|
12
|
-
<pre class="
|
12
|
+
<pre class="Vlt-prism--dark language-#{lexer.tag} Vlt-prism--copy-disabled"><code>#{highlighted_source}</code></pre>
|
13
13
|
HEREDOC
|
14
|
-
|
14
|
+
|
15
15
|
"FREEZESTART#{Base64.urlsafe_encode64(output)}FREEZEEND"
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
end
|
21
|
-
end
|
21
|
+
end
|
@@ -26,7 +26,7 @@ module Nexmo
|
|
26
26
|
highlighted_source = highlight(code, lexer)
|
27
27
|
|
28
28
|
<<~HEREDOC
|
29
|
-
<pre class="
|
29
|
+
<pre class="Vlt-prism--dark language-#{lexer.tag} Vlt-prism--copy-disabled"><code>#{highlighted_source}</code></pre>
|
30
30
|
HEREDOC
|
31
31
|
end
|
32
32
|
end
|
@@ -2,33 +2,33 @@ module Nexmo
|
|
2
2
|
module Markdown
|
3
3
|
class CodeSnippetFilter < Banzai::Filter
|
4
4
|
include OcticonsHelper
|
5
|
-
|
5
|
+
|
6
6
|
def call(input)
|
7
7
|
input.gsub(/```single_code_snippet(.+?)```/m) do |_s|
|
8
8
|
config = YAML.safe_load($1)
|
9
|
-
|
9
|
+
|
10
10
|
@renderer = get_renderer(config['language'])
|
11
|
-
|
11
|
+
|
12
12
|
lexer = Nexmo::Markdown::CodeLanguage.find(config['language']).lexer
|
13
13
|
lang = config['title'].delete('.')
|
14
|
-
|
14
|
+
|
15
15
|
application_html = generate_application_block(config['application'])
|
16
|
-
|
16
|
+
|
17
17
|
# Read the client
|
18
18
|
if config['client']
|
19
19
|
highlighted_client_source = generate_code_block(config['language'], config['client'], config['unindent'])
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
# Read the code
|
23
23
|
highlighted_code_source = generate_code_block(config['language'], config['code'], config['unindent'])
|
24
|
-
|
24
|
+
|
25
25
|
dependency_html = ''
|
26
26
|
if config['dependencies']
|
27
27
|
dependency_html = generate_dependencies(lexer.tag, config['dependencies'])
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
source_url = generate_source_url(config['code'])
|
31
|
-
|
31
|
+
|
32
32
|
client_html = ''
|
33
33
|
if highlighted_client_source
|
34
34
|
client_url = generate_source_url(config['client'])
|
@@ -37,54 +37,54 @@ module Nexmo
|
|
37
37
|
erb = File.read("#{GEM_ROOT}/lib/nexmo_markdown_renderer/views/code_snippets/_configure_client.html.erb")
|
38
38
|
client_html = ERB.new(erb).result(binding)
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
add_instructions = @renderer.add_instructions(config['file_name']).render_markdown
|
42
42
|
if config['code_only']
|
43
43
|
erb = File.read("#{GEM_ROOT}/lib/nexmo_markdown_renderer/views/code_snippets/_code_only.html.erb")
|
44
44
|
else
|
45
45
|
erb = File.read("#{GEM_ROOT}/lib/nexmo_markdown_renderer/views/code_snippets/_write_code.html.erb")
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
code_html = ERB.new(erb).result(binding)
|
49
|
-
|
49
|
+
|
50
50
|
return code_html if config['code_only']
|
51
|
-
|
51
|
+
|
52
52
|
config['run_command'] = config['run_command'].gsub('{filename}', config['file_name']) if config['run_command']
|
53
53
|
run_html = @renderer.run_command(config['run_command'], config['file_name'], config['code']['source']).to_s
|
54
|
-
|
54
|
+
|
55
55
|
prereqs = (application_html + dependency_html + client_html).strip
|
56
56
|
prereqs = "<h2>#{::I18n.t('.filters.prerequisites')}</h2>#{prereqs}" unless prereqs.empty?
|
57
57
|
prereqs + code_html + run_html
|
58
58
|
end
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
61
|
private
|
62
|
-
|
62
|
+
|
63
63
|
def highlight(source, lexer)
|
64
64
|
formatter = Rouge::Formatters::HTML.new
|
65
65
|
formatter.format(lexer.lex(source))
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
68
|
def generate_code_block(language, input, unindent)
|
69
69
|
filename = "#{Nexmo::Markdown::Config.docs_base_path}/#{input['source']}"
|
70
70
|
return '' unless input
|
71
71
|
raise "CodeSnippetFilter - Could not load #{filename} for language #{language}" unless File.exist?(filename)
|
72
|
-
|
72
|
+
|
73
73
|
code = File.read(filename)
|
74
74
|
lexer = Nexmo::Markdown::CodeLanguage.find(language).lexer
|
75
|
-
|
75
|
+
|
76
76
|
total_lines = code.lines.count
|
77
|
-
|
77
|
+
|
78
78
|
# Minus one since lines are not zero-indexed
|
79
79
|
from_line = (input['from_line'] || 1) - 1
|
80
80
|
to_line = (input['to_line'] || total_lines) - 1
|
81
|
-
|
81
|
+
|
82
82
|
code = code.lines[from_line..to_line].join
|
83
83
|
code.unindent! if unindent
|
84
|
-
|
84
|
+
|
85
85
|
highlight(code, lexer)
|
86
86
|
end
|
87
|
-
|
87
|
+
|
88
88
|
def get_renderer(language)
|
89
89
|
language = 'dotnet' if language == 'csharp'
|
90
90
|
case language
|
@@ -116,7 +116,7 @@ module Nexmo
|
|
116
116
|
raise "Unknown language: #{language}"
|
117
117
|
end
|
118
118
|
end
|
119
|
-
|
119
|
+
|
120
120
|
def generate_dependencies(language, dependencies)
|
121
121
|
# The only valid dependency for curl examples is `JWT`
|
122
122
|
if dependencies.map(&:upcase).include?('JWT')
|
@@ -129,19 +129,19 @@ module Nexmo
|
|
129
129
|
erb = File.read("#{GEM_ROOT}/lib/nexmo_markdown_renderer/views/code_snippets/_dependencies.html.erb")
|
130
130
|
ERB.new(erb).result(binding)
|
131
131
|
end
|
132
|
-
|
132
|
+
|
133
133
|
def generate_application_block(app)
|
134
134
|
return '' unless app
|
135
|
-
|
135
|
+
|
136
136
|
base_url = 'http://demo.ngrok.io'
|
137
137
|
base_url = 'https://example.com' if app['disable_ngrok']
|
138
|
-
|
138
|
+
|
139
139
|
app['name'] = 'ExampleProject' unless app['name']
|
140
|
-
|
140
|
+
|
141
141
|
# We should remove this default once we're sure that all Code Snippets
|
142
142
|
# have a type set e.g audit
|
143
143
|
app['type'] ||= 'voice'
|
144
|
-
|
144
|
+
|
145
145
|
if ['voice', 'rtc'].include? app['type']
|
146
146
|
app['event_url'] = "#{base_url}/webhooks/events" unless app['event_url']
|
147
147
|
app['answer_url'] = "#{base_url}/webhooks/answer" unless app['answer_url']
|
@@ -151,21 +151,21 @@ module Nexmo
|
|
151
151
|
else
|
152
152
|
raise "Invalid application type when creating code snippet: '#{app['type']}'"
|
153
153
|
end
|
154
|
-
|
154
|
+
|
155
155
|
id = SecureRandom.hex
|
156
|
-
|
156
|
+
|
157
157
|
ERB.new(erb).result(binding)
|
158
158
|
end
|
159
|
-
|
159
|
+
|
160
160
|
def generate_source_url(code)
|
161
161
|
# Source example: .repos/nexmo/nexmo-java-code-snippets/ExampleClass.java
|
162
162
|
# Direct link on GitHub is in form https://github.com/nexmo/nexmo-java-code-snippets/blob/master/ExampleClass.java
|
163
163
|
start_section = 'https://github.com'
|
164
|
-
|
164
|
+
|
165
165
|
# Insert "blob/master" and strip ".repos"
|
166
166
|
repo_path = '\\0blob/master/'
|
167
167
|
file_section = code['source'].sub('.repos', '').sub(%r{(-quickstart|-code-snippets|-code-snippets)/}, repo_path)
|
168
|
-
|
168
|
+
|
169
169
|
# Line highlighting
|
170
170
|
line_section = ''
|
171
171
|
if code['from_line']
|
@@ -178,10 +178,10 @@ module Nexmo
|
|
178
178
|
line_section += "-L#{File.read(code['source']).lines.count}"
|
179
179
|
end
|
180
180
|
end
|
181
|
-
|
181
|
+
|
182
182
|
start_section + file_section + line_section
|
183
183
|
end
|
184
184
|
end
|
185
|
-
|
185
|
+
|
186
186
|
end
|
187
|
-
end
|
187
|
+
end
|
@@ -39,7 +39,7 @@ module Nexmo
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def table(header, body)
|
42
|
-
'<div class="Vlt-table Vlt-table--
|
42
|
+
'<div class="Vlt-table Vlt-table--bordered">' \
|
43
43
|
'<table>' \
|
44
44
|
"<thead>#{header}</thead>" \
|
45
45
|
"<tbody>#{body}</tbody>" \
|
@@ -75,6 +75,20 @@ module Nexmo
|
|
75
75
|
'</ol>' \
|
76
76
|
end
|
77
77
|
end
|
78
|
+
|
79
|
+
def block_code(code, language)
|
80
|
+
lexer = ::Rouge::Lexer.find_fancy(language, code) || ::Rouge::Lexers::PlainText
|
81
|
+
|
82
|
+
# XXX HACK: Redcarpet strips hard tabs out of code blocks,
|
83
|
+
# so we assume you're not using leading spaces that aren't tabs,
|
84
|
+
# and just replace them here.
|
85
|
+
if lexer.tag == 'make'
|
86
|
+
code.gsub! /^ /, "\t"
|
87
|
+
end
|
88
|
+
|
89
|
+
formatter = ::Rouge::Formatters::HTMLLegacy.new(:css_class => "Vlt-prism--dark language-#{lexer.tag} Vlt-prism--copy-disabled")
|
90
|
+
formatter.format(lexer.lex(code))
|
91
|
+
end
|
78
92
|
end
|
79
93
|
end
|
80
94
|
end
|
@@ -4,29 +4,29 @@ module Nexmo
|
|
4
4
|
class Java < Base
|
5
5
|
def self.dependencies(deps)
|
6
6
|
{
|
7
|
-
'text' => t('services.code_snippet_renderer.add_instructions_to_file', file: 'build.gradle'),
|
7
|
+
'text' => ::I18n.t('services.code_snippet_renderer.add_instructions_to_file', file: 'build.gradle'),
|
8
8
|
'code' => deps.map { |d| "compile '#{d.gsub('@latest', '5.2.1')}'" }.join('<br />'),
|
9
9
|
'type' => 'groovy',
|
10
10
|
}
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def self.run_command(_command, filename, file_path)
|
14
14
|
package = file_path.gsub('.repos/nexmo/nexmo-java-code-snippets/src/main/java/', '').tr('/', '.').gsub(filename, '')
|
15
15
|
file = filename.gsub('.java', '')
|
16
16
|
main = "#{package}#{filename.gsub('.java', '')}"
|
17
17
|
chomped_package = package.chomp('.')
|
18
|
-
|
19
|
-
::I18n.t('.run_command', chomped_package: chomped_package, package: package, main: main, file: file)
|
18
|
+
|
19
|
+
::I18n.t('services.code_snippet_renderer.java.run_command', chomped_package: chomped_package, package: package, main: main, file: file)
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def self.create_instructions(filename)
|
23
|
-
::I18n.t('.create_instructions', file: filename.gsub('.java', ''))
|
23
|
+
::I18n.t('services.code_snippet_renderer.java.create_instructions', file: filename.gsub('.java', ''))
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
def self.add_instructions(filename)
|
27
|
-
::I18n.t('.add_instructions', file: filename.gsub('.java', ''))
|
27
|
+
::I18n.t('services.code_snippet_renderer.java.add_instructions', file: filename.gsub('.java', ''))
|
28
28
|
end
|
29
29
|
end
|
30
|
-
end
|
30
|
+
end
|
31
31
|
end
|
32
|
-
end
|
32
|
+
end
|
@@ -2,5 +2,5 @@
|
|
2
2
|
<div class="copy-button" data-lang="<%= lang %>" data-block="<%= config['source'] %>" data-section="code">
|
3
3
|
<%= octicon "clippy", :class => 'top left' %> <span><%= ::I18n.t('.copy-to-clipboad') %></span>
|
4
4
|
</div>
|
5
|
-
<pre class="
|
5
|
+
<pre class="language-<%= lexer.tag %> main-code Vlt-prism--dark Vlt-prism--copy-disabled"><code><%= highlighted_code_source %></code></pre>
|
6
6
|
</div>
|
@@ -10,7 +10,7 @@
|
|
10
10
|
<div class="copy-button" data-lang="<%= lang %>" data-block="<%= config['source'] %>" data-section="configure">
|
11
11
|
<%= octicon "clippy", :class => 'top left' %> <span>Copy to Clipboard</span>
|
12
12
|
</div>
|
13
|
-
<pre class="
|
13
|
+
<pre class="Vlt-prism--dark language-<%= lexer.tag %> Vlt-prism--copy-disabled"><code><%= highlighted_client_source %></code></pre>
|
14
14
|
</div>
|
15
15
|
|
16
16
|
<p><a data-section="configure" data-lang="<%= lang %>" data-block="<%= config['source'] %>" href="<%= client_url %>">View full source</a></p>
|
@@ -4,7 +4,7 @@
|
|
4
4
|
<div id="acc<%= id %>" class="Vlt-js-accordion__content Vlt-accordion__content Vlt-accordion__content--noborder">
|
5
5
|
<p><%=deps['text']%></p>
|
6
6
|
<% if deps['code'] %>
|
7
|
-
<pre class="
|
7
|
+
<pre class="Vlt-prism--dark language-<%= deps['type'] || 'bash' %> dependencies command-line Vlt-prism--copy-disabled" data-prompt='$'><code><%=deps['code']%></code></pre>
|
8
8
|
<% end %>
|
9
9
|
</div>
|
10
10
|
</div>
|
@@ -5,7 +5,7 @@
|
|
5
5
|
<div class="copy-button" data-lang="<%= lang %>" data-block="<%= config['source'] %>" data-section="code">
|
6
6
|
<%= octicon "clippy", :class => 'top left' %> <span><%= ::I18n.t('.code_snippets.copy_to_clipboard') %></span>
|
7
7
|
</div>
|
8
|
-
<pre class="
|
8
|
+
<pre class="language-<%= lexer.tag %> main-code Vlt-prism--dark Vlt-prism--copy-disabled"><code><%= highlighted_code_source %></code></pre>
|
9
9
|
|
10
10
|
</div>
|
11
11
|
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nexmo_markdown_renderer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nexmo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-03-
|
11
|
+
date: 2020-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: banzai
|
@@ -329,7 +329,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
329
329
|
- !ruby/object:Gem::Version
|
330
330
|
version: '0'
|
331
331
|
requirements: []
|
332
|
-
rubygems_version: 3.0.
|
332
|
+
rubygems_version: 3.0.3
|
333
333
|
signing_key:
|
334
334
|
specification_version: 4
|
335
335
|
summary: Middleware to render Markdown Documents in Nexmo Developer Platform.
|