| 
1 |  | -module Chunker  | 
 | 1 | +# frozen_string_literal: true  | 
2 | 2 | 
 
  | 
 | 3 | +module Chunker  | 
 | 4 | +  # Add a warning to the page with a link to docs v3  | 
3 | 5 |   class UrlToV3  | 
4 |  | - | 
5 | 6 |     attr_reader :url  | 
6 | 7 | 
 
  | 
7 | 8 |     def initialize(doc)  | 
8 | 9 |       current_url = doc.attr('current-url')  | 
9 | 10 |       outdir = doc.attr('outdir')  | 
10 | 11 |       current_url ||= 'index.html'  | 
11 |  | -      file_path = File.expand_path('v3-mapping.json', __dir__)  | 
12 |  | -      mapping = JSON.parse(File.read(file_path)) if File.exist?(file_path)  | 
13 |  | -      segments = outdir.sub('/tmp/docsbuild/target_repo/raw', '').split('/') # This only works in CI.   | 
 | 12 | +      m = mapping  | 
 | 13 | +      # This only works in CI.  | 
 | 14 | +      segments = outdir.sub('/tmp/docsbuild/target_repo/raw', '').split('/')  | 
14 | 15 |       version = segments[-1] || 'unknown'  | 
 | 16 | +      actual_url = get_actual_url(outdir, current_url)  | 
 | 17 | +      new_url = if m.key?(actual_url)  | 
 | 18 | +                  m[actual_url]  | 
 | 19 | +                else  | 
 | 20 | +                  '/docs'  | 
 | 21 | +                end  | 
 | 22 | +      render_warning(doc, version, new_url)  | 
 | 23 | +    end  | 
 | 24 | + | 
 | 25 | +    def mapping  | 
 | 26 | +      file_path = File.expand_path('v3-mapping.json', __dir__)  | 
 | 27 | +      JSON.parse(File.read(file_path)) if File.exist?(file_path)  | 
 | 28 | +    end  | 
15 | 29 | 
 
  | 
16 |  | -      path_dir = if segments.empty?  | 
 | 30 | +    def get_path_dir(outdir)  | 
 | 31 | +      segments = outdir.sub('/tmp/docsbuild/target_repo/raw', '').split('/')  | 
 | 32 | +      if segments.empty?  | 
17 | 33 |         ''  | 
18 | 34 |       elsif segments.length > 1  | 
19 | 35 |         segments[0...-1].join('/')  | 
20 | 36 |       else  | 
21 | 37 |         segments[0]  | 
22 | 38 |       end  | 
 | 39 | +    end  | 
23 | 40 | 
 
  | 
24 |  | -      actual_url = path_dir + '/*/' + current_url  | 
25 |  | -      new_url = if mapping.key?(actual_url)  | 
26 |  | -        mapping[actual_url]  | 
27 |  | -      else  | 
28 |  | -        '/docs'  | 
29 |  | -      end  | 
 | 41 | +    def get_actual_url(outdir, current_url)  | 
 | 42 | +      get_path_dir(outdir) + '/*/' + current_url  | 
 | 43 | +    end  | 
30 | 44 | 
 
  | 
 | 45 | +    def render_warning(doc, version, new_url)  | 
31 | 46 |       if version == '8.18'  | 
32 | 47 |         @url = Asciidoctor::Block.new(doc, :pass, source: <<~HTML)  | 
33 | 48 |           <div id="url-to-v3" style="version-warning">  | 
 | 
0 commit comments