|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Copyright © Adobe, Inc. All rights reserved. |
| 4 | +# See COPYING.txt for license details. |
| 5 | + |
| 6 | +# This plugin generates the page that contains a list of migrated topics. |
| 7 | +# |
| 8 | + |
| 9 | +module Jekyll |
| 10 | + # Custom generator for MRG pages |
| 11 | + class MigratedLog < Generator |
| 12 | + safe true |
| 13 | + |
| 14 | + def generate(site) |
| 15 | + # Make the site object available in any scope in this class. |
| 16 | + @site = site |
| 17 | + migrated_pages = @site.pages.filter { |page| page.data['layout'] == 'migrated' } |
| 18 | + |
| 19 | + url_prefix = site.config['url'] + site.config['baseurl'] |
| 20 | + |
| 21 | + migrated_pages_data = [] |
| 22 | + migrated_pages.each do |page| |
| 23 | + migrated_page = { |
| 24 | + path: page.path, |
| 25 | + title: page.data['title'], |
| 26 | + guide: @site.data['toc'][page.data['group']]['label'], |
| 27 | + migrated_from: url_prefix + page.url, |
| 28 | + migrated_to: page.data['migrated_to'] |
| 29 | + } |
| 30 | + migrated_pages_data << migrated_page |
| 31 | + end |
| 32 | + |
| 33 | + migrated_pages_by_group = migrated_pages_data.group_by { |page| page[:guide] } |
| 34 | + |
| 35 | + content = "The folowing is the list of topics that have been migrated and will be redirected soon.\n\n" |
| 36 | + migrated_pages_by_group.each do |guide, topics| |
| 37 | + content += "\n## #{guide}\n\n\n" |
| 38 | + topics.sort_by { |topic| topic[:title] } |
| 39 | + .each do |topic| |
| 40 | + content += "1. [#{topic[:title]}](#{topic[:migrated_from]}) has moved to <#{topic[:migrated_to]}>\n" |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + # PageWithoutAFile handles processing files without reading it. |
| 45 | + # 'migrated.md' is a virtual file that's been created during Jekyll run. |
| 46 | + # See details in https://www.rubydoc.info/gems/jekyll/Jekyll/PageWithoutAFile |
| 47 | + # See tests in https://github.com/jekyll/jekyll/blob/master/test/test_page_without_a_file.rb |
| 48 | + topic = PageWithoutAFile.new( |
| 49 | + @site, |
| 50 | + @site.source, |
| 51 | + '.', |
| 52 | + 'migrated.md' |
| 53 | + ) |
| 54 | + topic.content = content |
| 55 | + topic.data['title'] = 'Migrated topics' |
| 56 | + topic.data['layout'] = 'full-width' |
| 57 | + topic.data['github_link'] = false |
| 58 | + topic.data['feedback_link'] = false |
| 59 | + topic.process('migrated.md') |
| 60 | + |
| 61 | + # Add the newly constructed page object to the rest of pages |
| 62 | + # on the site. |
| 63 | + @site.pages << topic |
| 64 | + end |
| 65 | + end |
| 66 | +end |
0 commit comments