Module: MaRuKu::Strings

Included in:
In::Markdown::BlockLevelParser, MDElement
Defined in:
lib/maruku/string_utils.rb,
lib/maruku.rb

Overview

Utility functions for dealing with strings.

Constant Summary collapse

TAB_SIZE =
4

Instance Method Summary collapse

Instance Method Details

#parse_email_headers(s) ⇒ Symbol => String

Parses email headers, returning a hash. ‘hash` is the message; that is, anything past the headers.

Keys are downcased and converted to symbols; spaces become underscores. For example:

My key: true

becomes:

{:my_key => true}

Parameters:

Returns:

  • (Symbol => String)

    The header values



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/maruku/string_utils.rb', line 33

def parse_email_headers(s)
  headers = {}
  scanner = StringScanner.new(s)

  while scanner.scan(/(\w[\w\s\-]+): +(.*)\n/)
    k, v = normalize_key_and_value(scanner[1], scanner[2])
    headers[k.to_sym] = v
  end

  headers[:data] = scanner.rest
  headers
end

#sanitize_ref_id(s) ⇒ String

Normalize a link reference.

Parameters:

Returns:



84
85
86
# File 'lib/maruku/string_utils.rb', line 84

def sanitize_ref_id(s)
  s.downcase.gsub(/\s+/, ' ')
end

#spaces_before_first_char(s) ⇒ Fixnum

This returns the position of the first non-list character in a list item.

spaces_before_first_char(‘*Hello’) #=> 1 spaces_before_first_char(‘* Hello’) #=> 2 spaces_before_first_char(‘ * Hello’) #=> 3 spaces_before_first_char(‘ * Hello’) #=> 5 spaces_before_first_char(‘1.Hello’) #=> 2 spaces_before_first_char(‘ 1. Hello’) #=> 5

Parameters:

Returns:

  • (Fixnum)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/maruku/string_utils.rb', line 59

def spaces_before_first_char(s)
  s = MaRuKu::MDLine.new(s.gsub(/([^\t]*)(\t)/) { $1 + " " * (TAB_SIZE - $1.length % TAB_SIZE) })
  match = case s.md_type
    when :ulist
      # whitespace, followed by ('*'|'+'|'-') followed by
      # more whitespace, followed by an optional IAL, followed
      # by yet more whitespace
      s[/^\s*(\*|\+|\-)\s*(\{[:#\.].*?\})?\s*/]
    when :olist
      # whitespace, followed by a number, followed by a period,
      # more whitespace, an optional IAL, and more whitespace
      s[/^\s*\d+\.\s*(\{[:#\.].*?\})?\s*/]
    else
      tell_user "BUG (my bad): '#{s}' is not a list"
      ''
    end
  f = /\{(.*?)\}/.match(match)
  ial = f[1] if f
  [match.length, ial]
end

#split_lines(s) ⇒ String

Split a string into multiple lines, on line feeds and/or carriage returns.

Parameters:

Returns:



13
14
15
# File 'lib/maruku/string_utils.rb', line 13

def split_lines(s)
  s.split(/\r\n|\r|\n/)
end

#strip_indent(s, n) ⇒ String

Removes indentation from the beginning of ‘s`, up to at most `n` spaces. Tabs are counted as TAB_SIZE spaces.

Parameters:

Returns:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/maruku/string_utils.rb', line 103

def strip_indent(s, n)
  while n > 0
    case s[0, 1]
    when ' '
      n -= 1
    when "\t"
      n -= TAB_SIZE
    else
      break
    end
    s = s[1..-1]
  end

  MDLine.new(s)
end

#unquote(s) ⇒ String

Remove line-initial ‘>` characters for a quotation.

Parameters:

Returns:



92
93
94
# File 'lib/maruku/string_utils.rb', line 92

def unquote(s)
  s.gsub(/^>\s?/, '')
end