From d14c17f6f3cdc54149a65c97d1a97c11b6118a2d Mon Sep 17 00:00:00 2001 From: Gabriel Fontes Date: Fri, 17 Jun 2022 16:43:53 -0300 Subject: [PATCH 1/2] add support for gemini (gemtext) links --- lib/jekyll-relative-links/generator.rb | 52 +++++++++++++++++++++----- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/lib/jekyll-relative-links/generator.rb b/lib/jekyll-relative-links/generator.rb index aa99db4..a721f1f 100644 --- a/lib/jekyll-relative-links/generator.rb +++ b/lib/jekyll-relative-links/generator.rb @@ -13,7 +13,8 @@ class Generator < Jekyll::Generator FRAG_AND_TITLE_REGEX = %r!#{FRAGMENT_REGEX}#{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 + GEMINI_LINK_REGEX = %r!^=>\s*((?:{. )?[^#\s}]*(?: .})?)(#[^\s]*|)?[^\S\r\n]*#{LINK_TEXT_REGEX}?$!.freeze + LINK_REGEX = %r!(#{INLINE_LINK_REGEX}|#{REFERENCE_LINK_REGEX}|#{GEMINI_LINK_REGEX})!.freeze CONVERTER_CLASS = Jekyll::Converters::Markdown CONFIG_KEY = "relative_links" ENABLED_KEY = "enabled" @@ -37,7 +38,7 @@ def generate(site) documents = site.pages + site.docs_to_write if collections? documents.each do |document| - next unless markdown_extension?(document.extname) + next unless markdown_extension?(document.extname) or gemini_extension?(document.extname) next if document.is_a?(Jekyll::StaticFile) next if excluded?(document) @@ -72,19 +73,46 @@ def replace_relative_links!(document) Link = Struct.new(:link_type, :text, :path, :fragment, :title) def link_parts(matches) - last_inline = 5 - link_type = matches[2] ? :inline : :reference - link_text = matches[link_type == :inline ? 2 : last_inline + 1] - relative_path = matches[link_type == :inline ? 3 : last_inline + 2] - fragment = matches[link_type == :inline ? 4 : last_inline + 3] - title = matches[link_type == :inline ? 5 : last_inline + 4] - Link.new(link_type, link_text, relative_path, fragment, title) + # Determine type based on non null matches + type = + if matches[10] then :gemini + elsif matches[6] then :reference + elsif matches[2] then :inline + else raise "invalid link type" + end + + link_texts = { + :inline => matches[2], + :reference => matches[6], + :gemini => matches[12], + } + relative_paths = { + :inline => matches[3], + :reference => matches[7], + :gemini => matches[10], + } + fragments = { + :inline => matches[4], + :reference => matches[8], + :gemini => matches[11], + } + titles = { + :inline => matches[5], + :reference => matches[9], + :gemini => nil, + } + + Link.new(type, link_texts[type], relative_paths[type], fragments[type], titles[type]) end def context @context ||= JekyllRelativeLinks::Context.new(site) end + def gemini_extension?(extension) + extension == ".gmi" + end + def markdown_extension?(extension) markdown_converter.matches(extension) end @@ -118,8 +146,12 @@ def replacement_text(link) if link.link_type == :inline "[#{link.text}](#{link.path}#{link.title})" - else + elsif link.link_type == :reference "\n[#{link.text}]: #{link.path}#{link.title}" + elsif link.link_type == :gemini + "\n=> #{link.path} #{link.text}#{link.title}" + else + raise "invalid link type" end end From 18d60c161ecbfaf204e4cddf8e3b8fd9ef2aa584 Mon Sep 17 00:00:00 2001 From: Gabriel Fontes Date: Fri, 17 Jun 2022 19:53:34 -0300 Subject: [PATCH 2/2] gemini: remove extra line break --- lib/jekyll-relative-links/generator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll-relative-links/generator.rb b/lib/jekyll-relative-links/generator.rb index a721f1f..89a871c 100644 --- a/lib/jekyll-relative-links/generator.rb +++ b/lib/jekyll-relative-links/generator.rb @@ -149,7 +149,7 @@ def replacement_text(link) elsif link.link_type == :reference "\n[#{link.text}]: #{link.path}#{link.title}" elsif link.link_type == :gemini - "\n=> #{link.path} #{link.text}#{link.title}" + "=> #{link.path} #{link.text}#{link.title}" else raise "invalid link type" end