Class: Fronde::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/fronde/source.rb,
lib/fronde/source/html.rb,
lib/fronde/source/gemini.rb

Overview

Wrapper for each possible project source. Must be subclassed by specific source type (like Gemini or HTML)

Defined Under Namespace

Classes: Gemini, Html

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_config) ⇒ Source

Returns a new instance of Source.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fronde/source.rb', line 7

def initialize(source_config)
  @config = {
    'recursive' => true, 'is_blog' => false,
    'domain' => CONFIG.get('domain'), 'atom_feed' => '',
    'org-options' => {
      'section-numbers' => 'nil', 'with-toc' => 'nil'
    }
  }.merge(source_config)
  clean_config
  org_publish_options
  render_heading
end

Class Method Details

.canonical_config(config) ⇒ Object



122
123
124
125
126
# File 'lib/fronde/source.rb', line 122

def canonical_config(config)
  config = { 'path' => config } if config.is_a?(String)
  config['type'] ||= 'html'
  config
end

.new_from_config(config) ⇒ Object



128
129
130
131
132
# File 'lib/fronde/source.rb', line 128

def new_from_config(config)
  klass_name = config['type'].capitalize
  klass = Kernel.const_get("::Fronde::Source::#{klass_name}")
  klass.new(config)
end

Instance Method Details

#[](key) ⇒ Object



20
# File 'lib/fronde/source.rb', line 20

def [](key) = @config[key]

#[]=(key, value) ⇒ Object



22
23
24
# File 'lib/fronde/source.rb', line 22

def []=(key, value)
  @config[key] = value
end

#blog?Boolean

Returns:

  • (Boolean)


30
# File 'lib/fronde/source.rb', line 30

def blog? = !!@config['is_blog']

#exclude_file?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
# File 'lib/fronde/source.rb', line 76

def exclude_file?(file_name)
  # Obviously excluding index itself for blogs
  return true if file_name == File.join(@config['path'], 'index.org')

  exclusion_rules = @config['exclude']
  return false unless exclusion_rules

  file_name.match? exclusion_rules
end

#org_configObject



86
87
88
89
90
# File 'lib/fronde/source.rb', line 86

def org_config
  name = @config['name']
  [{ 'name' => name, 'attributes' => org_project_config },
   { 'name' => "#{name}-assets", 'attributes' => org_assets_config }]
end

#public_absolute_pathString

Return the absolute path as seen in User Agent.

The returned string always end with a slash (/).

Use #publication_path to locate published file on the file system.

Returns:

  • (String)

    the absolute path to this project



115
116
117
118
119
# File 'lib/fronde/source.rb', line 115

def public_absolute_path
  return @config['public_absolute_path'] if @config['public_absolute_path']

  @config['public_absolute_path'] = "/#{@config['target']}/".squeeze('/')
end

#publication_pathString

Return the publication absolute path on file system.

The returned string never end with a slash (/).

Use #public_absolute_path to get the absolute path of this project, as seen from a web browser.

Returns:

  • (String)

    the absolute path to the target dir of this project



100
101
102
103
104
105
# File 'lib/fronde/source.rb', line 100

def publication_path
  return @config['publication_path'] if @config['publication_path']

  publish_in = [@config['folder'], @config['target']]
  @config['publication_path'] = publish_in.join('/').delete_suffix('/')
end

#recursive?Boolean

Returns:

  • (Boolean)


28
# File 'lib/fronde/source.rb', line 28

def recursive? = !!@config['recursive']

#source_for(file_name) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fronde/source.rb', line 48

def source_for(file_name)
  relative_file_path = file_name.delete_prefix "#{publication_path}/"
  # file_name does not begin with source path.
  return if relative_file_path == file_name

  # Looks like a file at a deeper level, but current source is not
  # recursive.
  return if relative_file_path.include?('/') && !recursive?

  # Looks like a match. But does a source file for this one actually
  # exists?
  relative_source_path = relative_file_path.sub(
    /#{@config['ext']}\z/, '.org'
  )
  source_path = File.join(@config['path'], relative_source_path)
  return unless File.file?(source_path)

  source_path
end

#source_for?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fronde/source.rb', line 34

def source_for?(file_name)
  relative_file_path = file_name.delete_prefix "#{@config['path']}/"
  # file_name does not begin with source path.
  return false if relative_file_path == file_name

  # Looks like a file at a deeper level, but current source is not
  # recursive.
  return false if relative_file_path.include?('/') && !recursive?

  # We don’t check file if the file really exist as the current
  # check may be done before the file is actually written.
  true
end

#target_for(file_name) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/fronde/source.rb', line 68

def target_for(file_name)
  target = File.expand_path(file_name).delete_prefix "#{Dir.pwd}/"
  target.sub!(/\.org\z/, @config['ext'])
  project_relative_path = @config['path'].delete_prefix "#{Dir.pwd}/"
  target.delete_prefix! "#{project_relative_path}/"
  public_absolute_path + target
end

#to_hObject



32
# File 'lib/fronde/source.rb', line 32

def to_h = @config

#typeObject



26
# File 'lib/fronde/source.rb', line 26

def type = @config['type']