You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2021/09/07 10:25:44 UTC

[brooklyn-docs] 03/03: apply link rewriting to links imported as children

This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-docs.git

commit bb534ab630c4983a625d4a2d8bba44b8b05bc476
Author: Alex Heneveld <al...@cloudsoftcorp.com>
AuthorDate: Sat Sep 4 03:06:56 2021 +0100

    apply link rewriting to links imported as children
---
 _plugins/jekyll_relative_links.rb | 30 +++++++++++++++++++++---------
 _plugins/page_structure.rb        |  7 ++++++-
 _plugins/read.rb                  |  8 ++++----
 3 files changed, 31 insertions(+), 14 deletions(-)

diff --git a/_plugins/jekyll_relative_links.rb b/_plugins/jekyll_relative_links.rb
index 22691d4..5e915e5 100644
--- a/_plugins/jekyll_relative_links.rb
+++ b/_plugins/jekyll_relative_links.rb
@@ -7,7 +7,7 @@
 # - speculatively map .html to .md when doing the lookup
 # - make url_for_path public, and have way to inject site and initialize context outside of normal generator usage
 
-# distributed under the MIT License as follows (note this is only used to build the docs, not included with any AMP output):
+# distributed under the MIT License as follows (note this is only used to build the docs, not included with any Brooklyn output):
 
 # MIT License
 #
@@ -43,7 +43,7 @@ module JekyllRelativeLinks
     FRAGMENT_REGEX = %r!(#.+?|)?!.freeze
     TITLE_REGEX = %r{(\s+"(?:\\"|[^"])*(?<!\\)"|\s+"(?:\\'|[^'])*(?<!\\)')?}.freeze
     FRAG_AND_TITLE_REGEX = %r!#{FRAGMENT_REGEX}#{TITLE_REGEX}!.freeze
-    INLINE_LINK_REGEX = %r!\[#{LINK_TEXT_REGEX}\]\(([^\)]+?)#{FRAG_AND_TITLE_REGEX}\)!.freeze
+    INLINE_LINK_REGEX = %r!\[#{LINK_TEXT_REGEX}\]\(([^\)]*?)#{FRAG_AND_TITLE_REGEX}\)!.freeze
     REFERENCE_LINK_REGEX = %r!^\s*?\[#{LINK_TEXT_REGEX}\]: (.+?)#{FRAG_AND_TITLE_REGEX}\s*?$!.freeze
     LINK_REGEX = %r!(#{INLINE_LINK_REGEX}|#{REFERENCE_LINK_REGEX})!.freeze
     CONVERTER_CLASS = Jekyll::Converters::Markdown
@@ -91,26 +91,38 @@ module JekyllRelativeLinks
       end
     end
 
+
     def replace_relative_links!(document)
-      url_base = File.dirname(document.relative_path)
       return document if document.content.nil?
 
-      document.content = document.content.dup.gsub(LINK_REGEX) do |original|
+      document.content = replace_relative_links_in_content(document.content, document.relative_path)
+
+      replace_relative_links_excerpt!(document)
+    rescue ArgumentError => e
+      raise e unless e.to_s.start_with?("invalid byte sequence in UTF-8")
+    end
+
+    def replace_relative_links_in_content(content, relative_to_path)
+      url_base = File.dirname(relative_to_path)
+
+      content.dup.gsub(LINK_REGEX) do |original|
         link = link_parts(Regexp.last_match)
+
+        if (link.path == "" && link.fragment == "" && link.text && link.text.start_with?("http"))
+          link.path = link.text
+          return replacement_text(link)
+        end
+
         next original unless replaceable_link?(link.path)
 
         path = path_from_root(link.path, url_base)
-        url  = url_for_path(path, document.relative_path)
+        url  = url_for_path(path, relative_to_path)
 
         next original unless url
 
         link.path = url
         replacement_text(link)
       end
-
-      replace_relative_links_excerpt!(document)
-    rescue ArgumentError => e
-      raise e unless e.to_s.start_with?("invalid byte sequence in UTF-8")
     end
 
     def url_for_path_absolute(path)
diff --git a/_plugins/page_structure.rb b/_plugins/page_structure.rb
index f372534..5897dff 100644
--- a/_plugins/page_structure.rb
+++ b/_plugins/page_structure.rb
@@ -214,9 +214,14 @@ module PageStructureUtils
         # render the included content with the current page renderer
         info = { :filters => [Jekyll::Filters], :registers => { :site => site, :page => page } }
         path_for_cache = "include_page-#{context['page']}"
+
+        relative_link_parser = JekyllRelativeLinks::Generator.new(nil)
+        relative_link_parser.prepare_for_site(site)
+        $content = relative_link_parser.replace_relative_links_in_content($content, page.relative_path)
+
         page.render_liquid($content, site.site_payload, info, path_for_cache)
       end
     end
 end
 
-Liquid::Template.register_tag('child_content', PageStructureUtils::IncludePageContentTag)
\ No newline at end of file
+Liquid::Template.register_tag('child_content', PageStructureUtils::IncludePageContentTag)
diff --git a/_plugins/read.rb b/_plugins/read.rb
index f35672f..df0a26a 100644
--- a/_plugins/read.rb
+++ b/_plugins/read.rb
@@ -45,7 +45,7 @@ module JekyllRead
 
       # is there a better way to trim a leading / ?
       file = file.relative_path_from(Pathname.new("/")) unless file.relative?
-      raise "No such file #{file} in read call" unless file.exist?
+      raise "No such file #{file} in read call (from #{context.dig('page','path')})" unless file.exist?
       file
     end
 
@@ -56,9 +56,9 @@ module JekyllRead
       jekyllSite = context.registers[:site]
       targetPage = Jekyll::Page.new(jekyllSite, jekyllSite.source, File.dirname(file), File.basename(file))
 
-      @relative_link_parser = JekyllRelativeLinks::Generator.new(nil)
-      @relative_link_parser.prepare_for_site(jekyllSite)
-      @relative_link_parser.replace_relative_links!(targetPage)
+      relative_link_parser = JekyllRelativeLinks::Generator.new(nil)
+      relative_link_parser.prepare_for_site(jekyllSite)
+      relative_link_parser.replace_relative_links!(targetPage)
 
       targetPage.render(jekyllSite.layouts, jekyllSite.site_payload)
       targetPage.output