Class: YARD::I18n::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/yard/i18n/text.rb

Overview

Provides some convenient features for translating a text.

Since:

  • 0.8.0

Instance Method Summary collapse

Constructor Details

#initialize(input, options = {}) ⇒ Text

Creates a text object that has translation related features for the input text.

Parameters:

  • input (#each_line)

    a text to be translated.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :have_header (Boolean) — default: false

    whether the input text has header or not.

Since:

  • 0.8.0



11
12
13
14
# File 'lib/yard/i18n/text.rb', line 11

def initialize(input, options={})
  @input = input
  @options = options
end

Instance Method Details

#extract_messages {|:attribute, name, value, line_no| ... } ⇒ void

This method returns an undefined value.

Extracts translation target messages from @input.

Yields:

  • (:attribute, name, value, line_no)

    the block that recieves extracted an attribute in header. It may called many times.

  • (:paragraph, text, start_line_no)

    the block that recieves extracted a paragraph in body. Paragraph is a text block separated by one or more empty lines. Empty line is a line that contains only zero or more whitespaces. It may called many times.

Yield Parameters:

  • name (String)

    the name of extracted attribute.

  • value (String)

    the value of extracted attribute.

  • line_no (Integer)

    the defined line number of extracted attribute.

  • text (String)

    the text of extracted paragraph.

  • start_line_no (Integer)

    the start line number of extracted paragraph.

Since:

  • 0.8.0



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
# File 'lib/yard/i18n/text.rb', line 34

def extract_messages
  paragraph = ""
  paragraph_start_line = 0
  line_no = 0
  in_header = @options[:have_header]

  @input.each_line do |line|
    line_no += 1
    if in_header
      case line
      when /^#!\S+\s*$/
        in_header = false unless line_no == 1
      when /^\s*#\s*@(\S+)\s*(.+?)\s*$/
        name, value = $1, $2
        yield(:attribute, name, value, line_no)
      else
        in_header = false
        next if line.chomp.empty?
      end
      next if in_header
    end

    case line
    when /^\s*$/
      next if paragraph.empty?
      yield(:paragraph, paragraph.rstrip, paragraph_start_line)
      paragraph = ""
    else
      paragraph_start_line = line_no if paragraph.empty?
      paragraph << line
    end
  end
  unless paragraph.empty?
    yield(:paragraph, paragraph.rstrip, paragraph_start_line)
  end
end