From 4458df42a66e34d17273b1ad90e51761dd54fe21 Mon Sep 17 00:00:00 2001 From: Felix van Oost Date: Sun, 10 Dec 2023 21:01:24 +0000 Subject: [PATCH] Show rendered diagram count (#18) --- Gemfile.lock | 18 +++++++++--------- lib/jekyll/kroki.rb | 33 ++++++++++++++++++++++----------- lib/jekyll/kroki/version.rb | 2 +- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 67115e0..4c0e709 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - jekyll-kroki (0.2.2) + jekyll-kroki (0.3.0) faraday (~> 2.7) faraday-retry (~> 2.2) httpx (~> 1.1) @@ -11,7 +11,7 @@ PATH GEM remote: https://rubygems.org/ specs: - addressable (2.8.5) + addressable (2.8.6) public_suffix (>= 2.0.2, < 6.0) ast (2.4.2) base64 (0.2.0) @@ -21,7 +21,7 @@ GEM eventmachine (>= 0.12.9) http_parser.rb (~> 0) eventmachine (1.2.7) - faraday (2.7.11) + faraday (2.7.12) base64 faraday-net_http (>= 2.0, < 3.1) ruby2_keywords (>= 0.0.4) @@ -33,7 +33,7 @@ GEM google-protobuf (3.25.1-x86_64-linux) http-2-next (1.0.1) http_parser.rb (0.8.0) - httpx (1.1.3) + httpx (1.1.5) http-2-next (>= 1.0.1) i18n (1.14.1) concurrent-ruby (~> 1.0) @@ -57,7 +57,7 @@ GEM sass-embedded (~> 1.54) jekyll-watch (2.2.1) listen (~> 3.0) - json (2.6.3) + json (2.7.1) kramdown (2.4.0) rexml kramdown-parser-gfm (1.1.0) @@ -84,10 +84,10 @@ GEM rb-fsevent (0.11.2) rb-inotify (0.10.1) ffi (~> 1.0) - regexp_parser (2.8.2) + regexp_parser (2.8.3) rexml (3.2.6) rouge (4.2.0) - rubocop (1.57.2) + rubocop (1.58.0) json (~> 2.3) language_server-protocol (>= 3.17.0) parallel (~> 1.10) @@ -95,7 +95,7 @@ GEM rainbow (>= 2.2.2, < 4.0) regexp_parser (>= 1.8, < 3.0) rexml (>= 3.2.5, < 4.0) - rubocop-ast (>= 1.28.1, < 2.0) + rubocop-ast (>= 1.30.0, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 2.4.0, < 3.0) rubocop-ast (1.30.0) @@ -121,4 +121,4 @@ DEPENDENCIES rubocop (~> 1.21) BUNDLED WITH - 2.4.22 + 2.2.33 diff --git a/lib/jekyll/kroki.rb b/lib/jekyll/kroki.rb index 611078e..a4ab4f8 100644 --- a/lib/jekyll/kroki.rb +++ b/lib/jekyll/kroki.rb @@ -26,33 +26,44 @@ class << self def embed_site(site) # Get the URL of the Kroki instance kroki_url = kroki_url(site.config) - puts "[jekyll-kroki] Rendering diagrams using Kroki instance at '#{kroki_url}'" - connection = setup_connection(kroki_url) - site.documents.each do |doc| + rendered_diag = 0 + (site.pages + site.documents).each do |doc| next unless embeddable?(doc) - # Parse the HTML document, render and embed the diagrams, then convert it back into HTML - parsed_page = Nokogiri::HTML(doc.output) - embed_page(connection, parsed_page) - doc.output = parsed_page.to_html + # Render all supported diagram descriptions in the document + rendered_diag += embed_doc(connection, doc) + end + + unless rendered_diag.zero? + puts "[jekyll-kroki] Rendered #{rendered_diag} diagrams using Kroki instance at '#{kroki_url}'" end rescue StandardError => e exit(e) end - # Renders all diagram descriptions in a document and embeds them as inline SVGs in the HTML source. + # Renders all supported diagram descriptions in a document and embeds them as inline SVGs in the HTML source. # # @param [Faraday::Connection] The Faraday connection to use # @param [Nokogiri::HTML4::Document] The parsed HTML document - def embed_page(connection, parsed_doc) + # @param [Integer] The number of rendered diagrams + def embed_doc(connection, doc) + # Parse the HTML document + parsed_doc = Nokogiri::HTML(doc.output) + + rendered_diag = 0 SUPPORTED_LANGUAGES.each do |language| parsed_doc.css("code[class~='language-#{language}']").each do |diagram_desc| # Replace the diagram description with the SVG representation rendered by Kroki diagram_desc.replace(render_diagram(connection, diagram_desc, language)) + rendered_diag += 1 end end + + # Convert the document back to HTML + doc.output = parsed_doc.to_html + rendered_diag end # Renders a single diagram description using Kroki. @@ -80,10 +91,10 @@ def render_diagram(connection, diagram_desc, language) end # Sanitises a rendered diagram. Only