Class: Mdoc::Document

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mdoc/document.rb,
lib/mdoc/document/kramdown.rb

Direct Known Subclasses

Kramdown

Defined Under Namespace

Classes: Kramdown

Constant Summary collapse

LOOP_MAX =
99

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Document

rubocop:disable MethodLength



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
56
57
58
59
60
61
62
# File 'lib/mdoc/document.rb', line 14

def initialize(path)
  if path.is_a?(String)
    @file = path
    path = File.new(@file, 'r:bom|utf-8')
  end

  # initialize performed processor list
  @performed = {}

  position = nil # before meta, :meta, :body, :between, :pandoc_header
  pandoc_meta, raw_meta = [], ''
  @meta, @body = Meta.new, ''
  path.each do |line|
    # puts position.to_s + line if position
    line.chomp!

    if line.match(/^\s*$/)
      next if position.nil? || position == :between
    else
      position = :body if position == :between
    end

    if line.match(/^\%?\s*\-{3,}\s*$/) # meta headers
      position = :between if position == :meta
      position = :meta unless position
      next
    elsif line.match(/^\%\s*/)
      position = :pandoc_header if position.nil?
    else
      position = :body unless position # if position == :pandoc_header
    end

    if position == :pandoc_header
      pandoc_meta << line.gsub(/^\%\s*/, '')
      position = :between if pandoc_meta.size >= 3
    end

    raw_meta << line << "\n" if position == :meta
    @body << line << "\n" if position == :body
  end

  @meta.load(raw_meta) if raw_meta.size > 0
  if pandoc_meta.size > 0
    @meta.title, @meta.author, @meta.date = pandoc_meta[0 .. 2]
  end

  # source meta holds meta information from source file only
  @smeta = @meta.dup
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



9
10
11
# File 'lib/mdoc/document.rb', line 9

def body
  @body
end

#fileObject

Returns the value of attribute file.



9
10
11
# File 'lib/mdoc/document.rb', line 9

def file
  @file
end

#metaObject

Returns the value of attribute meta.



9
10
11
# File 'lib/mdoc/document.rb', line 9

def meta
  @meta
end

#out_fileObject

Returns the value of attribute out_file.



9
10
11
# File 'lib/mdoc/document.rb', line 9

def out_file
  @out_file
end

#performedObject

Returns the value of attribute performed.



9
10
11
# File 'lib/mdoc/document.rb', line 9

def performed
  @performed
end

#smetaObject

Returns the value of attribute smeta.



9
10
11
# File 'lib/mdoc/document.rb', line 9

def smeta
  @smeta
end

#tpl_fileObject

Returns the value of attribute tpl_file.



9
10
11
# File 'lib/mdoc/document.rb', line 9

def tpl_file
  @tpl_file
end

Instance Method Details

#apply!(pn) ⇒ Object

apply processors by processor name (if corresponding processor) class defined. rubocop:disable MethodLength



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mdoc/document.rb', line 68

def apply!(pn)
  prc = Mdoc.get_processor(pn)
  if performed[prc]
    if prc.new.repeatable?
      prc.new.process! self
      performed[prc] += 1
      # error if performed too many times (prevent dead loop)
      raise "loop max reached: #{prc}" if performed[prc] > LOOP_MAX
    end
  else # not performed
    prc.new.process! self
    performed[prc] = 1
  end
end