nexmo_markdown_renderer 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 746e034b9ef6f00f3004f92196e71c3b670cc677f931fa67cdf42252620fdc37
4
- data.tar.gz: 680b76bcce7a745ee748cb4e6a5db8d2724a2727271651179a18d43dee0a1701
3
+ metadata.gz: 7e21f8aa58664f86273978415744faa05026ec551b1bd99a6e0f7b54f7872eff
4
+ data.tar.gz: ec3ea04ec8714d98ec8aa17d318999142477d58dc7a8904b96396dc982825572
5
5
  SHA512:
6
- metadata.gz: 10cfc564ff9a5544f522fcd98a77afdae52c9140846ae199db099615218ccc8be61360f27ba1c73b46fb0326c4a613ddf625b7bef19df45ba5564a022c1fb1ad
7
- data.tar.gz: 31bd733108f5ebcb4608cc9bfbd7b7669902ce66cc94a922abd58a7819c6843d082a710307920efd1450b6b737b53490bcdf5c7f34ead2e07c9d2bdcb704618c
6
+ metadata.gz: 9788f8896e42b0e4f84084c231669a15a588c6335b3be8df01a60f3f73b3736a6d8ec832e650943c5de8506564893a78f3c368eca93c75c1a95d4fce1cb8d267
7
+ data.tar.gz: 5dc5835781068ada21d52a4108381222bca7293bdff94a26668dee3110310ba61c6e97d9a1b280c04b31253cca575bdb8190aecc44851c6c623d95b1675cc9b0
@@ -129,6 +129,7 @@ en:
129
129
  write_the_code: Write the code
130
130
  configure_client:
131
131
  initialize_dependencies: Initialize your dependencies
132
+ import_dependencies: Import dependencies
132
133
  application_messages_dispatch:
133
134
  no_application_html: If you do not have an application you can <a href="/messages/code-snippets/create-an-application">create one</a>. Make sure you also <a href="/messages/code-snippets/configure-webhooks">configure your webhooks</a>.
134
135
  application_rtc:
@@ -38,6 +38,15 @@ require_relative 'nexmo_markdown_renderer/models/tutorial/prerequisite'
38
38
  require_relative 'nexmo_markdown_renderer/models/tutorial/task'
39
39
  require_relative 'nexmo_markdown_renderer/models/use_case'
40
40
 
41
+ require_relative 'nexmo_markdown_renderer/filters/code_snippet/binding'
42
+ require_relative 'nexmo_markdown_renderer/filters/code_snippet/renderable'
43
+ require_relative 'nexmo_markdown_renderer/filters/code_snippet/create_application'
44
+ require_relative 'nexmo_markdown_renderer/filters/code_snippet/initialize_dependencies'
45
+ require_relative 'nexmo_markdown_renderer/filters/code_snippet/instructions'
46
+ require_relative 'nexmo_markdown_renderer/filters/code_snippet/import_dependencies'
47
+ require_relative 'nexmo_markdown_renderer/filters/code_snippet/install_dependencies'
48
+ require_relative 'nexmo_markdown_renderer/filters/code_snippet/run'
49
+
41
50
  Dir[File.join(__dir__, 'nexmo_markdown_renderer/filters/concerns', '*.rb')].each { |file| require_relative file }
42
51
  Dir[File.join(__dir__, 'nexmo_markdown_renderer/filters', '*.rb')].each { |file| require_relative file }
43
52
  Dir[File.join(__dir__, 'nexmo_markdown_renderer/filters/i18n', '*.rb')].each { |file| require_relative file }
@@ -0,0 +1,41 @@
1
+ module Nexmo
2
+ module Markdown
3
+ module Filters
4
+ module CodeSnippet
5
+ class Binding
6
+ def initialize(config)
7
+ @config = config
8
+ end
9
+
10
+ def language
11
+ @language ||= @config['language']
12
+ end
13
+
14
+ def lexer
15
+ @lexer ||= Nexmo::Markdown::CodeLanguage.find(language).lexer
16
+ end
17
+
18
+ def lang
19
+ @lang ||= @config['title'].delete('.')
20
+ end
21
+
22
+ def unindent
23
+ @unindent ||= @config['unindent']
24
+ end
25
+
26
+ def file_name
27
+ @file_name ||= @config['file_name']
28
+ end
29
+
30
+ def source
31
+ @source ||= @config['source']
32
+ end
33
+
34
+ def import_dependencies?
35
+ @config['import_dependencies']
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,61 @@
1
+ module Nexmo
2
+ module Markdown
3
+ module Filters
4
+ module CodeSnippet
5
+ class CreateApplication
6
+ include Renderable
7
+
8
+ NGROK_URL = 'http://demo.ngrok.io'.freeze
9
+ EXAMPLE_URL = 'https://example.com'.freeze
10
+
11
+ attr_reader :app
12
+
13
+ def initialize(app)
14
+ @app = app
15
+ end
16
+
17
+ def base_url
18
+ @base_url ||= @app['disable_ngrok'] && EXAMPLE_URL || NGROK_URL
19
+ end
20
+
21
+ def name
22
+ @name ||= @app['name'] || 'ExampleProject'
23
+ end
24
+
25
+ def type
26
+ # We should remove this default once we're sure that all Code Snippets
27
+ # have a type set e.g audit
28
+ @type ||= @app.fetch('type', 'voice')
29
+ end
30
+
31
+ def event_url
32
+ @event_url ||= @app.fetch('event_url', "#{base_url}/webhooks/events")
33
+ end
34
+
35
+ def answer_url
36
+ @answer_url ||= @app.fetch('answer_url', "#{base_url}/webhooks/answer")
37
+ end
38
+
39
+ def partial
40
+ @partial ||= begin
41
+ case type
42
+ when 'voice', 'rtc'
43
+ File.read("#{GEM_ROOT}/lib/nexmo_markdown_renderer/views/code_snippets/_application_#{type}.html.erb")
44
+ when 'messages', 'dispatch'
45
+ File.read("#{GEM_ROOT}/lib/nexmo_markdown_renderer/views/code_snippets/_application_messages_dispatch.html.erb")
46
+ else
47
+ raise "Invalid application type when creating code snippet: '#{type}'"
48
+ end
49
+ end
50
+ end
51
+
52
+ def render
53
+ return '' unless @app
54
+
55
+ ERB.new(partial).result(binding)
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,38 @@
1
+ module Nexmo
2
+ module Markdown
3
+ module Filters
4
+ module CodeSnippet
5
+ class ImportDependencies
6
+ include OcticonsHelper
7
+ include Renderable
8
+
9
+ attr_reader :config
10
+
11
+ def initialize(config, snippet)
12
+ @config = config
13
+ @snippet = snippet
14
+ end
15
+
16
+ def partial
17
+ @partial ||= File.read("#{GEM_ROOT}/lib/nexmo_markdown_renderer/views/code_snippets/_import_dependencies.html.erb")
18
+ end
19
+
20
+ def highlighted_import_source
21
+ @highlighted_import_source ||= ::Nexmo::Markdown::Utils.generate_code_block(
22
+ language,
23
+ @config,
24
+ unindent
25
+ )
26
+ end
27
+
28
+ def render
29
+ return '' unless @config
30
+
31
+ create_instructions = renderer.create_instructions(file_name).render_markdown
32
+ ERB.new(partial).result(binding)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,41 @@
1
+ module Nexmo
2
+ module Markdown
3
+ module Filters
4
+ module CodeSnippet
5
+ class InitializeDependencies
6
+ include OcticonsHelper
7
+ include Renderable
8
+
9
+ def initialize(config, snippet)
10
+ @config = config
11
+ @snippet = snippet
12
+ end
13
+
14
+ def partial
15
+ @partial ||= File.read("#{GEM_ROOT}/lib/nexmo_markdown_renderer/views/code_snippets/_configure_client.html.erb")
16
+ end
17
+
18
+ def highlighted_client_source
19
+ @highlighted_client_source ||= ::Nexmo::Markdown::Utils.generate_code_block(
20
+ language,
21
+ @config,
22
+ unindent
23
+ )
24
+ end
25
+
26
+ def render
27
+ return '' unless @config
28
+
29
+ create_instructions = if import_dependencies?
30
+ renderer.add_instructions(file_name).render_markdown
31
+ else
32
+ renderer.create_instructions(file_name).render_markdown
33
+ end
34
+
35
+ ERB.new(partial).result(binding)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,38 @@
1
+ module Nexmo
2
+ module Markdown
3
+ module Filters
4
+ module CodeSnippet
5
+ class InstallDependencies
6
+ include Renderable
7
+
8
+ def initialize(config, snippet)
9
+ @config = config
10
+ @snippet = snippet
11
+ end
12
+
13
+ def partial
14
+ @partial ||= File.read("#{GEM_ROOT}/lib/nexmo_markdown_renderer/views/code_snippets/_dependencies.html.erb")
15
+ end
16
+
17
+ def title
18
+ @title ||= begin
19
+ # The only valid dependency for curl examples is `JWT`
20
+ if @config.map(&:upcase).include?('JWT')
21
+ ::I18n.t('filters.generate_your_jwt')
22
+ else
23
+ ::I18n.t('filters.install_dependencies')
24
+ end
25
+ end
26
+ end
27
+
28
+ def render
29
+ return '' unless @config
30
+
31
+ deps = renderer.dependencies(@config)
32
+ ERB.new(partial).result(binding)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,46 @@
1
+ module Nexmo
2
+ module Markdown
3
+ module Filters
4
+ module CodeSnippet
5
+ class Instructions
6
+ include OcticonsHelper
7
+ include Renderable
8
+
9
+ attr_reader :config
10
+
11
+ def initialize(config, snippet)
12
+ @config = config
13
+ @snippet = snippet
14
+ end
15
+
16
+ def partial
17
+ @partial ||= begin
18
+ if @config['code_only']
19
+ File.read("#{GEM_ROOT}/lib/nexmo_markdown_renderer/views/code_snippets/_code_only.html.erb")
20
+ else
21
+ File.read("#{GEM_ROOT}/lib/nexmo_markdown_renderer/views/code_snippets/_write_code.html.erb")
22
+ end
23
+ end
24
+ end
25
+
26
+ def highlighted_code_source
27
+ @highlighted_code_source ||= ::Nexmo::Markdown::Utils.generate_code_block(
28
+ language,
29
+ @config['code'],
30
+ unindent
31
+ )
32
+ end
33
+
34
+ def source_url
35
+ @source_url ||= ::Nexmo::Markdown::Utils.generate_source_url(@config['code'])
36
+ end
37
+
38
+ def render
39
+ add_instructions = renderer.add_instructions(file_name).render_markdown
40
+ ERB.new(partial).result(binding)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,62 @@
1
+ require 'forwardable'
2
+
3
+ module Nexmo
4
+ module Markdown
5
+ module Filters
6
+ module CodeSnippet
7
+ module Renderable
8
+ extend Forwardable
9
+
10
+ def_delegators :@snippet, :language, :lang, :renderer, :lexer, :unindent, :file_name, :source, :import_dependencies?
11
+
12
+ def id
13
+ @id ||= SecureRandom.hex
14
+ end
15
+
16
+ def client_url
17
+ @client_url ||= ::Nexmo::Markdown::Utils.generate_source_url(@config)
18
+ end
19
+
20
+ def renderer
21
+ case normalized_language
22
+ when 'curl'
23
+ Nexmo::Markdown::CodeSnippetRenderer::Curl
24
+ when 'node'
25
+ Nexmo::Markdown::CodeSnippetRenderer::Javascript
26
+ when 'javascript'
27
+ Nexmo::Markdown::CodeSnippetRenderer::Javascript
28
+ when 'java'
29
+ Nexmo::Markdown::CodeSnippetRenderer::Java
30
+ when 'dotnet'
31
+ Nexmo::Markdown::CodeSnippetRenderer::Dotnet
32
+ when 'python'
33
+ Nexmo::Markdown::CodeSnippetRenderer::Python
34
+ when 'ruby'
35
+ Nexmo::Markdown::CodeSnippetRenderer::Ruby
36
+ when 'php'
37
+ Nexmo::Markdown::CodeSnippetRenderer::Php
38
+ when 'android'
39
+ Nexmo::Markdown::CodeSnippetRenderer::Android
40
+ when 'kotlin'
41
+ Nexmo::Markdown::CodeSnippetRenderer::Kotlin
42
+ when 'objective_c'
43
+ Nexmo::Markdown::CodeSnippetRenderer::ObjectiveC
44
+ when 'swift'
45
+ Nexmo::Markdown::CodeSnippetRenderer::Swift
46
+ else
47
+ raise "Unknown language: #{normalized_language}"
48
+ end
49
+ end
50
+
51
+ def normalized_language
52
+ if language == 'csharp'
53
+ 'dotnet'
54
+ else
55
+ language
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,29 @@
1
+ module Nexmo
2
+ module Markdown
3
+ module Filters
4
+ module CodeSnippet
5
+ class Run
6
+ include OcticonsHelper
7
+ include Renderable
8
+
9
+ def initialize(config, snippet)
10
+ @config = config
11
+ @snippet = snippet
12
+ end
13
+
14
+ def run_command
15
+ @run_command ||= begin
16
+ if @config['run_command']
17
+ @config['run_command'].gsub('{filename}', file_name)
18
+ end
19
+ end
20
+ end
21
+
22
+ def render
23
+ renderer.run_command(run_command, file_name, @config['code']['source']).to_s
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -5,183 +5,46 @@ module Nexmo
5
5
 
6
6
  def call(input)
7
7
  input.gsub(/```single_code_snippet(.+?)```/m) do |_s|
8
- config = YAML.safe_load($1)
8
+ @config = YAML.safe_load($1)
9
+ @binding = ::Nexmo::Markdown::Filters::CodeSnippet::Binding.new(@config)
9
10
 
10
- @renderer = get_renderer(config['language'])
11
+ return instructions if @config['code_only']
11
12
 
12
- lexer = Nexmo::Markdown::CodeLanguage.find(config['language']).lexer
13
- lang = config['title'].delete('.')
14
-
15
- application_html = generate_application_block(config['application'])
16
-
17
- # Read the client
18
- if config['client']
19
- highlighted_client_source = generate_code_block(config['language'], config['client'], config['unindent'])
20
- end
21
-
22
- # Read the code
23
- highlighted_code_source = generate_code_block(config['language'], config['code'], config['unindent'])
24
-
25
- dependency_html = ''
26
- if config['dependencies']
27
- dependency_html = generate_dependencies(lexer.tag, config['dependencies'])
28
- end
29
-
30
- source_url = generate_source_url(config['code'])
31
-
32
- client_html = ''
33
- if highlighted_client_source
34
- client_url = generate_source_url(config['client'])
35
- id = SecureRandom.hex
36
- create_instructions = @renderer.create_instructions(config['file_name']).render_markdown
37
- erb = File.read("#{GEM_ROOT}/lib/nexmo_markdown_renderer/views/code_snippets/_configure_client.html.erb")
38
- client_html = ERB.new(erb).result(binding)
39
- end
40
-
41
- add_instructions = @renderer.add_instructions(config['file_name']).render_markdown
42
- if config['code_only']
43
- erb = File.read("#{GEM_ROOT}/lib/nexmo_markdown_renderer/views/code_snippets/_code_only.html.erb")
44
- else
45
- erb = File.read("#{GEM_ROOT}/lib/nexmo_markdown_renderer/views/code_snippets/_write_code.html.erb")
46
- end
47
-
48
- code_html = ERB.new(erb).result(binding)
49
-
50
- return code_html if config['code_only']
51
-
52
- config['run_command'] = config['run_command'].gsub('{filename}', config['file_name']) if config['run_command']
53
- run_html = @renderer.run_command(config['run_command'], config['file_name'], config['code']['source']).to_s
13
+ "#{prerequisites}#{instructions}#{run}"
14
+ end
15
+ end
54
16
 
55
- prereqs = (application_html + dependency_html + client_html).strip
17
+ def prerequisites
18
+ @prerequisites ||= begin
19
+ prereqs = [application_html, install_dependencies, import_dependencies, initialize_dependencies].join.strip
56
20
  prereqs = "<h2>#{::I18n.t('.filters.prerequisites')}</h2>#{prereqs}" unless prereqs.empty?
57
- prereqs + code_html + run_html
21
+ prereqs
58
22
  end
59
23
  end
60
24
 
61
- private
62
-
63
- def highlight(source, lexer)
64
- formatter = Rouge::Formatters::HTML.new
65
- formatter.format(lexer.lex(source))
25
+ def application_html
26
+ @application_html ||= ::Nexmo::Markdown::Filters::CodeSnippet::CreateApplication.new(@config['application']).render
66
27
  end
67
28
 
68
- def generate_code_block(language, input, unindent)
69
- filename = "#{Nexmo::Markdown::Config.docs_base_path}/#{input['source']}"
70
- return '' unless input
71
- raise "CodeSnippetFilter - Could not load #{filename} for language #{language}" unless File.exist?(filename)
72
-
73
- code = File.read(filename)
74
- lexer = Nexmo::Markdown::CodeLanguage.find(language).lexer
75
-
76
- total_lines = code.lines.count
77
-
78
- # Minus one since lines are not zero-indexed
79
- from_line = (input['from_line'] || 1) - 1
80
- to_line = (input['to_line'] || total_lines) - 1
81
-
82
- code = code.lines[from_line..to_line].join
83
- code.unindent! if unindent
84
-
85
- highlight(code, lexer)
29
+ def install_dependencies
30
+ @install_dependencies ||= ::Nexmo::Markdown::Filters::CodeSnippet::InstallDependencies.new(@config['dependencies'], @binding).render
86
31
  end
87
32
 
88
- def get_renderer(language)
89
- language = 'dotnet' if language == 'csharp'
90
- case language
91
- when 'curl'
92
- Nexmo::Markdown::CodeSnippetRenderer::Curl
93
- when 'node'
94
- Nexmo::Markdown::CodeSnippetRenderer::Javascript
95
- when 'javascript'
96
- Nexmo::Markdown::CodeSnippetRenderer::Javascript
97
- when 'java'
98
- Nexmo::Markdown::CodeSnippetRenderer::Java
99
- when 'dotnet'
100
- Nexmo::Markdown::CodeSnippetRenderer::Dotnet
101
- when 'python'
102
- Nexmo::Markdown::CodeSnippetRenderer::Python
103
- when 'ruby'
104
- Nexmo::Markdown::CodeSnippetRenderer::Ruby
105
- when 'php'
106
- Nexmo::Markdown::CodeSnippetRenderer::Php
107
- when 'android'
108
- Nexmo::Markdown::CodeSnippetRenderer::Android
109
- when 'kotlin'
110
- Nexmo::Markdown::CodeSnippetRenderer::Kotlin
111
- when 'objective_c'
112
- Nexmo::Markdown::CodeSnippetRenderer::ObjectiveC
113
- when 'swift'
114
- Nexmo::Markdown::CodeSnippetRenderer::Swift
115
- else
116
- raise "Unknown language: #{language}"
117
- end
33
+ def import_dependencies
34
+ @import_dependencies ||= ::Nexmo::Markdown::Filters::CodeSnippet::ImportDependencies.new(@config['import_dependencies'], @binding).render
118
35
  end
119
36
 
120
- def generate_dependencies(language, dependencies)
121
- # The only valid dependency for curl examples is `JWT`
122
- if dependencies.map(&:upcase).include?('JWT')
123
- title = ::I18n.t('filters.generate_your_jwt')
124
- else
125
- title = ::I18n.t('filters.install_dependencies')
126
- end
127
- deps = @renderer.dependencies(dependencies)
128
- id = SecureRandom.hex
129
- erb = File.read("#{GEM_ROOT}/lib/nexmo_markdown_renderer/views/code_snippets/_dependencies.html.erb")
130
- ERB.new(erb).result(binding)
37
+ def initialize_dependencies
38
+ @initialize_dependencies ||= ::Nexmo::Markdown::Filters::CodeSnippet::InitializeDependencies.new(@config['client'], @binding).render
131
39
  end
132
40
 
133
- def generate_application_block(app)
134
- return '' unless app
135
-
136
- base_url = 'http://demo.ngrok.io'
137
- base_url = 'https://example.com' if app['disable_ngrok']
138
-
139
- app['name'] = 'ExampleProject' unless app['name']
140
-
141
- # We should remove this default once we're sure that all Code Snippets
142
- # have a type set e.g audit
143
- app['type'] ||= 'voice'
144
-
145
- if ['voice', 'rtc'].include? app['type']
146
- app['event_url'] = "#{base_url}/webhooks/events" unless app['event_url']
147
- app['answer_url'] = "#{base_url}/webhooks/answer" unless app['answer_url']
148
- erb = File.read("#{GEM_ROOT}/lib/nexmo_markdown_renderer/views/code_snippets/_application_#{app['type']}.html.erb")
149
- elsif ['messages', 'dispatch'].include? app['type']
150
- erb = File.read("#{GEM_ROOT}/lib/nexmo_markdown_renderer/views/code_snippets/_application_messages_dispatch.html.erb")
151
- else
152
- raise "Invalid application type when creating code snippet: '#{app['type']}'"
153
- end
154
-
155
- id = SecureRandom.hex
156
-
157
- ERB.new(erb).result(binding)
41
+ def instructions
42
+ @instructions ||= ::Nexmo::Markdown::Filters::CodeSnippet::Instructions.new(@config, @binding).render
158
43
  end
159
44
 
160
- def generate_source_url(code)
161
- # Source example: .repos/nexmo/nexmo-java-code-snippets/ExampleClass.java
162
- # Direct link on GitHub is in form https://github.com/nexmo/nexmo-java-code-snippets/blob/master/ExampleClass.java
163
- start_section = 'https://github.com'
164
-
165
- # Insert "blob/master" and strip ".repos"
166
- repo_path = '\\0blob/master/'
167
- file_section = code['source'].sub('.repos', '').sub(%r{(-quickstart|-code-snippets|-code-snippets)/}, repo_path)
168
-
169
- # Line highlighting
170
- line_section = ''
171
- if code['from_line']
172
- line_section += "#L#{code['from_line']}"
173
- if code['to_line']
174
- # If we've provided a to_line, use that
175
- line_section += "-L#{code['to_line']}" if code['to_line']
176
- else
177
- # By default we read to the end of the file
178
- line_section += "-L#{File.read(code['source']).lines.count}"
179
- end
180
- end
181
-
182
- start_section + file_section + line_section
45
+ def run
46
+ @run ||= ::Nexmo::Markdown::Filters::CodeSnippet::Run.new(@config, @binding).render
183
47
  end
184
48
  end
185
-
186
49
  end
187
50
  end
@@ -50,6 +50,8 @@ module Nexmo
50
50
  tab_link.content = content[:tab_title]
51
51
  elsif content[:language].key == 'objective_c' || content[:language].key == 'swift'
52
52
  tab_link.inner_html = "<svg><use xlink:href=\"/assets/images/brands/ios.svg#ios\" /></svg><span>" + content[:tab_title] + '</span>'
53
+ elsif content[:language].key == 'kotlin'
54
+ tab_link.inner_html = "<img class=\"Vlt-icon Vlt-icon--small\" src=\"/assets/images/brands/kotlin.svg#kotlin\" /><span>" + content[:tab_title] + '</span>'
53
55
  else
54
56
  tab_link.inner_html = "<svg><use xlink:href=\"/assets/images/brands/#{content[:language].key}.svg##{content[:language].key}\" /></svg><span>" + content[:tab_title] + '</span>'
55
57
  end
@@ -0,0 +1,50 @@
1
+ module Nexmo
2
+ module Markdown
3
+ class Utils
4
+ def self.generate_source_url(code)
5
+ # Source example: .repos/nexmo/nexmo-java-code-snippets/ExampleClass.java
6
+ # Direct link on GitHub is in form https://github.com/nexmo/nexmo-java-code-snippets/blob/master/ExampleClass.java
7
+ start_section = 'https://github.com'
8
+
9
+ # Insert "blob/master" and strip ".repos"
10
+ repo_path = '\\0blob/master/'
11
+ file_section = code['source'].sub('.repos', '').sub(%r{(-quickstart|-code-snippets|-code-snippets)/}, repo_path)
12
+
13
+ # Line highlighting
14
+ line_section = ''
15
+ if code['from_line']
16
+ line_section += "#L#{code['from_line']}"
17
+ if code['to_line']
18
+ # If we've provided a to_line, use that
19
+ line_section += "-L#{code['to_line']}" if code['to_line']
20
+ else
21
+ # By default we read to the end of the file
22
+ line_section += "-L#{File.read(code['source']).lines.count}"
23
+ end
24
+ end
25
+
26
+ start_section + file_section + line_section
27
+ end
28
+
29
+ def self.generate_code_block(language, input, unindent)
30
+ return '' unless input
31
+ filename = "#{Nexmo::Markdown::Config.docs_base_path}/#{input['source']}"
32
+ raise "CodeSnippetFilter - Could not load #{filename} for language #{language}" unless File.exist?(filename)
33
+
34
+ code = File.read(filename)
35
+ lexer = Nexmo::Markdown::CodeLanguage.find(language).lexer
36
+
37
+ total_lines = code.lines.count
38
+
39
+ # Minus one since lines are not zero-indexed
40
+ from_line = (input['from_line'] || 1) - 1
41
+ to_line = (input['to_line'] || total_lines) - 1
42
+
43
+ code = code.lines[from_line..to_line].join
44
+ code.unindent! if unindent
45
+ formatter = Rouge::Formatters::HTML.new
46
+ formatter.format(lexer.lex(code))
47
+ end
48
+ end
49
+ end
50
+ end
@@ -15,7 +15,7 @@
15
15
  <p><%= ::I18n.t('.code_snippets.nexmo_needs_to_connect_html') %></p>
16
16
  <% end %>
17
17
 
18
- <pre class="Vlt-prism--dark dependencies command-line Vlt-prism--copy-disabled language-bash" data-prompt='$'><code>nexmo app:create "<%=app['name'] %>" <%=app['answer_url'] %> <%=app['event_url'] %> --keyfile=private.key --type=rtc</code></pre>
18
+ <pre class="Vlt-prism--dark dependencies command-line Vlt-prism--copy-disabled language-bash" data-prompt='$'><code>nexmo app:create "<%= name %>" <%= answer_url %> <%= event_url %> --keyfile=private.key --type=rtc</code></pre>
19
19
 
20
20
  <h4><%= ::I18n.t('.code_snippets.application_rtc.use_your_existing_app') %></h4>
21
21
 
@@ -18,7 +18,7 @@
18
18
  <p><%= ::I18n.t('.code_snippets.nexmo_needs_to_connect_html') %></p>
19
19
  <% end %>
20
20
 
21
- <pre class="Vlt-prism--dark dependencies command-line Vlt-prism--copy-disabled language-bash" data-prompt='$'><code>nexmo app:create "<%=app['name'] %>" <%=app['answer_url'] %> <%=app['event_url'] %> --keyfile private.key</code></pre>
21
+ <pre class="Vlt-prism--dark dependencies command-line Vlt-prism--copy-disabled language-bash" data-prompt='$'><code>nexmo app:create "<%= name %>" <%= answer_url %> <%= event_url %> --keyfile private.key</code></pre>
22
22
  <% end %>
23
23
  </div>
24
24
  </div>
@@ -7,13 +7,13 @@
7
7
  <%= create_instructions %>
8
8
  <div class="copy-wrapper">
9
9
 
10
- <div class="copy-button" data-lang="<%= lang %>" data-block="<%= config['source'] %>" data-section="configure">
10
+ <div class="copy-button" data-lang="<%= lang %>" data-block="<%= source %>" data-section="configure">
11
11
  <%= octicon "clippy", :class => 'top left' %> <span>Copy to Clipboard</span>
12
12
  </div>
13
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
- <p><a data-section="configure" data-lang="<%= lang %>" data-block="<%= config['source'] %>" href="<%= client_url %>">View full source</a></p>
16
+ <p><a data-section="configure" data-lang="<%= lang %>" data-block="<%= source %>" href="<%= client_url %>">View full source</a></p>
17
17
 
18
18
  </div>
19
19
  </div>
@@ -0,0 +1,18 @@
1
+ <div class="Vlt-box Vlt-box--lesspadding Nxd-accordion-emphasis">
2
+ <h5 class="Vlt-js-accordion__trigger Vlt-accordion__trigger" data-accordion="acc<%= id %>" tabindex="0">
3
+ <%= ::I18n.t('.code_snippets.configure_client.import_dependencies') %>
4
+ </h5>
5
+
6
+ <div id="acc<%=id %>" class="Vlt-js-accordion__content Vlt-accordion__content Vlt-accordion__content--noborder">
7
+ <%= create_instructions %>
8
+ <div class="copy-wrapper">
9
+
10
+ <div class="copy-button" data-lang="<%= lang %>" data-block="<%= config['source'] %>" data-section="configure">
11
+ <%= octicon "clippy", :class => 'top left' %> <span>Copy to Clipboard</span>
12
+ </div>
13
+ <pre class="Vlt-prism--dark language-<%= lexer.tag %> Vlt-prism--copy-disabled"><code><%= highlighted_import_source %></code></pre>
14
+ </div>
15
+
16
+ <p><a data-section="configure" data-lang="<%= lang %>" data-block="<%= config['source'] %>" href="<%= client_url %>">View full source</a></p>
17
+ </div>
18
+ </div>
@@ -1,7 +1,7 @@
1
1
  # :nocov:
2
2
  module Nexmo
3
3
  module Markdown
4
- VERSION = '0.3.1'
4
+ VERSION = '0.4.0'
5
5
  end
6
6
  end
7
7
  # :nocov:
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.3.1
4
+ version: 0.4.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-04-21 00:00:00.000000000 Z
11
+ date: 2020-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: banzai
@@ -240,6 +240,14 @@ files:
240
240
  - lib/nexmo_markdown_renderer/filters/block_escape_filter.rb
241
241
  - lib/nexmo_markdown_renderer/filters/break_filter.rb
242
242
  - lib/nexmo_markdown_renderer/filters/code_filter.rb
243
+ - lib/nexmo_markdown_renderer/filters/code_snippet/binding.rb
244
+ - lib/nexmo_markdown_renderer/filters/code_snippet/create_application.rb
245
+ - lib/nexmo_markdown_renderer/filters/code_snippet/import_dependencies.rb
246
+ - lib/nexmo_markdown_renderer/filters/code_snippet/initialize_dependencies.rb
247
+ - lib/nexmo_markdown_renderer/filters/code_snippet/install_dependencies.rb
248
+ - lib/nexmo_markdown_renderer/filters/code_snippet/instructions.rb
249
+ - lib/nexmo_markdown_renderer/filters/code_snippet/renderable.rb
250
+ - lib/nexmo_markdown_renderer/filters/code_snippet/run.rb
243
251
  - lib/nexmo_markdown_renderer/filters/code_snippet_filter.rb
244
252
  - lib/nexmo_markdown_renderer/filters/code_snippet_list_filter.rb
245
253
  - lib/nexmo_markdown_renderer/filters/code_snippets_filter.rb
@@ -271,6 +279,7 @@ files:
271
279
  - lib/nexmo_markdown_renderer/filters/tooltip_filter.rb
272
280
  - lib/nexmo_markdown_renderer/filters/unfreeze_filter.rb
273
281
  - lib/nexmo_markdown_renderer/filters/use_case_list_filter.rb
282
+ - lib/nexmo_markdown_renderer/filters/utils.rb
274
283
  - lib/nexmo_markdown_renderer/initializers/doc_finder.rb
275
284
  - lib/nexmo_markdown_renderer/initializers/i18n.rb
276
285
  - lib/nexmo_markdown_renderer/initializers/redcarpet.rb
@@ -304,6 +313,7 @@ files:
304
313
  - lib/nexmo_markdown_renderer/views/code_snippets/_code_only.html.erb
305
314
  - lib/nexmo_markdown_renderer/views/code_snippets/_configure_client.html.erb
306
315
  - lib/nexmo_markdown_renderer/views/code_snippets/_dependencies.html.erb
316
+ - lib/nexmo_markdown_renderer/views/code_snippets/_import_dependencies.html.erb
307
317
  - lib/nexmo_markdown_renderer/views/code_snippets/_write_code.html.erb
308
318
  - lib/nexmo_markdown_renderer/views/code_snippets/list/plain.html.erb
309
319
  - lib/nexmo_markdown_renderer/views/concepts/list/plain.html.erb