mirror of
https://github.com/foo-dogsquared/asciidoctor-foodogsquared-extensions.git
synced 2025-01-31 04:58:07 +00:00
Update according to Rubocop linter
This commit is contained in:
parent
3f450e2197
commit
dca17c64e7
@ -1,3 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'asciidoctor'
|
require 'asciidoctor'
|
||||||
require 'asciidoctor/extensions'
|
require 'asciidoctor/extensions'
|
||||||
require_relative 'man-inline-macro/extension'
|
require_relative 'man-inline-macro/extension'
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'uri'
|
require 'uri'
|
||||||
|
|
||||||
class GitHubLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
|
class GitHubLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
|
||||||
@ -6,7 +8,7 @@ class GitHubLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
|
|||||||
named :github
|
named :github
|
||||||
name_positional_attributes 'caption'
|
name_positional_attributes 'caption'
|
||||||
|
|
||||||
def process parent, target, attrs
|
def process(parent, target, attrs)
|
||||||
doc = parent.document
|
doc = parent.document
|
||||||
|
|
||||||
text = attrs['caption'] || target
|
text = attrs['caption'] || target
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'base64'
|
require 'base64'
|
||||||
require 'json'
|
require 'json'
|
||||||
require 'open-uri'
|
require 'open-uri'
|
||||||
require 'uri'
|
require 'uri'
|
||||||
|
|
||||||
class GitHubRawIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
|
class GitHubRawIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
|
||||||
def handles? target
|
def handles?(target)
|
||||||
target.start_with? 'github:'
|
target.start_with? 'github:'
|
||||||
end
|
end
|
||||||
|
|
||||||
def warn_or_raise doc, warning
|
def warn_or_raise(doc, warning)
|
||||||
if (doc.safe > Asciidoctor::SafeMode::SERVER) && !(doc.attr? 'allow-uri-read')
|
if (doc.safe > Asciidoctor::SafeMode::SERVER) && !(doc.attr? 'allow-uri-read')
|
||||||
raise warning
|
raise warning
|
||||||
else
|
else
|
||||||
@ -16,7 +18,7 @@ class GitHubRawIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def process doc, reader, target, attrs
|
def process(doc, reader, target, attrs)
|
||||||
src = target.delete_prefix('github:').split('/', 3)
|
src = target.delete_prefix('github:').split('/', 3)
|
||||||
owner = src.at 0
|
owner = src.at 0
|
||||||
repo = src.at 1
|
repo = src.at 1
|
||||||
@ -28,14 +30,14 @@ class GitHubRawIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
|
|||||||
uri = URI.parse %(https://api.github.com/repos/#{owner}/#{repo}/contents/#{path})
|
uri = URI.parse %(https://api.github.com/repos/#{owner}/#{repo}/contents/#{path})
|
||||||
|
|
||||||
if attrs['rev']
|
if attrs['rev']
|
||||||
query = { :ref => attrs['rev'] }
|
query = { ref: attrs['rev'] }
|
||||||
uri.query = URI.encode_www_form query
|
uri.query = URI.encode_www_form query
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
headers = {
|
headers = {
|
||||||
'Header' => 'application/vnd.github+json',
|
'Header' => 'application/vnd.github+json',
|
||||||
'X-GitHub-Api-Version' => '2022-11-28',
|
'X-GitHub-Api-Version' => '2022-11-28'
|
||||||
}
|
}
|
||||||
|
|
||||||
headers['Authorization'] = "Token #{ENV['GITHUB_API_BEARER_TOKEN']}" if ENV['GITHUB_API_BEARER_TOKEN']
|
headers['Authorization'] = "Token #{ENV['GITHUB_API_BEARER_TOKEN']}" if ENV['GITHUB_API_BEARER_TOKEN']
|
||||||
@ -45,20 +47,18 @@ class GitHubRawIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
|
|||||||
|
|
||||||
# If the response is an array, it is likely to be a directory. In this
|
# If the response is an array, it is likely to be a directory. In this
|
||||||
# usecase, we'll just list them.
|
# usecase, we'll just list them.
|
||||||
content = if response.kind_of? Array
|
content = if response.is_a? Array
|
||||||
warning = %(given path '#{path}' from GitHub repo '#{repo}' is a directory)
|
warning = %(given path '#{path}' from GitHub repo '#{repo}' is a directory)
|
||||||
warn_or_raise doc, warning
|
warn_or_raise doc, warning
|
||||||
warning
|
warning
|
||||||
elsif response.kind_of? Object
|
elsif response.is_a? Object
|
||||||
if response['content'] && response['encoding'] == 'base64'
|
Base64.decode64 response['content'] if response['content'] && response['encoding'] == 'base64'
|
||||||
Base64.decode64 response['content']
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
reader.push_include content, target, target, 1, attrs
|
reader.push_include content, target, target, 1, attrs
|
||||||
end
|
end
|
||||||
rescue OpenURI::HTTPError => e
|
rescue OpenURI::HTTPError => e
|
||||||
warning = %(error while getting '#{path}' in GitHub repo '#{repo}: #{e}')
|
warning = %(error while getting '#{path}' in GitHub repo '#{namespaced_repo}: #{e}')
|
||||||
warn_or_raise doc, warning
|
warn_or_raise doc, warning
|
||||||
reader.push_include warning, target, target, 1, attrs
|
reader.push_include warning, target, target, 1, attrs
|
||||||
end
|
end
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'uri'
|
require 'uri'
|
||||||
|
|
||||||
class GitLabLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
|
class GitLabLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
|
||||||
@ -7,7 +9,7 @@ class GitLabLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
|
|||||||
name_positional_attributes 'caption'
|
name_positional_attributes 'caption'
|
||||||
default_attributes 'domain' => 'gitlab.com'
|
default_attributes 'domain' => 'gitlab.com'
|
||||||
|
|
||||||
def process parent, target, attrs
|
def process(parent, target, attrs)
|
||||||
doc = parent.document
|
doc = parent.document
|
||||||
|
|
||||||
text = attrs['caption'] || target
|
text = attrs['caption'] || target
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'base64'
|
require 'base64'
|
||||||
require 'json'
|
require 'json'
|
||||||
require 'open-uri'
|
require 'open-uri'
|
||||||
require 'uri'
|
require 'uri'
|
||||||
|
|
||||||
class GitLabRawIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
|
class GitLabRawIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
|
||||||
@@prefix = 'gitlab:'
|
@prefix = 'gitlab:'
|
||||||
|
|
||||||
def handles? target
|
def handles?(target)
|
||||||
target.start_with? @@prefix
|
target.start_with? @prefix
|
||||||
end
|
end
|
||||||
|
|
||||||
def warn_or_raise doc, warning
|
def warn_or_raise doc, warning
|
||||||
@ -18,14 +20,14 @@ class GitLabRawIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def process doc, reader, target, attrs
|
def process(doc, reader, target, attrs)
|
||||||
src = target.delete_prefix(@@prefix).split('/', 2)
|
src = target.delete_prefix(@prefix).split('/', 2)
|
||||||
owner = src.at 0
|
owner = src.at 0
|
||||||
repo = src.at 1
|
repo = src.at 1
|
||||||
namespaced_repo = "#{owner}/#{repo}"
|
namespaced_repo = "#{owner}/#{repo}"
|
||||||
|
|
||||||
raise %(there is no 'path' attribute given for GitLab repo '#{namespaced_repo}') unless (attrs.key? 'path')
|
raise %(there is no 'path' attribute given for GitLab repo '#{namespaced_repo}') unless attrs.key? 'path'
|
||||||
raise %(no given ref for getting file in '#{namespaced_repo}') unless (attrs.key? 'rev')
|
raise %(no given ref for getting file in '#{namespaced_repo}') unless attrs.key? 'rev'
|
||||||
|
|
||||||
path = attrs['path']
|
path = attrs['path']
|
||||||
rev = attrs['rev']
|
rev = attrs['rev']
|
||||||
@ -42,7 +44,7 @@ class GitLabRawIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
|
|||||||
uri += %(/repository/files/#{URI.encode_www_form_component path})
|
uri += %(/repository/files/#{URI.encode_www_form_component path})
|
||||||
|
|
||||||
# Then the revision.
|
# Then the revision.
|
||||||
query = { :ref => rev }
|
query = { ref: rev }
|
||||||
uri.query = URI.encode_www_form query
|
uri.query = URI.encode_www_form query
|
||||||
|
|
||||||
content = begin
|
content = begin
|
||||||
@ -52,9 +54,7 @@ class GitLabRawIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
|
|||||||
OpenURI.open_uri(uri, headers) do |f|
|
OpenURI.open_uri(uri, headers) do |f|
|
||||||
response = JSON.parse(f.read)
|
response = JSON.parse(f.read)
|
||||||
|
|
||||||
if response['content'] && response['encoding'] == 'base64'
|
Base64.decode64 response['content'] if response['content'] && response['encoding'] == 'base64'
|
||||||
Base64.decode64 response['content']
|
|
||||||
end
|
|
||||||
|
|
||||||
reader.push_include content, target, target, 1, attrs
|
reader.push_include content, target, target, 1, attrs
|
||||||
end
|
end
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class ManInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
|
class ManInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
|
||||||
use_dsl
|
use_dsl
|
||||||
|
|
||||||
@ -5,7 +7,7 @@ class ManInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
|
|||||||
name_positional_attributes 'volnum'
|
name_positional_attributes 'volnum'
|
||||||
default_attributes 'domain' => 'manpages.debian.org'
|
default_attributes 'domain' => 'manpages.debian.org'
|
||||||
|
|
||||||
def process parent, target, attrs
|
def process(parent, target, attrs)
|
||||||
doc = parent.document
|
doc = parent.document
|
||||||
text = manname = target
|
text = manname = target
|
||||||
suffix = (volnum = attrs['volnum']) ? %((#{volnum})) : ''
|
suffix = (volnum = attrs['volnum']) ? %((#{volnum})) : ''
|
||||||
|
@ -1,15 +1,17 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class SWHInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
|
class SWHInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
|
||||||
use_dsl
|
use_dsl
|
||||||
|
|
||||||
named :swh
|
named :swh
|
||||||
name_positional_attributes 'caption'
|
name_positional_attributes 'caption'
|
||||||
|
|
||||||
def process parent, target, attrs
|
def process(parent, target, attrs)
|
||||||
doc = parent.document
|
doc = parent.document
|
||||||
|
|
||||||
# We're only considering `swh:` starting with the scheme version. Also, it
|
# We're only considering `swh:` starting with the scheme version. Also, it
|
||||||
# looks nice aesthetically.
|
# looks nice aesthetically.
|
||||||
swhid = (target.start_with? 'swh:') ? target : %(swh:#{target})
|
swhid = target.start_with?('swh:') ? target : %(swh:#{target})
|
||||||
swhid_core_identifier = (swhid.split ';').at 0
|
swhid_core_identifier = (swhid.split ';').at 0
|
||||||
|
|
||||||
text = attrs['caption'] || swhid_core_identifier
|
text = attrs['caption'] || swhid_core_identifier
|
||||||
|
Loading…
Reference in New Issue
Block a user