Skip to content

Commit

Permalink
Change transforming file content into metadata and slides to improve …
Browse files Browse the repository at this point in the history
…reusability
  • Loading branch information
piotrmurach committed Jul 19, 2023
1 parent 96e900f commit ae3ee4f
Show file tree
Hide file tree
Showing 7 changed files with 564 additions and 238 deletions.
50 changes: 50 additions & 0 deletions lib/slideck/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,55 @@ def initialize(metadata)
freeze
end
private_class_method :new

# Determine equivalence with another object
#
# @example
# metadata == other
#
# @param [Object] other
# the other object to determine equivalence with
#
# @return [Boolean]
# true if this object is equivalent to the other, false otherwise
#
# @api public
def ==(other)
other.is_a?(self.class) &&
@metadata.keys.all? do |name|
send(name) == other.send(name)
end
end

# Determine equality with another object
#
# @example
# metadata.eql?(other)
#
# @param [Object] other
# the other object to determine equality with
#
# @return [Boolean]
# true if this object is equal to the other, false otherwise
#
# @api public
def eql?(other)
instance_of?(other.class) &&
@metadata.keys.all? do |name|
send(name).eql?(other.send(name))
end
end

# Generate hash value of this metadata
#
# @example
# metadata.hash
#
# @return [Integer]
#
# @api public
def hash
[self.class, *@metadata.keys.map { |name| send(name) }].hash
end
end # Metadata
end # Slideck
66 changes: 66 additions & 0 deletions lib/slideck/metadata_wrapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

module Slideck
# Responsible for wrapping parsed global and slide metadata
#
# @api private
class MetadataWrapper
# Create a MetadataWrapper instance
#
# @example
# MetadataWrapper.new(metadata, metadata_converter, metadata_defaults)
#
# @param [Slideck::Metadata] metadata
# the metadata
# @param [Slideck::MetadataConverter] metadata_converter
# the metadata converter
# @param [Slideck::MetadataDefaults] metadata_defaults
# the metadata defaults
#
# @api public
def initialize(metadata, metadata_converter, metadata_defaults)
@metadata = metadata
@metadata_converter = metadata_converter
@metadata_defaults = metadata_defaults
end

# Wrap parsed global and slide metadata
#
# @example
# metadata_wrapper.wrap({metadata: {}, slides: []})
#
# @param [Hash{Symbol => Hash, String}] deck
# the deck of parsed metadata and slides
#
# @return [Array<Slideck::Metadata, Hash>]
#
# @api public
def wrap(deck)
[
build_metadata(deck[:metadata], @metadata_defaults),
deck[:slides].map do |slide|
{
content: slide[:content],
metadata: build_metadata(slide[:metadata], {})
}
end
]
end

private

# Build metadata
#
# @param [Hash{Symbol => Object}] custom_metadata
# the custom metadata
# @param [#merge] defaults
# the defaults to merge with
#
# @return [Slideck::Metadata]
#
# @api private
def build_metadata(custom_metadata, defaults)
@metadata.from(@metadata_converter, custom_metadata, defaults)
end
end # MetadataWrapper
end # Slideck
78 changes: 19 additions & 59 deletions lib/slideck/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
require_relative "metadata_converter"
require_relative "metadata_defaults"
require_relative "metadata_parser"
require_relative "metadata_wrapper"
require_relative "parser"
require_relative "presenter"
require_relative "renderer"
require_relative "tracker"
require_relative "transformer"

module Slideck
# Parse and display slides
Expand Down Expand Up @@ -64,7 +66,8 @@ def initialize(screen, input, output, env)
#
# @api public
def run(filename, color: nil, watch: nil)
presenter = build_presenter(color) { read_slides(filename) }
transformer = build_transformer
presenter = build_presenter(color) { transformer.read(filename) }

if watch
listener = build_listener(filename) { presenter.reload.render }
Expand All @@ -78,79 +81,36 @@ def run(filename, color: nil, watch: nil)

private

# Read slides
# Build transformer
#
# @param [String] filename
# the filename to read slides from
#
# @return [Array<Slideck::Metadata, Array<Hash>>]
# @return [Slideck::Transformer]
#
# @api private
def read_slides(filename)
parse_slides(load_slides(filename))
end

# Load slides
#
# @param [String] filename
# the filename to load slides from
#
# @return [String]
#
# @api private
def load_slides(filename)
def build_transformer
loader = Loader.new(::File)
loader.load(filename)
end

# Parse slides
#
# @param [String] content
# the content with metadata and slides
#
# @return [Array<Slideck::Metadata, Array<Hash>>]
#
# @api private
def parse_slides(content)
metadata_parser = MetadataParser.new(::YAML, permitted_classes: [Symbol],
symbolize_names: true)
parser = Parser.new(::StringScanner, metadata_parser)
wrap_metadata(parser.parse(content))
Transformer.new(loader, build_parser, build_metadata_wrapper)
end

# Wrap parsed slides metadata
#
# @param [Hash] deck
# the deck of parsed slides
# Build parser
#
# @return [Array<Slideck::Metadata, Hash>]
# @return [Slideck::Parser]
#
# @api private
def wrap_metadata(deck)
metadata_defaults = MetadataDefaults.new(Alignment, Margin)
metadata = build_metadata(deck[:metadata], metadata_defaults)
slides = deck[:slides].map do |slide|
{content: slide[:content],
metadata: build_metadata(slide[:metadata], {})}
end

[metadata, slides]
def build_parser
metadata_parser = MetadataParser.new(
::YAML, permitted_classes: [Symbol], symbolize_names: true)
Parser.new(::StringScanner, metadata_parser)
end

# Build metadata
#
# @param [Hash{Symbol => Object}] custom_metadata
# the custom metadata
# @param [#merge] metadata_defaults
# the metadata defaults to merge with
# Build metadata wrapper
#
# @return [Slideck::Metadata]
# @return [Slideck::MetadataWrapper]
#
# @api private
def build_metadata(custom_metadata, metadata_defaults)
def build_metadata_wrapper
metadata_converter = MetadataConverter.new(Alignment, Margin)

Metadata.from(metadata_converter, custom_metadata, metadata_defaults)
metadata_defaults = MetadataDefaults.new(Alignment, Margin)
MetadataWrapper.new(Metadata, metadata_converter, metadata_defaults)
end

# Build presenter
Expand Down
42 changes: 42 additions & 0 deletions lib/slideck/transformer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

module Slideck
# Responsible for transforming parsed data into metadata and slides
#
# @api private
class Transformer
# Create a Transformer instance
#
# @example
# Transformer.new(loader, parser, metadata_wrapper)
#
# @param [Slideck::Loader] loader
# the loader
# @param [Slideck::Parser] parser
# the parser
# @param [Slideck::MetadataWrapper] metadata_wrapper
# the metadata wrapper
#
# @api public
def initialize(loader, parser, metadata_wrapper)
@loader = loader
@parser = parser
@metadata_wrapper = metadata_wrapper
end

# Read slides from a file
#
# @example
# transformer.read("slides.md")
#
# @param [String] filename
# the filename to read slides from
#
# @return [Array<Slideck::Metadata, Array<Hash>>]
#
# @api public
def read(filename)
@metadata_wrapper.wrap(@parser.parse(@loader.load(filename)))
end
end # Transformer
end # Slideck
Loading

0 comments on commit ae3ee4f

Please sign in to comment.