-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from SwedbankPay/feature/dx-914_fix_page_output
DX-914: Only replace the needle
- Loading branch information
Showing
5 changed files
with
217 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'htmlentities' | ||
require 'json' | ||
require_relative 'log_wrapper' | ||
|
||
module Kramdown | ||
module PlantUml | ||
# Processes Jekyll pages. | ||
class JekyllPageProcessor | ||
PROCESSED_KEY = :kramdown_plantuml_processed | ||
|
||
def initialize(page) | ||
raise ArgumentError, 'page cannot be nil' if page.nil? | ||
|
||
puts page.class | ||
|
||
@page = page | ||
end | ||
|
||
def process(site_destination_directory) | ||
@page.output = do_process | ||
@page.data[PROCESSED_KEY] = true | ||
@page.write(site_destination_directory) | ||
end | ||
|
||
def should_process? | ||
return false unless @page.output_ext == '.html' | ||
|
||
if !@page.data.nil? && @page.data.key?(PROCESSED_KEY) && @page.data[PROCESSED_KEY] | ||
logger.debug "Skipping #{@page.path} because it has already been processed." | ||
return false | ||
end | ||
|
||
true | ||
end | ||
|
||
class << self | ||
def needle(plantuml, options) | ||
hash = { 'plantuml' => plantuml, 'options' => options.to_h } | ||
|
||
<<~NEEDLE | ||
<!--#kramdown-plantuml.start#--> | ||
#{hash.to_json} | ||
<!--#kramdown-plantuml.end#--> | ||
NEEDLE | ||
rescue StandardError => e | ||
raise e if options.nil? || options.raise_errors? | ||
|
||
logger.error 'Error while placing needle.' | ||
logger.error e.to_s | ||
logger.debug_multiline plantuml | ||
end | ||
|
||
def logger | ||
@logger ||= ::Kramdown::PlantUml::LogWrapper.init | ||
end | ||
end | ||
|
||
private | ||
|
||
def do_process | ||
logger.debug "Replacing Jekyll needles in #{@page.path}" | ||
|
||
html = @page.output | ||
|
||
return html if html.nil? || html.empty? || !html.is_a?(String) | ||
|
||
html.gsub(/<!--#kramdown-plantuml\.start#-->(?<json>.*?)<!--#kramdown-plantuml\.end#-->/m) do | ||
json = $LAST_MATCH_INFO ? $LAST_MATCH_INFO[:json] : nil | ||
replace_needle(json) unless json.nil? | ||
end | ||
end | ||
|
||
def replace_needle(json) | ||
logger.debug 'Replacing Jekyll needle.' | ||
|
||
needle_hash = JSON.parse(json) | ||
options_hash = needle_hash['options'] | ||
options = ::Kramdown::PlantUml::Options.new({ plantuml: options_hash }) | ||
|
||
begin | ||
decode_and_convert(needle_hash, options) | ||
rescue StandardError => e | ||
raise e if options.raise_errors? | ||
|
||
logger.error 'Error while replacing Jekyll needle.' | ||
logger.error e.to_s | ||
logger.debug_multiline json | ||
end | ||
end | ||
|
||
def decode_and_convert(hash, options) | ||
encoded_plantuml = hash['plantuml'] | ||
plantuml = HTMLEntities.new.decode encoded_plantuml | ||
diagram = ::Kramdown::PlantUml::Diagram.new(plantuml, options) | ||
diagram.convert_to_svg | ||
end | ||
|
||
def logger | ||
@logger ||= ::Kramdown::PlantUml::LogWrapper.init | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rspec/its' | ||
require 'kramdown-plantuml/options' | ||
require 'kramdown-plantuml/jekyll_page_processor' | ||
|
||
Options = Kramdown::PlantUml::Options | ||
JekyllPageProcessor = ::Kramdown::PlantUml::JekyllPageProcessor | ||
|
||
describe JekyllPageProcessor do | ||
let(:page) do | ||
page = double('page') | ||
allow(page).to receive(:output).and_return(output) | ||
allow(page).to receive(:output=) | ||
allow(page).to receive(:output_ext).and_return(output_ext) | ||
allow(page).to receive(:data).and_return({}) | ||
allow(page).to receive(:write) | ||
allow(page).to receive(:path).and_return(File.join(__dir__, 'page.md')) | ||
page | ||
end | ||
|
||
subject { JekyllPageProcessor.new(page) } | ||
|
||
describe '#initialize' do | ||
context 'when page is nil' do | ||
let(:page) { nil } | ||
it { expect { subject }.to raise_error(ArgumentError, 'page cannot be nil') } | ||
end | ||
end | ||
|
||
describe '#needle' do | ||
subject { JekyllPageProcessor.needle(plantuml, options) } | ||
|
||
context 'with nil :options' do | ||
let(:options) { nil } | ||
let(:plantuml) { "@startuml\n@enduml" } | ||
|
||
it do | ||
is_expected.to eq <<~NEEDLE | ||
<!--#kramdown-plantuml.start#--> | ||
{"plantuml":"@startuml\\n@enduml","options":{}} | ||
<!--#kramdown-plantuml.end#--> | ||
NEEDLE | ||
end | ||
end | ||
|
||
context 'with nil :plantuml' do | ||
let(:options) { Options.new } | ||
let(:plantuml) { nil } | ||
|
||
it do | ||
is_expected.to eq <<~NEEDLE | ||
<!--#kramdown-plantuml.start#--> | ||
{"plantuml":null,"options":{}} | ||
<!--#kramdown-plantuml.end#--> | ||
NEEDLE | ||
end | ||
end | ||
|
||
context 'with valid :options and :plantuml' do | ||
let(:options) { { theme: { name: 'custom' } } } | ||
let(:plantuml) { "@startuml\n@enduml" } | ||
|
||
it do | ||
is_expected.to eq <<~NEEDLE | ||
<!--#kramdown-plantuml.start#--> | ||
{"plantuml":"@startuml\\n@enduml","options":{"theme":{"name":"custom"}}} | ||
<!--#kramdown-plantuml.end#--> | ||
NEEDLE | ||
end | ||
end | ||
end | ||
|
||
describe '#process' do | ||
before { subject.process(__dir__) } | ||
|
||
context 'with HTML output' do | ||
let(:output) { '<h1 id="hello">Hello</h1>' } | ||
let(:output_ext) { '.html' } | ||
its(:should_process?) { is_expected.to eq false } | ||
it { expect(page.output).to eq output } | ||
end | ||
end | ||
|
||
describe '#should_process?' do | ||
context 'with HTML output' do | ||
let(:output) { '<h1 id="hello">Hello</h1>' } | ||
let(:output_ext) { '.html' } | ||
its(:should_process?) { is_expected.to eq true } | ||
it { expect(page.output).to eq output } | ||
end | ||
|
||
context 'with CSS output' do | ||
let(:output) { 'body { display: none }' } | ||
let(:output_ext) { '.css' } | ||
its(:should_process?) { is_expected.to eq false } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters