Class: YARDReadme::DocstringParser

Inherits:
Object
  • Object
show all
Defined in:
lib/yard-readme/docstring_parser.rb

Overview

Ideally, I or someone would open a YARD PR to make leaving the tag text an option when registering a tag. Especially since this plugin's current behavior of replacing the YARD::DocstringParser could potentially conflict with other YARD plugins and is also more vulnerable to change.

Doing so would make this custom parser obsolete.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.readme_tag_namesObject

Returns the value of attribute readme_tag_names.



14
15
16
# File 'lib/yard-readme/docstring_parser.rb', line 14

def readme_tag_names
  @readme_tag_names
end

Class Method Details

.readme_tag_names_regexObject



16
17
18
# File 'lib/yard-readme/docstring_parser.rb', line 16

def readme_tag_names_regex
  @readme_tag_names_regex ||= /\A(#{readme_tag_names.join("|")})/
end

.readme_tag_names_regex?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/yard-readme/docstring_parser.rb', line 20

def readme_tag_names_regex?
  readme_tag_names && !readme_tag_names.empty?
end

.strip_readme_tag_arg(text) ⇒ Object



24
25
26
27
28
# File 'lib/yard-readme/docstring_parser.rb', line 24

def strip_readme_tag_arg(text)
  return text unless readme_tag_names_regex?

  text.sub(readme_tag_names_regex, "")
end

Instance Method Details

#parse_content(content) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/yard-readme/docstring_parser.rb', line 31

def parse_content(content)
  content = content.split(/\r?\n/) if content.is_a?(String)
  return '' if !content || content.empty?
  docstring = String.new("")

  indent = content.first[/^\s*/].length
  last_indent = 0
  orig_indent = 0
  directive = false
  last_line = ""
  tag_name = nil
  tag_buf = []

  (content + ['']).each_with_index do |line, index|
    indent = line[/^\s*/].length
    empty = (line =~ /^\s*$/ ? true : false)
    done = content.size == index

    if tag_name && (((indent < orig_indent && !empty) || done ||
        (indent == 0 && !empty)) || (indent <= last_indent && line =~ META_MATCH))
      buf = tag_buf.join("\n")
      if directive || tag_is_directive?(tag_name)
        directive = create_directive(tag_name, buf)
        if directive
          docstring << parse_content(directive.expanded_text).chomp
        end
      else
        readme_text = parse_readme_text(buf) if parse_readme_text?(tag_name, buf)
        docstring << readme_text if readme_text
        create_tag(tag_name, buf)
      end
      tag_name = nil
      tag_buf = []
      directive = false
      orig_indent = 0
    end

    # Found a meta tag
    if line =~ META_MATCH
      directive = $1
      tag_name = $2
      tag_buf = [($3 || '')]
    elsif tag_name && indent >= orig_indent && !empty
      orig_indent = indent if orig_indent == 0
      # Extra data added to the tag on the next line
      last_empty = last_line =~ /^[ \t]*$/ ? true : false

      tag_buf << '' if last_empty
      tag_buf << line.gsub(/^[ \t]{#{orig_indent}}/, '')
    elsif !tag_name
      # Regular docstring text
      docstring << line
      docstring << "\n"
    end

    last_indent = indent
    last_line = line
  end

  docstring
end

#parse_readme_text(text) ⇒ Object



93
94
95
96
# File 'lib/yard-readme/docstring_parser.rb', line 93

def parse_readme_text(text)
  readme_text = self.class.strip_readme_tag_arg(text)
  readme_text << "\n\n" if readme_text
end

#parse_readme_text?(tag_name, buf) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/yard-readme/docstring_parser.rb', line 98

def parse_readme_text?(tag_name, buf)
  tag_name == 'readme' && !buf.empty?
end