Class: PointBlank::Parsing::NullInline Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/mmmd/blankshell.rb

Overview

This class is abstract.

Null inline scanner element

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.parser_forObject

Returns the value of attribute parser_for.



1010
1011
1012
# File 'lib/mmmd/blankshell.rb', line 1010

def parser_for
  @parser_for
end

Class Method Details

.build(children) ⇒ ::PointBlank::DOM::DOMObject

Build child

Parameters:

  • children (Array)

Returns:



1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
# File 'lib/mmmd/blankshell.rb', line 1087

def self.build(children)
  obj = parser_for.new
  if parser_for.valid_children.empty?
    children.each do |child|
      child = child.first if child.is_a? Array
      child = construct_text(child) if child.is_a? String
      obj.append_child(child)
    end
  else
    tokens = children.map do |child|
      child.is_a?(Array) ? child.first : child
    end
    scanner = StackScanner.new(obj, init_tokens: tokens)
    scanner.scan
  end
  obj
end

.check_contents(elements) ⇒ Boolean

Check that contents can be contained within this element

Parameters:

  • elements (Array<String, Array(String, Class, Symbol)>)

Returns:

  • (Boolean)


1130
1131
1132
1133
1134
1135
1136
1137
1138
# File 'lib/mmmd/blankshell.rb', line 1130

def self.check_contents(elements)
  elements.each do |element|
    next unless element.is_a? ::PointBlank::DOM::DOMObject
    next if parser_for.valid_children.include? element.class

    return false
  end
  true
end

.check_unescaped(index, string) ⇒ nil, Integer

Check that the symbol at this index is not escaped

Parameters:

  • index (Integer)
  • string (String)

Returns:

  • (nil, Integer)


1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
# File 'lib/mmmd/blankshell.rb', line 1034

def self.check_unescaped(index, string)
  return index if index.zero?

  count = 0
  index -= 1
  while index >= 0 && string[index] == "\\"
    count += 1
    index -= 1
  end
  (count % 2).zero?
end

.construct_literal(string) ⇒ ::PointBlank::DOM::Text

Construct text literal for a string

Parameters:

  • string (String)

Returns:



1121
1122
1123
1124
1125
# File 'lib/mmmd/blankshell.rb', line 1121

def self.construct_literal(string)
  obj = ::PointBlank::DOM::Text.new
  obj.content = string
  obj
end

.construct_text(string) ⇒ ::PointBlank::DOM::Text

Construct text object for a string

Parameters:

  • string (String)

Returns:



1108
1109
1110
1111
1112
1113
1114
1115
1116
# File 'lib/mmmd/blankshell.rb', line 1108

def self.construct_text(string)
  obj = ::PointBlank::DOM::Text.new
  string = string.gsub(/\\([!"\#$%&'()*+,\-.\/:;<=>?@\[\\\]\^_`{|}~])/,
                       '\\1')
  string = string.gsub("\n", " ")
  string = MMMD::EntityUtils.decode_entities(string)
  obj.content = string
  obj
end

.find_unescaped(string, pattern) ⇒ Integer?

Find the first occurence of an unescaped pattern

Parameters:

  • string (String)
  • pattern (Regexp, String)

Returns:

  • (Integer, nil)


1050
1051
1052
1053
1054
1055
1056
1057
1058
# File 'lib/mmmd/blankshell.rb', line 1050

def self.find_unescaped(string, pattern)
  initial = 0
  while (index = string.index(pattern, initial))
    return index if check_unescaped(index, string)

    initial = index + 1
  end
  nil
end

.forward_walk(backlog) ⇒ Array<Array(String, Class, Symbol), String>

Forward-walk the backlog starting from the current valid element

Parameters:

  • backlog (Array<Array(String, Class, Symbol), String>)

Returns:

  • (Array<Array(String, Class, Symbol), String>)


# File 'lib/mmmd/blankshell.rb', line 1025

.iterate_tokens(string, pattern, &filter) ⇒ Array<String, Array(String, Class, Symbol)>

Iterate over every string/unescaped token part

Parameters:

  • string (String)
  • pattern (Regexp)
  • callback (#call)

Returns:

  • (Array<String, Array(String, Class, Symbol)>)


1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
# File 'lib/mmmd/blankshell.rb', line 1065

def self.iterate_tokens(string, pattern, &filter)
  tokens = []
  initial = 0
  while (index = string.index(pattern, initial))
    prefix = (index.zero? ? nil : string[initial..(index - 1)])
    tokens.append(prefix) if prefix
    unescaped = check_unescaped(index, string)
    match = filter.call(index.positive? ? string[..(index - 1)] : "",
                        string[index..],
                        unescaped)
    tokens.append(match)
    match = match.first if match.is_a? Array
    initial = index + match.length
  end
  remaining = string[initial..] || ""
  tokens.append(remaining) unless remaining.empty?
  tokens
end

.reverse_walk(backlog) ⇒ Array<Array(String, Class, Symbol), String>

Reverse-walk the backlog and construct a valid element from it

Parameters:

  • backlog (Array<Array(String, Class, Symbol), String>)

Returns:

  • (Array<Array(String, Class, Symbol), String>)


# File 'lib/mmmd/blankshell.rb', line 1020

.tokenize(string) ⇒ Array<Array(String, Class, Symbol), String>

Tokenize a string

Parameters:

  • string (String)

Returns:

  • (Array<Array(String, Class, Symbol), String>)


1016
1017
1018
# File 'lib/mmmd/blankshell.rb', line 1016

def self.tokenize(string)
  [string]
end