diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2019-02-04 15:29:17 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2019-02-04 15:29:17 +0300 |
commit | 91bd72255e13525bdcee92f74bc5eed5f35e3dce (patch) | |
tree | 22312ed1afadf0925c7ccc11d25a67c859722b13 /lib/checks | |
parent | 95f460ca8f4e50dd537990b77c1c0f5fb37b4dc7 (diff) |
Put anchors check files into Gitlab::Docs namespace
Diffstat (limited to 'lib/checks')
-rw-r--r-- | lib/checks/anchors.rb | 180 |
1 files changed, 91 insertions, 89 deletions
diff --git a/lib/checks/anchors.rb b/lib/checks/anchors.rb index f8f64de64..7c56eff4b 100644 --- a/lib/checks/anchors.rb +++ b/lib/checks/anchors.rb @@ -1,132 +1,134 @@ module Gitlab - module Nanoc - def self.config - @config ||= YAML.load(File.read('nanoc.yaml')) - end + module Docs + module Nanoc + def self.config + @config ||= YAML.load(File.read('nanoc.yaml')) + end - def self.output_dir - config.fetch('output_dir') + def self.output_dir + config.fetch('output_dir') + end end - end - class Page - attr_reader :file + class Page + attr_reader :file - def initialize(file) - @file = file - end + def initialize(file) + @file = file + end - def exists? - File.exists?(@file) - end + def exists? + File.exists?(@file) + end - def directory - File.dirname(@file) - end + def directory + File.dirname(@file) + end - def content - raise unless exists? + def content + raise unless exists? - @content ||= File.read(@file) - end + @content ||= File.read(@file) + end - def document - raise if content.to_s.empty? + def document + raise if content.to_s.empty? - @doc ||= Nokogiri::HTML(content) - end + @doc ||= Nokogiri::HTML(content) + end - def links - @links ||= document.css(:a).map do |link| - Gitlab::Link.new(link, self) + def links + @links ||= document.css(:a).map do |link| + Gitlab::Docs::Link.new(link, self) + end end - end - def has_anchor?(name) - document.at_css(%Q{[id="#{name}"]}) - end + def has_anchor?(name) + document.at_css(%Q{[id="#{name}"]}) + end - def self.build(path) - if path.end_with?('.html') - new(path) - else - new(File.join(path, 'index.html')) + def self.build(path) + if path.end_with?('.html') + new(path) + else + new(File.join(path, 'index.html')) + end end end - end - class Link - attr_reader :link, :href, :page + class Link + attr_reader :link, :href, :page - def initialize(link, page) - @link = link - @href = link[:href] - @page = page - end + def initialize(link, page) + @link = link + @href = link[:href] + @page = page + end - def to_anchor? - @href.to_s.include?('#') - end + def to_anchor? + @href.to_s.include?('#') + end - def anchor_name - raise ArguentError unless to_anchor? + def anchor_name + raise ArguentError unless to_anchor? - @href.to_s.partition('#').last - end + @href.to_s.partition('#').last + end - def internal_anchor? - raise ArguentError unless to_anchor? + def internal_anchor? + raise ArguentError unless to_anchor? - @href.to_s.partition('#').first.empty? - end + @href.to_s.partition('#').first.empty? + end - def internal? - @href.to_s.length > 0 && !@href.include?(':') - end + def internal? + @href.to_s.length > 0 && !@href.include?(':') + end - def path - @href.to_s.partition('#').first - end + def path + @href.to_s.partition('#').first + end - def absolute_path - raise unless internal? + def absolute_path + raise unless internal? - if @href.start_with?('/') - Gitlab::Nanoc.output_dir + path - else - ::File.expand_path(path, @page.directory) + if @href.start_with?('/') + Gitlab::Docs::Nanoc.output_dir + path + else + ::File.expand_path(path, @page.directory) + end end - end - def destination_page - if internal_anchor? - @page - else - Gitlab::Page.build(absolute_path) + def destination_page + if internal_anchor? + @page + else + Gitlab::Docs::Page.build(absolute_path) + end end - end - def source_file - @page.file - end + def source_file + @page.file + end - def destination_file - destination_page.file - end + def destination_file + destination_page.file + end - def destination_page_not_found? - !destination_page.exists? - end + def destination_page_not_found? + !destination_page.exists? + end - def destination_anchor_not_found? - !destination_page.has_anchor?(anchor_name) + def destination_anchor_not_found? + !destination_page.has_anchor?(anchor_name) + end end end end Nanoc::Check.define(:internal_anchors) do output_html_filenames.each do |file| - Gitlab::Page.new(file).links.each do |link| + Gitlab::Docs::Page.new(file).links.each do |link| next unless link.internal? next unless link.to_anchor? next if link.anchor_name == 'markdown-toc' @@ -139,7 +141,6 @@ Nanoc::Check.define(:internal_anchors) do - source file `#{link.source_file}` ERROR elsif link.destination_anchor_not_found? - (require 'pry'; binding.pry) if link.anchor_name == '1-stop-server' add_issue <<~ERROR Broken anchor detected! - anchor `##{link.anchor_name}` @@ -147,6 +148,7 @@ Nanoc::Check.define(:internal_anchors) do - source file `#{link.source_file}` - destination `#{link.destination_file}` ERROR + end end end |