class Mdoc::Document

Constants

LOOP_MAX

Attributes

body[RW]
file[RW]
meta[RW]
out_file[RW]
performed[RW]
smeta[RW]
tpl_file[RW]

Public Class Methods

new(path) click to toggle source

rubocop:disable MethodLength

# 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

Public Instance Methods

apply!(pn) click to toggle source

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

# 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