Sha256: 2dbefb47c795dee698ae3885886315209159c7586f79a5ee6fcbf24a7c8f1ec1

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

require "PDFlib"
Dir[File.dirname(__FILE__) + "/node/*.rb"].each do |f|
  require f.sub(/\.rb$/, '')
end
module FN
  module Node
    class Context
      attr_reader :doc
      attr_accessor :current
      
      def initialize
        @doc = XML::Document.new
        @doc.root = @current = FN::Node::Root()
      end
      
      def <<(node)
        add node
        @current = node
      end
      
      def root
        doc.root
      end
      
      def root?
        current == root
      end
      
      def add(node)
        @current << node
      end
      
      def retain_after
        old = @current
        yield self
        @current = old
      end
      
      def visit(struct)
        root.visit(struct)
        struct
      end
      
      def pre(item)
        if @current.first? 
          @current.first.prev = item
        else
          @current << item
        end
      end
      
      def inject_at_head(item)
        head = @doc.find_first("//begin_document")
        if head.first? 
          head.first.prev = item
        else
          head << item
        end
      end
      
      def inject_at_page(number)
        old = @current
        @current = @doc.find_first("//begin_page_ext[@number='#{number}']") or 
                   raise "page not found: #{number}.  Pages: #{@doc.find('//begin_page_ext').map{|n|n.to_s}.inspect}"
        yield self
        @current = old
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fn_document-0.9.6 lib/fn/node/context.rb
fn_document-0.9.4 lib/fn/node/context.rb
fn_document-0.9.3 lib/fn/node/context.rb
fn_document-0.9.2 lib/fn/node/context.rb