metanorma-plugin-openapi 0.1.0 → 0.1.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: 0cc137b68b59999c5d99577793b14fd3ece351ab14f95372ac8745c016d5e780
4
- data.tar.gz: a6f5628475121accbf69b485ddca657f1e433b6ed191ae958de85c2bbc97c900
3
+ metadata.gz: e24aac3a9834450d38c598a2fb5fe97f7fb9c72360dd15ccdf9a53e8659cb511
4
+ data.tar.gz: 3604d75b940bf467aa2c12dd985a5b075528c7a512e36983b97ebe9643adebd4
5
5
  SHA512:
6
- metadata.gz: 6adfce232953b977dfb8b7cf1f7474a6047d9ed106626e81e9a5f14f50fa27f7d6dbdaf38c677f6054de1509c40ffcfccc335b83d5ece16df744cea5e83e8965
7
- data.tar.gz: 0c21e681440b8a261190099b4d103766f7cc5668ddbb8a66ff1607bc5f2c0bac5632e4bb96577806e0fb0493d31a82dfef5a94dbf64189234d8e8e7d7715b812
6
+ metadata.gz: e76a0c1ed23c61b5b6f1e08776392c049adaa8f579eca12d588c9d5bbe4659b2281fec42fd47f4b4937700d58531cd5d2411629e152ff5024f0c71da0d610f47
7
+ data.tar.gz: 84fc464660f03dd22ecd87cfbf80cf88de6863caf4c543d651a13985247609df0a21a49bb4e40ec268df5aa11d3e92d96d4e47db3a5adfc7cbe5484320aa6fd5
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "asciidoctor"
4
+
5
+ module Metanorma
6
+ module Plugin
7
+ module Openapi
8
+ # Splices rendered AsciiDoc in place of an
9
+ #
10
+ # [openapi,path=/to/document.yaml]
11
+ # --
12
+ # --
13
+ #
14
+ # envelope before the document parses. A preprocessor, not a block
15
+ # processor: Parser.parse_blocks (what block processors get to
16
+ # parse through) cannot create sections, and the rendered sections
17
+ # must be real sections (numbering, TOC).
18
+ class Preprocessor < ::Asciidoctor::Extensions::Preprocessor
19
+ OPENAPI_ATTRS = /\A\[openapi([,\]].*)?\]\s*\z/
20
+
21
+ def process(document, reader)
22
+ ::Asciidoctor::Reader.new(splice(document, reader.readlines),
23
+ reader.cursor)
24
+ end
25
+
26
+ private
27
+
28
+ def splice(document, lines)
29
+ out = []
30
+ until lines.empty?
31
+ line = lines.shift
32
+ if line.match?(OPENAPI_ATTRS)
33
+ out.concat(consume_and_render(document, line, lines))
34
+ else
35
+ out << line
36
+ end
37
+ end
38
+ out
39
+ end
40
+
41
+ def dashed?(line)
42
+ line&.strip == "--"
43
+ end
44
+
45
+ def consume_and_render(document, attr_line, lines)
46
+ envelope(lines) or
47
+ raise ::ArgumentError,
48
+ "openapi block needs a -- -- envelope: #{attr_line}"
49
+ source = attrs_path(attr_line)
50
+ content = Renderer.new(load_document(document, source)).render
51
+ content.split("\n")
52
+ end
53
+
54
+ # The closing -- is optional; a missing opening envelope raises
55
+ # from consume_and_render
56
+ def envelope(lines)
57
+ lines.shift while lines.first && lines.first.strip.empty?
58
+ return nil unless dashed?(lines.shift)
59
+
60
+ body = []
61
+ while (line = lines.shift) && !dashed?(line)
62
+ body << line
63
+ end
64
+ body
65
+ end
66
+
67
+ def attrs_path(attr_line)
68
+ path = attr_line[/path=("([^"]+)"|(\S+?))\]/, 2] ||
69
+ attr_line[/path=("([^"]+)"|(\S+?))\]/, 3] ||
70
+ attr_line[/path=([^\]\s]+)/, 1]
71
+ raise ::ArgumentError, "openapi block needs a path" unless path
72
+
73
+ path
74
+ end
75
+
76
+ def load_document(document, source)
77
+ base = ::File.dirname(document.attributes["docfile"] || ".")
78
+ ::Lutaml::Openapi.parse(::File.read(::File.expand_path(source, base)))
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -1,13 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "lutaml/openapi"
4
- require "coradoc"
5
- require "coradoc/markdown"
6
- require "coradoc/asciidoc"
7
4
 
8
5
  module Metanorma
9
6
  module Plugin
10
7
  module Openapi
8
+ # metanorma-plugin-lutaml pins coradoc ~> 1.1.8, which excludes the
9
+ # 2.0 hub in every metanorma-cli bundle, so the monorepo (and its
10
+ # markdown spoke) may be absent at runtime; without it,
11
+ # CommonMark descriptions pass through with only the link bridge.
12
+ begin
13
+ require "coradoc"
14
+ require "coradoc/markdown"
15
+ require "coradoc/asciidoc"
16
+ MONOREPO = true
17
+ rescue LoadError
18
+ MONOREPO = false
19
+ end
20
+
11
21
  # Renders an OpenAPI document as normative AsciiDoc sections.
12
22
  # Descriptions are CommonMark (per the OpenAPI spec) and convert
13
23
  # to AsciiDoc through the coradoc tree — the metanorma-ogc#153
@@ -23,9 +33,11 @@ module Metanorma
23
33
 
24
34
  private
25
35
 
36
+ # The preface is a section: spliced content must stay at `==`
37
+ # level (a level-0 line mid-document degrades to a paragraph)
26
38
  def preface
27
39
  info = @document.info || {}
28
- lines = ["= #{info['title'] || 'OpenAPI document'}", ""]
40
+ lines = ["== #{info['title'] || 'OpenAPI document'}", ""]
29
41
  lines << "API version: #{@document.openapi}" if @document.openapi
30
42
  if (desc = info["description"])
31
43
  lines << to_asciidoc(desc) << ""
@@ -69,16 +81,23 @@ module Metanorma
69
81
  op.is_a?(Hash)
70
82
  end
71
83
  entries.flat_map do |method, operation|
72
- lines = ["=== #{method.to_s.upcase} #{path}", ""]
73
- if (desc = operation["description"])
74
- lines << to_asciidoc(desc) << ""
84
+ ["=== #{method.to_s.upcase} #{path}", "",
85
+ *operation_text(operation)]
86
+ end
87
+ end
88
+
89
+ # most real corpora carry summary (the one-liner), not
90
+ # description; the tiles bundled OAS has summary on all 39
91
+ def operation_text(operation)
92
+ %w[summary description].filter_map do |key|
93
+ if (text = operation[key])
94
+ to_asciidoc(text) << ""
75
95
  end
76
- lines
77
96
  end
78
97
  end
79
98
 
80
99
  def to_asciidoc(markdown)
81
- adoc = Coradoc.convert(markdown, from: :markdown, to: :asciidoc)
100
+ adoc = convert_markdown(markdown)
82
101
  # coradoc-markdown does not convert CommonMark links yet (they
83
102
  # pass through raw); bridge the dominant inline form to
84
103
  # AsciiDoc's https://url[text]. Remove once coradoc grows
@@ -87,6 +106,12 @@ module Metanorma
87
106
  "#{Regexp.last_match(2)}[#{Regexp.last_match(1)}]"
88
107
  end
89
108
  end
109
+
110
+ def convert_markdown(markdown)
111
+ return markdown unless MONOREPO
112
+
113
+ Coradoc.convert(markdown, from: :markdown, to: :asciidoc)
114
+ end
90
115
  end
91
116
  end
92
117
  end
@@ -3,7 +3,7 @@
3
3
  module Metanorma
4
4
  module Plugin
5
5
  module Openapi
6
- VERSION = "0.1.0"
6
+ VERSION = "0.1.1"
7
7
  end
8
8
  end
9
9
  end
@@ -7,7 +7,7 @@ module Metanorma
7
7
  module Openapi
8
8
  autoload :VERSION, "metanorma/plugin/openapi/version"
9
9
  autoload :Renderer, "metanorma/plugin/openapi/renderer"
10
- autoload :Block, "metanorma/plugin/openapi/block"
10
+ autoload :Preprocessor, "metanorma/plugin/openapi/preprocessor"
11
11
  end
12
12
  end
13
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metanorma-plugin-openapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -23,20 +23,6 @@ dependencies:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
25
  version: '0'
26
- - !ruby/object:Gem::Dependency
27
- name: coradoc
28
- requirement: !ruby/object:Gem::Requirement
29
- requirements:
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: '1.1'
33
- type: :runtime
34
- prerelease: false
35
- version_requirements: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - ">="
38
- - !ruby/object:Gem::Version
39
- version: '1.1'
40
26
  - !ruby/object:Gem::Dependency
41
27
  name: lutaml-model
42
28
  requirement: !ruby/object:Gem::Requirement
@@ -80,7 +66,7 @@ files:
80
66
  - Rakefile
81
67
  - examples/ogcapi-features-1.adoc
82
68
  - lib/metanorma/plugin/openapi.rb
83
- - lib/metanorma/plugin/openapi/block.rb
69
+ - lib/metanorma/plugin/openapi/preprocessor.rb
84
70
  - lib/metanorma/plugin/openapi/renderer.rb
85
71
  - lib/metanorma/plugin/openapi/version.rb
86
72
  homepage: https://github.com/metanorma/metanorma-plugin-openapi
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "asciidoctor"
4
-
5
- module Metanorma
6
- module Plugin
7
- module Openapi
8
- # [openapi,path=/path/to/document.yaml]
9
- # --
10
- # --
11
- #
12
- # Loads an OpenAPI document and renders it as normative AsciiDoc
13
- # sections. The path resolves against the source document.
14
- class Block < ::Asciidoctor::Extensions::BlockProcessor
15
- use_dsl
16
- named :openapi
17
- on_context :open
18
-
19
- def process(parent, _reader, attrs)
20
- source = attrs["path"] || parent.document.attr("openapi_document")
21
- unless source
22
- raise ::ArgumentError,
23
- "openapi block needs a path attribute"
24
- end
25
-
26
- content = Renderer.new(load_document(parent, source)).render
27
- parent.blocks << ::Asciidoctor::Block.new(
28
- parent, :open, source: content.split("\n"), attrs: {}
29
- )
30
- nil
31
- end
32
-
33
- private
34
-
35
- def load_document(parent, source)
36
- base = ::File.dirname(parent.document.attributes["docfile"] || ".")
37
- Lutaml::Openapi.parse(::File.read(::File.expand_path(source, base)))
38
- end
39
- end
40
- end
41
- end
42
- end