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
# File _plugins/gtn.rb, line 56 def self.cache @@cache ||= Jekyll::Cache.new('GtnFunctions') end
Public Instance Methods
# 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 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
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
Convert a fedi address to a link Params:
fedi_address
-
The fedi address to convert
Returns:
String
-
The URL at which their profile is accessible
Example:
{{ contributors[page.contributor].fediverse | fedi2link }} fedi2link("@hexylena@galaxians.garden") => "https://galaxians.garden/@hexylena"
# File _plugins/gtn.rb, line 242 def fedi2link(fedi_address) fedi_address.gsub(/^@?(?<user>.*)@(?<host>.*)$/) { |_m| "https://#{$LAST_MATCH_INFO[:host]}/@#{$LAST_MATCH_INFO[:user]}" } end
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
Gets the 'default' link for a material, hands on if it exists, otherwise slides. Params:
material
-
The material to get the link for
Returns:
String
-
The URL of the default link
# File _plugins/gtn.rb, line 361 def get_default_link(material) return 'NO LINK' if material.nil? return 'NO LINK' if material == true url = nil url = "topics/#{material['topic_name']}/tutorials/#{material['tutorial_name']}/slides.html" if material['slides'] if material['hands_on'] && (material['hands_on'] != 'external' && material['hands_on'] != '') url = "topics/#{material['topic_name']}/tutorials/#{material['tutorial_name']}/tutorial.html" end url end
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
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 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 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
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
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
# 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 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
# File _plugins/gtn.rb, line 259 def regex_replace(str, regex_search, value_replace) regex = /#{regex_search}/m str.gsub(regex, value_replace) end
Replaces newlines with newline + two spaces
# File _plugins/gtn.rb, line 151 def replace_newline_doublespace(text) text.gsub(/\n/, "\n ") end
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
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
# 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