Class: Markascend::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/markascend/env.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(autolink: %w[http https ftp mailto], inline_img: false, sandbox: false, toc: false, **opts) ⇒ Env

Returns a new instance of Env.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/markascend/env.rb', line 7

def initialize(autolink: %w[http https ftp mailto], inline_img: false, sandbox: false, toc: false, **opts)
  @autolink = autolink
  @inline_img = inline_img
  @sandbox = sandbox
  @toc = toc ? {} : false # {id => [x, header_content]}

  if opts[:path]
    pwd = File.dirname opts[:path]
    if File.directory?(pwd)
      @pwd = pwd
    end
  end

  if opts[:macros]
    @macros = {}
    opts[:macros].each do |m|
      meth = "parse_#{m}"
      if Macro.respond_to?(meth)
        @macros[m] = meth
      else
        raise ArgumentError, "macro processor #{meth} not defined"
      end
    end
  elsif @sandbox
    @macros = SANDBOX_MACROS
  else
    @macros = DEFAULT_MACROS
  end

  if opts[:line_units]
    @line_units = opts[:line_units].map do |m|
      meth = "parse_#{m}"
      if LineUnit.respond_to?(meth)
        meth
      else
        raise ArgumentError, "line-unit parser #{meth} not defined"
      end
    end
  else
    @line_units = DEFAULT_LINE_UNITS
  end

  @scope = opts[:scope] || Object.new.send(:binding)
  @options = {}   # for \options macro
  @footnotes = {} # {abbrev => details}. for [.] and [:] elements
  @srcs = []      # recursive parser stack, everyone has the contiguous right one scanned
  @warnings = {}  # {line => message}
  @hi = nil       # current syntax hiliter
end

Instance Attribute Details

Returns the value of attribute autolink.



3
4
5
# File 'lib/markascend/env.rb', line 3

def autolink
  @autolink
end

#footnotesObject (readonly)

Returns the value of attribute footnotes.



4
5
6
# File 'lib/markascend/env.rb', line 4

def footnotes
  @footnotes
end

#hiObject

Returns the value of attribute hi.



5
6
7
# File 'lib/markascend/env.rb', line 5

def hi
  @hi
end

#inline_imgObject (readonly)

Returns the value of attribute inline_img.



3
4
5
# File 'lib/markascend/env.rb', line 3

def inline_img
  @inline_img
end

#line_unitsObject (readonly)

Returns the value of attribute line_units.



4
5
6
# File 'lib/markascend/env.rb', line 4

def line_units
  @line_units
end

#macrosObject (readonly)

Returns the value of attribute macros.



4
5
6
# File 'lib/markascend/env.rb', line 4

def macros
  @macros
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/markascend/env.rb', line 4

def options
  @options
end

#pwdObject (readonly)

Returns the value of attribute pwd.



3
4
5
# File 'lib/markascend/env.rb', line 3

def pwd
  @pwd
end

#sandboxObject (readonly)

Returns the value of attribute sandbox.



3
4
5
# File 'lib/markascend/env.rb', line 3

def sandbox
  @sandbox
end

#scopeObject (readonly)

Returns the value of attribute scope.



4
5
6
# File 'lib/markascend/env.rb', line 4

def scope
  @scope
end

#srcsObject (readonly)

Returns the value of attribute srcs.



4
5
6
# File 'lib/markascend/env.rb', line 4

def srcs
  @srcs
end

#tocObject (readonly)

Returns the value of attribute toc.



3
4
5
# File 'lib/markascend/env.rb', line 3

def toc
  @toc
end

#warningsObject (readonly)

Returns the value of attribute warnings.



4
5
6
# File 'lib/markascend/env.rb', line 4

def warnings
  @warnings
end

Instance Method Details

#warn(msg) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/markascend/env.rb', line 57

def warn msg
  if @srcs.size
    current_src = @srcs.last
    line = @srcs.first.string.count("\n") - current_src.string[(current_src.pos)..-1].count("\n")
    @warnings[line] = msg
  else
    # warnings without source is set to line 0
    @warnings[0] = msg
  end
end