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

gitlab.com/gitlab-org/gitlab-docs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2019-02-06 12:32:25 +0300
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2019-02-06 12:32:25 +0300
commite1632cb8075bd3605b6ba1a074cdf9a5a633b6d5 (patch)
tree5916c61b0cbe0b6f0f165ab992066a6bd5dc0aa3 /lib/gitlab
parent8b549a511b729c28804e8d93c294f509e660625f (diff)
Extract anchors validation classes to separate files
Diffstat (limited to 'lib/gitlab')
-rw-r--r--lib/gitlab/docs/document.rb16
-rw-r--r--lib/gitlab/docs/element.rb32
-rw-r--r--lib/gitlab/docs/link.rb71
-rw-r--r--lib/gitlab/docs/nanoc.rb13
-rw-r--r--lib/gitlab/docs/page.rb46
5 files changed, 178 insertions, 0 deletions
diff --git a/lib/gitlab/docs/document.rb b/lib/gitlab/docs/document.rb
new file mode 100644
index 00000000..d4e486b2
--- /dev/null
+++ b/lib/gitlab/docs/document.rb
@@ -0,0 +1,16 @@
+module Gitlab
+ module Docs
+ class Document < Nokogiri::XML::SAX::Document
+ def initialize(page)
+ @page = page
+ end
+
+ def start_element(name, attributes = [])
+ Gitlab::Docs::Element.new(name, attributes).tap do |element|
+ @page.hrefs << element.href if element.link?
+ @page.ids << element.id if element.has_id?
+ end
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/docs/element.rb b/lib/gitlab/docs/element.rb
new file mode 100644
index 00000000..a78b9a01
--- /dev/null
+++ b/lib/gitlab/docs/element.rb
@@ -0,0 +1,32 @@
+module Gitlab
+ module Docs
+ class Element
+ def initialize(name, attributes)
+ @name = name
+ @attributes = attributes
+ end
+
+ def link?
+ @name == 'a' && !href.to_s.empty?
+ end
+
+ def has_id?
+ !id.to_s.empty?
+ end
+
+ def href
+ @href ||= attribute('href')
+ end
+
+ def id
+ @id ||= attribute('id')
+ end
+
+ private
+
+ def attribute(name)
+ @attributes.find { |attr| attr.first == name }&.last
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/docs/link.rb b/lib/gitlab/docs/link.rb
new file mode 100644
index 00000000..8509f0ca
--- /dev/null
+++ b/lib/gitlab/docs/link.rb
@@ -0,0 +1,71 @@
+module Gitlab
+ module Docs
+ class Link
+ attr_reader :link, :href, :page
+
+ def initialize(link, page)
+ @href = link
+ @page = page
+ end
+
+ def to_anchor?
+ @href.to_s.include?('#')
+ end
+
+ def anchor_name
+ raise ArgumentError unless to_anchor?
+
+ @href.to_s.partition('#').last.downcase
+ end
+
+ def internal_anchor?
+ raise ArgumentError unless to_anchor?
+
+ @href.to_s.partition('#').first.empty?
+ end
+
+ def internal?
+ @href.to_s.length > 0 && !@href.include?(':')
+ end
+
+ def path
+ @href.to_s.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
+
diff --git a/lib/gitlab/docs/nanoc.rb b/lib/gitlab/docs/nanoc.rb
new file mode 100644
index 00000000..70b4c397
--- /dev/null
+++ b/lib/gitlab/docs/nanoc.rb
@@ -0,0 +1,13 @@
+module Gitlab
+ module Docs
+ module Nanoc
+ def self.config
+ @config ||= YAML.load(File.read('nanoc.yaml'))
+ end
+
+ def self.output_dir
+ config.fetch('output_dir')
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/docs/page.rb b/lib/gitlab/docs/page.rb
new file mode 100644
index 00000000..8bd5e9b9
--- /dev/null
+++ b/lib/gitlab/docs/page.rb
@@ -0,0 +1,46 @@
+module Gitlab
+ module Docs
+ class Page
+ attr_reader :file
+ attr_accessor :hrefs, :ids
+
+ def initialize(file)
+ @file = file
+ @hrefs = []
+ @ids = []
+
+ return unless exists?
+
+ Nokogiri::HTML::SAX::Parser
+ .new(Gitlab::Docs::Document.new(self))
+ .parse(File.read(file))
+ end
+
+ def exists?
+ File.exists?(@file)
+ end
+
+ def directory
+ File.dirname(@file)
+ end
+
+ def links
+ @links ||= @hrefs.map do |link|
+ Gitlab::Docs::Link.new(link, self)
+ end
+ end
+
+ def has_anchor?(name)
+ @ids.include?(name)
+ end
+
+ def self.build(path)
+ if path.end_with?('.html')
+ new(path)
+ else
+ new(File.join(path, 'index.html'))
+ end
+ end
+ end
+ end
+end