Class: Markdownplus::CodeBlock

Inherits:
Block
  • Object
show all
Defined in:
lib/markdownplus/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Block

#append, #errors, #lines, #source, #source=, #warnings

Constructor Details

#initialize(value = nil) ⇒ CodeBlock

Returns a new instance of CodeBlock.



133
134
135
# File 'lib/markdownplus/parser.rb', line 133

def initialize(value=nil)
  @directives = value.split("|").collect{|v| v.strip} if value
end

Instance Attribute Details

#directivesObject (readonly)

Returns the value of attribute directives.



131
132
133
# File 'lib/markdownplus/parser.rb', line 131

def directives
  @directives
end

Instance Method Details

#executable?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/markdownplus/parser.rb', line 150

def executable?
  first_directive == "execute"
end

#first_directiveObject



170
171
172
# File 'lib/markdownplus/parser.rb', line 170

def first_directive
  directives.first if directives
end

#includable?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/markdownplus/parser.rb', line 146

def includable?
  first_directive == "include"
end

#includeObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/markdownplus/parser.rb', line 154

def include
  if includable?
    if lines.count == 0
      self.warnings << "No url given"
    else
      self.warnings << "More than one line given" if lines.count > 1
      begin
        self.source = open(lines.first).read
      rescue => e
        self.errors << "Error opening [#{lines.first}] [#{e.message}]"
      end
    end
    directives.shift
  end
end

#markdownObject



137
138
139
140
141
142
143
144
# File 'lib/markdownplus/parser.rb', line 137

def markdown
  s = source
  if s.end_with?("\n")
    result = "```#{directives.join("|")}\n#{source}```\n"
  else
    result = "```#{directives.join("|")}\n#{source}\n```\n"
  end
end