Class: Mdoc::Pipeline
- Inherits:
-
Object
- Object
- Mdoc::Pipeline
- Defined in:
- lib/mdoc/pipeline.rb
Instance Attribute Summary collapse
-
#processors ⇒ Object
readonly
Returns the value of attribute processors.
-
#writer ⇒ Object
Returns the value of attribute writer.
Instance Method Summary collapse
- #append(prc) ⇒ Object
-
#apply!(document) ⇒ Object
recursively apply processors to document.
- #enabled?(prc) ⇒ Boolean
-
#get_offset(opts) ⇒ Object
from :before, :after, calculate insert offset.
-
#get_prc(prc) ⇒ Object
get processor class in name.
-
#initialize(ary = []) ⇒ Pipeline
constructor
A new instance of Pipeline.
- #insert(prc, opts = {}) ⇒ Object
- #remove(prc) ⇒ Object
Constructor Details
#initialize(ary = []) ⇒ Pipeline
Returns a new instance of Pipeline.
6 7 8 9 |
# File 'lib/mdoc/pipeline.rb', line 6 def initialize(ary = []) @processors = [] append ary end |
Instance Attribute Details
#processors ⇒ Object (readonly)
Returns the value of attribute processors.
4 5 6 |
# File 'lib/mdoc/pipeline.rb', line 4 def processors @processors end |
#writer ⇒ Object
Returns the value of attribute writer.
3 4 5 |
# File 'lib/mdoc/pipeline.rb', line 3 def writer @writer end |
Instance Method Details
#append(prc) ⇒ Object
37 38 39 40 |
# File 'lib/mdoc/pipeline.rb', line 37 def append(prc) prc = get_prc(prc) prc.each { |p| @processors << p } end |
#apply!(document) ⇒ Object
recursively apply processors to document
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mdoc/pipeline.rb', line 53 def apply!(document) @processors.each do |pn| prc = Mdoc.get_processor(pn) prc.new.pre_processors.each { |p| document.apply!(p) } document.apply!(prc) prc.new.post_processors.each { |p| document.apply!(p) } end writer.new.process!(document) end |
#enabled?(prc) ⇒ Boolean
47 48 49 50 |
# File 'lib/mdoc/pipeline.rb', line 47 def enabled?(prc) prc = get_prc(prc) @processors.include(prc) end |
#get_offset(opts) ⇒ Object
from :before, :after, calculate insert offset
27 28 29 30 31 32 33 34 35 |
# File 'lib/mdoc/pipeline.rb', line 27 def get_offset(opts) phash = Hash[@processors.map.with_index.to_a] if opts[:before] offset = phash[Mdoc.get_processor(opts[:before])] elsif opts[:after] offset = phash[Mdoc.get_processor(opts[:after])] + 1 end offset end |
#get_prc(prc) ⇒ Object
get processor class in name
12 13 14 15 |
# File 'lib/mdoc/pipeline.rb', line 12 def get_prc(prc) prc = [prc] unless prc.is_a?(Array) prc.map { |pn| Mdoc.get_processor(pn) } end |
#insert(prc, opts = {}) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/mdoc/pipeline.rb', line 17 def insert(prc, opts = {}) prc = get_prc(prc) raise 'can not set before with after' if opts[:before] && opts[:after] ankor = opts[:before] || opts[:after] offset = get_offset(opts) if ankor ankor ? @processors.insert(offset, *prc) : @processors = prc.concat(@processors) end |
#remove(prc) ⇒ Object
42 43 44 45 |
# File 'lib/mdoc/pipeline.rb', line 42 def remove(prc) prc = get_prc(prc) prc.map { |pn| @processors.delete(pn) } end |