From 95f460ca8f4e50dd537990b77c1c0f5fb37b4dc7 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Fri, 1 Feb 2019 15:32:25 +0100 Subject: Make anchors validator a little more readable --- lib/checks/anchors.rb | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'lib/checks') diff --git a/lib/checks/anchors.rb b/lib/checks/anchors.rb index 94d52c9c..f8f64de6 100644 --- a/lib/checks/anchors.rb +++ b/lib/checks/anchors.rb @@ -20,14 +20,20 @@ module Gitlab File.exists?(@file) end - def path + def directory File.dirname(@file) end + def content + raise unless exists? + + @content ||= File.read(@file) + end + def document - raise ArgumentError unless exists? + raise if content.to_s.empty? - @doc ||= Nokogiri::HTML(File.read(@file)) + @doc ||= Nokogiri::HTML(content) end def links @@ -58,10 +64,6 @@ module Gitlab @page = page end - def path - @href.to_s.partition('#').first - end - def to_anchor? @href.to_s.include?('#') end @@ -82,13 +84,17 @@ module Gitlab @href.to_s.length > 0 && !@href.include?(':') end - def destination_path + def path + @href.to_s.partition('#').first + end + + def absolute_path raise unless internal? if @href.start_with?('/') Gitlab::Nanoc.output_dir + path else - ::File.expand_path(path, @page.path) + ::File.expand_path(path, @page.directory) end end @@ -96,7 +102,7 @@ module Gitlab if internal_anchor? @page else - Gitlab::Page.build(destination_path) + Gitlab::Page.build(absolute_path) end end @@ -133,12 +139,13 @@ 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}` - link `#{link.href}` - source file `#{link.source_file}` - - destination `#{link.destination_file} + - destination `#{link.destination_file}` ERROR end end -- cgit v1.2.3