nexmo_markdown_renderer 0.5.0 → 0.7.2

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: 4c9303cbd50b27ad02c68444daff15f65f3c3150f052bbfae839ee83e208f2f3
4
- data.tar.gz: 6598a455b0b1a5bc07e5f05b0425db82bcfd1d980f9a43e7b6455ec73e5c8258
3
+ metadata.gz: dd02562cf6db0de1c58951324ee7be71aeee914ea5bee6498ec12af45b78465e
4
+ data.tar.gz: 2a6ad3f742f90d3714c7a864d8f8a44060f6929ef824bb270251da45882b99b0
5
5
  SHA512:
6
- metadata.gz: 5c2bb7bf7351edeee752c0ee34f93c7ac2610df41ad78aa0642b7070b3f5a3675254343c52285de3f879c195a5ff78c186f2e7bcee60a712661b4b704a2d25dc
7
- data.tar.gz: d82606e5cb6920496674fd3aba4888e5b874b5dcbdad38f35f6c24ccbe1ef24c8069acb84afb338477df6068404f9c4b283ad9fdb694652cb6d689329110cf56
6
+ metadata.gz: 27b07ba6eebeb8023e0f541a8b99b4790935df5f45e79b691dc419c3f2f86019cdfa50212f0854d2eebec2a7a59d60f7739eb4c860994462b01943cc6be2834c
7
+ data.tar.gz: a6db2da85020d5661a24f1a33f7bd2217b2475e76d78c320b8eaadd87e066df6d5d3954d0a90d4a3bcf6e21087a1bc21714952a88c1bfa14f3f0cb93d3ab64be
@@ -2,9 +2,16 @@ module Nexmo
2
2
  module Markdown
3
3
  module Concerns
4
4
  module PrismCodeSnippet
5
+ include OcticonsHelper
6
+
5
7
  def code_snippet_body(lexer, body)
6
8
  <<~HEREDOC
7
- <pre class="#{prism_css_classes(lexer)}"><code>#{body}</code></pre>
9
+ <div class="copy-wrapper">
10
+ <div class="copy-button" data-lang="#{code_language_to_prism(lexer.tag)}" data-section="code">
11
+ #{octicon "clippy", :class => 'top left'} <span>#{::I18n.t('code_snippets.copy_to_clipboard') }</span>
12
+ </div>
13
+ <pre class="#{prism_css_classes(lexer)}"><code>#{body}</code></pre>
14
+ </div>
8
15
  HEREDOC
9
16
  end
10
17
 
@@ -4,9 +4,20 @@ module Nexmo
4
4
  class FrontmatterFilter < Banzai::Filter
5
5
  def call(input)
6
6
  input.gsub(/\A(---.+?---)/mo) do |frontmatter|
7
- frontmatter.gsub(/(\w*:)/) do |_key|
8
- "```#{$1}```"
7
+ output = frontmatter.gsub(/^languages:\n(^\s+- ([a-zA-Z]+)\n)+/) do |languages|
8
+ languages.gsub(/^\s+- ([a-zA-Z]+)\n+/) do |language|
9
+ " - ```#{$1}```\n\n"
10
+ end
9
11
  end
12
+ output = output.gsub(/^(\w*:)(.*)\n/) do |_key|
13
+ if $1 == "products:"
14
+ "```#{$1}#{$2}```\n\n"
15
+ else
16
+ "```#{$1}```#{$2}\n\n"
17
+ end
18
+ end
19
+
20
+ output
10
21
  end
11
22
  end
12
23
  end
@@ -1,17 +1,33 @@
1
- module Nexmo
1
+ module Nexmo
2
2
  module Markdown
3
3
  module I18n
4
4
  module Smartling
5
5
  class FrontmatterFilter < Banzai::Filter
6
6
  def call(input)
7
- input.gsub(/\A\*\*\* \*\* \* \*\* \*\*\*\n*([^-]*)\n*-+/m) do |_frontmatter|
8
- front = $1.gsub(/`(.*):`(.*)/) do |_config|
7
+ input.gsub(/\A\*\*\* \*\* \* \*\* \*\*\*\n*(.*?\n)!?(\*\*\* \*\* \* \*\* \*\*\*|----+)\n*/m) do |_frontmatter|
8
+ front = $1.gsub(/`products: (.*)`\n\n/) do |products|
9
+ "products: #{$1}\n\n"
10
+ end
11
+
12
+ front = front.gsub(/`(.*):`(.*)/) do |_config|
9
13
  "#{$1}: #{$2}"
10
14
  end
15
+
16
+ front = front.gsub(/languages: \n\n(.*)\n/m) do |_languages|
17
+ languages = $1.split("\n\n").map do |lang|
18
+ lang.gsub(/\* `(.*)`/) { |_l| " - #{$1}" }
19
+ end
20
+ <<~LANGUAGES
21
+ languages:
22
+ #{languages.join("\n")}
23
+ LANGUAGES
24
+ end
25
+
11
26
  <<~FRONTMATTER
12
27
  ---
13
28
  #{front}
14
29
  ---
30
+
15
31
  FRONTMATTER
16
32
  end
17
33
  end
@@ -91,8 +91,10 @@ module Nexmo
91
91
  code.gsub! /^ /, "\t"
92
92
  end
93
93
 
94
- formatter = ::Rouge::Formatters::HTMLLegacy.new(:css_class => prism_css_classes(lexer))
95
- formatter.format(lexer.lex(code))
94
+ formatter ||= Rouge::Formatters::HTML.new
95
+ highlighted_source = formatter.format(lexer.lex(code))
96
+
97
+ code_snippet_body(lexer, highlighted_source)
96
98
  end
97
99
  end
98
100
  end
@@ -0,0 +1,38 @@
1
+ module Nexmo
2
+ module Markdown
3
+ class SnippetVariablesFilter < Banzai::Filter
4
+ def call(input)
5
+ input.gsub(/```snippet_variables(.+?)```/m) do |_s|
6
+ config = YAML.safe_load(Regexp.last_match(1))
7
+
8
+ raise 'No variables provided' unless config
9
+ raise 'Must provide an array' unless config.is_a?(Array)
10
+
11
+ output = <<~HEREDOC
12
+ Key | Description
13
+ -- | --
14
+ HEREDOC
15
+ config.each do |key|
16
+
17
+ details = variables[key]
18
+ raise "#{key} is not a valid snippet variable" unless details
19
+
20
+ # We have some variables in the format TO_NUMBER.SMS etc, and we only want to render the first segment
21
+ # This can be multiple segments e.g. UUID.MODIFY.VOICE will be rendered as UUID
22
+ title = key.split('.').first
23
+
24
+ output += <<~HEREDOC
25
+ `#{title}` | #{details}
26
+ HEREDOC
27
+ end
28
+
29
+ output
30
+ end
31
+ end
32
+
33
+ def variables
34
+ @variables ||= YAML.safe_load(File.read("#{Nexmo::Markdown::Config.docs_base_path}/config/code_snippet_variables.yml"))
35
+ end
36
+ end
37
+ end
38
+ end
@@ -29,6 +29,7 @@ module Nexmo
29
29
  ConceptListFilter.new(options),
30
30
  LanguageFilter,
31
31
  ColumnsFilter,
32
+ SnippetVariablesFilter,
32
33
  MarkdownFilter.new(options),
33
34
 
34
35
  # As HTML
@@ -3,20 +3,20 @@ module Nexmo
3
3
  class CodeSnippet
4
4
  include ActiveModel::Model
5
5
  attr_accessor :title, :product, :category, :navigation_weight, :document_path, :url
6
-
6
+
7
7
  def self.by_product(product)
8
8
  all.select do |block|
9
9
  block.product == product
10
10
  end
11
11
  end
12
-
12
+
13
13
  def self.all
14
14
  blocks = files.map do |document_path|
15
15
  document = File.read(document_path)
16
16
  product = extract_product(document_path)
17
-
17
+
18
18
  frontmatter = YAML.safe_load(document)
19
-
19
+
20
20
  Nexmo::Markdown::CodeSnippet.new({
21
21
  title: frontmatter['title'],
22
22
  navigation_weight: frontmatter['navigation_weight'] || 999,
@@ -26,47 +26,50 @@ module Nexmo
26
26
  url: generate_url(document_path),
27
27
  })
28
28
  end
29
-
29
+
30
30
  blocks.sort_by(&:navigation_weight)
31
31
  end
32
-
32
+
33
33
  def self.generate_url(path)
34
34
  '/' + path.gsub(%r{#{origin}/\w{2}/}, '').gsub('.md', '')
35
35
  end
36
-
36
+
37
37
  def self.extract_product(path)
38
38
  # Remove the prefix
39
39
  path = path.gsub!(%r{#{origin}/\w{2}/}, '')
40
-
40
+
41
41
  # Each file is in the form code-snippets/<title>.md, so let's remove everything after code-snippets
42
42
  path = path.gsub(%r{/code-snippets/.*}, '')
43
-
43
+
44
44
  path
45
45
  end
46
-
46
+
47
47
  def self.extract_category(path)
48
48
  # Remove the prefix
49
49
  path = path.gsub(%r{#{origin}/\w{2}/}, '')
50
-
50
+
51
51
  # Each file is in the form code-snippets/<title>.md, so let's capture everything after code-snippets
52
52
  path = path.gsub(%r{.*/code-snippets/(.*)$}, '\1')
53
-
53
+
54
54
  parts = path.split('/')
55
55
  parts = parts[0...-1]
56
-
56
+
57
57
  return nil if parts.empty?
58
-
58
+
59
59
  parts.join('/').tr('-', ' ').humanize
60
60
  end
61
-
61
+
62
62
  def self.files
63
- Dir.glob("#{origin}/**/code-snippets/**/*.md")
63
+ root = "#{origin}/#{::I18n.default_locale}"
64
+ Dir.glob("#{root}/**/code-snippets/**/*.md").map do |path|
65
+ DocFinder.find(root: origin, document: path.gsub(root, ''), language: ::I18n.locale).path
66
+ end
64
67
  end
65
-
68
+
66
69
  def self.origin
67
70
  "#{Nexmo::Markdown::Config.docs_base_path}/_documentation"
68
71
  end
69
72
  end
70
-
73
+
71
74
  end
72
75
  end
@@ -6,8 +6,7 @@ module Nexmo
6
6
  def initialize(_options = {})
7
7
  super(
8
8
  I18n::Smartling::FrontmatterFilter,
9
- I18n::Smartling::EscapeFilter,
10
- I18n::Smartling::CodeBlockFilter
9
+ I18n::Smartling::EscapeFilter
11
10
  )
12
11
  end
13
12
  end
@@ -5,7 +5,7 @@ module Nexmo
5
5
  class Preprocessor < Banzai::Pipeline
6
6
  def initialize(_options = {})
7
7
  super(
8
- I18n::FrontmatterFilter
8
+ I18n::FrontmatterFilter,
9
9
  )
10
10
  end
11
11
  end
@@ -1,7 +1,7 @@
1
1
  # :nocov:
2
2
  module Nexmo
3
3
  module Markdown
4
- VERSION = '0.5.0'
4
+ VERSION = '0.7.2'
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.5.0
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nexmo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-05 00:00:00.000000000 Z
11
+ date: 2020-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: banzai
@@ -260,7 +260,6 @@ files:
260
260
  - lib/nexmo_markdown_renderer/filters/frontmatter_filter.rb
261
261
  - lib/nexmo_markdown_renderer/filters/heading_filter.rb
262
262
  - lib/nexmo_markdown_renderer/filters/i18n/frontmatter_filter.rb
263
- - lib/nexmo_markdown_renderer/filters/i18n/smartling/code_block_filter.rb
264
263
  - lib/nexmo_markdown_renderer/filters/i18n/smartling/escape_filter.rb
265
264
  - lib/nexmo_markdown_renderer/filters/i18n/smartling/frontmatter_filter.rb
266
265
  - lib/nexmo_markdown_renderer/filters/icon_filter.rb
@@ -276,6 +275,7 @@ files:
276
275
  - lib/nexmo_markdown_renderer/filters/php_inliner_filter.rb
277
276
  - lib/nexmo_markdown_renderer/filters/relative_link_filter.rb
278
277
  - lib/nexmo_markdown_renderer/filters/screenshot_filter.rb
278
+ - lib/nexmo_markdown_renderer/filters/snippet_variables_filter.rb
279
279
  - lib/nexmo_markdown_renderer/filters/tab_filter.rb
280
280
  - lib/nexmo_markdown_renderer/filters/techio_filter.rb
281
281
  - lib/nexmo_markdown_renderer/filters/tooltip_filter.rb
@@ -1,20 +0,0 @@
1
- module Nexmo
2
- module Markdown
3
- module I18n
4
- module Smartling
5
- class CodeBlockFilter < Banzai::Filter
6
- def call(input)
7
- input.gsub(/\n\n\s{4}(.*?)\n\n/m) do
8
- <<~CODE_BLOCK
9
-
10
- ````
11
- #{$1.split(/\n\s{4}/).join("\n")}
12
- ````
13
- CODE_BLOCK
14
- end
15
- end
16
- end
17
- end
18
- end
19
- end
20
- end