kwalify_to_json_schema 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dbf127bd97917313787d2e8627ab38a3ef403edbf32bf77af9b1be706525a71e
4
- data.tar.gz: 2b055ce7452bd80faa88b40c33d11644779f2ada1a2b7b63a0c7a1470fe55a2c
3
+ metadata.gz: 2e3e11103f87d4445e81beb29fe9b36d3dc17c0e03b399d2d088d9b1fa8504d8
4
+ data.tar.gz: c2e09d2aa94ceb1a5588f9e175b402ee11a3260d6b1441471eec1f304e7b29d6
5
5
  SHA512:
6
- metadata.gz: 03533367dd66aed289370d6aa7bfe03578dab440c59498bc030e8e4179d53917577e7064f12bc5e2ca30f32fda464fabf22c47424a70741a209669fef170d357
7
- data.tar.gz: 00a4daafa64abeebac771e70b8809ad3475a8e97ca4bc8d6dc26a92735e25b2cb9ab573b876d79127e87146b5efd48c60206b11b0ad25edeee71b00493b24c0a
6
+ metadata.gz: '09fd74c1ade4d0e9389d46ec6d93d40d9ce797a34bc951eaf0354c3dfe2f2464c2a2849e65b4dc0ebaa682648816eb16b0b79c91ba56f4815d0ac309473e28d8'
7
+ data.tar.gz: e78af6fbb9e4c2cd3e17e4168abd3615b5e798ffb318a737b5978a8613af4548a7ac4a36bae783e228b2d9314e0f4815ce6172ccb6a1b3789f8bdde491c89abb
@@ -25,11 +25,11 @@ module KwalifyToJsonSchema
25
25
 
26
26
  desc "convert KWALIFY_SCHEMA_FILE, RESULT_FILE",
27
27
  "Convert a Kwalify schema file to a JSON schema file. The result file extension will decide the format: .json or .yaml"
28
- option :issues_to_description,
29
- :type => :boolean,
30
- :default => false,
31
- :desc => "Will append any conversion issue to the schema description"
32
- option :custom_processing,
28
+ option(*Options.cli_option(Options::ID))
29
+ option(*Options.cli_option(Options::TITLE))
30
+ option(*Options.cli_option(Options::DESCRIPTION))
31
+ option(*Options.cli_option(Options::ISSUES_TO_DESCRIPTION))
32
+ option Options::CUSTOM_PROCESSING,
33
33
  :type => :string,
34
34
  :desc => <<~DESC
35
35
  Allows to provide a pre/post processing file on handled schemas.
@@ -38,10 +38,8 @@ module KwalifyToJsonSchema
38
38
  DESC
39
39
 
40
40
  def convert(kwalify_schema_file, result_file)
41
- opts = {
42
- Options::ISSUES_TO_DESCRIPTION => options[:issues_to_description],
43
- Options::CUSTOM_PROCESSING => custom_processing(options),
44
- }
41
+ opts = options.dup
42
+ opts[Options::CUSTOM_PROCESSING] = custom_processing(options)
45
43
  KwalifyToJsonSchema.convert_file(kwalify_schema_file, result_file, opts)
46
44
  end
47
45
 
@@ -49,10 +47,7 @@ module KwalifyToJsonSchema
49
47
 
50
48
  desc "convert_dir KWALIFY_SCHEMA_DIR, RESULT_DIR",
51
49
  "Convert all the Kwalify schema from a directory to a JSON schema"
52
- option :issues_to_description,
53
- :type => :boolean,
54
- :default => false,
55
- :desc => "Will append any conversion issue to the schema description"
50
+ option(*Options.cli_option(:issues_to_description))
56
51
  option :format,
57
52
  :type => :string,
58
53
  :enum => ["json", "yaml"],
@@ -63,7 +58,7 @@ module KwalifyToJsonSchema
63
58
  :default => false,
64
59
  :desc => "Process files recursively",
65
60
  :long_desc => ""
66
- option :custom_processing,
61
+ option Options::CUSTOM_PROCESSING,
67
62
  :type => :string,
68
63
  :desc => <<~DESC
69
64
  Allows to provide a pre/post processing file on handled schemas.
@@ -98,7 +93,7 @@ module KwalifyToJsonSchema
98
93
  begin
99
94
  processing_class = Object.const_get :CustomProcessing
100
95
  custom_processing = processing_class.new
101
- rescue NameError => e
96
+ rescue NameError
102
97
  raise "The 'CustomProcessing' module must be defined in #{pf}"
103
98
  end
104
99
  end
@@ -19,13 +19,13 @@ module KwalifyToJsonSchema
19
19
  attr_reader :issues
20
20
 
21
21
  # Converter options:
22
- # | Name | Type | Default value| Description |
23
- # |-----------------------|--------|--------------|-----------------------------------------------------|
24
- # | :id | String | nil | The JSON schema identifier |
25
- # | :title | String | nil | The JSON schema title |
26
- # | :description | String | nil | The JSON schema description |
27
- # | :issues_to_description| Boolean| false | To append the issuses to the JSON schema description|
28
- # | :custom_processing | Object | nil | To customize the conversion |
22
+ # | Name | Type | Default value| Description |
23
+ # |-----------------------|--------|--------------|------------------------------------------------------------------------------------------|
24
+ # | :id | string | nil | The JSON schema identifier |
25
+ # | :title | string | nil | The JSON schema title |
26
+ # | :description | string | nil | The JSON schema description. If not given the Kwalify description will be used if present|
27
+ # | :issues_to_description| boolean| false | To append the issuses to the JSON schema description |
28
+ # | :custom_processing | object | nil | To customize the conversion |
29
29
  # --
30
30
  def initialize(options_hash = {})
31
31
  @options = Options.new(options_hash)
@@ -46,6 +46,9 @@ module KwalifyToJsonSchema
46
46
  description << issues.map { |issue| "* #{issue}" }.join("\n")
47
47
  end
48
48
 
49
+ # Override description if given in option
50
+ json_schema["description"] = options.description if options.description
51
+
49
52
  postprocess(json_schema)
50
53
  end
51
54
 
@@ -56,7 +59,6 @@ module KwalifyToJsonSchema
56
59
  "$schema" => SCHEMA,
57
60
  "$id" => options.id,
58
61
  "title" => options.title,
59
- "description" => options.description,
60
62
  }.reject { |k, v| v.nil? }
61
63
  end
62
64
 
@@ -152,7 +154,7 @@ module KwalifyToJsonSchema
152
154
  end
153
155
  end
154
156
 
155
- new_issue "'unique' is not supported by JSON Schema" if kelem["unique"]
157
+ new_issue Limitations::UNIQUE_NOT_SUPPORTED if kelem["unique"]
156
158
 
157
159
  target
158
160
  end
@@ -173,6 +175,4 @@ module KwalifyToJsonSchema
173
175
  @issues << description
174
176
  end
175
177
  end
176
-
177
-
178
178
  end
@@ -0,0 +1,51 @@
1
+ module KwalifyToJsonSchema
2
+
3
+ # Convert a Kwalify schema file to JSON .schema.
4
+ # The destination file can be JSON or YAML.
5
+ # The file extension is used to select the format: .json or .yaml.
6
+ # Other extension will fallback to JSON.
7
+ # Converter options:
8
+ # | Name | Type | Default value| Description |
9
+ # |-----------------------|--------|--------------|------------------------------------------------------------------------------------------|
10
+ # | :id | string | nil | The JSON schema identifier |
11
+ # | :title | string | nil | The JSON schema title |
12
+ # | :description | string | nil | The JSON schema description. If not given the Kwalify description will be used if present|
13
+ # | :issues_to_description| boolean| false | To append the issuses to the JSON schema description |
14
+ # | :custom_processing | object | nil | To customize the conversion |
15
+ # --
16
+ # @param source Path to Kwalify YAML schema
17
+ # @param dest Path to resulting JSON schema
18
+ def self.convert_file(source, dest, options = {})
19
+ # Get a converter
20
+ converter = Converter.new(options)
21
+ # Convert
22
+ converted = converter.exec(Serialization.deserialize_from_file(source))
23
+ # Serialize
24
+ Serialization.serialize_to_file(dest, converted)
25
+ end
26
+
27
+ # Convert a Kwalify schema string to JSON .schema.
28
+ # The source and destination strings can be JSON or YAML.
29
+ # Other extension will fallback to JSON.
30
+ # Converter options:
31
+ # | Name | Type | Default value| Description |
32
+ # |-----------------------|--------|--------------|------------------------------------------------------------------------------------------|
33
+ # | :id | string | nil | The JSON schema identifier |
34
+ # | :title | string | nil | The JSON schema title |
35
+ # | :description | string | nil | The JSON schema description. If not given the Kwalify description will be used if present|
36
+ # | :issues_to_description| boolean| false | To append the issuses to the JSON schema description |
37
+ # | :custom_processing | object | nil | To customize the conversion |
38
+ # --
39
+ # @param kwalify_schema Kwalify schema as YAML or JSON
40
+ # @param source_format format of the source schema
41
+ # @param dest_format format of the destination schema
42
+ # @param options
43
+ def self.convert_string(kwalify_schema, source_format = "yaml", dest_format = "json", options = {})
44
+ # Get a converter
45
+ converter = Converter.new(options)
46
+ # Convert
47
+ converted = converter.exec(Serialization.deserialize_from_string(kwalify_schema, source_format))
48
+ # Serialize
49
+ Serialization.serialize_to_string(converted, dest_format)
50
+ end
51
+ end
@@ -1,8 +1,9 @@
1
1
  module KwalifyToJsonSchema
2
- # Enumeration of known implementation limitation
2
+ # Enumeration of known implementation limitations
3
3
  module Limitations
4
4
  DATE_TYPE_NOT_IMPLEMENTED = "Kwalify 'date' type is not supported and is ignored"
5
5
  TIME_TYPE_NOT_IMPLEMENTED = "Kwalify 'time' type is not supported and is ignored"
6
6
  TIMESTAMP_TYPE_NOT_IMPLEMENTED = "Kwalify 'timestamp' type is not supported and is ignored"
7
+ UNIQUE_NOT_SUPPORTED = "Kwalify 'unique' is not supported by JSON Schema and is ignored"
7
8
  end
8
9
  end
@@ -2,20 +2,20 @@ module KwalifyToJsonSchema
2
2
  # The possible options for the conversion and the associated accessors
3
3
  class Options
4
4
  # Converter options:
5
- # | Name | Type | Default value| Description |
6
- # |-----------------------|--------|--------------|-----------------------------------------------------|
7
- # | :id | String | nil | The JSON schema identifier |
8
- # | :title | String | nil | The JSON schema title |
9
- # | :description | String | nil | The JSON schema description |
10
- # | :issues_to_description| Boolean| false | To append the issuses to the JSON schema description|
11
- # | :custom_processing | Object | nil | To customize the conversion |
5
+ # | Name | Type | Default value| Description |
6
+ # |-----------------------|--------|--------------|------------------------------------------------------------------------------------------|
7
+ # | :id | string | nil | The JSON schema identifier |
8
+ # | :title | string | nil | The JSON schema title |
9
+ # | :description | string | nil | The JSON schema description. If not given the Kwalify description will be used if present|
10
+ # | :issues_to_description| boolean| false | To append the issuses to the JSON schema description |
11
+ # | :custom_processing | object | nil | To customize the conversion |
12
12
  # --
13
13
  DECLARATION = %q(
14
- ID # The JSON schema identifier [String] (nil)
15
- TITLE # The JSON schema title [String] (nil)
16
- DESCRIPTION # The JSON schema description [String] (nil)
17
- ISSUES_TO_DESCRIPTION # To append the issuses to the JSON schema description [Boolean] (false)
18
- CUSTOM_PROCESSING # To customize the conversion [Object] (nil)
14
+ ID # The JSON schema identifier [string] (nil)
15
+ TITLE # The JSON schema title [string] (nil)
16
+ DESCRIPTION # The JSON schema description. If not given the Kwalify description will be used if present [string] (nil)
17
+ ISSUES_TO_DESCRIPTION # To append the issuses to the JSON schema description [boolean] (false)
18
+ CUSTOM_PROCESSING # To customize the conversion [object] (nil)
19
19
  )
20
20
 
21
21
  # The options as Hash
@@ -40,13 +40,13 @@ module KwalifyToJsonSchema
40
40
  default_value = eval(default_value)
41
41
 
42
42
  # Create read accessor
43
- attr_reader_name = "#{name}#{type == "Boolean" ? "?" : ""}"
43
+ attr_reader_name = "#{name}#{type == "boolean" ? "?" : ""}"
44
44
 
45
45
  # Array entry as Hash for the option
46
46
  {
47
47
  const_name: const_name,
48
48
  const_name_full: "#{Options.name}::#{const_name}",
49
- name: name,
49
+ name: name.to_sym,
50
50
  description: description,
51
51
  type: type,
52
52
  default_value: default_value,
@@ -55,6 +55,11 @@ module KwalifyToJsonSchema
55
55
  }.compact
56
56
  end
57
57
 
58
+ # Same as :parse but give a Hash with the name as key
59
+ def self.parse_hash
60
+ parse.map { |e| [e[:name], e] }.to_h
61
+ end
62
+
58
63
  # Setup the constants and methods for the options
59
64
  # Example: ID will lead to get ID constant and :id method
60
65
  def self.setup
@@ -69,6 +74,12 @@ module KwalifyToJsonSchema
69
74
  }
70
75
  end
71
76
 
77
+ # Get description for option name
78
+ def self.cli_option(name)
79
+ o = parse_hash[name]
80
+ [o[:name], :type => o[:type].to_sym, :default => o[:default_value], :desc => o[:description]]
81
+ end
82
+
72
83
  setup
73
84
  end
74
85
  end
@@ -25,7 +25,7 @@ module KwalifyToJsonSchema
25
25
 
26
26
  # @return a Hash giving serialization/deserialization module and methods for a format (json/yaml)
27
27
  def self.serialization_for_format(format)
28
- serializer = { "json" => Json, "yaml" => Yaml }[format] || Json
28
+ { "json" => Json, "yaml" => Yaml }[format] || Json
29
29
  end
30
30
 
31
31
  class Language
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kwalify_to_json_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sylvain Gamot
@@ -87,25 +87,21 @@ executables:
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
- - "/home/sylvain/kwalify_to_json_schema/lib/kwalify_to_json_schema.rb"
91
- - "/home/sylvain/kwalify_to_json_schema/lib/kwalify_to_json_schema/cli.rb"
92
- - "/home/sylvain/kwalify_to_json_schema/lib/kwalify_to_json_schema/converter.rb"
93
- - "/home/sylvain/kwalify_to_json_schema/lib/kwalify_to_json_schema/custom_processing.rb"
94
- - "/home/sylvain/kwalify_to_json_schema/lib/kwalify_to_json_schema/kwalify_to_json_schema.rb"
95
- - "/home/sylvain/kwalify_to_json_schema/lib/kwalify_to_json_schema/limitations.rb"
96
- - "/home/sylvain/kwalify_to_json_schema/lib/kwalify_to_json_schema/options.rb"
97
- - "/home/sylvain/kwalify_to_json_schema/lib/kwalify_to_json_schema/serialization.rb"
98
- - "/home/sylvain/kwalify_to_json_schema/test/custom_processing.rb"
99
- - "/home/sylvain/kwalify_to_json_schema/test/test_kwalify_to_json_schema.rb"
100
- - "/home/sylvain/kwalify_to_json_schema/tools/all.rb"
101
- - "/home/sylvain/kwalify_to_json_schema/tools/doc_template.rb"
102
- - "/home/sylvain/kwalify_to_json_schema/tools/limitations.rb"
103
- - "/home/sylvain/kwalify_to_json_schema/tools/options.rb"
104
90
  - bin/kwalify_to_json_schema
105
- homepage: https://rubygems.org/gems/kwalify_to_json_schema
91
+ - lib/kwalify_to_json_schema.rb
92
+ - lib/kwalify_to_json_schema/cli.rb
93
+ - lib/kwalify_to_json_schema/converter.rb
94
+ - lib/kwalify_to_json_schema/custom_processing.rb
95
+ - lib/kwalify_to_json_schema/kwalify_to_json_schema.rb
96
+ - lib/kwalify_to_json_schema/limitations.rb
97
+ - lib/kwalify_to_json_schema/options.rb
98
+ - lib/kwalify_to_json_schema/serialization.rb
99
+ homepage: https://github.com/s-ga/kwalify_to_json_schema
106
100
  licenses:
107
101
  - MIT
108
- metadata: {}
102
+ metadata:
103
+ bug_tracker_uri: https://github.com/s-ga/kwalify_to_json_schema/issues
104
+ source_code_uri: https://github.com/s-ga/kwalify_to_json_schema
109
105
  post_install_message:
110
106
  rdoc_options: []
111
107
  require_paths:
@@ -1,51 +0,0 @@
1
- module KwalifyToJsonSchema
2
-
3
- # Convert a Kwalify schema file to JSON .schema.
4
- # The destination file can be JSON or YAML.
5
- # The file extension is used to select the format: .json or .yaml.
6
- # Other extension will fallback to JSON.
7
- # Converter options:
8
- # | Name | Type | Default value| Description |
9
- # |-----------------------|--------|--------------|-----------------------------------------------------|
10
- # | :id | String | nil | The JSON schema identifier |
11
- # | :title | String | nil | The JSON schema title |
12
- # | :description | String | nil | The JSON schema description |
13
- # | :issues_to_description| Boolean| false | To append the issuses to the JSON schema description|
14
- # | :custom_processing | Object | nil | To customize the conversion |
15
- # --
16
- # @param source Path to Kwalify YAML schema
17
- # @param dest Path to resulting JSON schema
18
- def self.convert_file(source, dest, options = {})
19
- # Get a converter
20
- converter = Converter.new(options)
21
- # Convert
22
- converted = converter.exec(Serialization.deserialize_from_file(source))
23
- # Serialize
24
- Serialization.serialize_to_file(dest, converted)
25
- end
26
-
27
- # Convert a Kwalify schema string to JSON .schema.
28
- # The source and destination strings can be JSON or YAML.
29
- # Other extension will fallback to JSON.
30
- # Converter options:
31
- # | Name | Type | Default value| Description |
32
- # |-----------------------|--------|--------------|-----------------------------------------------------|
33
- # | :id | String | nil | The JSON schema identifier |
34
- # | :title | String | nil | The JSON schema title |
35
- # | :description | String | nil | The JSON schema description |
36
- # | :issues_to_description| Boolean| false | To append the issuses to the JSON schema description|
37
- # | :custom_processing | Object | nil | To customize the conversion |
38
- # --
39
- # @param kwalify_schema Kwalify schema as YAML or JSON
40
- # @param source_format format of the source schema
41
- # @param dest_format format of the destination schema
42
- # @param options
43
- def self.convert_string(kwalify_schema, source_format = "yaml", dest_format = "json", options = {})
44
- # Get a converter
45
- converter = Converter.new(options)
46
- # Convert
47
- converted = converter.exec(Serialization.deserialize_from_string(kwalify_schema, source_format))
48
- # Serialize
49
- Serialization.serialize_to_string(converted, dest_format)
50
- end
51
- end
@@ -1,14 +0,0 @@
1
- # Customization of Kwalify to JSON schema
2
- class CustomProcessing
3
- def preprocess(kwalify_schema)
4
- # Remove and keep the wrapping name
5
- head = kwalify_schema.first
6
- @name = head.first
7
- head.last
8
- end
9
-
10
- def postprocess(json_schema)
11
- # Restore the wrapping name
12
- { @name => json_schema }
13
- end
14
- end
@@ -1,75 +0,0 @@
1
- require "minitest/autorun"
2
- require "json-schema"
3
- require_relative "../lib/kwalify_to_json_schema"
4
-
5
- module KwalifyToJsonSchema
6
- class Test < Minitest::Test
7
- @@debug = false
8
- @@tmpdir = Dir.mktmpdir
9
-
10
- [
11
- { test_group: "conversion", cli_options: [] },
12
- { test_group: "custom_processing", cli_options: ["--custom-processing", File.join(__dir__, "custom_processing.rb")] },
13
- ].each { |entry|
14
- test_group = entry[:test_group]
15
- cli_options = entry[:cli_options]
16
-
17
- # Create a test method for every Kwalify schema
18
- Dir.glob(File.join(__dir__, test_group, "kwalify", "*.yaml")).each { |source|
19
- test_file_base = File.basename(source, File.extname(source))
20
- test_name_base = test_file_base.gsub("#", "_")
21
- expected_formats = %w(json yaml)
22
-
23
- # Define a method for the test JSON output
24
- define_method("test_#{test_group}_#{test_name_base}_output".to_sym) {
25
- formats_done = 0
26
- expected_formats.map { |expected_format|
27
- output_file = test_file_base + ".#{expected_format}"
28
- expected = File.join(File.join(__dir__, test_group, "json_schema", expected_format, output_file))
29
-
30
- next unless File.exist?(expected)
31
- formats_done += 1
32
-
33
- ser = KwalifyToJsonSchema::Serialization::serialization_for_format(expected_format)
34
- dest = File.join(@@tmpdir, output_file)
35
-
36
- args = ["convert", source, dest]
37
- # Add issues to description if filename include "#issues_to_description"
38
- args << "--issues_to_description" if output_file.include?("#issues_to_description")
39
- args.concat cli_options
40
-
41
- # Convert
42
- # KwalifyToJsonSchema.convert_file(source, dest, options)
43
- KwalifyToJsonSchema::Cli.start(args)
44
-
45
- # Validate schema
46
- validate_json_schema_file(dest)
47
-
48
- if @@debug
49
- puts test_name_base
50
- puts ser.normalize(File.read(dest))
51
- end
52
- # Compare to expected result
53
- assert_equal(
54
- ser.normalize(File.read(expected)),
55
- ser.normalize(File.read(dest))
56
- )
57
- }
58
-
59
- skip "None of the expected #{expected_formats.join(", ")} result for test #{test_name_base} was found" if formats_done == 0
60
- }
61
- }
62
- }
63
-
64
- def validate_json_schema_file(schema_file)
65
- schema = KwalifyToJsonSchema::Serialization::deserialize_from_file(schema_file)
66
- validate_json_schema(schema)
67
- end
68
-
69
- def validate_json_schema(schema)
70
- # FIXME draft7 is not available in current json-schema gem
71
- metaschema = JSON::Validator.validator_for_name("draft4").metaschema
72
- JSON::Validator.validate!(metaschema, schema)
73
- end
74
- end
75
- end
@@ -1,4 +0,0 @@
1
- require_relative "../lib/kwalify_to_json_schema"
2
- require_relative "doc_template"
3
- require_relative "limitations"
4
- require_relative "options"
@@ -1,13 +0,0 @@
1
- require "erb"
2
-
3
- module DocTemplate
4
- def self.render(template_file, dest_file)
5
- template = ERB.new(File.read(template_file), nil, "-")
6
- File.write(dest_file, template.result(get_binding(template_file)))
7
- end
8
-
9
- def self.get_binding(template_file)
10
- template_dir = File.dirname template_file
11
- binding
12
- end
13
- end
@@ -1,19 +0,0 @@
1
- require_relative "../lib/kwalify_to_json_schema/limitations"
2
-
3
- # Gives implementation limitations
4
- module Limitations
5
-
6
- # @return list of limitation as array of strings
7
- def self.list
8
- KwalifyToJsonSchema::Limitations.constants.map { |cst|
9
- KwalifyToJsonSchema::Limitations.const_get(cst)
10
- }
11
- end
12
-
13
- # @return limitation as markdown text
14
- def self.markdown
15
- list.map { |l|
16
- "* #{l}"
17
- }.join("\n")
18
- end
19
- end
@@ -1,96 +0,0 @@
1
- require "yaml"
2
- require_relative "../lib/kwalify_to_json_schema/options"
3
-
4
- # Gives implementation limitations
5
- module Options
6
-
7
- # @return list of limitation as array of strings
8
- def self.list
9
- KwalifyToJsonSchema::Options.parse
10
- end
11
-
12
- # @return limitation as markdown text
13
- def self.ascii_table(formatting = ["%s"] * 4)
14
- header = ["Name", "Type", "Default value", "Description"]
15
-
16
- nb_cols = header.length
17
-
18
- table = [header] +
19
- [[""] * nb_cols] +
20
- list.map { |o|
21
- [
22
- formatting[0] % o[:name].to_sym.inspect,
23
- formatting[1] % o[:type],
24
- formatting[2] % o[:default_value].inspect,
25
- formatting[3] % o[:description],
26
- ]
27
- }
28
- nb_rows = table.length
29
-
30
- cols_max_length = (0..nb_cols - 1).map { |c|
31
- (0..nb_rows - 1).map { |r|
32
- cell = table[r][c]
33
- cell.length
34
- }.max
35
- }
36
-
37
- table.map.each_with_index { |row, r|
38
- row.map.each_with_index { |cell, c|
39
- max_length = cols_max_length[c]
40
- if r == 1
41
- "|-" + cell + ("-" * (max_length - cell.length))
42
- else
43
- "| " + cell + (" " * (max_length - cell.length))
44
- end
45
- }.join + "|"
46
- }.join "\n"
47
- end
48
-
49
- def self.markdown
50
- ascii_table [
51
- "`%s`",
52
- "`%s`",
53
- "`%s`",
54
- "_%s_",
55
- ]
56
- end
57
-
58
- def self.inject_as_code_comment(file)
59
- new_lines = []
60
- state = :init
61
- count = 0
62
-
63
- options_start = "Converter options:"
64
- options_stop = "--"
65
-
66
- File.read(file).each_line { |line|
67
- if line.strip.start_with? "#"
68
- content = line.strip[1..-1].strip
69
- case state
70
- when :init
71
- new_lines << line
72
- if content == options_start
73
- count += 1
74
- state = :in_options
75
- padding = line.index("#")
76
- new_lines.concat(ascii_table.lines.map { |l| "#{" " * padding}# #{l.chomp}\n" })
77
- end
78
- when :in_options
79
- if content.start_with? options_stop
80
- new_lines << line
81
- state = :init
82
- end
83
- end
84
- else
85
- state = :error unless state == :init
86
- new_lines << line
87
- end
88
- }
89
-
90
- if state == :error
91
- puts "Missing '#{options_stop}' delimiter after '#{options_start}' in file://#{file}"
92
- else
93
- File.write(file, new_lines.join) if count > 0
94
- end
95
- end
96
- end