Class: ArticleReference

Inherits:
Object
  • Object
show all
Defined in:
lib/articles-addresser.rb

Overview

Provides facilities to identify key traits of the included articles. Relative references found at inclusion location, are supposed to be relative to the location of:

- the master (including) document, when referencing a source 
- the master's destination folder, when referencing an output document 
  (e.g. a rendered linked article)

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ ArticleReference

The initialization requires the master document as received by the process function from the parser to let the constructed object be informed of:

- the value of document option `:to_dir`
- the value of the document attribute `docdir`

In case the document option ‘:to_dir` is not defined (happen when not converting to file), the destination directory is assumed to be the same as the one of the master document; that is `docdir`



20
21
22
23
24
25
26
# File 'lib/articles-addresser.rb', line 20

def initialize (doc)
  @to_dir = doc.options[:to_dir] 
  @root = if doc.attributes.key? 'docdir' then doc.attributes['docdir'] else '' end
  if @to_dir == nil then 
    @to_dir = @root 
  end
end

Instance Method Details

#abs_articleObject

Rebases the article path to the destination folder.



69
70
71
# File 'lib/articles-addresser.rb', line 69

def abs_article
  File.join(@to_dir, article)
end

#abs_targetObject

Makes the target path relative to the master document folder. If available, the master document folder is an absolute path, then the result is also an absolute path. When the master document folder is not known, the result is the target itself, whenever it is absolute or relative.



78
79
80
81
82
83
84
# File 'lib/articles-addresser.rb', line 78

def abs_target
  if @root == nil then
    return @target 
  end
  
  return File.absolute_path(@target, @root)
end

#articleObject

Provides the relative path of the processing article in the output space. The path is relative to the output folder of the master document.

i.e. <articles-dir>/<target>.html



60
61
62
63
64
65
66
# File 'lib/articles-addresser.rb', line 60

def article
  if @articles_folder.empty? then 
    "#{@base_target}.html" 
  else 
    File.join( @articles_folder, @base_target) + ".html"
  end
end

Provides the alias to be used at link location in the master document.



87
88
89
# File 'lib/articles-addresser.rb', line 87

def link
  return @link
end

#process_article(target, attributes) ⇒ Object

The method process_article collect additional information of an article linked in the document. It fixed article informations that are used to feed the other methods, therefore ‘process_article` must be called prior the read of any other property. While not optimal, this is necessary because the information provided actually refer to an included article, and multiple included articles might happen to be in the same document - then their references resolved by the same `ArticleReference` object.

The last call to ‘process_article` fixes the *processing article*.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/articles-addresser.rb', line 39

def process_article target, attributes 
  @target = target
  @base_target = File.basename(target, '.*')

  @articles_folder = if attributes.key?  'articles-dir' then
    attributes['articles-dir'] 
  else
    ''
  end

  @link = if attributes.key? 'link' then
    attributes['link'] 
  else 
    @base_target
  end
end