Skip to content

Commit 4458df4

Browse files
authored
Show rendered diagram count (#18)
1 parent b5c6c1f commit 4458df4

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

Gemfile.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
jekyll-kroki (0.2.2)
4+
jekyll-kroki (0.3.0)
55
faraday (~> 2.7)
66
faraday-retry (~> 2.2)
77
httpx (~> 1.1)
@@ -11,7 +11,7 @@ PATH
1111
GEM
1212
remote: https://rubygems.org/
1313
specs:
14-
addressable (2.8.5)
14+
addressable (2.8.6)
1515
public_suffix (>= 2.0.2, < 6.0)
1616
ast (2.4.2)
1717
base64 (0.2.0)
@@ -21,7 +21,7 @@ GEM
2121
eventmachine (>= 0.12.9)
2222
http_parser.rb (~> 0)
2323
eventmachine (1.2.7)
24-
faraday (2.7.11)
24+
faraday (2.7.12)
2525
base64
2626
faraday-net_http (>= 2.0, < 3.1)
2727
ruby2_keywords (>= 0.0.4)
@@ -33,7 +33,7 @@ GEM
3333
google-protobuf (3.25.1-x86_64-linux)
3434
http-2-next (1.0.1)
3535
http_parser.rb (0.8.0)
36-
httpx (1.1.3)
36+
httpx (1.1.5)
3737
http-2-next (>= 1.0.1)
3838
i18n (1.14.1)
3939
concurrent-ruby (~> 1.0)
@@ -57,7 +57,7 @@ GEM
5757
sass-embedded (~> 1.54)
5858
jekyll-watch (2.2.1)
5959
listen (~> 3.0)
60-
json (2.6.3)
60+
json (2.7.1)
6161
kramdown (2.4.0)
6262
rexml
6363
kramdown-parser-gfm (1.1.0)
@@ -84,18 +84,18 @@ GEM
8484
rb-fsevent (0.11.2)
8585
rb-inotify (0.10.1)
8686
ffi (~> 1.0)
87-
regexp_parser (2.8.2)
87+
regexp_parser (2.8.3)
8888
rexml (3.2.6)
8989
rouge (4.2.0)
90-
rubocop (1.57.2)
90+
rubocop (1.58.0)
9191
json (~> 2.3)
9292
language_server-protocol (>= 3.17.0)
9393
parallel (~> 1.10)
9494
parser (>= 3.2.2.4)
9595
rainbow (>= 2.2.2, < 4.0)
9696
regexp_parser (>= 1.8, < 3.0)
9797
rexml (>= 3.2.5, < 4.0)
98-
rubocop-ast (>= 1.28.1, < 2.0)
98+
rubocop-ast (>= 1.30.0, < 2.0)
9999
ruby-progressbar (~> 1.7)
100100
unicode-display_width (>= 2.4.0, < 3.0)
101101
rubocop-ast (1.30.0)
@@ -121,4 +121,4 @@ DEPENDENCIES
121121
rubocop (~> 1.21)
122122

123123
BUNDLED WITH
124-
2.4.22
124+
2.2.33

lib/jekyll/kroki.rb

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,44 @@ class << self
2626
def embed_site(site)
2727
# Get the URL of the Kroki instance
2828
kroki_url = kroki_url(site.config)
29-
puts "[jekyll-kroki] Rendering diagrams using Kroki instance at '#{kroki_url}'"
30-
3129
connection = setup_connection(kroki_url)
3230

33-
site.documents.each do |doc|
31+
rendered_diag = 0
32+
(site.pages + site.documents).each do |doc|
3433
next unless embeddable?(doc)
3534

36-
# Parse the HTML document, render and embed the diagrams, then convert it back into HTML
37-
parsed_page = Nokogiri::HTML(doc.output)
38-
embed_page(connection, parsed_page)
39-
doc.output = parsed_page.to_html
35+
# Render all supported diagram descriptions in the document
36+
rendered_diag += embed_doc(connection, doc)
37+
end
38+
39+
unless rendered_diag.zero?
40+
puts "[jekyll-kroki] Rendered #{rendered_diag} diagrams using Kroki instance at '#{kroki_url}'"
4041
end
4142
rescue StandardError => e
4243
exit(e)
4344
end
4445

45-
# Renders all diagram descriptions in a document and embeds them as inline SVGs in the HTML source.
46+
# Renders all supported diagram descriptions in a document and embeds them as inline SVGs in the HTML source.
4647
#
4748
# @param [Faraday::Connection] The Faraday connection to use
4849
# @param [Nokogiri::HTML4::Document] The parsed HTML document
49-
def embed_page(connection, parsed_doc)
50+
# @param [Integer] The number of rendered diagrams
51+
def embed_doc(connection, doc)
52+
# Parse the HTML document
53+
parsed_doc = Nokogiri::HTML(doc.output)
54+
55+
rendered_diag = 0
5056
SUPPORTED_LANGUAGES.each do |language|
5157
parsed_doc.css("code[class~='language-#{language}']").each do |diagram_desc|
5258
# Replace the diagram description with the SVG representation rendered by Kroki
5359
diagram_desc.replace(render_diagram(connection, diagram_desc, language))
60+
rendered_diag += 1
5461
end
5562
end
63+
64+
# Convert the document back to HTML
65+
doc.output = parsed_doc.to_html
66+
rendered_diag
5667
end
5768

5869
# Renders a single diagram description using Kroki.
@@ -80,10 +91,10 @@ def render_diagram(connection, diagram_desc, language)
8091
end
8192

8293
# Sanitises a rendered diagram. Only <script> elements are removed, which is the most minimal / naive
83-
# implementation possible and is definitely not secure.
94+
# implementation possible.
8495
#
8596
# @param [String] The diagram to santise in SVG format
86-
# @return [String] The sanitized diagram in SVG format
97+
# @return [String] The sanitised diagram
8798
def sanitise_diagram(diagram_svg)
8899
parsed_svg = Nokogiri::XML(diagram_svg)
89100
parsed_svg.xpath('//*[name()="script"]').each(&:remove)

lib/jekyll/kroki/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Jekyll
44
class Kroki
5-
VERSION = "0.2.2"
5+
VERSION = "0.3.0"
66
end
77
end

0 commit comments

Comments
 (0)