Welcome to mirror list, hosted at ThFree Co, Russian Federation.

link.rb « docs « gitlab « lib - gitlab.com/gitlab-org/gitlab-docs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c35b61b9f2ecf4ffad6dc447ff69438a6c49e6c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true

module Gitlab
  module Docs
    class Link
      attr_reader :link, :href, :page

      def initialize(link, page)
        @href = link.to_s
        @page = page
      end

      def to_anchor?
        !anchor_name.to_s.strip.empty?
      end

      def anchor_name
        return unless @href.include?('#')

        @href.partition('#').last
      end

      def internal_anchor?
        return false unless to_anchor?

        @href.partition('#').first.empty?
      end

      def internal?
        @href.length.positive? && !@href.include?(':')
      end

      def path
        @href.partition('#').first
      end

      def absolute_path
        raise unless internal?

        if @href.start_with?('/')
          Gitlab::Docs::Nanoc.output_dir + path
        else
          ::File.expand_path(path, @page.directory)
        end
      end

      def destination_page
        if internal_anchor?
          @page
        else
          Gitlab::Docs::Page.build(absolute_path)
        end
      end

      def source_file
        @page.file
      end

      def destination_file
        destination_page.file
      end

      def destination_page_not_found?
        !destination_page.exists?
      end

      def destination_anchor_not_found?
        !destination_page.has_anchor?(anchor_name)
      end
    end
  end
end