module Neruda::IndexOrgGenerator

Embeds methods responsible for generating an org file for a given

index.

Public Instance Methods

to_org(index_name = 'index', is_project: false) click to toggle source
# File lib/neruda/index/org_generator.rb, line 7
def to_org(index_name = 'index', is_project: false)
  return project_home_page(index_name) if is_project
  return all_tags_index if index_name == 'index'
  [org_header(index_name),
   org_articles(@index[index_name])].join("\n")
end
Also aliased as: to_s
to_s(index_name = 'index', is_project: false)
Alias for: to_org
write_org(index_name) click to toggle source
# File lib/neruda/index/org_generator.rb, line 15
def write_org(index_name)
  return unless save?
  slug = Neruda::OrgFile.slug index_name
  FileUtils.mkdir 'tags' unless Dir.exist? 'tags'
  content = to_org index_name
  orgdest = "tags/#{slug}.org"
  IO.write(orgdest, content)
end

Private Instance Methods

all_tags_index() click to toggle source
# File lib/neruda/index/org_generator.rb, line 44
def all_tags_index
  content = [
    org_header(R18n.t.neruda.index.all_tags, is_tag: false)
  ]
  sort_tags_by_name_and_weight.each do |t, tags|
    content << ''
    content << org_title(R18n.t.neruda.index.send(t), 'index-tags')
    next if tags.empty?
    tags.each do |k|
      content << "- #{tag_published_url(k)} (#{@index[k].length})"
    end
  end
  content.join("\n")
end
org_articles(articles_list) click to toggle source
# File lib/neruda/index/org_generator.rb, line 79
def org_articles(articles_list)
  last_year = nil
  articles_list.map do |article|
    year_title = ''
    year = article.timekey.slice(0, 4)
    if year != last_year
      year_title = format("\n%<title>s\n", title: org_title(year))
      last_year = year
    end
    year_title + org_entry(article)
  end
end
org_entry(article) click to toggle source
# File lib/neruda/index/org_generator.rb, line 92
def org_entry(article)
  line = "- *[[#{article.url}][#{article.title}]]*"
  if article.date
    art_date = article.datestring(:full, year: false)
    published = R18n.t.neruda.index.published_on art_date
    line += " / #{published}"
  end
  line += " \\\\\n  #{article.excerpt}" if article.excerpt != ''
  line
end
org_header(title = nil, is_tag: true) click to toggle source
# File lib/neruda/index/org_generator.rb, line 66
    def org_header(title = nil, is_tag: true)
      if is_tag
        title = @tags_names[title]
      elsif title.nil? || title == 'index'
        title = Neruda::Config.settings['title']
      end
      <<~HEADER.strip
        #+title: #{title}
        #+author: #{Neruda::Config.settings['author']}
        #+language: #{Neruda::Config.settings['lang']}
      HEADER
    end
org_title(year, html_class = 'index-year') click to toggle source
# File lib/neruda/index/org_generator.rb, line 103
    def org_title(year, html_class = 'index-year')
      year = R18n.t.neruda.index.unsorted if year == '0000'
      <<~ENDPROP
        * #{year}
        :PROPERTIES:
        :HTML_CONTAINER_CLASS: #{html_class}
        :UNNUMBERED: notoc
        :END:
      ENDPROP
    end
project_home_page(project_name) click to toggle source
# File lib/neruda/index/org_generator.rb, line 26
def project_home_page(project_name)
  content = [org_header(project_name, is_tag: false)]
  if @projects[project_name]&.any?
    content += org_articles(@projects[project_name])
  end
  content.join("\n")
end
tag_published_url(tag_name) click to toggle source
# File lib/neruda/index/org_generator.rb, line 59
def tag_published_url(tag_name)
  domain = Neruda::Config.settings['domain']
  title = @tags_names[tag_name]
  tag_link = "#{domain}/tags/#{tag_name}.html"
  "[[#{tag_link}][#{title}]]"
end
write_all_blog_home(verbose) click to toggle source
# File lib/neruda/index/org_generator.rb, line 34
def write_all_blog_home(verbose)
  Neruda::Config.sources.each do |project|
    next unless project['is_blog']
    next unless Dir.exist?(project['path'])
    warn "Generated blog home for #{project['name']}" if verbose
    orgdest = format('%<root>s/index.org', root: project['path'])
    IO.write(orgdest, to_org(project['name'], is_project: true))
  end
end