metanorma-plugin-asyncapi 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ea0aae0b9a7d2850a356442c84380e8d96801a770ac4fdf4949d85c79fb5ac77
4
+ data.tar.gz: ba4411db22f87b0b0f9ea4ab1f601c4d2042006e22e0d22922d74afa487aa248
5
+ SHA512:
6
+ metadata.gz: fee0318e56771c1fb83f7747f0661707e4b924a157c29d5e3ca5c3169d0b42e1d4686bc1398fda67be256dc19556214aff18a6e71105bc6621b09a9086f1d0ef
7
+ data.tar.gz: 54b03d10443bf063fa3dab00104ab5985f64b6c044fe1d8da3233746e9e61593aac0c0c269d6d66854749e9d61ae2a6af763a654d2da3413b648ddd40f38f06e
data/.rubocop_todo.yml ADDED
File without changes
@@ -0,0 +1,46 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ ## Our Standards
8
+
9
+ Examples of behavior that contributes to creating a positive environment include:
10
+
11
+ * Using welcoming and inclusive language
12
+ * Being respectful of differing viewpoints and experiences
13
+ * Gracefully accepting constructive criticism
14
+ * Focusing on what is best for the community
15
+ * Showing empathy towards other community members
16
+
17
+ Examples of unacceptable behavior by participants include:
18
+
19
+ * The use of sexualized language or imagery and unwelcome sexual attention or advances
20
+ * Trolling, insulting/derogatory comments, and personal or political attacks
21
+ * Public or private harassment
22
+ * Publishing others' private information, such as a physical or electronic address, without explicit permission
23
+ * Other conduct which could reasonably be considered inappropriate in a professional setting
24
+
25
+ ## Our Responsibilities
26
+
27
+ Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28
+
29
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30
+
31
+ ## Scope
32
+
33
+ This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34
+
35
+ ## Enforcement
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at ronald.tse@ribose.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38
+
39
+ Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40
+
41
+ ## Attribution
42
+
43
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44
+
45
+ [homepage]: http://contributor-covenant.org
46
+ [version]: http://contributor-covenant.org/version/1/4/
@@ -0,0 +1 @@
1
+ # metanorma-plugin-asyncapi
data/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # metanorma-plugin-asyncapi
2
+
3
+ Renders [AsyncAPI](https://www.asyncapi.com) documents as normative
4
+ Metanorma sections — the `[asyncapi,path=...]` preprocessor and
5
+ renderer over [lutaml-asyncapi](https://github.com/lutaml/lutaml-asyncapi).
6
+
7
+ ```adoc
8
+ [asyncapi,path=asyncapi.yaml]
9
+ --
10
+ --
11
+ ```
12
+
13
+ See PLAN.md at lutaml-asyncapi for the program.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "asciidoctor"
4
+
5
+ module Metanorma
6
+ module Plugin
7
+ module Asyncapi
8
+ # Splices rendered AsciiDoc in place of an
9
+ #
10
+ # [asyncapi,path=/to/document.yaml]
11
+ # --
12
+ # --
13
+ #
14
+ # envelope before the document parses. A preprocessor, not a
15
+ # block processor: Parser.parse_blocks (the only parse path a
16
+ # block processor gets) cannot create sections.
17
+ class Preprocessor < ::Asciidoctor::Extensions::Preprocessor
18
+ ASYNCAPI_ATTRS = /\A\[asyncapi([,\]].*)?\]\s*\z/
19
+
20
+ def process(document, reader)
21
+ ::Asciidoctor::Reader.new(splice(document, reader.readlines),
22
+ reader.cursor)
23
+ end
24
+
25
+ private
26
+
27
+ def splice(document, lines)
28
+ out = []
29
+ until lines.empty?
30
+ line = lines.shift
31
+ if line.match?(ASYNCAPI_ATTRS)
32
+ out.concat(consume_and_render(document, line, lines))
33
+ else
34
+ out << line
35
+ end
36
+ end
37
+ out
38
+ end
39
+
40
+ def dashed?(line)
41
+ line&.strip == "--"
42
+ end
43
+
44
+ # The closing -- is optional; a missing opening envelope raises
45
+ # from consume_and_render
46
+ def envelope(lines)
47
+ lines.shift while lines.first && lines.first.strip.empty?
48
+ return nil unless dashed?(lines.shift)
49
+
50
+ body = []
51
+ while (line = lines.shift) && !dashed?(line)
52
+ body << line
53
+ end
54
+ body
55
+ end
56
+
57
+ def consume_and_render(document, attr_line, lines)
58
+ envelope(lines) or
59
+ raise ::ArgumentError,
60
+ "asyncapi block needs a -- -- envelope: #{attr_line}"
61
+ base = ::File.dirname(document.attributes["docfile"] || ".")
62
+ source = envelope_path(attr_line)
63
+ renderer = Renderer.new(load_document(document, source, base))
64
+ content = renderer.render
65
+ content.split("\n")
66
+ end
67
+
68
+ def envelope_path(attr_line)
69
+ path = attr_line[/path=("([^"]+)"|(\S+?))\]/, 2] ||
70
+ attr_line[/path=("([^"]+)"|(\S+?))\]/, 3] ||
71
+ attr_line[/path=([^\]\s]+)/, 1]
72
+ raise ::ArgumentError, "asyncapi block needs a path" unless path
73
+
74
+ path
75
+ end
76
+
77
+ def load_document(_document, source, base)
78
+ # ::Lutaml — metanorma-plugin-lutaml defines
79
+ # Metanorma::Plugin::Lutaml, which would capture a lexically
80
+ # scoped Lutaml::Asyncapi lookup
81
+ ::Lutaml::Asyncapi.parse(
82
+ ::File.read(::File.expand_path(source, base)),
83
+ )
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,205 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/asyncapi"
4
+
5
+ module Metanorma
6
+ module Plugin
7
+ module Asyncapi
8
+ # metanorma-plugin-lutaml pins coradoc ~> 1.1.8, which excludes the
9
+ # 2.0 hub in every metanorma-cli bundle; descriptions pass through
10
+ # with only the link bridge when the monorepo is absent.
11
+ begin
12
+ require "coradoc"
13
+ require "coradoc/markdown"
14
+ require "coradoc/asciidoc"
15
+ MONOREPO = true
16
+ rescue LoadError
17
+ MONOREPO = false
18
+ end
19
+
20
+ # Renders an AsyncAPI document as normative AsciiDoc sections.
21
+ class Renderer
22
+ def initialize(document)
23
+ @document = document
24
+ end
25
+
26
+ def render
27
+ [preface, servers_section, channels_section,
28
+ operations_section, messages_section].compact.join("\n\n")
29
+ end
30
+
31
+ TEXT_KEYS = %w[summary description].freeze
32
+
33
+ private
34
+
35
+ def raw
36
+ @raw ||= @document.to_hash
37
+ end
38
+
39
+ def preface
40
+ info = raw["info"] || {}
41
+ lines = ["== #{info['title'] || 'AsyncAPI document'}", ""]
42
+ if @document.asyncapi
43
+ lines << "AsyncAPI version: #{@document.asyncapi}"
44
+ end
45
+ if (desc = info["description"])
46
+ lines << to_asciidoc(desc) << ""
47
+ end
48
+ lines.join("\n")
49
+ end
50
+
51
+ def servers_section
52
+ servers = raw["servers"]
53
+ return nil if servers.nil? || servers.empty?
54
+
55
+ lines = ["== Servers", ""]
56
+ servers.sort.each do |name, server|
57
+ lines << "=== #{name}" << ""
58
+ lines.concat(server_lines(server)) if server.is_a?(Hash)
59
+ lines << ""
60
+ end
61
+ lines.join("\n")
62
+ end
63
+
64
+ def server_lines(server)
65
+ out = []
66
+ out << "URL: `#{server['url']}`" if server["url"]
67
+ if server["protocol"]
68
+ out << "Protocol: `#{server['protocol']}`"
69
+ end
70
+ if server["description"]
71
+ out << to_asciidoc(server["description"]) << ""
72
+ end
73
+ out
74
+ end
75
+
76
+ def channels_section
77
+ channels = raw["channels"]
78
+ return nil if channels.nil? || channels.empty?
79
+
80
+ lines = ["== Channels", ""]
81
+ channels.sort.each do |id, channel|
82
+ lines << "=== #{id}" << ""
83
+ lines.concat(channel_lines(channel)) if channel.is_a?(Hash)
84
+ lines << ""
85
+ end
86
+ lines.join("\n")
87
+ end
88
+
89
+ def channel_lines(channel)
90
+ out = []
91
+ if channel["address"]
92
+ out << "Address: `#{channel['address']}`"
93
+ end
94
+ if channel["description"]
95
+ out << to_asciidoc(channel["description"]) << ""
96
+ end
97
+ out
98
+ end
99
+
100
+ def operations_section
101
+ ops = operations
102
+ return nil if ops.empty?
103
+
104
+ lines = ["== Operations", ""]
105
+ ops.each do |name, op|
106
+ lines << "=== #{name}" << ""
107
+ lines << "Action: `#{op['action']}`" if op["action"]
108
+ lines.concat(text_lines(op))
109
+ lines << ""
110
+ end
111
+ lines.join("\n")
112
+ end
113
+
114
+ # 3.x: top-level operations. 2.x: publish/subscribe embedded in
115
+ # channels, keyed "<channel> <action>".
116
+ def operations
117
+ root_ops = (raw["operations"] || {}).select do |_name, op|
118
+ op.is_a?(Hash)
119
+ end
120
+ root_ops.merge(embedded_operations)
121
+ end
122
+
123
+ def embedded_operations
124
+ ops = {}
125
+ (raw["channels"] || {}).each do |id, channel|
126
+ next unless channel.is_a?(Hash)
127
+
128
+ append_embedded(ops, id, channel)
129
+ end
130
+ ops
131
+ end
132
+
133
+ def append_embedded(ops, id, channel)
134
+ %w[publish subscribe].each do |action|
135
+ if (op = channel[action]).is_a?(Hash)
136
+ ops["#{id} #{action}"] = op.merge("action" => action)
137
+ end
138
+ end
139
+ end
140
+
141
+ def messages_section
142
+ messages = collect_messages
143
+ return nil if messages.empty?
144
+
145
+ lines = ["== Messages", ""]
146
+ messages.each do |name, message|
147
+ lines << "=== #{name}" << ""
148
+ lines.concat(message_lines(message)) if message.is_a?(Hash)
149
+ lines << ""
150
+ end
151
+ lines.join("\n")
152
+ end
153
+
154
+ def message_lines(message)
155
+ out = []
156
+ if message["contentType"]
157
+ out << "Payload content type: `#{message['contentType']}`"
158
+ end
159
+ out.concat(text_lines(message))
160
+ out
161
+ end
162
+
163
+ def text_lines(node)
164
+ TEXT_KEYS.filter_map do |key|
165
+ to_asciidoc(node[key]) << "" if node[key]
166
+ end
167
+ end
168
+
169
+ def collect_messages
170
+ found = {}
171
+ walk(raw) do |node|
172
+ next unless node.is_a?(Hash) && node["payload"]
173
+
174
+ name = node["name"] || node["title"] || "message"
175
+ found[name] = node unless found.key?(name)
176
+ end
177
+ found
178
+ end
179
+
180
+ def walk(node, &)
181
+ case node
182
+ when Hash
183
+ yield node
184
+ node.each_value { |v| walk(v, &) }
185
+ when Array
186
+ node.each { |v| walk(v, &) }
187
+ end
188
+ end
189
+
190
+ def to_asciidoc(markdown)
191
+ adoc = convert_markdown(markdown)
192
+ adoc.gsub(/\[([^\]]+)\]\((https?:\/\/[^)\s]+)\)/) do
193
+ "#{Regexp.last_match(2)}[#{Regexp.last_match(1)}]"
194
+ end
195
+ end
196
+
197
+ def convert_markdown(markdown)
198
+ return markdown unless MONOREPO
199
+
200
+ Coradoc.convert(markdown, from: :markdown, to: :asciidoc)
201
+ end
202
+ end
203
+ end
204
+ end
205
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Metanorma
4
+ module Plugin
5
+ module Asyncapi
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "asciidoctor/extensions"
4
+
5
+ module Metanorma
6
+ module Plugin
7
+ module Asyncapi
8
+ autoload :VERSION, "metanorma/plugin/asyncapi/version"
9
+ autoload :Renderer, "metanorma/plugin/asyncapi/renderer"
10
+ autoload :Preprocessor, "metanorma/plugin/asyncapi/preprocessor"
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metanorma-plugin-asyncapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ribose Inc.
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: asciidoctor
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: lutaml-asyncapi
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: 0.1.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 0.1.0
40
+ - !ruby/object:Gem::Dependency
41
+ name: lutaml-model
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 0.8.122
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 0.8.122
54
+ - !ruby/object:Gem::Dependency
55
+ name: nokogiri
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ - !ruby/object:Gem::Dependency
69
+ name: yeptris
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: 0.6.34
75
+ type: :runtime
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 0.6.34
82
+ description: 'Render AsyncAPI documents as normative Metanorma sections: the [asyncapi,path=...]
83
+ preprocessor and renderer over lutaml-asyncapi'
84
+ email:
85
+ - open.source@ribose.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".rubocop_todo.yml"
91
+ - CODE_OF_CONDUCT.md
92
+ - README-bootstrap.md
93
+ - README.md
94
+ - Rakefile
95
+ - lib/metanorma/plugin/asyncapi.rb
96
+ - lib/metanorma/plugin/asyncapi/preprocessor.rb
97
+ - lib/metanorma/plugin/asyncapi/renderer.rb
98
+ - lib/metanorma/plugin/asyncapi/version.rb
99
+ homepage: https://github.com/metanorma/metanorma-plugin-asyncapi
100
+ licenses:
101
+ - BSD-2-Clause
102
+ metadata:
103
+ homepage_uri: https://github.com/metanorma/metanorma-plugin-asyncapi
104
+ source_code_uri: https://github.com/metanorma/metanorma-plugin-asyncapi
105
+ changelog_uri: https://github.com/metanorma/metanorma-plugin-asyncapi/blob/main/CHANGELOG.md
106
+ rubygems_mfa_required: 'true'
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: 3.2.0
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubygems_version: 3.6.9
122
+ specification_version: 4
123
+ summary: AsyncAPI as normative Metanorma sections
124
+ test_files: []