Path: blob/main/_plugins/notebook-rmarkdown.rb
1677 views
# frozen_string_literal: true12require 'json'3require 'fileutils'4require './_plugins/notebook'56module Jekyll7module Generators8# Generate RMarkdown documents from GTN markdown9class RmarkdownGenerator < Generator10safe true1112def generate(site)13# For every tutorial with the 'notebook' key in the page data14site.pages.select { |page| Gtn::Notebooks.notebook_filter(page.data, 'r') }.each do |page|15# We get the path to the tutorial source16dir = File.dirname(File.join('.', page.url))17fn = File.join('.', page.url).sub(/html$/, 'Rmd')1819# Tag our source page20page.data['tags'] = page.data['tags'] || []21page.data['tags'].push('rmarkdown-notebook')2223Jekyll.logger.info "[GTN/Notebooks/R] Rendering RMarkdown #{fn}"24last_modified = Gtn::ModificationTimes.obtain_time(page.path)25notebook = Gtn::Notebooks.render_rmarkdown(site, page.data, page.content, page.url, last_modified, fn)2627topic_id = dir.split('/')[-3]28tutorial_id = dir.split('/')[-1]2930# Write it out!31page2 = PageWithoutAFile.new(site, '', dir, "#{topic_id}-#{tutorial_id}.Rmd")32page2.content = notebook33page2.data['layout'] = nil34page2.data['citation_target'] = 'R'35site.pages << page236end3738page3 = PageWithoutAFile.new(site, '', File.join('assets', 'css'), 'r-notebook.css')39page3.content = Gtn::Notebooks.generate_css40page3.data['layout'] = nil41site.pages << page342end43end44end45end464748