readme_yard 0.2.0 → 0.3.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.
data/lib/readme_yard.rb CHANGED
@@ -1,45 +1,44 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "yard"
3
4
  require "yard-readme"
5
+ require "tty-markdown"
4
6
  require_relative "readme_yard/version"
5
7
  require_relative "readme_yard/error"
6
8
  require_relative "readme_yard/logger"
7
9
  require_relative "readme_yard/readme_tag"
8
10
  require_relative "readme_yard/example_tag"
9
11
  require_relative "readme_yard/comment_tag"
12
+ require_relative "readme_yard/code_tag"
10
13
  require_relative "readme_yard/source_tag"
11
- require_relative "readme_yard/object_tag"
14
+ require_relative "readme_yard/value_tag"
15
+ require_relative "readme_yard/string_tag"
16
+ require_relative "readme_yard/tag_registry"
17
+ require_relative "readme_yard/yard_opts_manager"
12
18
 
13
- YARDReadme::DocstringParser.readme_tag_names = %w[comment source object]
19
+ YARD::Readme::DocstringParser.readme_tag_names = ReadmeYard::TagRegistry.tag_names
14
20
 
15
21
  #
16
22
  # @readme
17
- # Build a better README with [YARD](https://yardoc.org),
18
- # one that summarizes and contextualizes the code and documentation,
19
- # without duplicating them.
23
+ # Build a better README with [YARD](https://yardoc.org)
24
+ # by generating it straight from the source.
20
25
  #
21
- # This gem aims to minimize the effort needed to keep
22
- # your code, docs, and README useful, syncd, and correct.
26
+ # This gem aims to minimize the effort needed to keep your
27
+ # README, documentation, and source code synced, useful,
28
+ # and correct. Among its features, it introduces the @readme tag
29
+ # that enables you to embed code comments directly into README sections,
30
+ # eliminating redundancy and keeping documentation consistent
31
+ # across your codebase and project README.
23
32
  #
24
- # For a glimpse of how it works, check out the [README_YARD.md](https://github.com/mattruzicka/readme_yard/blob/main/README_YARD.md)
25
- # template from which this gem's README was generated.
33
+ # Look at the [README_YARD.md](https://github.com/mattruzicka/readme_yard/blob/main/README_YARD.md)
34
+ # template for this project to see how it works.
26
35
  # If you're reading the README, that means this text is here
27
- # because `{@readme ReadmeYard}` is in the README_YARD file
28
- # and someone ran `readme build` at the command line.
36
+ # because the custom `{@readme ReadmeYard}` markdown tag is in
37
+ # README_YARD.md and `readme build` was run at the command line.
29
38
  #
30
39
  # Here's the [full documentation](https://rubydoc.info/github/mattruzicka/readme_yard).
31
40
  #
32
41
  class ReadmeYard
33
- #
34
- # @readme comment
35
- #
36
- # @example
37
- # ReadmeYard.hello_world #=> "Hello 🌎 🌍 🌏"
38
- #
39
- def self.hello_world
40
- "Hello 🌎 🌍 🌏"
41
- end
42
-
43
42
  #
44
43
  # @see #command_line_usage
45
44
  #
@@ -49,23 +48,13 @@ class ReadmeYard
49
48
  case arg
50
49
  when "build"
51
50
  readme_yard.build(options: options)
52
- when "doc"
53
- readme_yard.doc(options: options)
51
+ when "yard"
52
+ readme_yard.yard(options: options)
54
53
  else
55
- puts TTY::Markdown.parse(readme_yard.command_line_usage)
54
+ Logger.puts_md(readme_yard.command_line_usage)
56
55
  end
57
56
  rescue Error => e
58
- puts TTY::Markdown.parse(e.message)
59
- end
60
-
61
- TAG_CLASS_LOOKUP = { "readme" => ReadmeTag,
62
- "example" => ExampleTag,
63
- "source" => SourceTag,
64
- "comment" => CommentTag,
65
- "object" => ObjectTag }.freeze
66
-
67
- def self.lookup_tag_class(tag_name)
68
- TAG_CLASS_LOOKUP[tag_name]
57
+ Logger.puts_md(e.message)
69
58
  end
70
59
 
71
60
  def initialize
@@ -85,13 +74,13 @@ class ReadmeYard
85
74
  #
86
75
  # `readme build` - Reads from README_YARD.md and writes to README.md.
87
76
  #
88
- # `readme doc` - Same as `readme build` + generates yard docs.
77
+ # `readme yard` - Same as `readme build` + generates yard docs.
89
78
  #
90
79
  def command_line_usage
91
80
  yard_parse_this_file
92
81
  yard_object = YARD::Registry.at("#{self.class.name}##{__method__}")
93
82
  yard_tags = yard_object.tags(:readme)
94
- "\n#{ReadmeTag.format_markdown(yard_object, yard_tags)}\n\n"
83
+ "\n#{ReadmeTag.format_tags(yard_object, yard_tags)}\n\n"
95
84
  end
96
85
 
97
86
  #
@@ -105,22 +94,18 @@ class ReadmeYard
105
94
  # `-q` silence yardoc output statements
106
95
  #
107
96
  def build(options: "-nq")
108
- find_or_upsert_yardopts
109
- run_yardoc(options: options)
97
+ YARD::CLI::Yardoc.run(options || "-nq")
110
98
  File.write(readme_path, gsub_tags!(readme_yard_md))
111
99
  end
112
100
 
113
101
  #
114
102
  # @readme Same as "build" + generates yard docs.
115
103
  #
116
- def doc(options: "-q")
104
+ def yard(options: "-q")
105
+ YardOptsManager.upsert_yardopts
117
106
  build(options: options || "-q")
118
107
  end
119
108
 
120
- def run_yardoc(options: "-nq")
121
- YARD::CLI::Yardoc.run(options || "-nq")
122
- end
123
-
124
109
  private
125
110
 
126
111
  def readme_yard_md
@@ -144,43 +129,6 @@ class ReadmeYard
144
129
  " for usage.\n\n{@readme ReadmeYard#default_readme_yard_md}"
145
130
  end
146
131
 
147
- YARDOPTS_FILE = ".yardopts"
148
-
149
- def find_or_upsert_yardopts
150
- File.exist?(YARDOPTS_FILE) ? update_yardopts_file : create_yardopts_file
151
- end
152
-
153
- def update_yardopts_file
154
- text = File.read(YARDOPTS_FILE)
155
- text_addition = build_yardopts_text_addition(text)
156
- File.open(YARDOPTS_FILE, "a") { |f| f.write(text_addition) } if text_addition
157
- end
158
-
159
- def build_yardopts_text_addition(yardopts_text)
160
- return if yardopts_text.match?(/\s*--plugin\s+readme\W/)
161
-
162
- readme_plugin_opts = default_readme_plugin_opts(yardopts_text)
163
- case yardopts_text
164
- when /\s*--markup\s+markdown/, /\s*-m\s+markdown/
165
- readme_plugin_opts
166
- when /\s*--markup\s/, /\s*-m\s/
167
- warn_about_supported_markdown
168
- readme_plugin_opts
169
- else
170
- readme_plugin_opts << "--markup markdown\n"
171
- end
172
- end
173
-
174
- def default_readme_plugin_opts(yardopts_text)
175
- readme_opts = +""
176
- readme_opts << "\n" unless yardopts_text.lines.last.include?("\n")
177
- readme_opts << "--plugin readme\n"
178
- end
179
-
180
- def create_yardopts_file
181
- File.write(YARDOPTS_FILE, "--plugin readme\n--markup markdown\n")
182
- end
183
-
184
132
  def gsub_tags!(markdown)
185
133
  markdown.gsub!(/([^`]|^)(\{@\w+\s.+\})/) { |string| format_tag_markdown(string) }
186
134
  markdown
@@ -201,12 +149,17 @@ class ReadmeYard
201
149
 
202
150
  def format_yard_tags_markdown(yard_object, tag_name, string)
203
151
  yard_tags = yard_object.tags(tag_name)
152
+ tag_class = TagRegistry.find_class(tag_name)
153
+
204
154
  if yard_tags.empty?
205
- warn_about_yard_tags_not_found(yard_object, tag_name)
206
- string
155
+ if tag_class.respond_to?(:format_yard_object)
156
+ tag_class.format_yard_object(yard_object) || string
157
+ else
158
+ Logger.warn("The `@#{tag_name}` tag is missing from `#{yard_object}`.")
159
+ string
160
+ end
207
161
  else
208
- tag_class = self.class.lookup_tag_class(tag_name)
209
- tag_class.format_markdown(yard_object, yard_tags)
162
+ tag_class.format_tags(yard_object, yard_tags)
210
163
  end
211
164
  end
212
165
 
@@ -223,13 +176,4 @@ class ReadmeYard
223
176
  current_file_path = File.join(gem_spec.full_gem_path, "lib", "readme_yard.rb")
224
177
  YARD.parse(current_file_path)
225
178
  end
226
-
227
- def warn_about_supported_markdown
228
- Logger.warn "\n*Readme Yard* works best with markdown." \
229
- "\nConsider adding `--markup markdown` to your `.yardopts` file.\n\n"
230
- end
231
-
232
- def warn_about_yard_tags_not_found(yard_object, tag_name)
233
- Logger.warn "The `@#{tag_name}` *Readme Yard* tag is missing from #{yard_object}."
234
- end
235
179
  end
data/readme_yard.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
 
11
11
  spec.summary = "Build a better README with YARD."
12
12
  spec.homepage = "https://github.com/mattruzicka/readme_yard"
13
- spec.required_ruby_version = ">= 2.5"
13
+ spec.required_ruby_version = ">= 3.0"
14
14
 
15
15
  spec.metadata["homepage_uri"] = spec.homepage
16
16
  spec.metadata["source_code_uri"] = spec.homepage
@@ -22,12 +22,16 @@ Gem::Specification.new do |spec|
22
22
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
23
23
  end
24
24
 
25
- spec.executables << "readme"
25
+ spec.bindir = "bin"
26
+ spec.executables = ["readme"]
26
27
  spec.require_paths = ["lib"]
27
28
 
28
29
  # Uncomment to register a new dependency of your gem
29
30
  spec.add_dependency "tty-markdown", "~> 0.7"
30
- spec.add_dependency "yard-readme", "~> 0.3"
31
+ spec.add_dependency "yard", "~> 0.9"
32
+ spec.add_dependency "yard-readme", "~> 0.5"
33
+
34
+ spec.add_development_dependency "irb"
31
35
 
32
36
  # For more information and examples about making a new gem, checkout our
33
37
  # guide at: https://bundler.io/guides/creating_gem.html
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: readme_yard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Ruzicka
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2021-08-08 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: tty-markdown
@@ -24,22 +23,48 @@ dependencies:
24
23
  - - "~>"
25
24
  - !ruby/object:Gem::Version
26
25
  version: '0.7'
26
+ - !ruby/object:Gem::Dependency
27
+ name: yard
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '0.9'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.9'
27
40
  - !ruby/object:Gem::Dependency
28
41
  name: yard-readme
29
42
  requirement: !ruby/object:Gem::Requirement
30
43
  requirements:
31
44
  - - "~>"
32
45
  - !ruby/object:Gem::Version
33
- version: '0.3'
46
+ version: '0.5'
34
47
  type: :runtime
35
48
  prerelease: false
36
49
  version_requirements: !ruby/object:Gem::Requirement
37
50
  requirements:
38
51
  - - "~>"
39
52
  - !ruby/object:Gem::Version
40
- version: '0.3'
41
- description:
42
- email:
53
+ version: '0.5'
54
+ - !ruby/object:Gem::Dependency
55
+ name: irb
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
43
68
  executables:
44
69
  - readme
45
70
  extensions: []
@@ -59,14 +84,18 @@ files:
59
84
  - bin/readme
60
85
  - bin/setup
61
86
  - lib/readme_yard.rb
87
+ - lib/readme_yard/code_tag.rb
62
88
  - lib/readme_yard/comment_tag.rb
63
89
  - lib/readme_yard/error.rb
64
90
  - lib/readme_yard/example_tag.rb
65
91
  - lib/readme_yard/logger.rb
66
- - lib/readme_yard/object_tag.rb
67
92
  - lib/readme_yard/readme_tag.rb
68
93
  - lib/readme_yard/source_tag.rb
94
+ - lib/readme_yard/string_tag.rb
95
+ - lib/readme_yard/tag_registry.rb
96
+ - lib/readme_yard/value_tag.rb
69
97
  - lib/readme_yard/version.rb
98
+ - lib/readme_yard/yard_opts_manager.rb
70
99
  - readme_yard.gemspec
71
100
  homepage: https://github.com/mattruzicka/readme_yard
72
101
  licenses:
@@ -75,7 +104,6 @@ metadata:
75
104
  homepage_uri: https://github.com/mattruzicka/readme_yard
76
105
  source_code_uri: https://github.com/mattruzicka/readme_yard
77
106
  changelog_uri: https://github.com/mattruzicka/readme_yard/blob/master/CHANGELOG.md
78
- post_install_message:
79
107
  rdoc_options: []
80
108
  require_paths:
81
109
  - lib
@@ -83,15 +111,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
83
111
  requirements:
84
112
  - - ">="
85
113
  - !ruby/object:Gem::Version
86
- version: '2.5'
114
+ version: '3.0'
87
115
  required_rubygems_version: !ruby/object:Gem::Requirement
88
116
  requirements:
89
117
  - - ">="
90
118
  - !ruby/object:Gem::Version
91
119
  version: '0'
92
120
  requirements: []
93
- rubygems_version: 3.2.3
94
- signing_key:
121
+ rubygems_version: 3.6.8
95
122
  specification_version: 4
96
123
  summary: Build a better README with YARD.
97
124
  test_files: []
@@ -1,21 +0,0 @@
1
- class ReadmeYard
2
- #
3
- # @readme
4
- # ```@readme object``` - Embeds the comment and source.
5
- #
6
- class ObjectTag
7
- class << self
8
- #
9
- # This method's comment and code is in the README because
10
- # because `@readme object` is below, in the actual source code.
11
- #
12
- # @readme object
13
- #
14
- def format_tag_markdown(yard_object, _tag)
15
- text = CommentTag.format_docstring_as_comment(yard_object)
16
- text << "\n#{yard_object.source}"
17
- ExampleTag.format_ruby(text)
18
- end
19
- end
20
- end
21
- end