Skip to content
This repository was archived by the owner on Oct 7, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/_sidebar-template.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</a>
<ul class="doc-link__items" style='<%= "<%= active_keywords[1] -\%\>" -%>'>
<%- value.each do |value2| -%>
<%- if !value2["Link"].nil? && reveal(raw(value2["Link"])) -%>
<%- if !value2["Link"].nil? && is_link_valid(raw(value2["Link"])) -%>
<li>
<%- if !value2["Slug"] -%>
<%- value2["Slug"] = get_slug(value2) -%>
Expand Down
65 changes: 55 additions & 10 deletions lib/gluegun.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'github/markdown'
require 'erb'
require 'fileutils'
require 'net/http'

module Gluegun

Expand All @@ -20,7 +21,15 @@ def self.gluegun_generate_index(site_config_file)
]
@site_map = YAML.load(open(config_file).read)
dest_path = @site_map['Output']
# Check if first link is valid before generating index
if !is_link_valid(@site_map['Documents'][0].values[0][0]["Link"])
puts "ERROR: " + @site_map['Documents'][0].keys[0] + " -> " + @site_map['Documents'][0].values[0][0].keys[0] +
": gluegun cannot generate index page from this link. " +
"Please check links under (" + @site_map['Documents'][0].values[0][0].keys[0] + ") in site.yml file. "
exit (false)
end
if !dest_path.nil?
puts "Generating Index Page..."
FileUtils.mkdir_p(dest_path) unless File.exist?(dest_path)
File.open(File.join(dest_path, html_file), "w+") do |f|
partial_erb_arr.each do |partial_erb|
Expand All @@ -31,6 +40,7 @@ def self.gluegun_generate_index(site_config_file)
f.puts(ERB.new(File.read(partial_erb), nil, '-').result(binding))
end
end
puts "Completed. Host the generated html files at: " + dest_path
else
puts "Missing destination directory in site.yml file."
end
Expand All @@ -53,24 +63,23 @@ def self.gluegun_generate_pages(site_config_file)
@site_map['Documents'].each do |categories|
categories.each do |key, value|
if !value
puts "ERROR: " + key + ": gluegun cannot not generate docs from this link. " +
puts "ERROR: " + key + ": gluegun cannot generate docs from this link. " +
"Please check links under (" + key + ") in site.yml file. "
exit (false)
end
value.each do |key2|
if ! key2["Link"]
puts "ERROR: " + key + " -> " + key2.keys[0] + ": gluegun cannot not generate docs from this link. " +
puts "ERROR: " + key + " -> " + key2.keys[0] + ": gluegun cannot generate docs from this link. " +
"Please check links under (" + key2.keys[0] + ") in site.yml file. "
exit (false)
else
puts "\t - " + key2.keys[0] + " ....DONE"
orig_link = key2["Link"].dup
link = raw(key2['Link'])
end
if !key2["Slug"]
key2["Slug"] = get_slug(key2)
end
begin
if is_link_valid(key2['Link'])
File.open(File.join(dest_path,"/#{key2["Slug"]}.html"), "w+") do |f|
partial_erb_arr.each do |partial_erb|
# Set nil to "-" to activate "<%-" and "-%>" characters
Expand All @@ -80,10 +89,10 @@ def self.gluegun_generate_pages(site_config_file)
f.puts(ERB.new(File.read(partial_erb), nil, '-').result(binding))
end
end
rescue OpenURI::HTTPError
else
# Calling an empty puts to create a new line.
puts
puts "WARNING: " + key + " -> " + key2.keys[0] + ": gluegun cannot not fetch content from this link. " +
puts "WARNING: " + key + " -> " + key2.keys[0] + ": gluegun cannot fetch content from this link. " +
"Please check this link (" + orig_link + ") in site.yml file. "
end
end
Expand All @@ -94,7 +103,6 @@ def self.gluegun_generate_pages(site_config_file)
copy_with_path('lib/img', dest_path)
copy_with_path('lib/js', dest_path)
copy_with_path('lib/vendors', dest_path)
puts "Completed. Host the generated html files at: " + dest_path
else
puts "Missing document links in the site.yml file."
end
Expand Down Expand Up @@ -126,9 +134,12 @@ def self.generate_sidebar(site_config_file)

def self.reveal(link)
begin
response = GitHub::Markdown.render_gfm(open(link).read)
return response
rescue OpenURI::HTTPError
if (!link.nil?)
filtered = filter_banners(open(link).read)
response = GitHub::Markdown.render_gfm(filtered)
return response
end
rescue => e
end
end

Expand Down Expand Up @@ -171,6 +182,40 @@ def self.get_github_link(link)
return link
end

def self.is_link_valid(link)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also use a gem to do this. Or do it ourselves.

begin
uri = URI.parse(link)
req = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"
req.use_ssl = true
end
res = req.request_head(uri.path)
if res.code === "200"
return true
else
return false
end
rescue => e
return false
end
end

def self.filter_banners(doc)
if !@site_map['Rules'].nil?
if !@site_map['Rules']['Filter'].nil?
@site_map['Rules']['Filter'].each do |pattern|
if !pattern.nil?
regex = Regexp.new(pattern)
if doc =~ regex
doc.gsub!(regex, '')
end
end
end
end
end
return doc
end

def self.copy_with_path(src, dst)
FileUtils.mkdir_p(File.dirname(dst))
FileUtils.cp_r(src, dst)
Expand Down
11 changes: 11 additions & 0 deletions site-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Plugins: # Option
index: "INDEX_NAME"
- Google Analytics: "UA-XXXXX-Y"

Rules: # Optional - Regex rules
Filter: # Pattern to be filtered out from the fetched markdown
- Enter regex here

Documents: # Required - Documents with links to generate site
- Category Heading: # Required- Category Name
- Document Title: # Required - Document Title
Expand All @@ -37,6 +41,13 @@ Documents: # Requir
# index: "xxxxxxxx"
# - Google Analytics: "UA-xxxxxxx-Y"

# Rules: # Optional - Regex rules
# Filter: # Pattern to be filtered out from the fetched markdown
# - s?\[\!\[Slack\].*$??
# - s?\[\!\[Gitter\].*$??
# Replace:
# - minio MINIO

# Documents: # Required - Documents with links to generate site
# - Minio Server: # Required- Category Name
# - Minio Server Quick Start Guide: # Required - Document Title
Expand Down
5 changes: 5 additions & 0 deletions site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Plugins:
Label: "Talk to community"
Link: "/"

Rules:
Filter:
- s?\[\!\[Slack\].*$??
- s?\[\!\[Gitter\].*$??

Documents:
- Get Started:
- Gluegun Quick Start Guide:
Expand Down