Skip to content

Commit

Permalink
Merge pull request #91 from SwedbankPay/feature/dx-1660_gh-86_do_not_…
Browse files Browse the repository at this point in the history
…overwrite_sidebar

DX-1660: Use Jekyll's source directory
  • Loading branch information
asbjornu authored Dec 13, 2021
2 parents 325cd26 + 6d11dac commit 7421798
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 330 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ kramdown:
plantuml:
theme:
name: my-custom-theme
directory: path/to/themes
directory: spec/examples
```

### Dimensions and Styling
Expand Down
3 changes: 0 additions & 3 deletions lib/kramdown-plantuml.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# frozen_string_literal: true

require_relative 'kramdown_html'
require_relative 'kramdown-plantuml/jekyll_provider'

::Kramdown::PlantUml::JekyllProvider.install
103 changes: 0 additions & 103 deletions lib/kramdown-plantuml/jekyll_page_processor.rb

This file was deleted.

56 changes: 6 additions & 50 deletions lib/kramdown-plantuml/jekyll_provider.rb
Original file line number Diff line number Diff line change
@@ -1,75 +1,31 @@
# frozen_string_literal: true

require_relative 'log_wrapper'
require_relative 'jekyll_page_processor'

module Kramdown
module PlantUml
# Provides an instance of Jekyll if available.
module JekyllProvider
class << self
attr_reader :site_destination_dir
attr_reader :site_source_dir

def jekyll
return @jekyll if defined? @jekyll

@jekyll = load_jekyll
end

def install
return @installed = false if jekyll.nil?

find_site_destination_dir
register_hook
@installed = true
end

def installed?
@installed
end

def needle(plantuml, options)
JekyllPageProcessor.needle(plantuml, options)
end

private

def find_site_destination_dir
def find_site_source_dir
if jekyll.sites.nil? || jekyll.sites.empty?
logger.debug 'Jekyll detected, hooking into :site:post_write.'
logger.warn 'Jekyll detected, but no sites found.'
return nil
end

@site_destination_dir = jekyll.sites.first.dest
logger.debug "Jekyll detected, hooking into :site:post_write of '#{@site_destination_dir}'."
@site_destination_dir
end

def register_hook
Jekyll::Hooks.register :site, :post_write do |site|
site_post_write(site)
end
end

def site_post_write(site)
logger.debug 'Jekyll:site:post_write triggered.'
@site_destination_dir ||= site.dest

site.pages.each do |page|
processor = JekyllPageProcessor.new(page)

next unless processor.should_process?

processor.process(site.dest)
end

site.posts.each do |post|
processor = JekyllPageProcessor.new(post)

next unless processor.should_process?

processor.process(site.dest)
end
@site_source_dir = jekyll.sites.first.source
logger.debug "Jekyll detected, using '#{@site_source_dir}' as base directory."
@site_source_dir
end

def load_jekyll
Expand Down
2 changes: 1 addition & 1 deletion lib/kramdown-plantuml/plantuml_diagram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(plantuml, options)
raise ArgumentError, 'options cannot be nil' if options.nil?
raise ArgumentError, "options must be a '#{Options}'." unless options.is_a?(Options)

@plantuml = plantuml
@plantuml = plantuml.strip unless plantuml.nil?
@options = options
@theme = Theme.new(options)
@logger = LogWrapper.init
Expand Down
6 changes: 3 additions & 3 deletions lib/kramdown-plantuml/theme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def apply(plantuml)
def resolve(directory)
jekyll = JekyllProvider

return directory if directory.nil? || directory.empty? || !jekyll.installed?
return directory if directory.nil? || directory.empty?

directory = File.absolute_path(directory, jekyll.site_destination_dir)
directory = File.absolute_path(directory, jekyll.site_source_dir)

log_or_raise "The theme directory '#{directory}' cannot be found" unless Dir.exist?(directory)

Expand All @@ -51,7 +51,7 @@ def resolve(directory)
def log_or_raise(message)
raise IOError, message if @raise_errors

logger.warn message
@logger.warn message
end

def theme(plantuml)
Expand Down
24 changes: 7 additions & 17 deletions lib/kramdown_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require_relative 'kramdown-plantuml/plantuml_error'
require_relative 'kramdown-plantuml/options'
require_relative 'kramdown-plantuml/plantuml_diagram'
require_relative 'kramdown-plantuml/jekyll_provider'

module Kramdown
module Converter
Expand All @@ -16,19 +15,9 @@ class Html
alias super_convert_codeblock convert_codeblock

def convert_codeblock(element, indent)
return super_convert_codeblock(element, indent) unless plantuml?(element)
return super_convert_codeblock(element, indent) unless plantuml? element

jekyll = ::Kramdown::PlantUml::JekyllProvider

# If Jekyll is successfully loaded, we'll wait with converting the
# PlantUML diagram to SVG since a theme may be configured that needs to
# be copied to the assets directory before the PlantUML conversion can
# be performed. We therefore place a needle in the haystack that we will
# convert in the :site:pre_render hook.
options = ::Kramdown::PlantUml::Options.new(@options)
return jekyll.needle(element.value, options) if jekyll.installed?

convert_plantuml(element.value, options)
convert_plantuml(element.value)
end

private
Expand All @@ -37,14 +26,15 @@ def plantuml?(element)
element.attr['class'] == 'language-plantuml'
end

def convert_plantuml(plantuml, options)
diagram = ::Kramdown::PlantUml::PlantUmlDiagram.new(plantuml, options)
def convert_plantuml(plantuml)
puml_opts = ::Kramdown::PlantUml::Options.new(@options)
diagram = ::Kramdown::PlantUml::PlantUmlDiagram.new(plantuml, puml_opts)
diagram.svg.to_s
rescue StandardError => e
raise e if options.raise_errors?
raise e if puml_opts.nil? || puml_opts.raise_errors?

logger = ::Kramdown::PlantUml::LogWrapper.init
logger.error "Error while replacing needle: #{e.inspect}"
logger.error "Error while converting diagram: #{e.inspect}"
end
end
end
Expand Down
99 changes: 0 additions & 99 deletions spec/jekyll_page_processor_spec.rb

This file was deleted.

Loading

0 comments on commit 7421798

Please sign in to comment.