Module: MaRuKu::Strings

### Home Classes Methods

### In Files

- lib/maruku.rb
- lib/maruku/string_utils.rb

### Methods

- #parse_email_headers
- #sanitize_ref_id
- #spaces_before_first_char
- #split_lines
- #strip_indent
- #unquote

### Class/Module Index

Quicksearch

- MaRuKu
- MaRuKu::AttributeList
- MaRuKu::Errors
- MaRuKu::Exception
- MaRuKu::HTMLElement
- MaRuKu::HTMLFragment
- MaRuKu::Helpers
- MaRuKu::In
- MaRuKu::In::Markdown
- MaRuKu::In::Markdown::BlockLevelParser
- MaRuKu::In::Markdown::BlockLevelParser::BlockContext
- MaRuKu::In::Markdown::BlockLevelParser::LineSource
- MaRuKu::In::Markdown::SpanExtension
- MaRuKu::In::Markdown::SpanLevelParser
- MaRuKu::In::Markdown::SpanLevelParser::CharSourceDebug
- MaRuKu::In::Markdown::SpanLevelParser::CharSourceManual
- MaRuKu::In::Markdown::SpanLevelParser::CharSourceStrscan
- MaRuKu::In::Markdown::SpanLevelParser::HTMLHelper
- MaRuKu::In::Markdown::SpanLevelParser::SpanContext
- MaRuKu::MDDocument
- MaRuKu::MDElement
- MaRuKu::MDHTMLElement
- MaRuKu::MDLine
- MaRuKu::NokogiriHTMLFragment
- MaRuKu::Out
- MaRuKu::Out::EntityTable
- MaRuKu::Out::HTML
- MaRuKu::Out::HTML::HTMLElement
- MaRuKu::Out::Latex
- MaRuKu::Out::Latex::MDDocumentExtensions
- MaRuKu::Out::Markdown
- MaRuKu::REXMLHTMLFragment
- MaRuKu::Section
- MaRuKu::Strings
- Maruku
- Maruku::In
- Maruku::In::Markdown
- Maruku::In::Markdown::FencedCode
- Object
- String
- Textile2Signature

No matching classes.

# MaRuKu::Strings

Utility functions for dealing with strings.

### Constants

TAB_SIZE

### Public Instance Methods

parse_email_headers(s) click to toggle source

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:

    !!!plain
    My key: true

becomes:

    {:my_key => true}

@param s [String] The email @return [Symbol => String] The header values

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

Normalize a link reference.

@param s [String] @return [String]

    # File lib/maruku/string_utils.rb, line 84
    def sanitize_ref_id(s)
      s.downcase.gsub(/\s+/, ' ')
    end

spaces_before_first_char(s) click to toggle source

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

@example 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

@param s [String] @return [Fixnum]

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

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

@param s [String] @return [String]

    # File lib/maruku/string_utils.rb, line 13
    def split_lines(s)
      s.split(/\r\n|\r|\n/)
    end

strip_indent(s, n) click to toggle source

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

@param s [String] @param n [Fixnum] @return [String]

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

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

@param s [String] @return [String]

    # File lib/maruku/string_utils.rb, line 92
    def unquote(s)
      s.gsub(/^>\s?/, '')
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.