-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Remove the `Renderer` class by merging into `GeneratedPage` * DRY up conversions with the Transformable concern * Clean up repo Gemfile * Move yard to Gemfile top-level * add return docs
- Loading branch information
1 parent
0d03040
commit 2715084
Showing
8 changed files
with
195 additions
and
302 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
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
62 changes: 62 additions & 0 deletions
62
bridgetown-core/lib/bridgetown-core/concerns/transformable.rb
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,62 @@ | ||
# frozen_string_literal: true | ||
|
||
module Bridgetown | ||
module Transformable | ||
# Transforms an input document by running it through available converters | ||
# (requires a `converter` method to be present on the including class) | ||
# | ||
# @param document [Bridgetown::GeneratedPage, Bridgetown::Resource::Base] | ||
# @return String | ||
# @yieldparam converter [Bridgetown::Converter] | ||
# @yieldparam index [Integer] index of the conversion step | ||
# @yieldparam output [String] | ||
def transform_content(document) | ||
converters.each_with_index.inject(document.content.to_s) do |content, (converter, index)| | ||
output = if converter.method(:convert).arity == 1 | ||
converter.convert content | ||
else | ||
converter.convert content, document | ||
end | ||
|
||
yield converter, index, output if block_given? | ||
|
||
output.html_safe | ||
rescue StandardError => e | ||
Bridgetown.logger.error "Conversion error:", | ||
"#{converter.class} encountered an error while "\ | ||
"converting `#{document.relative_path}'" | ||
raise e | ||
end | ||
end | ||
|
||
# Transforms an input document by placing it within the specified layout | ||
# | ||
# @param layout [Bridgetown::Layout] | ||
# @param output [String] the output from document content conversions | ||
# @param document [Bridgetown::GeneratedPage, Bridgetown::Resource::Base] | ||
# @return String | ||
# @yieldparam converter [Bridgetown::Converter] | ||
# @yieldparam layout_output [String] | ||
def transform_with_layout(layout, output, document) | ||
layout_converters = site.matched_converters_for_convertible(layout) | ||
layout_input = layout.content.dup | ||
|
||
layout_converters.inject(layout_input) do |content, converter| | ||
next(content) unless [2, -2].include?(converter.method(:convert).arity) # rubocop:disable Performance/CollectionLiteralInLoop | ||
|
||
layout.current_document = document | ||
layout.current_document_output = output | ||
layout_output = converter.convert content, layout | ||
|
||
yield converter, layout_output if block_given? | ||
|
||
layout_output | ||
rescue StandardError => e | ||
Bridgetown.logger.error "Conversion error:", | ||
"#{converter.class} encountered an error while "\ | ||
"converting `#{document.relative_path}'" | ||
raise e | ||
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
Oops, something went wrong.