Skip to content

Commit

Permalink
Show rendered diagram count (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixvanoost authored Dec 10, 2023
1 parent b5c6c1f commit 4458df4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
18 changes: 9 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -84,18 +84,18 @@ 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)
parser (>= 3.2.2.4)
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)
Expand All @@ -121,4 +121,4 @@ DEPENDENCIES
rubocop (~> 1.21)

BUNDLED WITH
2.4.22
2.2.33
33 changes: 22 additions & 11 deletions lib/jekyll/kroki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -80,10 +91,10 @@ def render_diagram(connection, diagram_desc, language)
end

# Sanitises a rendered diagram. Only <script> elements are removed, which is the most minimal / naive
# implementation possible and is definitely not secure.
# implementation possible.
#
# @param [String] The diagram to santise in SVG format
# @return [String] The sanitized diagram in SVG format
# @return [String] The sanitised diagram
def sanitise_diagram(diagram_svg)
parsed_svg = Nokogiri::XML(diagram_svg)
parsed_svg.xpath('//*[name()="script"]').each(&:remove)
Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll/kroki/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Jekyll
class Kroki
VERSION = "0.2.2"
VERSION = "0.3.0"
end
end

0 comments on commit 4458df4

Please sign in to comment.