Method: Mdoc::Document#initialize

Defined in:
lib/mdoc/document.rb

#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