Class: Fronde::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/fronde/index.rb,
lib/fronde/index/org_generator.rb,
lib/fronde/index/atom_generator.rb

Overview

Reopen Index class to embed Atom feeds sepecific methods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ Index

Returns a new instance of Index.



13
14
15
16
17
18
19
20
21
# File 'lib/fronde/index.rb', line 13

def initialize(project)
  @project = project
  @index = { 'index' => [] }
  @entries = []
  @tags_names = {}
  @date = Time.now
  generate_feeds if @project.blog?
  sort_feeds!
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



11
12
13
# File 'lib/fronde/index.rb', line 11

def date
  @date
end

#projectObject (readonly)

Returns the value of attribute project.



11
12
13
# File 'lib/fronde/index.rb', line 11

def project
  @project
end

Class Method Details

.all_blog_index(&block) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/fronde/index.rb', line 52

def all_blog_index(&block)
  all_blogs = CONFIG.sources.filter_map do |project|
    Index.new(project) if project.blog?
  end
  return all_blogs unless block

  all_blogs.each(&block)
end

Instance Method Details

#all_tagsObject



23
24
25
# File 'lib/fronde/index.rb', line 23

def all_tags
  @index.keys.reject { |tag| tag == 'index' }
end

#blog_home_pageObject



9
10
11
# File 'lib/fronde/index/org_generator.rb', line 9

def blog_home_page
  org_index @project['title'], '__HOME_PAGE__', @entries.map(&:to_h)
end

#emacs_keywordsObject



47
48
49
# File 'lib/fronde/index.rb', line 47

def emacs_keywords
  @tags_names.map { |slug, title| "#{title}\x1f#{slug}" }.join("\x1e")
end

#empty?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/fronde/index.rb', line 27

def empty?
  @index['index'].empty?
end

#sort_by(kind) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fronde/index.rb', line 31

def sort_by(kind)
  accepted_values = %i[name weight]
  unless accepted_values.include?(kind)
    error_msg = I18n.t(
      'fronde.error.index.wrong_sort_kind',
      kind: kind, accepted_values: accepted_values.inspect
    )
    raise ArgumentError, error_msg
  end
  sort_tags_by_name_and_weight[:"by_#{kind}"].map do |tag|
    @tags_names[tag] + " (#{@index[tag].length})"
  end.reverse
  # Reverse in order to have most important or A near next prompt
  # and avoid to scroll to find the beginning of the list.
end

#to_atom(index_name = 'index') ⇒ Object



8
9
10
11
12
13
# File 'lib/fronde/index/atom_generator.rb', line 8

def to_atom(index_name = 'index')
  entries = @index[index_name][0...10].map(&:to_h)
  return atom_index(entries) if index_name == 'index'

  atom_file(index_name, entries)
end

#to_org(index_name = 'index') ⇒ Object Also known as: to_s



13
14
15
16
17
18
19
20
# File 'lib/fronde/index/org_generator.rb', line 13

def to_org(index_name = 'index')
  return all_tags_index if index_name == 'index'

  org_index(
    @tags_names[index_name], index_name,
    (@index[index_name] || []).map(&:to_h)
  )
end

#write_all_feeds(verbose: true) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/fronde/index/atom_generator.rb', line 21

def write_all_feeds(verbose: true)
  FileUtils.mkdir_p "#{@project.publication_path}/feeds"
  @index.each_key do |tag|
    write_atom(tag)
    puts I18n.t('fronde.index.atom_generated', tag:) if verbose
  end
end

#write_all_org(verbose: true) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/fronde/index/org_generator.rb', line 29

def write_all_org(verbose: true)
  FileUtils.mkdir_p "#{@project['path']}/tags"
  @index.each_key do |tag|
    write_org(tag)
    puts I18n.t('fronde.index.index_generated', tag:) if verbose
  end
  write_blog_home_page(verbose)
end

#write_atom(index_name) ⇒ Object



15
16
17
18
19
# File 'lib/fronde/index/atom_generator.rb', line 15

def write_atom(index_name)
  slug = Slug.slug index_name
  atomdest = "#{@project.publication_path}/feeds/#{slug}.xml"
  File.write atomdest, to_atom(index_name)
end

#write_org(index_name) ⇒ Object



23
24
25
26
27
# File 'lib/fronde/index/org_generator.rb', line 23

def write_org(index_name)
  slug = Slug.slug index_name
  orgdest = "#{@project['path']}/tags/#{slug}.org"
  File.write orgdest, to_org(index_name)
end