Class: Rouge::Lexers::YAML

Inherits:
RegexLexer show all
Defined in:
lib/rouge/lexers/yaml.rb

Constant Summary

Constants inherited from RegexLexer

RegexLexer::MAX_NULL_SCANS

Constants included from Token::Tokens

Token::Tokens::Num, Token::Tokens::Str

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RegexLexer

append, #delegate, get_state, #get_state, #goto, #group, #groups, #in_state?, #pop!, prepend, #push, #recurse, replace_state, #reset!, #reset_stack, #stack, start, start_procs, #state, state, #state?, state_definitions, states, #step, #stream_tokens, #token

Methods inherited from Rouge::Lexer

aliases, all, assert_utf8!, #debug, default_options, demo, demo_file, desc, filenames, find, find_fancy, guess, guess_by_filename, guess_by_mimetype, guess_by_source, guesses, #initialize, lex, #lex, mimetypes, #option, #options, #reset!, #stream_tokens, tag, #tag

Methods included from Token::Tokens

token

Constructor Details

This class inherits a constructor from Rouge::Lexer

Class Method Details

.analyze_text(text) ⇒ Object



11
12
13
14
# File 'lib/rouge/lexers/yaml.rb', line 11

def self.analyze_text(text)
  # look for the %YAML directive
  return 1 if text =~ /\A\s*%YAML/m
end

Instance Method Details

#continue_indent(match) ⇒ Object



58
59
60
61
# File 'lib/rouge/lexers/yaml.rb', line 58

def continue_indent(match)
  puts "    yaml: continue_indent" if @debug
  @next_indent += match.size
end

#dedent?(level) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/rouge/lexers/yaml.rb', line 33

def dedent?(level)
  level < self.indent
end

#indentObject



28
29
30
31
# File 'lib/rouge/lexers/yaml.rb', line 28

def indent
  raise 'empty indent stack!' if @indent_stack.empty?
  @indent_stack.last
end

#indent?(level) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/rouge/lexers/yaml.rb', line 37

def indent?(level)
  level > self.indent
end

#reset_indentObject

reset the indentation levels



21
22
23
24
25
26
# File 'lib/rouge/lexers/yaml.rb', line 21

def reset_indent
  puts "    yaml: reset_indent" if @debug
  @indent_stack = [0]
  @next_indent = 0
  @block_scalar_indent = nil
end

#save_indent(match) ⇒ Object

Save a possible indentation level



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rouge/lexers/yaml.rb', line 42

def save_indent(match)
  @next_indent = match.size
  puts "    yaml: indent: #{self.indent}/#@next_indent" if @debug
  puts "    yaml: popping indent stack - before: #@indent_stack" if @debug
  if dedent?(@next_indent)
    @indent_stack.pop while dedent?(@next_indent)
    puts "    yaml: popping indent stack - after: #@indent_stack" if @debug
    puts "    yaml: indent: #{self.indent}/#@next_indent" if @debug

    # dedenting to a state not previously indented to is an error
    [match[0...self.indent], match[self.indent..-1]]
  else
    [match, '']
  end
end

#set_indent(match, opts = {}) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/rouge/lexers/yaml.rb', line 63

def set_indent(match, opts={})
  if indent < @next_indent
    @indent_stack << @next_indent
  end

  @next_indent += match.size unless opts[:implicit]
end