module Jekyll::GtnFunctions

The main GTN function library

Constants

ELIXIR_NODES

List of elixir node country IDs (ISO 3166-1 alpha-2) and their names

Public Class Methods

cache() click to toggle source
# File _plugins/gtn.rb, line 56
def self.cache
  @@cache ||= Jekyll::Cache.new('GtnFunctions')
end

Public Instance Methods

convert_to_material_list(site, materials) click to toggle source
# File _plugins/gtn.rb, line 275
def convert_to_material_list(site, materials)
  # [{"name"=>"introduction", "topic"=>"admin"}]
  if materials.nil?
    return []
  end

  materials.map do |m|
    if m.key?('name') && m.key?('topic')
      found = TopicFilter.fetch_tutorial_material(site, m['topic'], m['name'])
      Jekyll.logger.warn "Could not find material #{m['topic']}/#{m['name']} in the site data" if found.nil?
      found
    elsif m.key?('external') && m['external']
      {
        'layout' => 'tutorial_hands_on',
        'name' => m['name'],
        'title' => m['name'],
        'hands_on' => 'external',
        'hands_on_url' => m['link'],
      }
    else
      Jekyll.logger.warn "[GTN] Unsure how to render #{m}"
    end
  end
end
convert_workflow_path_to_trs(str) click to toggle source

Convert a workflow path to a TRS path Params:

str

The workflow path

Returns:

String

The TRS path

Example:

{{ "topics/metagenomics/tutorials/mothur-miseq-sop-short/workflows/workflow1_quality_control.ga" |
   convert_workflow_path_to_trs }}
=> "/api/ga4gh/trs/v2/tools/metagenomics-mothur-miseq-sop-short/versions/workflow1_quality_control"
# File _plugins/gtn.rb, line 311
def convert_workflow_path_to_trs(str)
  return 'GTN_TRS_ERROR_NIL' if str.nil?

  m = str.match(%r{topics/(?<topic>.*)/tutorials/(?<tutorial>.*)/workflows/(?<workflow>.*)\.ga})
  return "/api/ga4gh/trs/v2/tools/#{m[:topic]}-#{m[:tutorial]}/versions/#{m[:workflow].downcase}" if m

  'GTN_TRS_ERROR'
end
elixirnode2name(name) click to toggle source

Returns the name of an elixir node, given its country ID Params:

name

The country ID of the node (ISO 3166-1 alpha-2)

Returns:

String

The name of the node

# File _plugins/gtn.rb, line 93
def elixirnode2name(name)
  ELIXIR_NODES[name]
end
filter_authors(contributors, contributions) click to toggle source

Basically a dupe of 'get_authors' Params:

contributors

The contributors to the material

contributions

The contributions to the material

Returns:

Array

The “authors” of the material

TODO(hexylena) de-duplicate

Example:

{% assign authors = page.contributors | filter_authors:page.contributions -%}
# File _plugins/gtn.rb, line 225
def filter_authors(contributors, contributions)
  return contributors if !contributors.nil?

  contributions['authorship']
end
fix_box_titles(content, lang, key) click to toggle source

Fix the titles of boxes in a page Params:

content

The content to fix

lang

The language of the content

key

The key of the content

Returns:

String

The fixed content

# File _plugins/gtn.rb, line 209
def fix_box_titles(content, lang, key)
  Gtn::Boxify.replace_elements(content, lang, key)
end
get_topic(page) click to toggle source

Get the topic of a page's path Params:

page

The page to get the topic of, it will inspect page

Returns:

String

The topic of the page

Example:

{{ page | get_topic }}
# File _plugins/gtn.rb, line 350
def get_topic(page)
  # Arrays that will store all introduction slides and tutorials we discover.
  page['path'].split('/')[1]
end
gtn_mod_date(path) click to toggle source

Returns the last modified date of a page Params:

page

The page to get the last modified date of

Returns:

String

The last modified date of the page

# File _plugins/gtn.rb, line 174
def gtn_mod_date(path)
  # Automatically strips any leading slashes.
  Gtn::ModificationTimes.obtain_time(path.gsub(%r{^/}, ''))
end
how_many_topic_feedbacks(feedback, name) click to toggle source

How many times has a topic been mentioned in feedback? Params:

feedback

The feedback to search through

name

The name of the topic to search for

Returns:

Integer

The number of times the topic has been mentioned

# File _plugins/gtn.rb, line 186
def how_many_topic_feedbacks(feedback, name)
  feedback.select { |x| x['topic'] == name }.length
end
how_many_tutorial_feedbacks(feedback, name) click to toggle source

How many times has a tutorial been mentioned in feedback? Params:

feedback

The feedback to search through

name

The name of the tutorial to search for

Returns:

Integer

The number of times the tutorial has been mentioned

# File _plugins/gtn.rb, line 197
def how_many_tutorial_feedbacks(feedback, name)
  feedback.select { |x| x['tutorial'] == name }.length
end
humanize_types(type) click to toggle source

Return human text for ruby types Params:

type

The type to humanize

Returns:

String

The humanized type

Example:

humanize_types("seq") # => "List of Items"
# File _plugins/gtn.rb, line 137
def humanize_types(type)
  data = {
    'seq' => 'List of Items',
    'str' => 'Free Text',
    'map' => 'A dictionary/map',
    'float' => 'Decimal Number',
    'int' => 'Integer Number',
    'bool' => 'Boolean'
  }
  data[type]
end
last_modified_at(page) click to toggle source

Returns the last modified date of a page Params:

page

The page to get the last modified date of

Returns:

String

The last modified date of the page

TODO: These two could be unified tbh

# File _plugins/gtn.rb, line 163
def last_modified_at(page)
  Gtn::ModificationTimes.obtain_time(page['path'])
end
layout_to_human(layout) click to toggle source
# File _plugins/gtn.rb, line 320
def layout_to_human(layout)
  case layout
  when /slides/
    'Slides'
  when /tutorial_hands_on/
    'Hands-on'
  when 'faq'
    'FAQs'
  when 'news'
    'News'
  end
end
load_svg(path) click to toggle source

Load an SVG file directly into the page Params:

path

The path of the SVG file (relative to GTN workspace root)

Returns:

String

The SVG file contents

Example:

{{ "assets/images/mastodon.svg" | load_svg }}
# File _plugins/gtn.rb, line 255
def load_svg(path)
  File.read(path).gsub(/\R+/, '')
end
regex_replace(str, regex_search, value_replace) click to toggle source
# File _plugins/gtn.rb, line 259
def regex_replace(str, regex_search, value_replace)
  regex = /#{regex_search}/m
  str.gsub(regex, value_replace)
end
regex_replace_once(str, regex_search, value_replace) click to toggle source

This method does a single regex replacement

Example

{{ content | regex_replace: '<hr>', '' }}
# File _plugins/gtn.rb, line 270
def regex_replace_once(str, regex_search, value_replace)
  regex = /#{regex_search}/m
  str.sub(regex, value_replace)
end
replace_newline_doublespace(text) click to toggle source

Replaces newlines with newline + two spaces

# File _plugins/gtn.rb, line 151
def replace_newline_doublespace(text)
  text.gsub(/\n/, "\n  ")
end
slugify_unsafe(text) click to toggle source

A slightly more unsafe slugify function Params:

text

The text to slugify

Returns:

String

The slugified text

Example:

slugify_unsafe("Hello, World!") # => "Hello-World"
# File _plugins/gtn.rb, line 123
def slugify_unsafe(text)
  # Gets rid of *most* things without making it completely unusable?
  text.gsub(%r{["'\\/;:,.!@#$%^&*()]}, '').gsub(/\s/, '-').gsub(/-+/, '-')
end
top_citations(citations) click to toggle source

Obtain the most cited paper in the GTN Params:

citations

The citations to search through

Returns:

Hash

The papers including their text citation and citation count

# File _plugins/gtn.rb, line 104
def top_citations(citations)
  if citations.nil?
    {}
  else
    citations.sort_by { |_k, v| v }.reverse.to_h.first(20).to_h do |k, v|
      [k, { 'count' => v, 'text' => Gtn::Scholar.render_citation(k) }]
    end
  end
end
topic_name_from_page(page, site) click to toggle source
# File _plugins/gtn.rb, line 333
def topic_name_from_page(page, site)
  if page.key? 'topic_name'
    site.data[page['topic_name']]['title']
  else
    site.data.fetch(page['url'].split('/')[2], { 'title' => '' })['title']
  end
end