class YARDReadme::DocstringParser

@readme

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.

Attributes

readme_tag_names[RW]

Public Class Methods

readme_tag_names_regex() click to toggle source
# 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?() click to toggle source
# 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) click to toggle source
# 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

Public Instance Methods

parse_content(content) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/yard-readme/docstring_parser.rb, line 98
def parse_readme_text?(tag_name, buf)
  tag_name == 'readme' && !buf.empty?
end