Class: Smartgen::MarkupFile

Inherits:
Object
  • Object
show all
Defined in:
lib/smartgen/markup_file.rb

Overview

A MarkupFile is created for each file that will be generated.

It is also exposed in layouts, as markup_file variable.

It receives the path of the file and process it using the first suitable engine, based on the extension of the file.

It can also receive an options hash with indexer options:

MarkupFile.new 'some/path', :indexer => true

Or, if you want to pass options for the Indexer:

MarkupFile.new 'some/path', :indexer => { :numbered_index => true }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ MarkupFile

Returns a new instance of MarkupFile.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/smartgen/markup_file.rb', line 40

def initialize(path, options={})
  @path = path
  @extension = File.extname(path)
  @filename = filename_for(path)
  @engine = engine_for(@extension) || self.class.engines.first

  @contents = engine.process(raw_contents, options[:metadata])

  if options[:indexer]
    @indexer = Smartgen::Indexer.new(@contents, options[:indexer])
    @contents = @indexer.result
  end

end

Instance Attribute Details

#contentsObject

The contents of the file after processing.



35
36
37
# File 'lib/smartgen/markup_file.rb', line 35

def contents
  @contents
end

#engineObject

The engine used to process the file.



32
33
34
# File 'lib/smartgen/markup_file.rb', line 32

def engine
  @engine
end

#extensionObject

The extension of the file.



29
30
31
# File 'lib/smartgen/markup_file.rb', line 29

def extension
  @extension
end

#filenameObject

The name of the file, without extension.



26
27
28
# File 'lib/smartgen/markup_file.rb', line 26

def filename
  @filename
end

#indexerObject

The indexer instance, if options[:indexer] is given



38
39
40
# File 'lib/smartgen/markup_file.rb', line 38

def indexer
  @indexer
end

#pathObject

The path of the file.



23
24
25
# File 'lib/smartgen/markup_file.rb', line 23

def path
  @path
end

Class Method Details

.enginesObject

Returns an array with the supported engines.

The priority of the engines is defined by their order in this array. The first ones are the ones with higher priority.



65
66
67
68
69
70
71
# File 'lib/smartgen/markup_file.rb', line 65

def engines
  if @engines.blank?
    @engines = [Smartgen::Engine::Textile.new, Smartgen::Engine::Markdown.new, Smartgen::Engine::ERB.new]
  end

  @engines
end

.register(engine) ⇒ Object

Register an engine to be used when generating files.

The engine will be added with the highest priority.



76
77
78
# File 'lib/smartgen/markup_file.rb', line 76

def register(engine)
  engines.unshift engine.new
end

Instance Method Details

#raw_contentsObject

The contents of the file before processing.



56
57
58
# File 'lib/smartgen/markup_file.rb', line 56

def raw_contents
  File.read(path)
end