bunto-relative-links 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2e414b31671f3ef2b4a168c87f4da03773058919
4
+ data.tar.gz: 3474ab341a7d6dc6e8cee72939be5da9330da96f
5
+ SHA512:
6
+ metadata.gz: 89873515806e6650f06b336e333a3b06b27c2fea6997ed63bb6ef1af7ab53e37b178dba92ff7733072b3676231769483e8b274ca99e6a8932c834f7b599bc80f
7
+ data.tar.gz: 78609e675dadaae1ae323928bb05884bb988aae3a6d5701225b757056599371a1be781a478c8c81f61e4adeb227c5259b89e7df38309a9b5b62d363c0597d989
@@ -0,0 +1,6 @@
1
+ require "bunto"
2
+ require_relative "bunto-relative-links/generator"
3
+ require_relative "bunto-relative-links/context"
4
+
5
+ module BuntoRelativeLinks
6
+ end
@@ -0,0 +1,13 @@
1
+ module BuntoRelativeLinks
2
+ class Context
3
+ attr_reader :site
4
+
5
+ def initialize(site)
6
+ @site = site
7
+ end
8
+
9
+ def registers
10
+ { :site => site }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,111 @@
1
+ module BuntoRelativeLinks
2
+ class Generator < Bunto::Generator
3
+ attr_accessor :site
4
+
5
+ # Use Bunto's native relative_url filter
6
+ include Bunto::Filters::URLFilters
7
+
8
+ LINK_TEXT_REGEX = %r!([^\]]+)!
9
+ FRAGMENT_REGEX = %r!(#.+?)?!
10
+ INLINE_LINK_REGEX = %r!\[#{LINK_TEXT_REGEX}\]\(([^\)]+?)#{FRAGMENT_REGEX}\)!
11
+ REFERENCE_LINK_REGEX = %r!^\[#{LINK_TEXT_REGEX}\]: (.+?)#{FRAGMENT_REGEX}$!
12
+ LINK_REGEX = %r!(#{INLINE_LINK_REGEX}|#{REFERENCE_LINK_REGEX})!
13
+ CONVERTER_CLASS = Bunto::Converters::Markdown
14
+
15
+ safe true
16
+ priority :lowest
17
+
18
+ def initialize(site)
19
+ @site = site
20
+ @context = context
21
+ end
22
+
23
+ def generate(site)
24
+ @site = site
25
+ @context = context
26
+
27
+ site.pages.each do |page|
28
+ next unless markdown_extension?(page.extname)
29
+ replace_relative_links!(page)
30
+ end
31
+ end
32
+
33
+ def replace_relative_links!(page)
34
+ url_base = File.dirname(page.path)
35
+
36
+ page.content.gsub!(LINK_REGEX) do |original|
37
+ link_type, link_text, relative_path, fragment = link_parts(Regexp.last_match)
38
+ next original if fragment?(relative_path) || absolute_url?(relative_path)
39
+
40
+ path = path_from_root(relative_path, url_base)
41
+ url = url_for_path(path)
42
+
43
+ if url
44
+ replacement_text(link_type, link_text, url, fragment)
45
+ else
46
+ original
47
+ end
48
+ end
49
+ rescue ArgumentError => e
50
+ raise e unless e.to_s.start_with?("invalid byte sequence in UTF-8")
51
+ end
52
+
53
+ private
54
+
55
+ def link_parts(matches)
56
+ link_type = matches[2] ? :inline : :reference
57
+ link_text = matches[link_type == :inline ? 2 : 5]
58
+ relative_path = matches[link_type == :inline ? 3 : 6]
59
+ fragment = matches[link_type == :inline ? 4 : 7]
60
+ [link_type, link_text, relative_path, fragment]
61
+ end
62
+
63
+ def context
64
+ BuntoRelativeLinks::Context.new(site)
65
+ end
66
+
67
+ def markdown_extension?(extension)
68
+ markdown_converter.matches(extension)
69
+ end
70
+
71
+ def markdown_converter
72
+ @markdown_converter ||= site.find_converter_instance(CONVERTER_CLASS)
73
+ end
74
+
75
+ def url_for_path(path)
76
+ target = potential_targets.find { |p| p.relative_path.sub(%r!\A/!, "") == path }
77
+ relative_url(target.url) if target
78
+ end
79
+
80
+ def potential_targets
81
+ @potential_targets ||= (site.pages + site.static_files)
82
+ end
83
+
84
+ def path_from_root(relative_path, url_base)
85
+ relative_path.sub!(%r!\A/!, "")
86
+ absolute_path = File.expand_path(relative_path, url_base)
87
+ absolute_path.sub(%r!\A#{Regexp.escape(Dir.pwd)}/!, "")
88
+ end
89
+
90
+ def replacement_text(type, text, url, fragment = nil)
91
+ url << fragment if fragment
92
+
93
+ if type == :inline
94
+ "[#{text}](#{url})"
95
+ else
96
+ "[#{text}]: #{url}"
97
+ end
98
+ end
99
+
100
+ def absolute_url?(string)
101
+ return unless string
102
+ Addressable::URI.parse(string).absolute?
103
+ rescue Addressable::URI::InvalidURIError
104
+ nil
105
+ end
106
+
107
+ def fragment?(string)
108
+ string && string.start_with?("#")
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,3 @@
1
+ module BuntoRelativeLinks
2
+ VERSION = "0.4.1".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bunto-relative-links
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Balter
8
+ - Suriyaa Kudo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-08-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bunto
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3.4'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '3.4'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rubocop
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '0.43'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '0.43'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.5'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.5'
56
+ description:
57
+ email:
58
+ - ben.balter@github.com
59
+ - github@suriyaa.tk
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - lib/bunto-relative-links.rb
65
+ - lib/bunto-relative-links/context.rb
66
+ - lib/bunto-relative-links/generator.rb
67
+ - lib/bunto-relative-links/version.rb
68
+ homepage: https://github.com/bunto/bunto-relative-links
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.6.12
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: A Bunto plugin to convert relative links to markdown files to their rendered
92
+ equivalents.
93
+ test_files: []