From a4b41e884026e15a82259acd3e3c3f3f3ae93dfd Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Wed, 10 May 2023 13:03:36 +0800 Subject: [PATCH] project: init Asciidoctor custom extension --- .gitignore | 58 +++++++++++++++++++ Gemfile | 7 +++ Gemfile.lock | 47 +++++++++++++++ asciidoctor-custom-extensions.gemspec | 16 +++++ lib/asciidoctor-custom-extensions.rb | 10 ++++ .../swhid_link_inline_macro.rb | 23 ++++++++ swhid-sample.adoc | 10 ++++ 7 files changed, 171 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 asciidoctor-custom-extensions.gemspec create mode 100644 lib/asciidoctor-custom-extensions.rb create mode 100644 lib/asciidoctor/custom_extensions/swhid_link_inline_macro.rb create mode 100644 swhid-sample.adoc diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b83ab0b --- /dev/null +++ b/.gitignore @@ -0,0 +1,58 @@ +### Ruby ### +*.gem +*.rbc +/.config +/coverage/ +/InstalledFiles +/pkg/ +/spec/reports/ +/spec/examples.txt +/test/tmp/ +/test/version_tmp/ +/tmp/ + +# Used by dotenv library to load environment variables. +# .env + +# Ignore Byebug command history file. +.byebug_history + +## Specific to RubyMotion: +.dat* +.repl_history +build/ +*.bridgesupport +build-iPhoneOS/ +build-iPhoneSimulator/ + +## Specific to RubyMotion (use of CocoaPods): +# +# We recommend against adding the Pods directory to your .gitignore. However +# you should judge for yourself, the pros and cons are mentioned at: +# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control +# +# vendor/Pods/ + +## Documentation cache and generated files: +/.yardoc/ +/_yardoc/ +/doc/ +/rdoc/ + +## Environment normalization: +/.bundle/ +/vendor/bundle +/lib/bundler/man/ + +# for a library or gem, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# Gemfile.lock +# .ruby-version +# .ruby-gemset + +# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: +.rvmrc + +# Used by RuboCop. Remote config files pulled in from inherit_from directive. +# .rubocop-https?--* + diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..414353c --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +source 'https://rubygems.org' + +gem 'asciidoctor-bibtex' +gem 'asciidoctor-rouge' +gem 'rouge' + +gemspec diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..a98b858 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,47 @@ +PATH + remote: . + specs: + asciidoctor-custom-extensions (1.0.0) + asciidoctor (~> 2.0) + +GEM + remote: https://rubygems.org/ + specs: + asciidoctor (2.0.18) + asciidoctor-bibtex (0.8.0) + asciidoctor (~> 2.0) + bibtex-ruby (~> 5.1) + citeproc-ruby (~> 1) + csl-styles (~> 1) + latex-decode (~> 0.2) + asciidoctor-rouge (0.4.0) + asciidoctor (>= 1.5.6, < 2.1) + rouge (>= 2.2, < 4) + bibtex-ruby (5.1.6) + latex-decode (~> 0.0) + citeproc (1.0.10) + namae (~> 1.0) + citeproc-ruby (1.1.14) + citeproc (~> 1.0, >= 1.0.9) + csl (~> 1.6) + csl (1.6.0) + namae (~> 1.0) + rexml + csl-styles (1.0.1.11) + csl (~> 1.0) + latex-decode (0.4.0) + namae (1.1.1) + rexml (3.2.5) + rouge (3.30.0) + +PLATFORMS + x86_64-linux + +DEPENDENCIES + asciidoctor-bibtex + asciidoctor-custom-extensions! + asciidoctor-rouge + rouge + +BUNDLED WITH + 2.4.6 diff --git a/asciidoctor-custom-extensions.gemspec b/asciidoctor-custom-extensions.gemspec new file mode 100644 index 0000000..08fda60 --- /dev/null +++ b/asciidoctor-custom-extensions.gemspec @@ -0,0 +1,16 @@ +Gem::Specification.new do |s| + s.name = 'asciidoctor-custom-extensions' + s.version = '1.0.0' + s.licenses = ['MIT'] + s.summary = 'My custom Asciidoctor extensions' + s.authors = ['Yor Neighme'] + s.email = 'yor.neighme@example.com' + s.metadata = { + 'bug_tracker_uri' => 'https://example.com/yoruserneighme/website/issues', + 'source_code_uri' => 'https://example.com/yoruserneighme/website.git' + } + + s.files = Dir['*.gemspec', 'lib/**/*'] + s.required_ruby_version = '>= 2.6' + s.add_runtime_dependency 'asciidoctor', '~> 2.0' +end diff --git a/lib/asciidoctor-custom-extensions.rb b/lib/asciidoctor-custom-extensions.rb new file mode 100644 index 0000000..7013fbb --- /dev/null +++ b/lib/asciidoctor-custom-extensions.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +require 'asciidoctor' +require 'asciidoctor/extensions' + +require_relative './asciidoctor/custom_extensions/swhid_link_inline_macro' + +Asciidoctor::Extensions.register do + inline_macro SWHInlineMacro +end diff --git a/lib/asciidoctor/custom_extensions/swhid_link_inline_macro.rb b/lib/asciidoctor/custom_extensions/swhid_link_inline_macro.rb new file mode 100644 index 0000000..b0b4d08 --- /dev/null +++ b/lib/asciidoctor/custom_extensions/swhid_link_inline_macro.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class SWHInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor + use_dsl + + named :swh + name_positional_attributes 'caption' + + def process(parent, target, attrs) + doc = parent.document + + # We're only considering `swh:` starting with the scheme version. Also, it + # looks nice aesthetically. + swhid = target.start_with?('swh:') ? target : %(swh:#{target}) + swhid_core_identifier = (swhid.split ';').at 0 + + text = attrs['caption'] || swhid_core_identifier + target = %(https://archive.softwareheritage.org/#{swhid}) + + doc.register :links, target + create_anchor parent, text, type: :link, target: target + end +end diff --git a/swhid-sample.adoc b/swhid-sample.adoc new file mode 100644 index 0000000..f7e5bc5 --- /dev/null +++ b/swhid-sample.adoc @@ -0,0 +1,10 @@ +// Should produce a link at https://archive.softwareheritage.org/$SWHID with +// '$SWHID_CORE_IDENTIFIER' as the link text. +swh:swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2;origin=https://github.com/pop-os/system76-firmware;lines=471-538[] + +// Similar as above but with the link text replaced with 'replacing the caption'. +swh:swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2;origin=https://github.com/pop-os/system76-firmware;lines=471-538[replacing the caption] + +// For aesthetic purposes, you could also use the `swh` macro with the `swh:` +// cut off from the SWHID. +swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2;origin=https://github.com/pop-os/system76-firmware;lines=471-538[]